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

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

Introduction

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

Prototype

public static void run(IRunnableWithProgress operation, boolean fork, IProgressMonitor monitor, Display display)
        throws InvocationTargetException, InterruptedException 

Source Link

Document

Runs the given runnable in a modal context, passing it a progress monitor.

Usage

From source file:org.eclipse.scada.configuration.generator.ui.AbstractFileRunner.java

License:Open Source License

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final List<IFile> files = SelectionHelper.list(getSelection(), IFile.class);

    final ProgressMonitorDialog dlg = new ProgressMonitorDialog(getShell());

    dlg.open();/* w w  w  .  j av  a2 s  . co  m*/

    try {
        ModalContext.run(new IRunnableWithProgress() {

            @Override
            public void run(final IProgressMonitor monitor)
                    throws InvocationTargetException, InterruptedException {

                try {
                    monitor.beginTask("Running generator", files.size());
                    for (final IFile file : files) {
                        final SubProgressMonitor sub = new SubProgressMonitor(monitor, 1);
                        try {
                            runFile(file, sub);
                        } catch (final Exception e) {
                            throw new InvocationTargetException(e);
                        } finally {
                            sub.done();
                        }
                    }
                } finally {
                    monitor.done();
                }
            }

        }, true, dlg.getProgressMonitor(), getShell().getDisplay());
    } catch (final Exception e) {
        e.printStackTrace();
        ErrorDialog.openError(getShell(), null, null, StatusHelper.convertStatus(Activator.PLUGIN_ID, e));
    } finally {
        dlg.close();
    }

    return null;
}

From source file:org.eclipse.scout.rt.ui.swt.busy.SwtBusyUtility.java

License:Open Source License

/**
 * Must be called outside of the display thread.
 * <p>/*w ww .  j a  va 2 s .  c om*/
 * Does NOT call {@link Display#syncExec(Runnable)} since this could lead to dead-locks.
 * <p>
 * Blocks and returns once the runnable has finished running.
 * <p>
 * Uses {@link BusyIndicator#showWhile(Display, Runnable)} and
 * {@link ModalContext#run(IRunnableWithProgress, boolean, IProgressMonitor, Display)}
 */
public static void showBusyIndicator(final SwtBusyHandler busyHandler, final IRunnableWithProgress runnable,
        IProgressMonitor monitor) {
    if (!busyHandler.isEnabled()) {
        return;
    }

    final Object lock = new Object();
    synchronized (lock) {

        final Display display = busyHandler.getDisplay();
        display.asyncExec(new Runnable() {
            @Override
            public void run() {
                if (!busyHandler.isEnabled()) {
                    return;
                }

                BusyIndicator.showWhile(display, new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //use modal context to prevent freezing of the gui
                            ModalContext.run(new IRunnableWithProgress() {
                                @Override
                                public void run(IProgressMonitor monitor2)
                                        throws InvocationTargetException, InterruptedException {
                                    try {
                                        runnable.run(monitor2);
                                    } finally {
                                        synchronized (lock) {
                                            lock.notifyAll();
                                        }
                                    }
                                }
                            }, true, new NullProgressMonitor(), display);
                        } catch (Throwable t) {
                            LOG.warn("run modal context", t);
                        }
                    }
                });

            }
        });

        try {
            lock.wait();
        } catch (InterruptedException e) {
            //nop
        }
    }
}

From source file:org.eclipse.servicesregistry.testutils.TestProject.java

License:Open Source License

public void dispose() throws CoreException {
    final IRunnableWithProgress deleteProjectRunnable = new IRunnableWithProgress() {

        @Override//from w  ww. ja  v  a2 s.c o m
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                project.refreshLocal(IResource.DEPTH_INFINITE, null);
                project.delete(true, true, null);
            } catch (ResourceException re) {
                /*
                 * silently swallow the resource exception. For some reason this exception gets thrown
                 * from time to time and reports the test failing. The project deletion itself happens
                 * after the test has completed and a failure will not report a problem in the test.
                  * Only ResourceException is caught in order not to hide unexpected errors.   
                 */
                return;
            } catch (ConcurrentModificationException e) {
                /*
                 * silently swallow the ConcurrentModificationException exception. For some reason this exception gets thrown
                 * from time to time and reports the test failing. The project deletion itself happens
                 * after the test has completed and a failure will not report a problem in the test.
                  * Only ConcurrentModificationException is caught in order not to hide unexpected errors.   
                 */
                return;
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };

    try {
        ModalContext.run(deleteProjectRunnable, true, new NullProgressMonitor(),
                PlatformUI.getWorkbench().getDisplay());
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof CoreException) {
            throw (CoreException) e.getTargetException();
        }
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new IllegalStateException("Interruption not supported", e);
    }
}

