Example usage for javax.swing SwingUtilities invokeAndWait

List of usage examples for javax.swing SwingUtilities invokeAndWait

Introduction

In this page you can find the example usage for javax.swing SwingUtilities invokeAndWait.

Prototype

public static void invokeAndWait(final Runnable doRun) throws InterruptedException, InvocationTargetException 

Source Link

Document

Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.

Usage

From source file:net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.CreateFileOfCurrentSQLCommand.java

/**
 * Create and show a new  progress monitor with the ability to cancel the task.
 *///from www . jav  a 2s .  com
protected void createProgressAbortDialog() {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                /*
                 *  Copied from FormatSQLCommand.
                 *  Is there a better way to get the CommentSpec[] ?
                 */

                CodeReformator cr = new CodeReformator(CodeReformatorConfigFactory.createConfig(getSession()));

                String reformatedSQL = cr.reformat(resultSetExportCommand.getSql());

                String targetFile = resultSetExportCommand.getTargetFile().getAbsolutePath();

                // i18n[CreateFileOfCurrentSQLCommand.progress.title=Exporting to a file.]
                String title = s_stringMgr.getString("CreateFileOfCurrentSQLCommand.progress.title",
                        targetFile);
                progressDialog = new SQL2FileProgressAbortDialog((Frame) null, title, targetFile, reformatedSQL,
                        new IAbortEventHandler() {
                            @Override
                            public void cancel() {
                                /* 
                                 * We need to cancel the statement at this point for the case, that we are waiting for the first rows.
                                 */
                                if (stmt != null) {
                                    try {
                                        stmt.cancel();
                                    } catch (SQLException e1) {
                                        // nothing todo
                                    }
                                }
                            }
                        });
            }
        });
    } catch (Exception e) {
        throw new RuntimeException("Could not create the Progress Monitor.", e);
    }

}

From source file:nl.xs4all.home.freekdb.b52reader.gui.MainGuiTest.java

private void waitForGuiTasks() throws InterruptedException, InvocationTargetException {
    SwingUtilities.invokeAndWait(() -> mockFrame.getTitle());
}

From source file:org.apache.cassandra.contrib.circuit.CircuitFrame.java

public static void main(final String[] args) throws IOException {
    if (args.length != 2) {
        System.err.println("Usage: java " + CircuitFrame.class.getName() + " <host> <port>");
        System.exit(1);/*from   ww w . j a va  2 s.c  o m*/
    }
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                CircuitFrame app = new CircuitFrame(args[0], Integer.parseInt(args[1]));
                app.setVisible(true);
            }
        });
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.apache.cayenne.modeler.dialog.db.DbLoaderHelper.java

/**
 * Performs reverse engineering of the DB using internal DbLoader. This
 * method should be invoked outside EventDispatchThread, or it will throw an
 * exception./*from  ww w  .j a v a  2  s. c  o  m*/
 */
public void execute() {
    stoppingReverseEngineering = false;

    // load catalogs...
    if (adapter.supportsCatalogsOnReverseEngineering()) {
        LongRunningTask loadCatalogsTask = new LoadCatalogsTask(Application.getFrame(), "Loading Catalogs");
        loadCatalogsTask.startAndWait();
    }

    if (stoppingReverseEngineering) {
        return;
    }

    // load schemas...
    LongRunningTask loadSchemasTask = new LoadSchemasTask(Application.getFrame(), "Loading Schemas");
    loadSchemasTask.startAndWait();

    if (stoppingReverseEngineering) {
        return;
    }

    final DbLoaderOptionsDialog dialog = new DbLoaderOptionsDialog(schemas, catalogs, dbUserName, dbCatalog,
            false);

    try {
        // since we are not inside EventDispatcher Thread, must run it via
        // SwingUtilities
        SwingUtilities.invokeAndWait(new Runnable() {

            public void run() {
                dialog.setVisible(true);
                dialog.dispose();
            }
        });
    } catch (Throwable th) {
        processException(th, "Error Reengineering Database");
        return;
    }

    if (dialog.getChoice() == DbLoaderOptionsDialog.CANCEL) {
        return;
    }

    this.filterBuilder.catalog(dialog.getSelectedCatalog());
    this.filterBuilder.schema(dialog.getSelectedSchema());
    this.filterBuilder.includeTables(dialog.getTableNamePattern());
    this.filterBuilder.setProceduresFilters(dialog.isLoadingProcedures());
    this.filterBuilder.includeProcedures(dialog.getProcedureNamePattern());

    this.meaningfulPk = dialog.isMeaningfulPk();
    this.addedObjEntities = new ArrayList<ObjEntity>();

    this.loader.setNameGenerator(dialog.getNamingStrategy());

    // load DataMap...
    LongRunningTask loadDataMapTask = new LoadDataMapTask(Application.getFrame(), "Reengineering DB");
    loadDataMapTask.startAndWait();
}

