Search in sources :

Example 46 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project xtext-xtend by eclipse.

the class XtendLibClasspathAdder method addLibsToClasspath.

public void addLibsToClasspath(IJavaProject javaProject, IProgressMonitor monitor) {
    try {
        SubMonitor progress = SubMonitor.convert(monitor, 2);
        IProject project = javaProject.getProject();
        if (!project.hasNature(PLUGIN_NATURE) || !addToPluginManifest(project, progress.newChild(1)))
            addToClasspath(javaProject, progress.newChild(1));
    } catch (Exception exc) {
        LOG.error("Error adding Xtend libs to classpath", exc);
    }
}
Also used : SubMonitor(org.eclipse.core.runtime.SubMonitor) IProject(org.eclipse.core.resources.IProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException)

Example 47 with SubMonitor

use of org.eclipse.core.runtime.SubMonitor in project AutoRefactor by JnRouvignac.

the class ApplyRefactoringsJob method run0.

private IStatus run0(IProgressMonitor monitor) throws Exception {
    if (refactoringUnits.isEmpty()) {
        // No java project exists.
        return Status.OK_STATUS;
    }
    final SubMonitor loopMonitor = SubMonitor.convert(monitor, refactoringUnits.size());
    try {
        RefactoringUnit toRefactor;
        while ((toRefactor = refactoringUnits.poll()) != null) {
            final ICompilationUnit compilationUnit = toRefactor.getCompilationUnit();
            final JavaProjectOptions options = toRefactor.getOptions();
            try {
                loopMonitor.subTask("Applying refactorings to " + getClassName(compilationUnit));
                final AggregateASTVisitor refactoring = new AggregateASTVisitor(refactoringRulesToApply);
                applyRefactoring(compilationUnit, refactoring, options, loopMonitor.newChild(1));
            } catch (OperationCanceledException e) {
                throw e;
            } catch (Exception e) {
                final String msg = "Exception when applying refactorings to file \"" + compilationUnit.getPath() + "\": " + e.getMessage();
                throw new UnhandledException(null, msg, e);
            }
        }
    } finally {
        loopMonitor.done();
    }
    return Status.OK_STATUS;
}
Also used : UnhandledException(org.autorefactor.util.UnhandledException) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) AggregateASTVisitor(org.autorefactor.refactoring.rules.AggregateASTVisitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IllegalStateException(org.autorefactor.util.IllegalStateException) UnhandledException(org.autorefactor.util.UnhandledException)

Example 48 with SubMonitor

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

the class FileBufferOperationHandler method doRun.

/**
 * Runs the given operation.
 *
 * @param files the file on which to run this operation
 * @param location the file buffer location
 * @param fileBufferOperation the operation to run
 */
protected final void doRun(final IFile[] files, final IPath location, final IFileBufferOperation fileBufferOperation) {
    Job job = new Job(fileBufferOperation.getOperationName()) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            IStatus status;
            try {
                int ticks = 100;
                SubMonitor subMonitor = SubMonitor.convert(monitor, fFileBufferOperation.getOperationName(), ticks);
                IPath[] locations;
                if (files != null) {
                    ticks -= 30;
                    locations = generateLocations(files, subMonitor.split(30));
                } else
                    locations = new IPath[] { location };
                if (locations != null && locations.length > 0) {
                    FileBufferOperationRunner runner = new FileBufferOperationRunner(FileBuffers.getTextFileBufferManager(), getShell());
                    runner.execute(locations, fileBufferOperation, subMonitor.split(ticks));
                }
                status = Status.OK_STATUS;
            } catch (OperationCanceledException e) {
                // $NON-NLS-1$
                status = new Status(IStatus.CANCEL, EditorsUI.PLUGIN_ID, IStatus.OK, "", null);
            } catch (CoreException e) {
                // $NON-NLS-1$
                status = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "", e);
            }
            return status;
        }
    };
    job.setUser(true);
    job.schedule();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) FileBufferOperationRunner(org.eclipse.core.filebuffers.manipulation.FileBufferOperationRunner) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Example 49 with SubMonitor

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

