Search in sources :

Example 1 with NullChange

use of org.eclipse.ltk.core.refactoring.NullChange in project che by eclipse.

the class MultiStateUndoChange method perform.

/**
	 * {@inheritDoc}
	 */
public Change perform(IProgressMonitor pm) throws CoreException {
    if (fValidationState == null || fValidationState.isValid(needsSaving(), false).hasFatalError())
        return new NullChange();
    if (pm == null)
        pm = new NullProgressMonitor();
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    //$NON-NLS-1$
    pm.beginTask("", 2);
    ITextFileBuffer buffer = null;
    try {
        manager.connect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
        buffer = manager.getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
        IDocument document = buffer.getDocument();
        ContentStamp currentStamp = ContentStamps.get(fFile, document);
        // perform the changes
        LinkedList list = new LinkedList();
        for (int index = 0; index < fUndos.length; index++) {
            UndoEdit edit = fUndos[index];
            UndoEdit redo = edit.apply(document, TextEdit.CREATE_UNDO);
            list.addFirst(redo);
        }
        // try to restore the document content stamp
        boolean success = ContentStamps.set(document, fContentStampToRestore);
        if (needsSaving()) {
            buffer.commit(pm, false);
            if (!success) {
                // We weren't able to restore document stamp.
                // Since we save restore the file stamp instead
                ContentStamps.set(fFile, fContentStampToRestore);
            }
        }
        return createUndoChange((UndoEdit[]) list.toArray(new UndoEdit[list.size()]), currentStamp);
    } catch (BadLocationException e) {
        throw Changes.asCoreException(e);
    } finally {
        if (buffer != null)
            manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ContentStamp(org.eclipse.ltk.core.refactoring.ContentStamp) UndoEdit(org.eclipse.text.edits.UndoEdit) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IDocument(org.eclipse.jface.text.IDocument) LinkedList(java.util.LinkedList) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with NullChange

use of org.eclipse.ltk.core.refactoring.NullChange 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 3 with NullChange

use of org.eclipse.ltk.core.refactoring.NullChange in project che by eclipse.

the class ClasspathChange method perform.

@Override
public Change perform(IProgressMonitor pm) throws CoreException {
    pm.beginTask(RefactoringCoreMessages.ClasspathChange_progress_message, 1);
    try {
        if (!JavaConventions.validateClasspath(fProject, fNewClasspath, fOutputLocation).matches(IStatus.ERROR)) {
            IClasspathEntry[] oldClasspath = fProject.getRawClasspath();
            IPath oldOutputLocation = fProject.getOutputLocation();
            fProject.setRawClasspath(fNewClasspath, fOutputLocation, new SubProgressMonitor(pm, 1));
            return new ClasspathChange(fProject, oldClasspath, oldOutputLocation);
        } else {
            return new NullChange();
        }
    } finally {
        pm.done();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) NullChange(org.eclipse.ltk.core.refactoring.NullChange) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 4 with NullChange

use of org.eclipse.ltk.core.refactoring.NullChange 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 5 with NullChange

use of org.eclipse.ltk.core.refactoring.NullChange 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

NullChange (org.eclipse.ltk.core.refactoring.NullChange)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IResource (org.eclipse.core.resources.IResource)3 HashMap (java.util.HashMap)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 ResourceDescription (org.eclipse.ui.ide.undo.ResourceDescription)2 LinkedList (java.util.LinkedList)1 Entry (java.util.Map.Entry)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Status (org.eclipse.core.runtime.Status)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1