Example usage for javax.swing SwingUtilities isEventDispatchThread

List of usage examples for javax.swing SwingUtilities isEventDispatchThread

Introduction

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

Prototype

public static boolean isEventDispatchThread() 

Source Link

Document

Returns true if the current thread is an AWT event dispatching thread.

Usage

From source file:com.googlecode.vfsjfilechooser2.plaf.basic.BasicVFSDirectoryModel.java

/**
 * Set the busy state for the model. The model is considered
 * busy when it is running a separate (interruptable)
 * thread in order to load the contents of a directory.
 *///  w w w. j a  va2s.  c o m
private void setBusy(final boolean busy, int fid) {
    aLock.writeLock().lock();

    try {
        if (fid == fetchID) {
            boolean oldValue = this.busy;
            this.busy = busy;

            if ((changeSupport != null) && (busy != oldValue)) {
                Runnable r = (new Runnable() {
                    public void run() {
                        firePropertyChange("busy", !busy, busy);
                    }
                });

                if (SwingUtilities.isEventDispatchThread()) {
                    r.run();
                } else {
                    SwingUtilities.invokeLater(r);
                }
            }
        }
    } finally {
        aLock.writeLock().unlock();
    }
}

From source file:net.sf.mzmine.project.impl.StorableScan.java

@Override
public synchronized void addMassList(final @Nonnull MassList massList) {

    // Remove all mass lists with same name, if there are any
    MassList currentMassLists[] = massLists.toArray(new MassList[0]);
    for (MassList ml : currentMassLists) {
        if (ml.getName().equals(massList.getName()))
            removeMassList(ml);//ww  w. j a v a 2s . com
    }

    StorableMassList storedMassList;
    if (massList instanceof StorableMassList) {
        storedMassList = (StorableMassList) massList;
    } else {
        DataPoint massListDataPoints[] = massList.getDataPoints();
        try {
            int mlStorageID = rawDataFile.storeDataPoints(massListDataPoints);
            storedMassList = new StorableMassList(rawDataFile, mlStorageID, massList.getName(), this);
        } catch (IOException e) {
            logger.severe("Could not write data to temporary file " + e.toString());
            return;
        }
    }

    // Add the new mass list
    massLists.add(storedMassList);

    // Add the mass list to the tree model
    MZmineProjectImpl project = (MZmineProjectImpl) MZmineCore.getCurrentProject();

    // Check if we are adding to the current project
    if (Arrays.asList(project.getDataFiles()).contains(rawDataFile)) {
        final RawDataTreeModel treeModel = project.getRawDataTreeModel();
        final MassList newMassList = storedMassList;
        Runnable swingCode = new Runnable() {
            @Override
            public void run() {
                treeModel.addObject(newMassList);
            }
        };

        try {
            if (SwingUtilities.isEventDispatchThread())
                swingCode.run();
            else
                SwingUtilities.invokeAndWait(swingCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.brown.gui.CatalogViewer.java

/**
 * /*from ww  w. j a v a 2  s .  c o  m*/
 */
protected void viewerInit() {
    // ----------------------------------------------
    // MENU
    // ----------------------------------------------
    JMenu menu;
    JMenuItem menuItem;

    // 
    // File Menu
    //
    menu = new JMenu("File");
    menu.getPopupMenu().setLightWeightPopupEnabled(false);
    menu.setMnemonic(KeyEvent.VK_F);
    menu.getAccessibleContext().setAccessibleDescription("File Menu");
    menuBar.add(menu);

    menuItem = new JMenuItem("Open Catalog From File");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("Open Catalog From File");
    menuItem.addActionListener(this.menuHandler);
    menuItem.putClientProperty(MenuHandler.MENU_ID, MenuOptions.CATALOG_OPEN_FILE);
    menu.add(menuItem);

    menuItem = new JMenuItem("Open Catalog From Jar");
    menuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("Open Catalog From Project Jar");
    menuItem.addActionListener(this.menuHandler);
    menuItem.putClientProperty(MenuHandler.MENU_ID, MenuOptions.CATALOG_OPEN_JAR);
    menu.add(menuItem);

    menu.addSeparator();

    menuItem = new JMenuItem("Quit", KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("Quit Program");
    menuItem.addActionListener(this.menuHandler);
    menuItem.putClientProperty(MenuHandler.MENU_ID, MenuOptions.QUIT);
    menu.add(menuItem);

    // ----------------------------------------------
    // CATALOG TREE PANEL
    // ----------------------------------------------
    this.catalogTree = new JTree();
    this.catalogTree.setEditable(false);
    this.catalogTree.setCellRenderer(new CatalogViewer.CatalogTreeRenderer());
    this.catalogTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    this.catalogTree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) CatalogViewer.this.catalogTree
                    .getLastSelectedPathComponent();
            if (node == null)
                return;

            Object user_obj = node.getUserObject();
            String new_text = ""; // <html>";
            boolean text_mode = true;
            if (user_obj instanceof WrapperNode) {
                CatalogType catalog_obj = ((WrapperNode) user_obj).getCatalogType();
                new_text += CatalogViewer.this.getAttributesText(catalog_obj);
            } else if (user_obj instanceof AttributesNode) {
                AttributesNode wrapper = (AttributesNode) user_obj;
                new_text += wrapper.getAttributes();

            } else if (user_obj instanceof PlanTreeCatalogNode) {
                final PlanTreeCatalogNode wrapper = (PlanTreeCatalogNode) user_obj;
                text_mode = false;

                CatalogViewer.this.mainPanel.remove(0);
                CatalogViewer.this.mainPanel.add(wrapper.getPanel(), BorderLayout.CENTER);
                CatalogViewer.this.mainPanel.validate();
                CatalogViewer.this.mainPanel.repaint();

                if (SwingUtilities.isEventDispatchThread() == false) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            wrapper.centerOnRoot();
                        }
                    });
                } else {
                    wrapper.centerOnRoot();
                }

            } else {
                new_text += CatalogViewer.this.getSummaryText();
            }

            // Text Mode
            if (text_mode) {
                if (CatalogViewer.this.text_mode == false) {
                    CatalogViewer.this.mainPanel.remove(0);
                    CatalogViewer.this.mainPanel.add(CatalogViewer.this.textInfoPanel);
                }
                CatalogViewer.this.textInfoTextArea.setText(new_text);

                // Scroll to top
                CatalogViewer.this.textInfoTextArea.grabFocus();
            }

            CatalogViewer.this.text_mode = text_mode;
        }
    });
    this.generateCatalogTree(this.catalog, this.catalog_file_path.getName());

    //
    // Text Information Panel
    //
    this.textInfoPanel = new JPanel();
    this.textInfoPanel.setLayout(new BorderLayout());
    this.textInfoTextArea = new JTextArea();
    this.textInfoTextArea.setEditable(false);
    this.textInfoTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    this.textInfoTextArea.setText(this.getSummaryText());
    this.textInfoTextArea.addFocusListener(new FocusListener() {
        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void focusGained(FocusEvent e) {
            CatalogViewer.this.scrollTextInfoToTop();
        }
    });
    this.textInfoScroller = new JScrollPane(this.textInfoTextArea);
    this.textInfoPanel.add(this.textInfoScroller, BorderLayout.CENTER);
    this.mainPanel = new JPanel(new BorderLayout());
    this.mainPanel.add(textInfoPanel, BorderLayout.CENTER);

    //
    // Search Toolbar
    //
    JPanel searchPanel = new JPanel();
    searchPanel.setLayout(new BorderLayout());
    JPanel innerSearchPanel = new JPanel();
    innerSearchPanel.setLayout(new BoxLayout(innerSearchPanel, BoxLayout.X_AXIS));
    innerSearchPanel.add(new JLabel("Search: "));
    this.searchField = new JTextField(30);
    innerSearchPanel.add(this.searchField);
    searchPanel.add(innerSearchPanel, BorderLayout.EAST);

    this.searchField.addKeyListener(new KeyListener() {
        private String last = null;

        @Override
        public void keyReleased(KeyEvent e) {
            String value = CatalogViewer.this.searchField.getText().toLowerCase().trim();
            if (!value.isEmpty() && (this.last == null || !this.last.equals(value))) {
                CatalogViewer.this.search(value);
            }
            this.last = value;
        }

        @Override
        public void keyTyped(KeyEvent e) {
            // Do nothing...
        }

        @Override
        public void keyPressed(KeyEvent e) {
            // Do nothing...
        }
    });

    // Putting it all together
    JScrollPane scrollPane = new JScrollPane(this.catalogTree);
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    topPanel.add(searchPanel, BorderLayout.NORTH);
    topPanel.add(scrollPane, BorderLayout.CENTER);

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, topPanel, this.mainPanel);
    splitPane.setDividerLocation(400);

    this.add(splitPane, BorderLayout.CENTER);
}

