Search in sources :

Example 41 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class MoveCompilationUnitChange method doPerformReorg.

@Override
Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    String name;
    String newName = getNewName();
    if (newName == null)
        name = getCu().getElementName();
    else
        name = newName;
    // get current modification stamp
    long currentStamp = IResource.NULL_STAMP;
    IResource resource = getCu().getResource();
    if (resource != null) {
        currentStamp = resource.getModificationStamp();
    }
    IPackageFragment destination = getDestinationPackage();
    fUndoable = !destination.exists() || !destination.getCompilationUnit(name).exists();
    IPackageFragment[] createdPackages = null;
    if (!destination.exists()) {
        IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) destination.getParent();
        createdPackages = createDestination(packageFragmentRoot, destination, pm);
    }
    // perform the move and restore modification stamp
    getCu().move(destination, null, newName, true, pm);
    if (fStampToRestore != IResource.NULL_STAMP) {
        ICompilationUnit moved = destination.getCompilationUnit(name);
        IResource movedResource = moved.getResource();
        if (movedResource != null) {
            movedResource.revertModificationStamp(fStampToRestore);
        }
    }
    if (fDeletePackages != null) {
        for (int i = fDeletePackages.length - 1; i >= 0; i--) {
            fDeletePackages[i].delete(true, pm);
        }
    }
    if (fUndoable) {
        return new MoveCompilationUnitChange(destination, getCu().getElementName(), getOldPackage(), currentStamp, createdPackages);
    } else {
        return null;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 42 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class RenamePackageChange method createUndoChange.

@Override
protected Change createUndoChange(long stampToRestore) throws CoreException {
    IPackageFragment pack = getPackage();
    if (pack == null)
        return new NullChange();
    Map<IResource, Long> stamps = new HashMap<IResource, Long>();
    if (!fRenameSubpackages) {
        addStamps(stamps, pack.getCompilationUnits());
    } else {
        IPackageFragment[] allPackages = JavaElementUtil.getPackageAndSubpackages(pack);
        for (int i = 0; i < allPackages.length; i++) {
            IPackageFragment currentPackage = allPackages[i];
            addStamps(stamps, currentPackage.getCompilationUnits());
        }
    }
    return new RenamePackageChange(createNewPath(), getNewName(), getOldName(), stampToRestore, stamps, fRenameSubpackages);
// Note: This reverse change only works if the renamePackage change did
// not merge the source package into an existing target.
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) NullChange(org.eclipse.ltk.core.refactoring.NullChange) HashMap(java.util.HashMap) IResource(org.eclipse.core.resources.IResource)

Example 43 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class CopyResourceChange method deleteIfAlreadyExists.

/**
	 * returns false if source and destination are the same (in workspace or on disk)
	 * in such case, no action should be performed
	 * @param pm the progress monitor
	 * @param newName the new name
	 * @return returns <code>true</code> if the resource already exists
	 * @throws CoreException thrown when teh resource cannpt be accessed
	 */
private boolean deleteIfAlreadyExists(IProgressMonitor pm, String newName) throws CoreException {
    //$NON-NLS-1$
    pm.beginTask("", 1);
    IResource current = getDestination().findMember(newName);
    if (current == null)
        return true;
    if (!current.exists())
        return true;
    IResource resource = getResource();
    Assert.isNotNull(resource);
    if (ReorgUtils.areEqualInWorkspaceOrOnDisk(resource, current))
        return false;
    if (current instanceof IFile)
        ((IFile) current).delete(false, true, new SubProgressMonitor(pm, 1));
    else if (current instanceof IFolder)
        ((IFolder) current).delete(false, true, new SubProgressMonitor(pm, 1));
    else
        Assert.isTrue(false);
    return true;
}
Also used : IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IFolder(org.eclipse.core.resources.IFolder)

Example 44 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class CopyResourceChange method perform.

/* non java-doc
	 * @see IChange#perform(ChangeContext, IProgressMonitor)
	 */
@Override
public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    try {
        pm.beginTask(getName(), 2);
        String newName = getNewResourceName();
        IResource resource = getResource();
        boolean performReorg = deleteIfAlreadyExists(new SubProgressMonitor(pm, 1), newName);
        if (!performReorg)
            return null;
        getResource().copy(getDestinationPath(newName), getReorgFlags(), new SubProgressMonitor(pm, 1));
        markAsExecuted(resource);
        return null;
    } finally {
        pm.done();
    }
}
Also used : IResource(org.eclipse.core.resources.IResource) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 45 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class DeletePackageFragmentRootChange method doDelete.

@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;
    //$NON-NLS-1$
    pm.beginTask("", 2);
    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];
        //$NON-NLS-1$
        IFile classpathFile = javaProject.getProject().getFile(".classpath");
        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;
}
Also used : IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Entry(java.util.Map.Entry) IJavaProject(org.eclipse.jdt.core.IJavaProject) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ResourceDescription(org.eclipse.ui.ide.undo.ResourceDescription) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IResource(org.eclipse.core.resources.IResource)

Aggregations

IResource (org.eclipse.core.resources.IResource)1487 IFile (org.eclipse.core.resources.IFile)390 CoreException (org.eclipse.core.runtime.CoreException)390 IPath (org.eclipse.core.runtime.IPath)315 IProject (org.eclipse.core.resources.IProject)286 ArrayList (java.util.ArrayList)237 IFolder (org.eclipse.core.resources.IFolder)187 IContainer (org.eclipse.core.resources.IContainer)175 Path (org.eclipse.core.runtime.Path)141 File (java.io.File)124 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)109 IOException (java.io.IOException)103 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)95 IStatus (org.eclipse.core.runtime.IStatus)89 List (java.util.List)83 IJavaProject (org.eclipse.jdt.core.IJavaProject)75 HashSet (java.util.HashSet)71 Status (org.eclipse.core.runtime.Status)66 HashMap (java.util.HashMap)60 IAdaptable (org.eclipse.core.runtime.IAdaptable)59