Search in sources :

Example 1 with ContentStamp

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

Aggregations

LinkedList (java.util.LinkedList)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ContentStamp (org.eclipse.ltk.core.refactoring.ContentStamp)1 NullChange (org.eclipse.ltk.core.refactoring.NullChange)1 UndoEdit (org.eclipse.text.edits.UndoEdit)1