Example usage for org.eclipse.jface.operation IRunnableWithProgress run

List of usage examples for org.eclipse.jface.operation IRunnableWithProgress run

Introduction

In this page you can find the example usage for org.eclipse.jface.operation IRunnableWithProgress run.

Prototype

public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException;

Source Link

Document

Runs this operation.

Usage

From source file:org.eclipse.wst.server.ui.internal.wizard.TaskWizard.java

License:Open Source License

/**
 * Cancel the client selection./*from  w w w . j  ava2 s.c o m*/
 *
 * @return boolean
 */
public boolean performCancel() {
    final List list = getAllWizardFragments();
    IRunnableWithProgress runnable = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException {
            try {
                Iterator iterator = list.iterator();
                while (iterator.hasNext())
                    executeTask((WizardFragment) iterator.next(), CANCEL, monitor);
            } catch (CoreException ce) {
                throw new InvocationTargetException(ce);
            }
        }
    };

    Throwable t = null;
    try {
        if (getContainer() != null)
            getContainer().run(true, true, runnable);
        else
            runnable.run(new NullProgressMonitor());
        return true;
    } catch (InvocationTargetException te) {
        t = te.getCause();
    } catch (Exception e) {
        t = e;
    }
    if (Trace.SEVERE) {
        Trace.trace(Trace.STRING_SEVERE, "Error cancelling task wizard", t);
    }

    if (t instanceof CoreException) {
        EclipseUtil.openError(t.getLocalizedMessage(), ((CoreException) t).getStatus());
    } else
        EclipseUtil.openError(t.getLocalizedMessage());

    return false;

}

From source file:org.eclipse.wst.sse.ui.internal.handlers.ToggleLineCommentHandler.java

License:Open Source License

/**
 * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractCommentHandler#processAction(
 *    org.eclipse.ui.texteditor.ITextEditor, org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument,
 *    org.eclipse.jface.text.ITextSelection)
 *//*from  w  w w  .  j  av  a 2s .c  o m*/
protected void processAction(ITextEditor textEditor, final IStructuredDocument document,
        ITextSelection textSelection) {

    IStructuredModel model = null;
    DocumentRewriteSession session = null;
    boolean changed = false;

    try {
        // get text selection lines info
        int selectionStartLine = textSelection.getStartLine();
        int selectionEndLine = textSelection.getEndLine();

        int selectionEndLineOffset = document.getLineOffset(selectionEndLine);
        int selectionEndOffset = textSelection.getOffset() + textSelection.getLength();

        // adjust selection end line
        if ((selectionEndLine > selectionStartLine) && (selectionEndLineOffset == selectionEndOffset)) {
            selectionEndLine--;
        }

        // save the selection position since it will be changing
        Position selectionPosition = null;
        selectionPosition = new Position(textSelection.getOffset(), textSelection.getLength());
        document.addPosition(selectionPosition);

        model = StructuredModelManager.getModelManager().getModelForEdit(document);
        if (model != null) {
            //makes it so one undo will undo all the edits to the document
            model.beginRecording(this, SSEUIMessages.ToggleComment_label,
                    SSEUIMessages.ToggleComment_description);

            //keeps listeners from doing anything until updates are all done
            model.aboutToChangeModel();
            if (document instanceof IDocumentExtension4) {
                session = ((IDocumentExtension4) document)
                        .startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
            }
            changed = true;

            //get the display for the editor if we can
            Display display = null;
            if (textEditor instanceof StructuredTextEditor) {
                StructuredTextViewer viewer = ((StructuredTextEditor) textEditor).getTextViewer();
                if (viewer != null) {
                    display = viewer.getControl().getDisplay();
                }
            }

            //create the toggling operation
            IRunnableWithProgress toggleCommentsRunnable = new ToggleLinesRunnable(
                    model.getContentTypeIdentifier(), document, selectionStartLine, selectionEndLine, display);

            //if toggling lots of lines then use progress monitor else just run the operation
            if ((selectionEndLine - selectionStartLine) > TOGGLE_LINES_MAX_NO_BUSY_INDICATOR
                    && display != null) {
                ProgressMonitorDialog dialog = new ProgressMonitorDialog(display.getActiveShell());
                dialog.run(false, true, toggleCommentsRunnable);
            } else {
                toggleCommentsRunnable.run(new NullProgressMonitor());
            }
        }
    } catch (InvocationTargetException e) {
        Logger.logException("Problem running toggle comment progess dialog.", e); //$NON-NLS-1$
    } catch (InterruptedException e) {
        Logger.logException("Problem running toggle comment progess dialog.", e); //$NON-NLS-1$
    } catch (BadLocationException e) {
        Logger.logException("The given selection " + textSelection + " must be invalid", e); //$NON-NLS-1$ //$NON-NLS-2$
    } finally {
        //clean everything up
        if (session != null && document instanceof IDocumentExtension4) {
            ((IDocumentExtension4) document).stopRewriteSession(session);
        }

        if (model != null) {
            model.endRecording(this);
            if (changed) {
                model.changedModel();
            }
            model.releaseFromEdit();
        }
    }
}