From source file:org.eclipse.thym.ui.internal.engine.EngineDownloadDialog.java

License:Open Source License

@Override
protected void okPressed() {
    final DownloadableCordovaEngine[] downloads = getDownloadsList();
    IRunnableWithProgress runnable = new IRunnableWithProgress() {
        @Override//from   w w w  .j av a  2  s .  c  om
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            getShell().getDisplay().syncExec(new Runnable() {
                @Override
                public void run() {
                    toggleOKButton(false);
                }
            });
            engineProvider.downloadEngine(downloads, monitor);
        }
    };

    Button cancelBtn = getButton(IDialogConstants.CANCEL_ID);
    progressMonitorPart.attachToCancelComponent(cancelBtn);
    try {
        ModalContext.run(runnable, true, progressMonitorPart, getShell().getDisplay());
    } catch (InvocationTargetException e) {
        if (e.getTargetException() != null) {
            if (e.getTargetException() instanceof CoreException) {
                StatusManager.handle((CoreException) e.getTargetException());
            } else {
                ErrorDialog.openError(getShell(), "Error downloading engine", null, new Status(IStatus.ERROR,
                        HybridUI.PLUGIN_ID, "Error downloading the engine", e.getTargetException()));
            }
        }
    } catch (InterruptedException e) {
        throw new OperationCanceledException();
    } finally {
        if (getShell() != null && !getShell().isDisposed()) {
            progressMonitorPart.removeFromCancelComponent(cancelBtn);
        }
        super.okPressed();
    }

}

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.wst.sse.sieditor.test.ui.v2.common.TestTypeSearchDialog.java

License:Open Source License

private void waitTypesTableToRefresh() throws InvocationTargetException, InterruptedException {
    IRunnableWithProgress delay = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            Thread.sleep(dialog.getTEXT_TYPED_DELAY() * 2);
        }/*from  w  w  w.j ava 2 s .c  o  m*/
    };
    ModalContext.run(delay, true, new NullProgressMonitor(), display);
}

From source file:org.eclipse.wst.sse.sieditor.test.util.ThreadUtils.java

License:Open Source License

public static void waitOutOfUI(final long milliseconds) {
    IRunnableWithProgress runnable = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            Thread.sleep(milliseconds);
        }/*w  w  w.  ja  va 2  s  . c om*/
    };
    try {
        ModalContext.run(runnable, true, new NullProgressMonitor(), Display.getDefault());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.wst.sse.sieditor.test.util.ThreadUtils.java

License:Open Source License

public static void callOutOfUI(IRunnableWithProgress runnable) {
    try {//from w w w  .  j ava2 s  .c  om
        ModalContext.run(runnable, true, new NullProgressMonitor(), Display.getDefault());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.wst.sse.sieditor.test.v2.ui.editor.SIETestUtils.java

License:Open Source License

public static void executeWait(final long milliseconds) {
    try {/* w  w w  . ja v  a  2s . c o m*/
        ModalContext.run(new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                Thread.sleep(milliseconds);
            }
        }, true, new NullProgressMonitor(), Display.getDefault());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.jboss.tools.aerogear.hybrid.ui.internal.engine.EngineDownloadDialog.java

License:Open Source License

private void run(IRunnableWithProgress runnable) {

    progressMonitorPart.attachToCancelComponent(getButton(IDialogConstants.CANCEL_ID));

    try {//from  w  w  w .j ava 2  s.  com
        ModalContext.run(runnable, true, progressMonitorPart, getShell().getDisplay());

    } catch (InvocationTargetException e) {
        if (e.getTargetException() != null) {
            if (e.getTargetException() instanceof CoreException) {
                StatusManager.handle((CoreException) e.getTargetException());
            } else {
                ErrorDialog.openError(getShell(), "Error downloading engine", null, new Status(IStatus.ERROR,
                        HybridUI.PLUGIN_ID, "Error downloading the engine", e.getTargetException()));
            }
        }
    } catch (InterruptedException e) {
        throw new OperationCanceledException();
    }

    progressMonitorPart.removeFromCancelComponent(getButton(IDialogConstants.CANCEL_ID));

}