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:dynamicrefactoring.interfaz.wizard.RefactoringWizard.java

License:Open Source License

/**
 * Mtodo llamado cuando se pulsa el botn "Finish" en el asistente. Se
 * crear la operacin que se deba ejecutar, y se ejecutar utilizando
 * el propio asistente como contexto de ejecucin.
 * /*from   ww w  . j av  a2  s  .  c  om*/
 * @return <code>true</code> para indicar que la solicitud de finalizacin
 *         ha sido aceptada; <code>false</code> para indicar que ha sido
 *         rechazada.
 */
@Override
public boolean performFinish() {
    IRunnableWithProgress operation = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException {
            writeRefactoring(configureRefactoring());
        }
    };

    try {
        operation.run(new NullProgressMonitor());
    } catch (InterruptedException e) {
        String message = Messages.RefactoringWizard_CreationInterrupted + ".\n" + e.getMessage(); //$NON-NLS-1$
        logger.error(message);
        Logger.getRootLogger().error(message);
        return false;
    } catch (InvocationTargetException e) {
        Throwable realException = e.getTargetException();
        logger.error(realException.getMessage());
        MessageDialog.openError(getShell(), Messages.RefactoringWizard_Error, realException.getMessage());
        return false;
    }
    return true;
}

From source file:es.axios.udig.ui.commons.util.DialogUtil.java

License:LGPL

/**
 * Runs a blocking task in a ProgressDialog. It is ran in such a way that
 * even if the task blocks it can be cancelled. This is unlike the normal
 * ProgressDialog.run(...) method which requires that the
 * {@link IProgressMonitor} be checked and the task to "nicely" cancel.
 * /*from  ww  w.j  a va2s .  co  m*/
 * @param dialogTitle
 *            The title of the Progress dialog
 * @param showRunInBackground
 *            if true a button added to the dialog that will make the job be
 *            ran in the background.
 * @param process
 *            the task to execute.
 * @param runASync
 * @param confirmCancelRequests
 *            wether to ask the user to confirm the cancelation when the
 *            cancel button is pressed
 */
public static void runInProgressDialog(final String dialogTitle, final boolean showRunInBackground,
        final IRunnableWithProgress process, boolean runASync, final boolean confirmCancelRequests) {
    Runnable object = new Runnable() {

        public void run() {

            Shell shell = Display.getDefault().getActiveShell();

            ProgressMonitorDialog dialog = DialogUtil.openProgressMonitorDialog(shell, dialogTitle,
                    showRunInBackground, confirmCancelRequests);

            try {

                dialog.run(true, true, new IRunnableWithProgress() {

                    public void run(IProgressMonitor monitor)
                            throws InvocationTargetException, InterruptedException {
                        try {
                            PlatformGISMediator.runBlockingOperation(new IRunnableWithProgress() {

                                public void run(IProgressMonitor monitor)
                                        throws InvocationTargetException, InterruptedException {
                                    process.run(monitor);
                                }
                            }, monitor);

                        } catch (InvocationTargetException e) {
                            throw e;
                        } catch (InterruptedException e) {
                            throw e;

                        } catch (Exception e) {
                            // TODO feedback to user is required
                            e.printStackTrace();
                        }

                    }
                });
            } catch (Exception e) {
                // TODO feedback to user is required
                e.printStackTrace();
            }
        }
    };

    if (runASync)
        Display.getDefault().asyncExec(object);
    // TODO should be tested with this method
    // PlatformGISMediator.asyncInDisplayThread(object, false);
    else
        PlatformGISMediator.syncInDisplayThread(object);
}

From source file:es.axios.udig.ui.commons.util.DialogUtil.java

License:LGPL

public static void runsyncInDisplayThread(final String dialogTitle, final boolean showRunInBackground,
        final IRunnableWithProgress process, final boolean confirmCancelRequests) {
    Runnable object = new Runnable() {

        public void run() {

            Shell shell = Display.getDefault().getActiveShell();

            ProgressMonitorDialog dialog = DialogUtil.openProgressMonitorDialog(shell, dialogTitle,
                    showRunInBackground, confirmCancelRequests);

            try {

                dialog.run(true, true, new IRunnableWithProgress() {

                    public void run(final IProgressMonitor monitor)
                            throws InvocationTargetException, InterruptedException {
                        try {
                            PlatformGISMediator.syncInDisplayThread(new Runnable() {

                                public void run() {
                                    try {
                                        process.run(monitor);
                                    } catch (InvocationTargetException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }//from  w w w  .j  a v  a2  s  .c om
                                }
                            });

                        } catch (Exception e) {
                            // TODO feedback to user is required
                            e.printStackTrace();
                        }

                    }
                });
            } catch (Exception e) {
                // TODO feedback to user is required
                e.printStackTrace();
            }
        }
    };

    // if (runASync)
    // Display.getDefault().asyncExec(object);
    // TODO should be tested with this method
    // PlatformGISMediator.asyncInDisplayThread(object, false);
    // else
    PlatformGISMediator.syncInDisplayThread(object);
}