From source file:org.fusesource.ide.branding.wizards.NewCamelTestWizard.java

License:Open Source License

private IRunnableWithProgress addJUnitToClasspath(IJavaProject project, final IRunnableWithProgress runnable,
        boolean isJUnit4) {
    String typeToLookup = isJUnit4 ? "org.junit.*" : "junit.awtui.*"; //$NON-NLS-1$//$NON-NLS-2$
    ClasspathFixProposal[] fixProposals = ClasspathFixProcessor.getContributedFixImportProposals(project,
            typeToLookup, null);/*w w  w  .j  a  va 2s.c o  m*/
    String superClass = "org.apache.camel.test.junit4.*";
    // String superClass = fPage1.getSuperClass();
    ClasspathFixProposal[] fixProposals2 = ClasspathFixProcessor.getContributedFixImportProposals(project,
            superClass, null);

    List<ClasspathFixProposal> proposals = new ArrayList<ClasspathFixProposal>();
    proposals.addAll(Arrays.asList(fixProposals));
    proposals.addAll(Arrays.asList(fixProposals2));
    fixProposals = proposals.toArray(new ClasspathFixProposal[proposals.size()]);

    ClasspathFixSelectionDialog dialog = new ClasspathFixSelectionDialog(getShell(), isJUnit4, project,
            fixProposals);
    if (dialog.open() != 0) {
        throw new OperationCanceledException();
    }

    final ClasspathFixProposal fix = dialog.getSelectedClasspathFix();
    if (fix != null) {
        return new IRunnableWithProgress() {

            /*
             * (non-Javadoc)
             * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
             */
            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                if (monitor == null) {
                    monitor = new NullProgressMonitor();
                }
                monitor.beginTask(WizardMessages.NewTestCaseCreationWizard_create_progress, 4);
                try {
                    Change change = fix.createChange(new SubProgressMonitor(monitor, 1));
                    new PerformChangeOperation(change).run(new SubProgressMonitor(monitor, 1));

                    runnable.run(new SubProgressMonitor(monitor, 2));
                } catch (OperationCanceledException e) {
                    throw new InterruptedException();
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                } finally {
                    monitor.done();
                }
            }
        };
    }
    return runnable;
}

From source file:org.fusesource.ide.branding.wizards.NewCamelTestWizard.java

License:Open Source License

