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

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

Introduction

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

Prototype

public static void setAllowReadAndDispatch(boolean allowReadAndDispatch) 

Source Link

Document

Sets whether ModalContext may process events (by calling Display.readAndDispatch()) while running operations.

Usage

From source file:com.helio.boomer.rap.security.login.AbstractLoginDialog.java

License:Open Source License

public void handle(final Callback[] callbacks) throws IOException {
    this.callbackArray = callbacks;
    final Display display = Display.getDefault();
    display.syncExec(new Runnable() {

        public void run() {
            isCancelled = false;/* w w  w .  j  a v  a  2s .  c  o m*/
            setBlockOnOpen(false);
            open();
            final Button okButton = getButton(IDialogConstants.OK_ID);
            okButton.setText("Login");

            okButton.addSelectionListener(new SelectionListener() {
                public void widgetSelected(final SelectionEvent event) {
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });

            final Button cancel = getButton(IDialogConstants.CANCEL_ID);
            cancel.addSelectionListener(new SelectionListener() {

                public void widgetSelected(final SelectionEvent event) {
                    isCancelled = true;
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });
        }
    });
    try {
        ModalContext.setAllowReadAndDispatch(true); // Works for now.
        ModalContext.run(new IRunnableWithProgress() {
            public void run(final IProgressMonitor monitor) {
                // Wait here until OK or cancel is pressed, then let it rip. The event listener
                // is responsible for closing the dialog (in the loginSucceeded event).
                while (!processCallbacks) {
                    try {
                        Thread.sleep(100);
                    } catch (final Exception e) {
                        // do nothing
                    }
                }
                processCallbacks = false;
                // Call the adapter to handle the callbacks
                if (!isCancelled())
                    internalHandle();
            }
        }, true, new NullProgressMonitor(), Display.getDefault());
    } catch (final Exception e) {
        final IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:msi.gama.lang.gaml.web.workspace.ui.AbstractLoginDialog.java

License:Open Source License

public void handle(final Callback[] callbacks) throws IOException {
    this.callbackArray = callbacks;
    final Display display = Display.getDefault();
    display.syncExec(new Runnable() {

        public void run() {
            isCancelled = false;/*w w  w  . j  av a2  s. c om*/
            setBlockOnOpen(false);
            open();
        }
    });
    try {
        ModalContext.setAllowReadAndDispatch(true); // Works for now.
        ModalContext.run(new IRunnableWithProgress() {

            public void run(final IProgressMonitor monitor) {
                // Wait here until OK or cancel is pressed, then let it rip.
                // The event
                // listener
                // is responsible for closing the dialog (in the
                // loginSucceeded
                // event).
                while (!processCallbacks && (RWT.getApplicationContext()
                        .getAttribute("credential" + RWT.getUISession().getHttpSession()) == null)) {
                    try {
                        //                     System.out.println("waiting");
                        Thread.sleep(10);
                    } catch (final Exception e) {
                        e.printStackTrace();
                    }
                }
                processCallbacks = true;
                // Call the adapter to handle the callbacks
                if (!isCancelled()) {
                    display.syncExec(new Runnable() {

                        public void run() {
                            close();
                        }
                    });
                }
                //                  internalHandle();
                //                  close();
            }
        }, true, new NullProgressMonitor(), Display.getDefault());
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.equinox.security.sample.keystore.ui.AbstractLoginDialog.java

License:Open Source License

public void handle(final Callback[] callbacks) throws IOException {

    this.callbackArray = callbacks;

    final Display display = Display.getDefault();
    display.syncExec(new Runnable() {
        public void run() {
            isCancelled = false;/*  w w w . java2  s.co m*/
            setBlockOnOpen(false);
            open();

            final Button okButton = getButton(IDialogConstants.OK_ID);
            okButton.setText(SampleMessages.loginButton);

            okButton.addSelectionListener(new SelectionListener() {
                public void widgetSelected(final SelectionEvent event) {
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });

            final Button cancel = getButton(IDialogConstants.CANCEL_ID);

            cancel.addSelectionListener(new SelectionListener() {
                public void widgetSelected(final SelectionEvent event) {
                    isCancelled = true;
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });
        }
    });

    try {
        ModalContext.setAllowReadAndDispatch(true); //Works for now.
        ModalContext.run(new IRunnableWithProgress() {
            public void run(final IProgressMonitor monitor) {

                // Wait here until OK or cancel is pressed, then let it rip. The event listener
                // is responsible for closing the dialog (in the loginSucceeded event).
                while (!processCallbacks) {
                    try {
                        Thread.sleep(100);
                    } catch (final Exception e) {
                        //do nothing
                    }
                }
                processCallbacks = false;

                // Call the adapter to handle the callbacks
                if (!isCancelled())
                    internalHandle();
            }
        }, true, new NullProgressMonitor(), Display.getDefault());
    } catch (final Exception e) {
        final IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:org.eclipse.rap.security.dummy.AbstractLoginDialog.java

License:Open Source License

public void handle(final Callback[] callbacks) throws IOException {
    this.callbackArray = callbacks;
    final Display display = Display.getDefault();
    display.syncExec(new Runnable() {

        public void run() {
            isCancelled = false;/*from  w w w  .  j a  v a  2 s .  com*/
            setBlockOnOpen(false);
            open();
            final Button okButton = getButton(IDialogConstants.OK_ID);
            okButton.setText("Login");
            okButton.addSelectionListener(new SelectionListener() {

                public void widgetSelected(final SelectionEvent event) {
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });
            final Button cancel = getButton(IDialogConstants.CANCEL_ID);
            cancel.addSelectionListener(new SelectionListener() {

                public void widgetSelected(final SelectionEvent event) {
                    isCancelled = true;
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });
        }
    });
    try {
        ModalContext.setAllowReadAndDispatch(true); // Works for now.
        ModalContext.run(new IRunnableWithProgress() {

            public void run(final IProgressMonitor monitor) {
                // Wait here until OK or cancel is pressed, then let it rip. The event
                // listener
                // is responsible for closing the dialog (in the loginSucceeded
                // event).
                while (!processCallbacks) {
                    try {
                        Thread.sleep(100);
                    } catch (final Exception e) {
                        // do nothing
                    }
                }
                processCallbacks = false;
                // Call the adapter to handle the callbacks
                if (!isCancelled())
                    internalHandle();
            }
        }, true, new NullProgressMonitor(), Display.getDefault());
    } catch (final Exception e) {
        final IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
}

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

License:Open Source License

/**
 * Internal method for running the workbench UI. This entails processing and
 * dispatching events until the workbench is closed or restarted.
 * /*from   ww  w  . j  av a  2s. c om*/
 * @return return code {@link PlatformUI#RETURN_OK RETURN_OK}for normal
 *         exit; {@link PlatformUI#RETURN_RESTART RETURN_RESTART}if the
 *         workbench was terminated with a call to
 *         {@link IWorkbench#restart IWorkbench.restart};
 *         {@link PlatformUI#RETURN_UNSTARTABLE RETURN_UNSTARTABLE}if the
 *         workbench could not be started; other values reserved for future
 *         use
 * @since 3.0
 */
private int runUI() {
    UIStats.start(UIStats.START_WORKBENCH, "Workbench"); //$NON-NLS-1$

    // deadlock code
    boolean avoidDeadlock = true;

    String[] commandLineArgs = Platform.getCommandLineArgs();
    for (int i = 0; i < commandLineArgs.length; i++) {
        if (commandLineArgs[i].equalsIgnoreCase("-allowDeadlock")) { //$NON-NLS-1$
            avoidDeadlock = false;
        }
    }

    final UISynchronizer synchronizer;

    if (avoidDeadlock) {
        UILockListener uiLockListener = new UILockListener(display);
        Job.getJobManager().setLockListener(uiLockListener);
        synchronizer = new UISynchronizer(display, uiLockListener);
        display.setSynchronizer(synchronizer);
        // declare the main thread to be a startup thread.
        UISynchronizer.startupThread.set(Boolean.TRUE);
    } else
        synchronizer = null;

    // // prime the splash nice and early
    // if (createSplash)
    // createSplashWrapper();

    // ModalContext should not spin the event loop (there is no UI yet to
    // block)
    ModalContext.setAllowReadAndDispatch(false);

    // if the -debug command line argument is used and the event loop is
    // being
    // run while starting the Workbench, log a warning.
    if (WorkbenchPlugin.getDefault().isDebugging()) {
        display.asyncExec(new Runnable() {
            public void run() {
                if (isStarting()) {
                    WorkbenchPlugin.log(StatusUtil.newStatus(IStatus.WARNING,
                            "Event loop should not be run while the Workbench is starting.", //$NON-NLS-1$
                            new RuntimeException()));
                }
            }
        });
    }

    Listener closeListener = new Listener() {
        public void handleEvent(Event event) {
            event.doit = close();
        }
    };

    // Initialize an exception handler.
    Window.IExceptionHandler handler = ExceptionHandler.getInstance();

    try {
        // react to display close event by closing workbench nicely
        display.addListener(SWT.Close, closeListener);

        // install backstop to catch exceptions thrown out of event loop
        Window.setExceptionHandler(handler);

        final boolean[] initOK = new boolean[1];

        if (getSplash() != null) {

            final boolean[] initDone = new boolean[] { false };
            final Throwable[] error = new Throwable[1];
            Thread initThread = new Thread() {
                /*
                 * (non-Javadoc)
                 * 
                 * @see java.lang.Thread#run()
                 */
                public void run() {
                    try {
                        // declare us to be a startup thread so that our
                        // syncs will be executed
                        UISynchronizer.startupThread.set(Boolean.TRUE);
                        initOK[0] = Workbench.this.init();
                    } catch (Throwable e) {
                        error[0] = e;
                    } finally {
                        initDone[0] = true;
                        display.wake();
                    }
                }
            };
            initThread.start();
            while (true) {
                if (!display.readAndDispatch()) {
                    if (initDone[0])
                        break;
                    display.sleep();
                }
            }
            Throwable throwable = error[0];
            if (throwable != null) {
                if (throwable instanceof Error)
                    throw (Error) throwable;
                if (throwable instanceof Exception)
                    throw (Exception) throwable;

                // how very exotic - something that isn't playing by the
                // rules. Wrap it in an error and bail
                throw new Error(throwable);
            }
        } else {
            // initialize workbench and restore or open one window
            initOK[0] = init();

        }

        // let the advisor run its start up code
        if (initOK[0]) {
            advisor.postStartup(); // may trigger a close/restart
        }

        if (initOK[0] && runEventLoop) {
            workbenchService = WorkbenchPlugin.getDefault().getBundleContext()
                    .registerService(IWorkbench.class.getName(), this, null);
            Runnable earlyStartup = new Runnable() {
                public void run() {
                    // start eager plug-ins
                    startPlugins();
                    addStartupRegistryListener();
                }
            };
            e4Context.set(PartRenderingEngine.EARLY_STARTUP_HOOK, earlyStartup);
            // start workspace auto-save
            final int millisecondInterval = getAutoSaveJobTime();
            if (millisecondInterval > 0) {
                autoSaveJob = new WorkbenchJob("Workbench Auto-Save Job") { //$NON-NLS-1$
                    @Override
                    public IStatus runInUIThread(IProgressMonitor monitor) {
                        final int nextDelay = getAutoSaveJobTime();
                        persist(false);
                        monitor.done();
                        // repeat
                        if (nextDelay > 0) {
                            this.schedule(nextDelay);
                        }
                        return Status.OK_STATUS;
                    }
                };
                autoSaveJob.setSystem(true);
                autoSaveJob.schedule(millisecondInterval);
            }

            // WWinPluginAction.refreshActionList();

            display.asyncExec(new Runnable() {
                public void run() {
                    UIStats.end(UIStats.START_WORKBENCH, this, "Workbench"); //$NON-NLS-1$
                    UIStats.startupComplete();
                }
            });

            getWorkbenchTestable().init(display, this);

            // allow ModalContext to spin the event loop
            ModalContext.setAllowReadAndDispatch(true);
            isStarting = false;

            if (synchronizer != null)
                synchronizer.started();
            // the event loop
            // runEventLoop(handler, display);
        }
        returnCode = PlatformUI.RETURN_OK;
    } catch (final Exception e) {
        if (!display.isDisposed()) {
            handler.handleException(e);
        } else {
            String msg = "Exception in Workbench.runUI after display was disposed"; //$NON-NLS-1$
            WorkbenchPlugin.log(msg, new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, 1, msg, e));
        }
    }

    // restart or exit based on returnCode
    return returnCode;
}

From source file:org.rdm.authentication.core.AbstractLoginDialog.java

License:Open Source License

public void handle(final Callback[] callbacks) throws IOException {
    this.callbackArray = callbacks;
    final Display display = Display.getDefault();
    display.syncExec(new Runnable() {

        public void run() {
            isCancelled = false;//from www. ja v  a  2  s  . c  o  m
            setBlockOnOpen(false);
            open();
            final Button okButton = getButton(IDialogConstants.OK_ID);
            okButton.setText("Login");
            okButton.addSelectionListener(new SelectionListener() {
                private static final long serialVersionUID = 1L;

                public void widgetSelected(final SelectionEvent event) {
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });
            final Button cancel = getButton(IDialogConstants.CANCEL_ID);
            cancel.addSelectionListener(new SelectionListener() {
                private static final long serialVersionUID = 1L;

                public void widgetSelected(final SelectionEvent event) {
                    isCancelled = true;
                    processCallbacks = true;
                }

                public void widgetDefaultSelected(final SelectionEvent event) {
                    // nothing to do
                }
            });
        }
    });
    try {
        ModalContext.setAllowReadAndDispatch(true); // Works for now.
        ModalContext.run(new IRunnableWithProgress() {

            public void run(final IProgressMonitor monitor) {
                // Wait here until OK or cancel is pressed, then let it rip. The event
                // listener
                // is responsible for closing the dialog (in the loginSucceeded
                // event).
                while (!processCallbacks) {
                    try {
                        Thread.sleep(100);
                    } catch (final Exception e) {
                        // do nothing
                    }
                }
                processCallbacks = false;
                // Call the adapter to handle the callbacks
                if (!isCancelled())
                    internalHandle();
            }
        }, true, new NullProgressMonitor(), Display.getDefault());
    } catch (final Exception e) {
        final IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
}