From source file:com.intellij.util.net.HttpConfigurable.java

@SuppressWarnings("MethodMayBeStatic")
private void runAboveAll(final Runnable runnable) {
    final Runnable throughSwing = new Runnable() {
        @Override// ww  w .  ja v  a 2s .c  om
        public void run() {
            if (SwingUtilities.isEventDispatchThread()) {
                runnable.run();
                return;
            }
            try {
                SwingUtilities.invokeAndWait(runnable);
            } catch (InterruptedException e) {
                LOG.info(e);
            } catch (InvocationTargetException e) {
                LOG.info(e);
            }
        }
    };
    if (ProgressManager.getInstance().getProgressIndicator() != null) {
        if (ProgressManager.getInstance().getProgressIndicator().isModal()) {
            WaitForProgressToShow.runOrInvokeAndWaitAboveProgress(runnable);
        } else {
            throughSwing.run();
        }
    } else {
        throughSwing.run();
    }
}

From source file:jamel.gui.JamelWindow.java

/**
 * Prints a String in the console panel.
 * @param s the String to print./*w ww .  j  a  v a  2s.  c o m*/
 */
public void println(final String s) {
    final String cr = "<br>";//System.getProperty("line.separator" );
    if (SwingUtilities.isEventDispatchThread()) {
        consoleText.append(s);
        consoleText.append(cr);
        consolePane.setText(consoleText.toString());
    } else {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                consoleText.append(s);
                consoleText.append(cr);
                consolePane.setText(consoleText.toString());
            }
        });
    }
}