private IRunnableWithProgress addCamelTestToPomDeps(final IJavaProject project,
        final IRunnableWithProgress runnable) throws Exception {
    return new IRunnableWithProgress() {
        @Override//from ww w .j av a  2  s .  c  o  m
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            if (monitor == null) {
                monitor = new NullProgressMonitor();
            }
            monitor.beginTask(WizardMessages.NewTestCaseCreationWizard_create_progress, 4);

            // first load the pom file into some model
            IPath pomPathValue = project.getProject().getRawLocation() != null
                    ? project.getProject().getRawLocation().append("pom.xml")
                    : ResourcesPlugin.getWorkspace().getRoot().getLocation()
                            .append(project.getPath().append("pom.xml"));
            String pomPath = pomPathValue.toOSString();
            final File pomFile = new File(pomPath);
            try {
                final Model model = MavenPlugin.getMaven().readModel(pomFile);

                // then check if camel-test is already a dep
                boolean isBlueprint = isBlueprintFile(fPage1.getXmlFileUnderTest().getLocationURI().toString());
                boolean hasCamelSpringTestDep = false;
                boolean hasCamelBPTestDep = false;
                List<Dependency> deps = model.getDependencies();
                for (Dependency dep : deps) {
                    if (dep.getArtifactId().startsWith(CAMEL_ARTIFACT_ID_WILDCARD)
                            && dep.getGroupId().equalsIgnoreCase(CAMEL_GROUP_ID) && camelVersion == null) {
                        camelVersion = dep.getVersion();
                    }
                    if (!isBlueprint && dep.getArtifactId().equalsIgnoreCase(CAMEL_SPRING_TEST_ARTIFACT_ID)) {
                        hasCamelSpringTestDep = true;
                        break;
                    }
                    if (isBlueprint && dep.getArtifactId().equalsIgnoreCase(CAMEL_BP_TEST_ARTIFACT_ID)) {
                        hasCamelBPTestDep = true;
                        break;
                    }
                }

                boolean changes = false;

                if (!isBlueprint && !hasCamelSpringTestDep) {
                    Dependency dep = new Dependency();
                    dep.setGroupId(CAMEL_GROUP_ID);
                    dep.setArtifactId(CAMEL_SPRING_TEST_ARTIFACT_ID);
                    dep.setVersion(camelVersion);
                    dep.setScope(CAMEL_TEST_SCOPE);
                    model.addDependency(dep);
                    changes = true;
                }

                if (isBlueprint && !hasCamelBPTestDep) {
                    Dependency dep = new Dependency();
                    dep.setGroupId(CAMEL_GROUP_ID);
                    dep.setArtifactId(CAMEL_BP_TEST_ARTIFACT_ID);
                    dep.setVersion(camelVersion);
                    dep.setScope(CAMEL_TEST_SCOPE);
                    model.addDependency(dep);
                    changes = true;
                }

                if (changes) {
                    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(pomFile))) {
                        MavenPlugin.getMaven().writeModel(model, os);
                        IFile pomIFile = project.getProject().getFile("pom.xml");
                        if (pomIFile != null) {
                            pomIFile.refreshLocal(IResource.DEPTH_INFINITE, monitor);
                        }
                        runnable.run(new SubProgressMonitor(monitor, 2));
                    } catch (Exception ex) {
                        Activator.getLogger().error(ex);
                    }
                }
            } catch (Exception ex) {
                Activator.getLogger().error(ex);
            } finally {
                monitor.done();
            }
        }
    };
}

From source file:org.jboss.tools.arquillian.ui.internal.wizards.NewArquillianJUnitTestWizard.java

License:Open Source License

private IRunnableWithProgress addArquillianToClasspath(IJavaProject project,
        final IRunnableWithProgress runnable) {
    ClasspathFixProposal[] fixProposals = new ArquillianClasspathFixProposal[1];
    fixProposals[0] = new ArquillianClasspathFixProposal(project, 15);

    ClasspathFixSelectionDialog dialog = new ClasspathFixSelectionDialog(getShell(), project, fixProposals);
    if (dialog.open() != 0) {
        throw new OperationCanceledException();
    }/*from w w  w.  j  ava2 s .  c o m*/

    final ClasspathFixProposal fix = dialog.getSelectedClasspathFix();
    if (fix != null) {
        return new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                if (monitor == null) {
                    monitor = new NullProgressMonitor();
                }
                monitor.beginTask("Create Arquillian JUnit test case", 4);
                try {
                    Change change = fix.createChange(new SubProgressMonitor(monitor, 1));
                    new PerformChangeOperation(change).run(new SubProgressMonitor(monitor, 1));

                    runnable.run(new SubProgressMonitor(monitor, 2));
                } catch (OperationCanceledException e) {
                    throw new InterruptedException();
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                } finally {
                    monitor.done();
                }
            }
        };
    }
    return runnable;
}

From source file:org.jboss.tools.discovery.core.internal.connectors.JBossDiscoveryUi.java

License:Open Source License

/**
 * Install the specified connectors.//from  w  w w  .  j  ava2  s  .  c  o  m
 * @param descriptors the specified connectors. Those must have actual content to install.
 * @param context
 * @return true if installation performed successfully
 */
