Example usage for org.eclipse.jface.action StatusLineManager setCancelEnabled

List of usage examples for org.eclipse.jface.action StatusLineManager setCancelEnabled

Introduction

In this page you can find the example usage for org.eclipse.jface.action StatusLineManager setCancelEnabled.

Prototype

@Override
    public void setCancelEnabled(boolean enabled) 

Source Link

Usage

From source file:com.github.haixing_hu.swt.window.ApplicationWindowEx.java

License:Open Source License

/**
 * This implementation of IRunnableContext#run(boolean, boolean,
 * IRunnableWithProgress) blocks until the runnable has been run,
 * regardless of the value of <code>fork</code>.
 * It is recommended that <code>fork</code> is set to
 * true in most cases. If <code>fork</code> is set to <code>false</code>,
 * the runnable will run in the UI thread and it is the runnable's
 * responsibility to call <code>Display.readAndDispatch()</code>
 * to ensure UI responsiveness./*  w  w  w. j a  v  a  2  s  .  co m*/
 */
@Override
public void run(final boolean fork, boolean cancelable, final IRunnableWithProgress runnable)
        throws InvocationTargetException, InterruptedException {
    try {
        operationInProgress = true;
        final StatusLineManager mgr = getStatusLineManager();
        if (mgr == null) {
            runnable.run(new NullProgressMonitor());
            return;
        }
        final boolean cancelWasEnabled = mgr.isCancelEnabled();

        final Control contents = getContents();
        final Display display = contents.getDisplay();
        final Shell shell = getShell();
        final boolean contentsWasEnabled = contents.getEnabled();
        MenuManagerEx manager = getMenuBarManager();
        Menu menuBar = null;
        if (manager != null) {
            menuBar = manager.getMenu();
            manager = null;
        }
        boolean menuBarWasEnabled = false;
        if (menuBar != null) {
            menuBarWasEnabled = menuBar.getEnabled();
        }

        final Control toolbarControl = getToolBarControl();
        boolean toolbarWasEnabled = false;
        if (toolbarControl != null) {
            toolbarWasEnabled = toolbarControl.getEnabled();
        }

        final Control coolbarControl = getCoolBarControl();
        boolean coolbarWasEnabled = false;
        if (coolbarControl != null) {
            coolbarWasEnabled = coolbarControl.getEnabled();
        }

        // Disable the rest of the shells on the current display
        final Shell[] shells = display.getShells();
        final boolean[] enabled = new boolean[shells.length];
        for (int i = 0; i < shells.length; i++) {
            final Shell current = shells[i];
            if (current == shell) {
                continue;
            }
            if ((current != null) && !current.isDisposed()) {
                enabled[i] = current.getEnabled();
                current.setEnabled(false);
            }
        }

        final Control currentFocus = display.getFocusControl();
        try {
            contents.setEnabled(false);
            if (menuBar != null) {
                menuBar.setEnabled(false);
            }
            if (toolbarControl != null) {
                toolbarControl.setEnabled(false);
            }
            if (coolbarControl != null) {
                coolbarControl.setEnabled(false);
            }
            mgr.setCancelEnabled(cancelable);
            final Exception[] holder = new Exception[1];
            BusyIndicator.showWhile(display, new Runnable() {
                @Override
                public void run() {
                    try {
                        ModalContext.run(runnable, fork, mgr.getProgressMonitor(), display);
                    } catch (final InvocationTargetException ite) {
                        holder[0] = ite;
                    } catch (final InterruptedException ie) {
                        holder[0] = ie;
                    }
                }
            });

            if (holder[0] != null) {
                if (holder[0] instanceof InvocationTargetException) {
                    throw (InvocationTargetException) holder[0];
                } else if (holder[0] instanceof InterruptedException) {
                    throw (InterruptedException) holder[0];
                }
            }
        } finally {
            operationInProgress = false;
            // Enable the rest of the shells on the current display
            for (int i = 0; i < shells.length; i++) {
                final Shell current = shells[i];
                if (current == shell) {
                    continue;
                }
                if ((current != null) && !current.isDisposed()) {
                    current.setEnabled(enabled[i]);
                }
            }
            if (!contents.isDisposed()) {
                contents.setEnabled(contentsWasEnabled);
            }
            if ((menuBar != null) && !menuBar.isDisposed()) {
                menuBar.setEnabled(menuBarWasEnabled);
            }
            if ((toolbarControl != null) && !toolbarControl.isDisposed()) {
                toolbarControl.setEnabled(toolbarWasEnabled);
            }
            if ((coolbarControl != null) && !coolbarControl.isDisposed()) {
                coolbarControl.setEnabled(coolbarWasEnabled);
            }
            mgr.setCancelEnabled(cancelWasEnabled);
            if ((currentFocus != null) && !currentFocus.isDisposed()) {
                // It's necessary to restore focus after reenabling the controls
                // because disabling them causes focus to jump elsewhere.
                // Use forceFocus rather than setFocus to avoid SWT's
                // search for children which can take focus, so focus
                // ends up back on the actual control that previously had it.
                currentFocus.forceFocus();
            }
        }
    } finally {
        operationInProgress = false;
    }
}

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 . c o  m
        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);
        }
    }
}