Search in sources :

Example 51 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.

the class ConvertLineDelimitersOperation method computeTextEdit.

@Override
protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException {
    IDocument document = fileBuffer.getDocument();
    int lineCount = document.getNumberOfLines();
    SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.ConvertLineDelimitersOperation_task_generatingChanges, lineCount);
    try {
        MultiTextEditWithProgress multiEdit = new MultiTextEditWithProgress(FileBuffersMessages.ConvertLineDelimitersOperation_task_applyingChanges);
        for (int i = 0; i < lineCount; i++) {
            final String delimiter = document.getLineDelimiter(i);
            if (delimiter != null && delimiter.length() > 0 && !delimiter.equals(fLineDelimiter)) {
                IRegion region = document.getLineInformation(i);
                multiEdit.addChild(new ReplaceEdit(region.getOffset() + region.getLength(), delimiter.length(), fLineDelimiter));
            }
            subMonitor.split(1);
        }
        return multiEdit.getChildrenSize() <= 0 ? null : multiEdit;
    } catch (BadLocationException x) {
        // $NON-NLS-1$
        throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) SubMonitor(org.eclipse.core.runtime.SubMonitor) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 52 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.

the class GenericFileBufferOperationRunner method releaseFileBuffers.

private void releaseFileBuffers(IPath[] locations, IProgressMonitor progressMonitor) throws CoreException {
    SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.FileBufferOperationRunner_task_disconnecting, locations.length);
    final ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    for (IPath location : locations) {
        fileBufferManager.disconnect(location, LocationKind.NORMALIZE, subMonitor.split(1));
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) SubMonitor(org.eclipse.core.runtime.SubMonitor)

Example 53 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor 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 54 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor 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)

Example 55 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.

the class RemoveTrailingWhitespaceOperation method computeTextEdit.

@Override
protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException {
    IDocument document = fileBuffer.getDocument();
    int lineCount = document.getNumberOfLines();
    SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_generatingChanges, lineCount);
    try {
        MultiTextEditWithProgress multiEdit = new MultiTextEditWithProgress(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_applyingChanges);
        for (int i = 0; i < lineCount; i++) {
            IRegion region = document.getLineInformation(i);
            if (region.getLength() == 0)
                continue;
            int lineStart = region.getOffset();
            int lineExclusiveEnd = lineStart + region.getLength();
            int j = lineExclusiveEnd - 1;
            while (j >= lineStart && Character.isWhitespace(document.getChar(j))) --j;
            ++j;
            if (j < lineExclusiveEnd)
                multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j));
            subMonitor.split(1);
        }
        return multiEdit.getChildrenSize() <= 0 ? null : multiEdit;
    } catch (BadLocationException x) {
        // $NON-NLS-1$
        throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) SubMonitor(org.eclipse.core.runtime.SubMonitor) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

SubMonitor (org.eclipse.core.runtime.SubMonitor)61 CoreException (org.eclipse.core.runtime.CoreException)27 IStatus (org.eclipse.core.runtime.IStatus)25 Status (org.eclipse.core.runtime.Status)24 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)15 IOException (java.io.IOException)12 IFile (org.eclipse.core.resources.IFile)10 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)10 InputStream (java.io.InputStream)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 IPath (org.eclipse.core.runtime.IPath)9 IProject (org.eclipse.core.resources.IProject)8 File (java.io.File)7 MultiStatus (org.eclipse.core.runtime.MultiStatus)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 IContainer (org.eclipse.core.resources.IContainer)4 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)4 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)4 URI (java.net.URI)3