Search in sources :

Example 56 with IPackageFragmentRoot

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;
    }
}
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 57 with IPackageFragmentRoot

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();
    }
}
Also used : JavaElementResourceMapping(org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) ResourceChange(org.eclipse.ltk.core.refactoring.resource.ResourceChange) Change(org.eclipse.ltk.core.refactoring.Change) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 58 with IPackageFragmentRoot

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();
    }
}
Also used : NullChange(org.eclipse.ltk.core.refactoring.NullChange) DeleteResourceChange(org.eclipse.ltk.core.refactoring.resource.DeleteResourceChange) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 59 with IPackageFragmentRoot

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;
}
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)

Example 60 with IPackageFragmentRoot

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;
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)179 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)96 Test (org.junit.Test)95 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)78 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)37 File (java.io.File)26 IJavaElement (org.eclipse.jdt.core.IJavaElement)22 IResource (org.eclipse.core.resources.IResource)17 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)16 IPath (org.eclipse.core.runtime.IPath)15 ArrayList (java.util.ArrayList)13 IType (org.eclipse.jdt.core.IType)12 CoreException (org.eclipse.core.runtime.CoreException)11 Path (org.eclipse.core.runtime.Path)11 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 JavaModelException (org.eclipse.jdt.core.JavaModelException)10 IFile (org.eclipse.core.resources.IFile)9 IProject (org.eclipse.core.resources.IProject)9