the class ResourceTextFileBuffer method commitFileBufferContent.

@Override
protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException {
    if (!isSynchronized() && !overwrite) {
        String message = NLSUtility.format(FileBuffersMessages.FileBuffer_error_outOfSync, getFileStore().toURI());
        throw new CoreException(new Status(IStatus.WARNING, FileBuffersPlugin.PLUGIN_ID, IResourceStatus.OUT_OF_SYNC_LOCAL, message, null));
    }
    String encoding = computeEncoding();
    if (fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16.equals(encoding))
        encoding = CHARSET_UTF_16LE;
    Charset charset;
    try {
        charset = Charset.forName(encoding);
    } catch (UnsupportedCharsetException ex) {
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_unsupported_encoding_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
        throw new CoreException(s);
    } catch (IllegalCharsetNameException ex) {
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_illegal_encoding_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
        throw new CoreException(s);
    }
    CharsetEncoder encoder = charset.newEncoder();
    encoder.onMalformedInput(CodingErrorAction.REPLACE);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    InputStream stream;
    try {
        byte[] bytes;
        ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(fDocument.get()));
        if (byteBuffer.hasArray())
            bytes = byteBuffer.array();
        else {
            bytes = new byte[byteBuffer.limit()];
            byteBuffer.get(bytes);
        }
        stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
    } catch (CharacterCodingException ex) {
        Assert.isTrue(ex instanceof UnmappableCharacterException);
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_charset_mapping_failed_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CHARSET_MAPPING_FAILED, message, ex);
        throw new CoreException(s);
    }
    /*
		 * XXX:
		 * This is a workaround for a corresponding bug in Java readers and writer,
		 * see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
		 */
    if (fBOM == IContentDescription.BOM_UTF_8 && CHARSET_UTF_8.equals(encoding))
        stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
    if (fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16LE.equals(encoding))
        stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_16LE), stream);
    if (fFile.exists()) {
        // here the file synchronizer should actually be removed and afterwards added again. However,
        // we are already inside an operation, so the delta is sent AFTER we have added the listener
        fFile.setContents(stream, overwrite, true, monitor);
        if (fDocument instanceof IDocumentExtension4) {
            fSynchronizationStamp = ((IDocumentExtension4) fDocument).getModificationStamp();
            fFile.revertModificationStamp(fSynchronizationStamp);
        } else
            fSynchronizationStamp = fFile.getModificationStamp();
        if (fAnnotationModel instanceof IPersistableAnnotationModel) {
            IPersistableAnnotationModel persistableModel = (IPersistableAnnotationModel) fAnnotationModel;
            persistableModel.commit(fDocument);
        }
    } else {
        SubMonitor subMonitor = SubMonitor.convert(monitor, FileBuffersMessages.ResourceTextFileBuffer_task_saving, 2);
        ContainerCreator creator = new ContainerCreator(fFile.getWorkspace(), fFile.getParent().getFullPath());
        creator.createContainer(subMonitor.split(1));
        fFile.create(stream, false, subMonitor.split(1));
        // set synchronization stamp to know whether the file synchronizer must become active
        fSynchronizationStamp = fFile.getModificationStamp();
        subMonitor.split(1);
    // TODO commit persistable annotation model
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) IPersistableAnnotationModel(org.eclipse.core.filebuffers.IPersistableAnnotationModel) IStatus(org.eclipse.core.runtime.IStatus) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) SubMonitor(org.eclipse.core.runtime.SubMonitor) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CoreException(org.eclipse.core.runtime.CoreException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) ContainerCreator(org.eclipse.core.filebuffers.manipulation.ContainerCreator)

Example 50 with SubMonitor

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

the class ContainerCreator method createProject.

private IProject createProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException {
    SubMonitor subMonitor = SubMonitor.convert(monitor, 2);
    projectHandle.create(subMonitor.split(1));
    projectHandle.open(subMonitor.split(1));
    return projectHandle;
}
Also used : SubMonitor(org.eclipse.core.runtime.SubMonitor)

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