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;
}
}
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.
}
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;
}
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();
}
}
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;
}
Aggregations