/** * Create a ContainerDescription from the specified container handle. Typically used when the * container handle represents a resource that actually exists, although it will not fail if the * resource is non-existent. * * @param container the container to be described */ public ContainerDescription(IContainer container) { super(container); this.name = container.getName(); if (container.isLinked()) { this.location = container.getLocationURI(); } try { if (container.isAccessible()) { defaultCharSet = container.getDefaultCharset(false); IResource[] resourceMembers = container.members(); members = new AbstractResourceDescription[resourceMembers.length]; for (int i = 0; i < resourceMembers.length; i++) { members[i] = (AbstractResourceDescription) ResourceDescription.fromResource(resourceMembers[i]); } } } catch (CoreException e) { // Eat this exception because it only occurs when the resource // does not exist and we have already checked this. // We do not want to throw exceptions on the simple constructor, as // no one has actually tried to do anything yet. } }
@Override protected Change doDelete(IProgressMonitor pm) throws CoreException { ISourceManipulation element = getSourceManipulation(); // we have to save dirty compilation units before deleting them. Otherwise // we will end up showing ghost compilation units in the package explorer // since the primary working copy still exists. if (element instanceof ICompilationUnit) { pm.beginTask("", 2); // $NON-NLS-1$ ICompilationUnit unit = (ICompilationUnit) element; saveCUnitIfNeeded(unit, new SubProgressMonitor(pm, 1)); IResource resource = unit.getResource(); ResourceDescription resourceDescription = ResourceDescription.fromResource(resource); element.delete(false, new SubProgressMonitor(pm, 1)); resourceDescription.recordStateFromHistory(resource, new SubProgressMonitor(pm, 1)); return new UndoDeleteResourceChange(resourceDescription); } else if (element instanceof IPackageFragment) { ICompilationUnit[] units = ((IPackageFragment) element).getCompilationUnits(); pm.beginTask("", units.length + 1); // $NON-NLS-1$ for (int i = 0; i < units.length; i++) { // fix https://bugs.eclipse.org/bugs/show_bug.cgi?id=66835 saveCUnitIfNeeded(units[i], new SubProgressMonitor(pm, 1)); } element.delete(false, new SubProgressMonitor(pm, 1)); return new NullChange(); // caveat: real undo implemented by UndoablePackageDeleteChange } else { element.delete(false, pm); return null; // should not happen } }
@Override protected Change doDelete(IProgressMonitor pm) throws CoreException { if (! confirmDeleteIfReferenced()) return new NullChange(); int resourceUpdateFlags= IResource.KEEP_HISTORY; int jCoreUpdateFlags= IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH; pm.beginTask("", 2); //$NON-NLS-1$ IPackageFragmentRoot root= getRoot(); IResource rootResource= root.getResource(); CompositeChange result= new CompositeChange(getName()); ResourceDescription rootDescription = ResourceDescription.fromResource(rootResource); IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(root); HashMap<IFile, String> classpathFilesContents= new HashMap<IFile, String>(); for (int i= 0; i < referencingProjects.length; i++) { IJavaProject javaProject= referencingProjects[i]; IFile classpathFile= javaProject.getProject().getFile(".classpath"); //$NON-NLS-1$ if (classpathFile.exists()) { classpathFilesContents.put(classpathFile, getFileContents(classpathFile)); } } root.delete(resourceUpdateFlags, jCoreUpdateFlags, new SubProgressMonitor(pm, 1)); rootDescription.recordStateFromHistory(rootResource, new SubProgressMonitor(pm, 1)); for (Iterator<Entry<IFile, String>> iterator= classpathFilesContents.entrySet().iterator(); iterator.hasNext();) { Entry<IFile, String> entry= iterator.next(); IFile file= entry.getKey(); String contents= entry.getValue(); //Restore time stamps? This should probably be some sort of UndoTextFileChange. TextFileChange classpathUndo= new TextFileChange(Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_restore_file, BasicElementLabels.getPathLabel(file.getFullPath(), true)), file); classpathUndo.setEdit(new ReplaceEdit(0, getFileLength(file), contents)); result.add(classpathUndo); } result.add(new UndoDeleteResourceChange(rootDescription)); pm.done(); return result; }
@Override protected Change doDelete(IProgressMonitor pm) throws CoreException { ISourceManipulation element= getSourceManipulation(); // we have to save dirty compilation units before deleting them. Otherwise // we will end up showing ghost compilation units in the package explorer // since the primary working copy still exists. if (element instanceof ICompilationUnit) { pm.beginTask("", 2); //$NON-NLS-1$ ICompilationUnit unit= (ICompilationUnit)element; saveCUnitIfNeeded(unit, new SubProgressMonitor(pm, 1)); IResource resource= unit.getResource(); ResourceDescription resourceDescription = ResourceDescription.fromResource(resource); element.delete(false, new SubProgressMonitor(pm, 1)); resourceDescription.recordStateFromHistory(resource, new SubProgressMonitor(pm, 1)); return new UndoDeleteResourceChange(resourceDescription); } else if (element instanceof IPackageFragment) { ICompilationUnit[] units= ((IPackageFragment)element).getCompilationUnits(); pm.beginTask("", units.length + 1); //$NON-NLS-1$ for (int i = 0; i < units.length; i++) { // fix https://bugs.eclipse.org/bugs/show_bug.cgi?id=66835 saveCUnitIfNeeded(units[i], new SubProgressMonitor(pm, 1)); } element.delete(false, new SubProgressMonitor(pm, 1)); return new NullChange(); // caveat: real undo implemented by UndoablePackageDeleteChange } else { element.delete(false, pm); return null; //should not happen } }
protected static ContainerDescription fromContainer( IContainer container, boolean usingVirtualFolder) { IPath fullPath = container.getFullPath(); ContainerDescription firstCreatedParent = null; ContainerDescription currentContainerDescription = null; // Does the container exist already? If so, then the parent exists and // we use the normal creation constructor. IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IContainer currentContainer = (IContainer) root.findMember(fullPath); if (currentContainer != null) { return (ContainerDescription) ResourceDescription.fromResource(container); } // Create container descriptions for any uncreated parents in the given // path. currentContainer = root; for (int i = 0; i < fullPath.segmentCount(); i++) { String currentSegment = fullPath.segment(i); IResource resource = currentContainer.findMember(currentSegment); if (resource != null) { // parent already exists, no need to create a description for it currentContainer = (IContainer) resource; } else { if (i == 0) { // parent does not exist and it is a project firstCreatedParent = new ProjectDescription(root.getProject(currentSegment)); currentContainerDescription = firstCreatedParent; } else { IFolder folderHandle = currentContainer.getFolder(new Path(currentSegment)); ContainerDescription currentFolder; currentFolder = new FolderDescription(folderHandle, usingVirtualFolder); currentContainer = folderHandle; if (currentContainerDescription != null) { currentContainerDescription.addMember(currentFolder); } currentContainerDescription = currentFolder; if (firstCreatedParent == null) { firstCreatedParent = currentFolder; } } } } return firstCreatedParent; }
@Override protected Change doDelete(IProgressMonitor pm) throws CoreException { if (!confirmDeleteIfReferenced()) return new NullChange(); int resourceUpdateFlags = IResource.KEEP_HISTORY; int jCoreUpdateFlags = IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH; pm.beginTask("", 2); // $NON-NLS-1$ IPackageFragmentRoot root = getRoot(); IResource rootResource = root.getResource(); CompositeChange result = new CompositeChange(getName()); ResourceDescription rootDescription = ResourceDescription.fromResource(rootResource); IJavaProject[] referencingProjects = JavaElementUtil.getReferencingProjects(root); HashMap<IFile, String> classpathFilesContents = new HashMap<IFile, String>(); for (int i = 0; i < referencingProjects.length; i++) { IJavaProject javaProject = referencingProjects[i]; IFile classpathFile = javaProject.getProject().getFile(".classpath"); // $NON-NLS-1$ if (classpathFile.exists()) { classpathFilesContents.put(classpathFile, getFileContents(classpathFile)); } } root.delete(resourceUpdateFlags, jCoreUpdateFlags, new SubProgressMonitor(pm, 1)); rootDescription.recordStateFromHistory(rootResource, new SubProgressMonitor(pm, 1)); for (Iterator<Entry<IFile, String>> iterator = classpathFilesContents.entrySet().iterator(); iterator.hasNext(); ) { Entry<IFile, String> entry = iterator.next(); IFile file = entry.getKey(); String contents = entry.getValue(); // Restore time stamps? This should probably be some sort of UndoTextFileChange. TextFileChange classpathUndo = new TextFileChange( Messages.format( RefactoringCoreMessages.DeletePackageFragmentRootChange_restore_file, BasicElementLabels.getPathLabel(file.getFullPath(), true)), file); classpathUndo.setEdit(new ReplaceEdit(0, getFileLength(file), contents)); result.add(classpathUndo); } result.add(new UndoDeleteResourceChange(rootDescription)); pm.done(); return result; }
public UndoDeleteResourceChange(ResourceDescription resourceDescription) { fResourceDescription = resourceDescription; }
public UndoDeleteResourceChange(ResourceDescription resourceDescription) { fResourceDescription= resourceDescription; }