From source file:eu.esdihumboldt.hale.ui.io.util.ThreadProgressMonitor.java

License:Open Source License

/**
 * Run the given operation in a forked thread with a progress monitor dialog
 * or in the current thread with a sub progress monitor if possible.
 * /*  w ww . ja  v  a 2s. c  om*/
 * @param op the operation to execute
 * @param isCancelable if the operation can be canceled
 * @throws Exception if any error occurs executing the operation
 */
public static void runWithProgressDialog(final IRunnableWithProgress op, final boolean isCancelable)
        throws Exception {
    IProgressMonitor pm = getCurrent();
    if (pm == null) {
        // no current progress monitor associated to thread, so
        // in display thread, launch a new progress monitor dialog
        final Display display = PlatformUI.getWorkbench().getDisplay();
        final AtomicReference<Exception> error = new AtomicReference<Exception>();
        final IRunnableWithProgress progressOp = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                // create a custom progress monitor to be able to decide
                // whether the progress is done
                StatesIfDoneProgressMonitor cpm = new StatesIfDoneProgressMonitor(monitor);
                // register the progress monitor
                register(cpm);
                try {
                    op.run(cpm);
                } finally {
                    // deregister the progress monitor
                    remove(cpm);
                }
            }
        };
        display.syncExec(new Runnable() {

            @Override
            public void run() {
                try {
                    new ProgressMonitorDialog(display.getActiveShell()).run(true, isCancelable, progressOp);
                } catch (Exception e) {
                    error.set(e);
                }
            }
        });
        if (error.get() != null) {
            throw error.get();
        }
    } else {
        // progress monitor associated to this thread, so
        // run the operation in the same thread
        boolean useOriginalMonitor = false;
        if (pm instanceof StatesIfDoneProgressMonitor) {
            useOriginalMonitor = ((StatesIfDoneProgressMonitor) pm).isDone();
        }

        if (useOriginalMonitor) {
            // use the original monitor
            pm.subTask(""); // reset subtask name
            op.run(pm);
        } else {
            // use a sub progress monitor
            IProgressMonitor sm = new StatesIfDoneProgressMonitor(
                    new SubProgressMonitor(pm, 0, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
            register(sm);
            try {
                op.run(sm);
            } finally {
                remove(sm);
            }
        }
    }
}

From source file:eu.numberfour.n4js.ui.preferences.MutableProgressMonitorDialog.java

License:Open Source License

@Override
public void run(final boolean fork, final boolean cancelable, final IRunnableWithProgress runnable)
        throws InvocationTargetException, InterruptedException {

    super.run(fork, cancelable,
            monitor -> runnable.run(new CancelableProgressMonitor(monitor, MutableProgressMonitorDialog.this)));
}

From source file:eu.udig.tools.internal.ui.util.DialogUtil.java

License:LGPL

/**
 * Runs a blocking task in a ProgressDialog. It is ran in such a way that
 * even if the task blocks it can be cancelled. This is unlike the normal
 * ProgressDialog.run(...) method which requires that the
 * {@link IProgressMonitor} be checked and the task to "nicely" cancel.
 * /*w w  w . ja  va 2  s  . c o m*/
 * @param dialogTitle
 *            The title of the Progress dialog
 * @param showRunInBackground
 *            if true a button added to the dialog that will make the job be
 *            ran in the background.
 * @param process
 *            the task to execute.
 * @param runASync
 * @param confirmCancelRequests
 *            wether to ask the user to confirm the cancelation when the
 *            cancel button is pressed
 */
public static void runInProgressDialog(final String dialogTitle, final boolean showRunInBackground,
        final IRunnableWithProgress process, boolean runASync, final boolean confirmCancelRequests) {
    Runnable object = new Runnable() {

        @Override
        public void run() {

            Shell shell = Display.getDefault().getActiveShell();

            ProgressMonitorDialog dialog = DialogUtil.openProgressMonitorDialog(shell, dialogTitle,
                    showRunInBackground, confirmCancelRequests);

            try {

                dialog.run(true, true, new IRunnableWithProgress() {

                    @Override
                    public void run(IProgressMonitor monitor)
                            throws InvocationTargetException, InterruptedException {
                        try {
                            PlatformGISMediator.runBlockingOperation(new IRunnableWithProgress() {

                                @Override
                                public void run(IProgressMonitor monitor)
                                        throws InvocationTargetException, InterruptedException {
                                    process.run(monitor);
                                }
                            }, monitor);

                        } catch (InvocationTargetException e) {
                            throw e;
                        } catch (InterruptedException e) {
                            throw e;

                        } catch (Exception e) {
                            // TODO feedback to user is required
                            e.printStackTrace();
                        }

                    }
                });
            } catch (Exception e) {
                // TODO feedback to user is required
                e.printStackTrace();
            }
        }
    };

    if (runASync)
        Display.getDefault().asyncExec(object);
    // TODO should be tested with this method
    // PlatformGISMediator.asyncInDisplayThread(object, false);
    else
        PlatformGISMediator.syncInDisplayThread(object);
}

From source file:eu.udig.tools.internal.ui.util.DialogUtil.java

License:LGPL

public static void runsyncInDisplayThread(final String dialogTitle, final boolean showRunInBackground,
        final IRunnableWithProgress process, final boolean confirmCancelRequests) {
    Runnable object = new Runnable() {

        @Override/*w  w  w . j a v  a2  s.  co  m*/
        public void run() {

            Shell shell = Display.getDefault().getActiveShell();

            ProgressMonitorDialog dialog = DialogUtil.openProgressMonitorDialog(shell, dialogTitle,
                    showRunInBackground, confirmCancelRequests);

            try {

                dialog.run(true, true, new IRunnableWithProgress() {

                    @Override
                    public void run(final IProgressMonitor monitor)
                            throws InvocationTargetException, InterruptedException {
                        try {
                            PlatformGISMediator.syncInDisplayThread(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        process.run(monitor);
                                    } catch (InvocationTargetException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            });

                        } catch (Exception e) {
                            // TODO feedback to user is required
                            e.printStackTrace();
                        }

                    }
                });
            } catch (Exception e) {
                // TODO feedback to user is required
                e.printStackTrace();
            }
        }
    };

    // if (runASync)
    // Display.getDefault().asyncExec(object);
    // TODO should be tested with this method
    // PlatformGISMediator.asyncInDisplayThread(object, false);
    // else
    PlatformGISMediator.syncInDisplayThread(object);
}