From source file:com.diversityarrays.kdxplore.trials.TrialSelectionDialog.java

private void setMessage(final String msg) {
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override/*from   w w  w  .  ja  va  2  s.c  o m*/
            public void run() {
                setMessage(msg);
            }
        });
    } else {
        messageLabel.setText(msg);
    }
}

From source file:net.sf.dsig.DSApplet.java

/**
 * Sign a form using the certificate with the supplied alias
 * @param formId the id of the form to lookup through DOM traversal
 * @param alias the alias of the selected certificate
 * @return true if signing has completed successfully, false otherwise
 * @category JavaScript exposed method/*  w  ww . j  a  v a 2s.c o m*/
 * @since 2.1.0
 */
public boolean sign(final String formId, final String alias) {
    class SignInternalRunnable implements Runnable {
        private boolean successful;

        public boolean isSuccessful() {
            return successful;
        }

        @Override
        public void run() {
            successful = signInternal(formId, alias);
        }
    }
    SignInternalRunnable sir = new SignInternalRunnable();

    available.acquireUninterruptibly();
    try {
        if (alias != null) {
            return signInternal(formId, alias);
        }

        // Null alias means the certificate selection dialog box will pop-up; 
        // hence, we need to be running in Swing's event dispatch thread
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeAndWait(sir);
        } else {
            sir.run();
        }

        return sir.isSuccessful();
    } catch (Exception e) {
        logger.error("Internal sign failed", e);

        return false;
    } finally {
        available.release();
    }
}

From source file:com.net2plan.gui.utils.onlineSimulationPane.OnlineSimulationPane.java

private void updateSimulationInfo() {
    if (SwingUtilities.isEventDispatchThread()) {
        updateSimulationLog(simKernel.getSimulationInfo());
    } else {/*from   www  .  ja va2 s  .co  m*/
        try {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    updateSimulationLog(simKernel.getSimulationInfo());
                }
            });
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatPanel.java

/**
 * Adds a typing notification message to the conversation panel.
 *
 * @param typingNotification the typing notification to show
 *///from w w w  .j  ava 2 s.  c  om
public void addTypingNotification(final String typingNotification) {
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                addTypingNotification(typingNotification);
            }
        });
        return;
    }

    typingNotificationLabel.setText(typingNotification);

    if (typingNotification != null && !typingNotification.equals(" "))
        typingNotificationLabel.setIcon(typingIcon);
    else
        typingNotificationLabel.setIcon(null);

    revalidate();
    repaint();
}

From source file:net.sf.dsig.DSApplet.java

/**
 * Sign the supplied plaintext using the certificate with the supplied alias
 * @param plaintext the plaintext to sign
 * @param alias the alias of the selected certificate
 * @return a jsonResponse containing the signature
 * @since 2.2.0//from  www .ja  v  a 2s .  co m
 * @category JavaScript exposed method
 */
public String signPlaintext(final String plaintext, final String alias) {
    class SignPlaintextInternalRunnable implements Runnable {
        private String jsonResponse;

        public String getJsonResponse() {
            return jsonResponse;
        }

        @Override
        public void run() {
            jsonResponse = signPlaintextInternal(plaintext, alias);
        }
    }
    SignPlaintextInternalRunnable spir = new SignPlaintextInternalRunnable();

    available.acquireUninterruptibly();
    try {
        if (alias != null) {
            return signPlaintextInternal(plaintext, alias);
        }

        // Null alias means the certificate selection dialog box will pop-up; 
        // hence, we need to be running in Swing's event dispatch thread
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeAndWait(spir);
        } else {
            spir.run();
        }

        return spir.getJsonResponse();
    } catch (Exception e) {
        logger.error("Internal sign plaintext failed", e);

        return "";
    } finally {
        available.release();
    }
}