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.team.internal.ui.actions.TeamAction.java

License:Open Source License

/**
 * Convenience method for running an operation with progress and
 * error feedback./*from   w w w.  j  a va2 s  .com*/
 * 
 * @param runnable  the runnable which executes the operation
 * @param problemMessage  the message to display in the case of errors
 * @param progressKind  one of PROGRESS_BUSYCURSOR or PROGRESS_DIALOG
 */
final protected void run(final IRunnableWithProgress runnable, final String problemMessage, int progressKind) {
    final Exception[] exceptions = new Exception[] { null };
    switch (progressKind) {
    case PROGRESS_BUSYCURSOR:
        BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
            public void run() {
                try {
                    runnable.run(new NullProgressMonitor());
                } catch (InvocationTargetException e) {
                    exceptions[0] = e;
                } catch (InterruptedException e) {
                    exceptions[0] = null;
                }
            }
        });
        break;
    default:
    case PROGRESS_DIALOG:
        try {
            new ProgressMonitorDialog(getShell()).run(true, true, runnable);
        } catch (InvocationTargetException e) {
            exceptions[0] = e;
        } catch (InterruptedException e) {
            exceptions[0] = null;
        }
        break;
    }
    if (exceptions[0] != null) {
        handle(exceptions[0], null, problemMessage);
    }
}

From source file:org.eclipse.team.internal.ui.Utils.java

License:Open Source License

public static void runWithProgress(Shell parent, boolean cancelable, final IRunnableWithProgress runnable)
        throws InvocationTargetException, InterruptedException {
    boolean createdShell = false;
    try {/*w ww  . j  a v  a2s .  c o m*/
        if (parent == null || parent.isDisposed()) {
            Display display = Display.getCurrent();
            if (display == null) {
                // cannot provide progress (not in UI thread)
                runnable.run(new NullProgressMonitor());
                return;
            }
            // get the active shell or a suitable top-level shell
            parent = display.getActiveShell();
            if (parent == null) {
                parent = new Shell(display);
                createdShell = true;
            }
        }
        // pop up progress dialog after a short delay
        final Exception[] holder = new Exception[1];
        BusyIndicator.showWhile(parent.getDisplay(), new Runnable() {

            public void run() {
                try {
                    runnable.run(new NullProgressMonitor());
                } catch (InvocationTargetException e) {
                    holder[0] = e;
                } catch (InterruptedException e) {
                    holder[0] = e;
                }
            }
        });
        if (holder[0] != null) {
            if (holder[0] instanceof InvocationTargetException) {
                throw (InvocationTargetException) holder[0];
            } else {
                throw (InterruptedException) holder[0];
            }
        }
        //new TimeoutProgressMonitorDialog(parent, TIMEOUT).run(true
        // /*fork*/, cancelable, runnable);
    } finally {
        if (createdShell)
            parent.dispose();
    }
}

From source file:org.eclipse.ui.internal.SaveableHelper.java

License:Open Source License

/**
 * Runs a progress monitor operation.//from   w  w  w  . j a v  a 2  s .co  m
 * Returns true if success, false if canceled or an error occurred.
 */
static boolean runProgressMonitorOperation(String opName, final IRunnableWithProgress progressOp,
        final IRunnableContext runnableContext, final IShellProvider shellProvider) {
    final boolean[] success = new boolean[] { false };
    IRunnableWithProgress runnable = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            progressOp.run(monitor);
            // Only indicate success if the monitor wasn't canceled
            if (!monitor.isCanceled())
                success[0] = true;
        }
    };

    try {
        runnableContext.run(false, true, runnable);
    } catch (InvocationTargetException e) {
        String title = NLS.bind(WorkbenchMessages.EditorManager_operationFailed, opName);
        Throwable targetExc = e.getTargetException();
        WorkbenchPlugin.log(title, new Status(IStatus.WARNING, PlatformUI.PLUGIN_ID, 0, title, targetExc));
        StatusUtil.handleStatus(title, targetExc, StatusManager.SHOW, shellProvider.getShell());
        // Fall through to return failure
    } catch (InterruptedException e) {
        // The user pressed cancel. Fall through to return failure
    } catch (OperationCanceledException e) {
        // The user pressed cancel. Fall through to return failure
    }
    return success[0];
}

From source file:org.eclipse.ui.internal.WorkbenchWindow.java

License:Open Source License

public void run(final boolean fork, boolean cancelable, final IRunnableWithProgress runnable)
        throws InvocationTargetException, InterruptedException {
    final StatusLineManager manager = getStatusLineManager();

    // Temporary Hack for bug 330106, remove when bug 334093 is fixed
    boolean progressHack = manager.getControl() == null;
    if (manager == null || progressHack) {
        runnable.run(new NullProgressMonitor());
    } else {//from  w  ww . j  a v  a  2  s  .  com
        boolean wasCancelEnabled = manager.isCancelEnabled();
        try {
            manager.setCancelEnabled(cancelable);

            final InvocationTargetException[] ite = new InvocationTargetException[1];
            final InterruptedException[] ie = new InterruptedException[1];

            BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
                public void run() {
                    try {
                        ModalContext.run(runnable, fork, manager.getProgressMonitor(), getShell().getDisplay());
                    } catch (InvocationTargetException e) {
                        ite[0] = e;
                    } catch (InterruptedException e) {
                        ie[0] = e;
                    } finally {
                        manager.getProgressMonitor().done();
                    }
                }
            });

            if (ite[0] != null) {
                throw ite[0];
            } else if (ie[0] != null) {
                throw ie[0];
            }
        } finally {
            manager.setCancelEnabled(wasCancelEnabled);
        }
    }
}

From source file:org.eclipse.wb.tests.designer.android.tests.AndroidProjectUtils.java

