Search in sources :

Example 1 with StatesIfDoneProgressMonitor

use of eu.esdihumboldt.hale.ui.util.io.internal.StatesIfDoneProgressMonitor in project hale by halestudio.

the class ThreadProgressMonitor method runWithProgressDialog.

/**
 * 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.
 *
 * @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
            // reset subtask name
            pm.subTask("");
            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);
            }
        }
    }
}
Also used : StatesIfDoneProgressMonitor(eu.esdihumboldt.hale.ui.util.io.internal.StatesIfDoneProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationTargetException(java.lang.reflect.InvocationTargetException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) Display(org.eclipse.swt.widgets.Display) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Aggregations

StatesIfDoneProgressMonitor (eu.esdihumboldt.hale.ui.util.io.internal.StatesIfDoneProgressMonitor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 Display (org.eclipse.swt.widgets.Display)1