Search in sources :

Example 71 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class DeleteResourceChange method saveFileIfNeeded.

private static void saveFileIfNeeded(IFile file, IProgressMonitor pm) throws CoreException {
    ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
    if (buffer != null && buffer.isDirty() && buffer.isStateValidated() && buffer.isSynchronized()) {
        //$NON-NLS-1$
        pm.beginTask("", 2);
        buffer.commit(new SubProgressMonitor(pm, 1), false);
        file.refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(pm, 1));
        pm.done();
    } else {
        //$NON-NLS-1$
        pm.beginTask("", 1);
        pm.worked(1);
        pm.done();
    }
}
Also used : ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 72 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class UndoTextFileChange method isValid.

/**
	 * {@inheritDoc}
	 */
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
    if (pm == null)
        pm = new NullProgressMonitor();
    //$NON-NLS-1$
    pm.beginTask("", 1);
    try {
        if (fValidationState == null)
            //$NON-NLS-1$
            throw new CoreException(new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), "UndoTextFileChange has not been initialialized"));
        ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
        fDirty = buffer != null && buffer.isDirty();
        return fValidationState.isValid(needsSaving(), true);
    } finally {
        pm.done();
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 73 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class UndoTextFileChange method perform.

/**
	 * {@inheritDoc}
	 */
public Change perform(IProgressMonitor pm) throws CoreException {
    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);
        boolean[] setContentStampSuccess = { false };
        UndoEdit redo = performEdits(buffer, document, setContentStampSuccess);
        if (needsSaving()) {
            buffer.commit(pm, false);
            if (!setContentStampSuccess[0]) {
                // We weren't able to restore document stamp.
                // Since we save restore the file stamp instead
                ContentStamps.set(fFile, fContentStampToRestore);
            }
        }
        return createUndoChange(redo, currentStamp);
    } catch (BadLocationException e) {
        if (fValidationState == null || !fValidationState.wasDerived())
            throw Changes.asCoreException(e);
        else
            return new NullChange();
    } catch (MalformedTreeException e) {
        if (fValidationState == null || !fValidationState.wasDerived())
            throw Changes.asCoreException(e);
        else
            return new NullChange();
    } catch (CoreException e) {
        if (fValidationState == null || !fValidationState.wasDerived())
            throw e;
        else
            return new NullChange();
    } finally {
        if (buffer != null)
            manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) UndoEdit(org.eclipse.text.edits.UndoEdit) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 74 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class ModificationStampValidationState method getBuffer.

protected static ITextFileBuffer getBuffer(IFile file) {
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    IPath path = file.getFullPath();
    ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE);
    return buffer;
}
Also used : IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 75 with ITextFileBuffer

use of org.eclipse.core.filebuffers.ITextFileBuffer in project che by eclipse.

the class UndoDocumentChange method performEdits.

private UndoEdit performEdits() throws BadLocationException, MalformedTreeException {
    ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    ITextFileBuffer fileBuffer = fileBufferManager.getTextFileBuffer(fDocument);
    if (fileBuffer == null || !fileBuffer.isSynchronizationContextRequested()) {
        return fUndo.apply(fDocument, TextEdit.CREATE_UNDO);
    }
    /** The lock for waiting for computation in the UI thread to complete. */
    final Lock completionLock = new Lock();
    final UndoEdit[] result = new UndoEdit[1];
    final BadLocationException[] exception = new BadLocationException[1];
    Runnable runnable = new Runnable() {

        public void run() {
            synchronized (completionLock) {
                try {
                    result[0] = fUndo.apply(fDocument, TextEdit.CREATE_UNDO);
                } catch (BadLocationException e) {
                    exception[0] = e;
                } finally {
                    completionLock.fDone = true;
                    completionLock.notifyAll();
                }
            }
        }
    };
    synchronized (completionLock) {
        fileBufferManager.execute(runnable);
        while (!completionLock.fDone) {
            try {
                completionLock.wait(500);
            } catch (InterruptedException x) {
            }
        }
    }
    if (exception[0] != null) {
        throw exception[0];
    }
    return result[0];
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)133 Test (org.junit.Test)85 IDocument (org.eclipse.jface.text.IDocument)54 IFileBuffer (org.eclipse.core.filebuffers.IFileBuffer)37 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)35 IPath (org.eclipse.core.runtime.IPath)31 IFile (org.eclipse.core.resources.IFile)18 BadLocationException (org.eclipse.jface.text.BadLocationException)15 IFileStore (org.eclipse.core.filesystem.IFileStore)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 CoreException (org.eclipse.core.runtime.CoreException)8 Position (org.eclipse.jface.text.Position)8 IFileInfo (org.eclipse.core.filesystem.IFileInfo)7 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)7 Match (org.eclipse.search.ui.text.Match)7 IFolder (org.eclipse.core.resources.IFolder)5 IStatus (org.eclipse.core.runtime.IStatus)5 Path (org.eclipse.core.runtime.Path)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 Status (org.eclipse.core.runtime.Status)4