From source file:org.apache.empire.samples.cxf.wssample.server.RunServer.java

public static void main(String[] args) {
    try {/* www . ja  va  2 s . c  o  m*/
        final ServerControl control = new ServerControl();
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                ServerGUI gui = new ServerGUI(control);
                gui.setVisible(true);
            }
        });
        control.start();
    } catch (Throwable t) {
        t.printStackTrace();
        log.error(t);
        JOptionPane.showMessageDialog(null, "Please have a look at the logfile.", "A Error occurred!",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:org.apache.jmeter.util.JMeterUtils.java

/**
 * Run the runnable in AWT Thread if current thread is not AWT thread
 * otherwise runs call {@link SwingUtilities#invokeAndWait(Runnable)}
 * @param synchronous flag, whether we will wait for the AWT Thread to finish its job.
 * @param runnable {@link Runnable}//from   w  w  w  .  j  a  v  a2s  .c  o m
 */
public static void runSafe(boolean synchronous, Runnable runnable) {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {
        if (synchronous) {
            try {
                SwingUtilities.invokeAndWait(runnable);
            } catch (InterruptedException e) {
                log.warn("Interrupted in thread " + Thread.currentThread().getName(), e);
            } catch (InvocationTargetException e) {
                throw new Error(e);
            }
        } else {
            SwingUtilities.invokeLater(runnable);
        }
    }
}

From source file:org.colombbus.tangara.ConfigurationWindow.java

/**
 * This method launches a password window allowing to display the ConfigurationWindow.
 *//* w  w w .ja  v a  2s.c  om*/
private void displayPasswordWindow() {
    String passwordWindow = Messages.getString("ConfigurationWindow.password.window");
    String enterPassword = Messages.getString("ConfigurationWindow.password.passwordMessage");
    String typedPassword = (String) JOptionPane.showInputDialog(parent, enterPassword, passwordWindow,
            JOptionPane.PLAIN_MESSAGE, null, null, null);
    if (typedPassword == null) {
        cancelWindow = true;
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    exit();
                }
            });
        } catch (InterruptedException e) {
            LOG.warn("displayPasswordWindow", e);
        } catch (InvocationTargetException e) {
            LOG.warn("displayPasswordWindow", e);
        }
    } else if (!typedPassword.equals(password)) {
        String wrongPasswordTitle = Messages.getString("ConfigurationWindow.password.wrongPasswordTitle");
        String wrongPasswordMessage = Messages.getString("ConfigurationWindow.password.wrongPasswordMessage");
        JOptionPane.showMessageDialog(null, wrongPasswordMessage, wrongPasswordTitle,
                JOptionPane.WARNING_MESSAGE);
        displayPasswordWindow();
    }
}

From source file:org.colombbus.tangara.EditorFrame.java

/**
 * Selects the command mode, simpler than the program mode. It allows the
 * user to see immediately the effect of the typed commands.
 *//*from ww w.  j  a  va 2  s  .c o m*/