From source file:ext.org.eclipse.jdt.internal.ui.compare.JavaHistoryActionImpl.java

License:Open Source License

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

    MultiTextEdit edit = new MultiTextEdit();
    try {/*from   w w  w. j  av a 2 s .c  o m*/
        TextEdit res = rewriter.rewriteAST(document, options);
        edit.addChildren(res.removeChildren());
    } catch (IllegalArgumentException e) {
        JavaPlugin.log(e);
    }

    try {
        new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS).performEdits();
    } catch (BadLocationException e) {
        JavaPlugin.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);
    }
}

From source file:ext.org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal.java

License:Open Source License

public void resolve(MultiFixTarget[] targets, final IProgressMonitor monitor) throws CoreException {
    if (targets.length == 0)
        return;//from w w  w  .  j  a va2 s  . c  o m

    if (fCleanUp == null)
        return;

    String changeName;
    String[] descriptions = fCleanUp.getStepDescriptions();
    if (descriptions.length == 1) {
        changeName = descriptions[0];
    } else {
        changeName = CorrectionMessages.FixCorrectionProposal_MultiFixChange_label;
    }

    final CleanUpRefactoring refactoring = new CleanUpRefactoring(changeName);
    for (int i = 0; i < targets.length; i++) {
        refactoring.addCleanUpTarget(targets[i]);
    }

    refactoring.addCleanUp(fCleanUp);

    IRunnableContext context = new IRunnableContext() {
        public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
                throws InvocationTargetException, InterruptedException {
            runnable.run(monitor == null ? new NullProgressMonitor() : monitor);
        }
    };

    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    RefactoringExecutionHelper helper = new RefactoringExecutionHelper(refactoring, IStatus.INFO,
            RefactoringSaveHelper.SAVE_REFACTORING, shell, context);
    try {
        helper.perform(true, true);
    } catch (InterruptedException e) {
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        if (cause instanceof CoreException) {
            throw (CoreException) cause;
        } else {
            throw new CoreException(
                    new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, cause.getLocalizedMessage(), cause));
        }
    }
}

From source file:greclipse.org.eclipse.jdt.internal.junit.wizards.NewTestCaseCreationWizard.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 v a  2s.c om

    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() {

            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;
}