License:Open Source License

/**
 * @return {@link true} if new Android project successfully created.
 *//*  w  ww  .  ja  v a  2s. com*/
public static boolean createNewProject(IProject project, IAndroidTarget target, String minSdk)
        throws Exception {
    // create Android project by ADT wizard creator
    NewProjectWizardState projectValues = new NewProjectWizardState(Mode.ANY);
    {
        projectValues.useExisting = false;
        projectValues.projectName = project.getName();
        projectValues.projectNameModifiedByUser = true;
        {
            String locationString = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()
                    + project.getFullPath().toOSString();
            projectValues.projectLocation = new File(locationString);
        }
        projectValues.applicationName = projectValues.projectName;
        projectValues.packageName = PACKAGE_NAME;
        projectValues.packageNameModifiedByUser = true;
        projectValues.activityName = projectValues.projectName + "Activity";
        projectValues.createActivity = true;
        projectValues.target = target;
        {
            if (minSdk != null) {
                projectValues.minSdk = minSdk;
                projectValues.minSdkModifiedByUser = true;
            }
        }
    }
    IRunnableContext context = new IRunnableContext() {
        @Override
        public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
                throws InvocationTargetException, InterruptedException {
            runnable.run(new NullProgressMonitor());
        }
    };
    NewProjectCreator creator = new NewProjectCreator(projectValues, context);
    return creator.createAndroidProjects();
}

From source file:org.eclipse.wst.command.internal.env.ui.widgets.SimpleCommandEngineManager.java

License:Open Source License

/**
 * This method undoes the commands that were executed in the runForwardToNextStop
 * method./*from w w w .ja v  a 2s .  c  om*/
 * @param context the runnable context.
 * @return returns if all commands have been undone or not.
 */
public boolean runUndoToNextStop(IRunnableContext context) {
    final boolean result[] = new boolean[] { true };

    IRunnableWithProgress undoOperation = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) {
            result[0] = engine_.undoToLastStop(monitor);
        }
    };

    try {
        if (context == null) {
            // We don't have a container yet so just run the operation.
            undoOperation.run(null);
        } else {
            // We have a container where this operation can run and have
            // its progress displayed.
            context.run(false, false, undoOperation);
        }
    } catch (Exception exc) {
        // For now we will ignore all exceptions.
    }

    return result[0];
}

From source file:org.eclipse.wst.command.internal.env.ui.widgets.SimpleCommandEngineManager.java

License:Open Source License

/**
 * The method executes the CommandFragment commands in the context provided.
 * //from  w  w w.  j  av  a2s. co m
 * @param context the context
 * @return returns the status of executing the commands.
 */
public IStatus runForwardToNextStop(IRunnableContext context) {
    IRunnableWithProgress operation = null;

    doNotRunInTransaction_ = false;

    do {
        // We will stop unless we are changing transaction modes.
        foundStop_ = true;

        if (doNotRunInTransaction_) {
            operation = getNoTransactionOperation();
        } else {
            operation = getTransactionOperation();
        }

        try {
            if (context == null) {
                // We don't have a container yet so just run the operation.
                operation.run(null);
            } else {
                // We have a container where this operation can run and have
                // its progress displayed.
                context.run(false, false, operation);
            }
        } catch (InterruptedException exc) {
            //TODO should log these exceptions.
            exc.printStackTrace();
        } catch (InvocationTargetException exc) {
            //TODO should log these exceptions.
            exc.printStackTrace();
        }
    } while (!foundStop_);

    return engine_.getLastStatus();
}

From source file:org.eclipse.wst.common.componentcore.ui.internal.taskwizard.TaskWizard.java

License:Open Source License

/**
 * Cancel the client selection.//  w  ww .j  ava  2s  .  co  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;
    }
    Trace.trace(Trace.SEVERE, "Error cancelling task wizard", t);
    handleThrowable(t);
    return false;

}

From source file:org.eclipse.wst.common.frameworks.internal.ui.WorkspaceModifyComposedOperation.java

License:Open Source License

@Override
protected void execute(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
    try {//from   w  w  w  . ja  v a2  s.  c  o m
        List runnables = getRunnables();
        if (runnables.size() == 0) {
            return;
        }
        monitor.beginTask("", runnables.size());//$NON-NLS-1$
        for (int i = 0; i < runnables.size(); i++) {
            IRunnableWithProgress op = (IRunnableWithProgress) runnables.get(i);
            op.run(new SubProgressMonitor(monitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
        }
    } finally {
        monitor.done();
    }
}

From source file:org.eclipse.wst.jsdt.internal.ui.compare.JavaHistoryActionImpl.java

License:Open Source License

void applyChanges(ASTRewrite rewriter, final IDocument document, final ITextFileBuffer textFileBuffer,
        Shell shell, boolean inEditor, Map options)
        throws CoreException, InvocationTargetException, InterruptedException {

    MultiTextEdit edit = new MultiTextEdit();
    try {// w ww .  ja v a2s.c o  m
        TextEdit res = rewriter.rewriteAST(document, options);
        edit.addChildren(res.removeChildren());
    } catch (IllegalArgumentException e) {
        JavaScriptPlugin.log(e);
    }

    try {
        new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS).performEdits();
    } catch (BadLocationException e) {
        JavaScriptPlugin.log(e);
    }

    IRunnableWithProgress r = new IRunnableWithProgress() {
        public void run(IProgressMonitor pm) throws InvocationTargetException {
            try {
                textFileBuffer.commit(pm, false);
            } catch (CoreException ex) {
                throw new InvocationTargetException(ex);
            }
        }
    };

    if (inEditor) {
        // we don't show progress
        r.run(new NullProgressMonitor());
    } else {
        PlatformUI.getWorkbench().getProgressService().run(true, false, r);
    }
}