public void setCommandMode() {
    // We check if we are not already in this mode.
    if (!commandMode) {
        if (programDividerLocation != null) {
            programDividerLocation[0] = jSplitPane1.getDividerLocation();
            programDividerLocation[1] = jSplitPane2.getDividerLocation();
        }
        Runnable commandModeRun = new Runnable() {
            @Override
            public void run() {
                graphicsPane.freeze(false);
                ((java.awt.CardLayout) modePanel.getLayout()).show(modePanel, "commandMode"); //$NON-NLS-1$
                banner.setCommandMode();
                commandPane.requestFocusInWindow();
                commandMode = true;
                console.disableDragAndDrop();
                if (popupManager != null)
                    popupManager.closePopup();
                if (commandDividerLocation != null) {
                    jSplitPane1.setDividerLocation(commandDividerLocation[0]);
                    // InvokeLater is required here, otherwise this second
                    // setDividerLocation is ignored
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            jSplitPane2.setDividerLocation(commandDividerLocation[1]);
                        }
                    });
                }
            }
        };
        if (SwingUtilities.isEventDispatchThread()) {
            commandModeRun.run();
        } else {
            try {
                SwingUtilities.invokeAndWait(commandModeRun);
            } catch (InterruptedException e) {
                LOG.error(e);
            } catch (InvocationTargetException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:org.colombbus.tangara.EditorFrame.java

/**
 * Selects program mode, more complex than the command mode. It allows the
 * user to insert commands from the history list.
 *///from w  ww  .j  a va 2s .  co m
public void setProgramMode() {
    // We check if we are not already in this mode.
    if (commandMode) {
        if (commandDividerLocation == null) {
            commandDividerLocation = new int[2];
        }
        commandDividerLocation[0] = jSplitPane1.getDividerLocation();
        commandDividerLocation[1] = jSplitPane2.getDividerLocation();
        Runnable programModeRun = new Runnable() {
            @Override
            public void run() {
                graphicsPane.freeze(true);
                ((java.awt.CardLayout) modePanel.getLayout()).show(modePanel, "programMode"); //$NON-NLS-1$
                banner.setProgramMode();
                getProgramManager().requestFocus();
                commandMode = false;
                console.enableDragAndDrop();
                if (popupManager != null)
                    popupManager.closePopup();
                if (programDividerLocation != null) {
                    jSplitPane1.setDividerLocation(programDividerLocation[0]);
                    // InvokeLater is required here, otherwise this second
                    // setDividerLocation is ignored
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            jSplitPane2.setDividerLocation(programDividerLocation[1]);
                        }
                    });
                }
            }
        };
        if (SwingUtilities.isEventDispatchThread()) {
            programModeRun.run();
        } else {
            try {
                SwingUtilities.invokeAndWait(programModeRun);
            } catch (InterruptedException e) {
                LOG.error(e);
            } catch (InvocationTargetException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:org.eclipse.jubula.rc.swing.driver.EventThreadQueuerAwtImpl.java

/** {@inheritDoc} */
public Object invokeAndWait(String name, IRunnable runnable) throws StepExecutionException {

    Validate.notNull(runnable, "runnable must not be null"); //$NON-NLS-1$

    RunnableWrapper wrapper = new RunnableWrapper(name, runnable);
    try {//w ww . ja  va2s  .c  o  m
        if (SwingUtilities.isEventDispatchThread()) {
            wrapper.run();
        } else {
            SwingUtilities.invokeAndWait(wrapper);
        }

        StepExecutionException exception = wrapper.getException();
        if (exception != null) {
            throw new InvocationTargetException(exception);
        }
    } catch (InterruptedException ie) {
        // this (the waiting) thread was interrupted -> error
        log.error(ie);
        throw new StepExecutionException(ie);
    } catch (InvocationTargetException ite) {
        // the run() method from IRunnable has thrown an exception
        // -> log on info
        // -> throw a StepExecutionException
        Throwable thrown = ite.getTargetException();
        if (thrown instanceof StepExecutionException) {
            if (log.isInfoEnabled()) {
                log.info(ite);
            }
            throw (StepExecutionException) thrown;
        }

        // any other (unchecked) Exception from IRunnable.run()
        log.error("exception thrown by '" + wrapper.getName() //$NON-NLS-1$
                + "':", thrown); //$NON-NLS-1$
        throw new StepExecutionException(thrown);
    }

    return wrapper.getResult();
}