public static boolean install(Collection<ConnectorDescriptor> descriptors, IRunnableContext context) {
    for (ConnectorDescriptor toInstall : descriptors) {
        if (toInstall.getInstallableUnits() == null || toInstall.getInstallableUnits().isEmpty()) {
            return false;
        }
    }
    try {
        IRunnableWithProgress runner = createInstallJob(descriptors);
        if (context != null) {
            context.run(true, true, runner);
        } else {
            runner.run(new NullProgressMonitor());
        }

        // update stats
        new DiscoveryFeedbackJob(descriptors instanceof List ? (List<ConnectorDescriptor>) descriptors
                : new ArrayList<>(descriptors)).schedule();
        recordInstalled(descriptors);
    } catch (InvocationTargetException e) {
        IStatus status = new Status(IStatus.ERROR, DiscoveryActivator.PLUGIN_ID,
                NLS.bind(Messages.ConnectorDiscoveryWizard_installProblems,
                        new Object[] { e.getCause().getMessage() }),
                e.getCause());
        StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.BLOCK | StatusManager.LOG);
        return false;
    } catch (InterruptedException e) {
        // canceled
        return false;
    }
    return true;
}

From source file:org.jboss.tools.foundation.ui.xpl.taskwizard.TaskWizard.java

License:Open Source License

/**
 * Cancel the client selection.//  www .  j a v  a2 s. co m
 *
 * @return boolean
 */
public boolean performCancel() {
    final List<WizardFragment> list = getAllWizardFragments();
    IRunnableWithProgress runnable = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException {
            try {
                Iterator<WizardFragment> iterator = list.iterator();
                while (iterator.hasNext())
                    executeTask((WizardFragment) iterator.next(), CANCEL, monitor);
            } catch (CoreException ce) {
                throw new InvocationTargetException(ce);
            }
        }
    };

    Throwable t = null;
    try {
        if (getContainer() != null)
            getContainer().run(true, true, runnable);
        else
            runnable.run(new NullProgressMonitor());
        return true;
    } catch (InvocationTargetException te) {
        t = te.getCause();
    } catch (Exception e) {
        t = e;
    }
    Trace.trace(Trace.STRING_SEVERE, "Error cancelling task wizard", t); //$NON-NLS-1$
    handleThrowable(t);
    return false;

}

From source file:org.jkiss.dbeaver.runtime.DummyRunnableContext.java

License:Apache License

@Override
public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
        throws InvocationTargetException, InterruptedException {
    runnable.run(new NullProgressMonitor());
}

From source file:org.jkiss.dbeaver.ui.editors.text.BaseTextDocumentProvider.java

License:Open Source License

@Override
protected IRunnableContext getOperationRunner(final IProgressMonitor monitor) {
    return new IRunnableContext() {
        @Override//  ww  w  .j  a  v a 2s .  c om
        public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
                throws InvocationTargetException, InterruptedException {
            runnable.run(monitor);
        }
    };
}

From source file:org.locationtech.udig.catalog.internal.ui.ResourceSelectionPage.java

License:Open Source License

private List<IResolve> getGeoResources(final IResolve resolve, boolean fork) {
    if (resolveMap.get(resolve) == null || resolveMap.isEmpty()) {
        final List<IResolve> list = new ArrayList<IResolve>();

        try {//from   www .  j  av a  2 s.  com
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                public void run(IProgressMonitor monitor) {
                    monitor.beginTask(Messages.ResourceSelectionPage_searching, IProgressMonitor.UNKNOWN);
                    try {
                        List<IResolve> members = resolve.members(monitor);
                        list.addAll(members);
                        if (schemaSelected != null) {
                            for (IResolve resolve2 : members) {
                                IResolveFolder folder = (IResolveFolder) resolve2;
                                if (folder.getTitle() != schemaSelected) {
                                    list.remove(resolve2);
                                }
                            }
                        }
                    } catch (Exception e) {
                        // do nothing
                        CatalogUIPlugin.log("Error finding resources", e); //$NON-NLS-1$
                    }
                    monitor.done();
                }

            };
            if (fork) {
                getContainer().run(false, true, runnable);
            } else {
                runnable.run(new NullProgressMonitor());
            }
        } catch (Exception e) {
            CatalogUIPlugin.log("", e); //$NON-NLS-1$
        }
        resolveMap.put(resolve, list);
    }
    return resolveMap.get(resolve);
}