Search in sources :

Example 46 with IFileBuffer

use of org.eclipse.core.filebuffers.IFileBuffer in project eclipse.platform.text by eclipse.

the class GenericFileBufferOperationRunner method createFileBuffers.

private IFileBuffer[] createFileBuffers(IPath[] locations, IProgressMonitor progressMonitor) throws CoreException {
    SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.FileBufferOperationRunner_task_connecting, locations.length);
    try {
        IFileBuffer[] fileBuffers = new ITextFileBuffer[locations.length];
        for (int i = 0; i < locations.length; i++) {
            fFileBufferManager.connect(locations[i], LocationKind.NORMALIZE, subMonitor.split(1));
            fileBuffers[i] = fFileBufferManager.getFileBuffer(locations[i], LocationKind.NORMALIZE);
        }
        return fileBuffers;
    } catch (CoreException x) {
        try {
            releaseFileBuffers(locations, new NullProgressMonitor());
        } catch (CoreException e) {
        }
        throw x;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IFileBuffer(org.eclipse.core.filebuffers.IFileBuffer) SubMonitor(org.eclipse.core.runtime.SubMonitor) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 47 with IFileBuffer

use of org.eclipse.core.filebuffers.IFileBuffer in project eclipse.platform.text by eclipse.

the class GenericFileBufferOperationRunner method execute.

/**
 * Executes the given operation for all file buffers specified by the given locations.
 *
 * @param locations the file buffer locations
 * @param operation the operation to be performed
 * @param monitor the progress monitor, or <code>null</code> if progress reporting is not desired
 * @throws CoreException in case of error
 * @throws OperationCanceledException in case the execution get canceled
 */
public void execute(IPath[] locations, final IFileBufferOperation operation, IProgressMonitor monitor) throws CoreException, OperationCanceledException {
    final int size = locations.length;
    SubMonitor subMonitor = SubMonitor.convert(monitor, operation.getOperationName(), size * 200);
    try {
        IFileBuffer[] fileBuffers = createFileBuffers(locations, subMonitor.split(size * 10));
        IFileBuffer[] fileBuffers2Save = findFileBuffersToSave(fileBuffers);
        fFileBufferManager.validateState(fileBuffers2Save, subMonitor.split(size * 10), fValidationContext);
        if (!isCommitable(fileBuffers2Save)) {
            throw new OperationCanceledException();
        }
        IFileBuffer[] unsynchronizedFileBuffers = findUnsynchronizedFileBuffers(fileBuffers);
        performOperation(unsynchronizedFileBuffers, operation, subMonitor.split(size * 40));
        final IFileBuffer[] synchronizedFileBuffers = findSynchronizedFileBuffers(fileBuffers);
        fIsCompleted = false;
        fThrowable = null;
        synchronized (fCompletionLock) {
            executeInContext(new Runnable() {

                @Override
                public void run() {
                    synchronized (fCompletionLock) {
                        try {
                            SafeRunner.run(new ISafeRunnable() {

                                @Override
                                public void handleException(Throwable throwable) {
                                    fThrowable = throwable;
                                }

                                @Override
                                public void run() throws Exception {
                                    performOperation(synchronizedFileBuffers, operation, subMonitor.split(50));
                                }
                            });
                        } finally {
                            fIsCompleted = true;
                            fCompletionLock.notifyAll();
                        }
                    }
                }
            });
            while (!fIsCompleted) {
                try {
                    fCompletionLock.wait(500);
                } catch (InterruptedException x) {
                }
            }
        }
        if (fThrowable != null) {
            if (fThrowable instanceof CoreException)
                throw (CoreException) fThrowable;
            throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, fThrowable.getLocalizedMessage(), fThrowable));
        }
        commit(fileBuffers2Save, subMonitor.split(size * 80));
    } finally {
        releaseFileBuffers(locations, subMonitor.split(size * 10));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) IFileBuffer(org.eclipse.core.filebuffers.IFileBuffer) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) SubMonitor(org.eclipse.core.runtime.SubMonitor) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable)

Aggregations

IFileBuffer (org.eclipse.core.filebuffers.IFileBuffer)47 Test (org.junit.Test)38 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)37 IDocument (org.eclipse.jface.text.IDocument)10 ArrayList (java.util.ArrayList)3 IStateValidationSupport (org.eclipse.core.filebuffers.IStateValidationSupport)3 PrintStream (java.io.PrintStream)2 PrintWriter (java.io.PrintWriter)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2 IStatus (org.eclipse.core.runtime.IStatus)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)2 IFile (org.eclipse.core.resources.IFile)1 IResource (org.eclipse.core.resources.IResource)1 IResourceRuleFactory (org.eclipse.core.resources.IResourceRuleFactory)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1