use of org.eclipse.jdt.core.IPackageFragmentRoot 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.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class PackageFragmentRootReorgChange method perform.
@Override
public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
pm.beginTask(getName(), 2);
try {
String newName = getNewResourceName();
IPackageFragmentRoot root = getRoot();
ResourceMapping mapping = JavaElementResourceMapping.create(root);
final Change result = doPerformReorg(getDestinationProjectPath().append(newName), new SubProgressMonitor(pm, 1));
markAsExecuted(root, mapping);
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class CreatePackageChange method perform.
@Override
public Change perform(IProgressMonitor pm) throws CoreException {
try {
pm.beginTask(RefactoringCoreMessages.CreatePackageChange_Creating_package, 1);
if (fPackageFragment.exists()) {
return new NullChange();
} else {
IPackageFragmentRoot root = (IPackageFragmentRoot) fPackageFragment.getParent();
root.createPackageFragment(fPackageFragment.getElementName(), false, pm);
return new DeleteResourceChange(fPackageFragment.getPath(), true);
}
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.IPackageFragmentRoot 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;
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class JavaElementUtil method getParentSubpackage.
/**
* @param pack the package fragment; may not be null
* @return the parent package fragment, or null if the given package fragment is the default package or a top level package
*/
public static IPackageFragment getParentSubpackage(IPackageFragment pack) {
if (pack.isDefaultPackage())
return null;
final int index = pack.getElementName().lastIndexOf('.');
if (index == -1)
return null;
final IPackageFragmentRoot root = (IPackageFragmentRoot) pack.getParent();
final String newPackageName = pack.getElementName().substring(0, index);
final IPackageFragment parent = root.getPackageFragment(newPackageName);
if (parent.exists())
return parent;
else
return null;
}
Aggregations