Example usage for java.awt EventQueue invokeAndWait

List of usage examples for java.awt EventQueue invokeAndWait

Introduction

In this page you can find the example usage for java.awt EventQueue invokeAndWait.

Prototype

public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException 

Source Link

Document

Causes runnable to have its run method called in the #isDispatchThread dispatch thread of Toolkit#getSystemEventQueue the system EventQueue .

Usage

From source file:org.key2gym.client.Main.java

/**
 * Launches and waits for the MainFrame to close.
 *///from  w  w w .jav a2 s  . c  o m
private static void launchAndWaitMainFrame() {
    try {
        EventQueue.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                MainFrame.getInstance().setVisible(true);
            }
        });
    } catch (InterruptedException | InvocationTargetException ex) {
        logger.error("Unexpected exception:", ex);
    }

    synchronized (MainFrame.getInstance()) {
        while (MainFrame.getInstance().isVisible()) {
            try {
                MainFrame.getInstance().wait();
            } catch (InterruptedException ex) {
                logger.error("Unexpected exception:", ex);
            }
        }
    }
}

From source file:cc.pinel.mangue.ui.ChaptersPanel.java

/**
 * Loads all chapters from the given manga.
 * // w  w  w  . j  av a 2s .c  o  m
 * @param manga the manga
 */
public void loadChapters(final Manga manga) {
    if (this.manga != null && this.manga.getId().equals(manga.getId()))
        return;

    this.manga = manga;
    rememberManga();

    new StorageHandler(main.getContext(), "Loading mangas...") {
        /**
         * {@inheritDoc}
         */
        public void handleRun() throws Exception {
            final String lastChapterNumber = new StateStorage(main.getContext()).getChapter(manga.getId());

            final ConnectivityHandler handler = new ConnectivityHandler(main.getContext(),
                    "Loading chapters...") {
                /**
                 * {@inheritDoc}
                 */
                public void handleConnected() throws Exception {
                    JSONParser parser = new JSONParser();
                    JSONArray chapters = (JSONArray) parser
                            .parse(IOUtils.toString(new URL(manga.getAllChaptersLink()).openStream()));

                    chaptersPages.removeAllItems();
                    for (int i = chapters.size() - 1; i >= 0; i--) {
                        JSONObject chapter = (JSONObject) chapters.get(i);
                        String chapterNumber = chapter.get("chapter").toString();
                        String chapterName = StringUtils.unescapeHtml(chapter.get("chapter_name").toString());
                        String chapterTitle = chapterNumber
                                + (chapterName != null && chapterName.length() != 0 ? ": " + chapterName : "");

                        final KWTSelectableLabel chapterLabel = new KWTSelectableLabel(chapterTitle);
                        chapterLabel.setName(chapterNumber);
                        chapterLabel.setFocusable(true);
                        chapterLabel.setEnabled(true);
                        chapterLabel.addActionListener(chapterListener);

                        // last read chapter
                        if (lastChapterNumber != null && chapterNumber.equals(lastChapterNumber))
                            highlightLabel(chapterLabel);

                        chaptersPages.addItem(chapterLabel);
                    }

                    EventQueue.invokeAndWait(new Runnable() {
                        public void run() {
                            chaptersPages.first();

                            requestFocus();
                            repaint();
                        }
                    });
                }
            };

            main.getContext().getConnectivity().submitSingleAttemptConnectivityRequest(handler, true);
        }
    }.start();
}

From source file:org.parosproxy.paros.extension.history.ProxyListenerLog.java

private void addHistory(HttpMessage msg, int type) {

    HistoryReference historyRef = null;/*from w  w  w  .  j a  v a 2 s . c  om*/

    try {
        historyRef = new HistoryReference(model.getSession(), type, msg);
    } catch (Exception e) {
        return;
    }

    if (type != HistoryReference.TYPE_MANUAL && type != HistoryReference.TYPE_HIDDEN) {
        return;
    }

    // add history to list (log panel).  Must use event queue because this proxylistener may not be run from event queue.
    synchronized (historyList) {
        if (type == HistoryReference.TYPE_MANUAL) {

            if (pattern == null) {
                addHistoryInEventQueue(historyRef);
            } else {
                StringBuffer sb = new StringBuffer();
                sb.append(msg.getRequestHeader().toString());
                sb.append(msg.getRequestBody().toString());
                if (!msg.getResponseHeader().isEmpty()) {
                    sb.append(msg.getResponseHeader().toString());
                    sb.append(msg.getResponseBody().toString());

                }
                if (pattern.matcher(sb.toString()).find()) {
                    addHistoryInEventQueue(historyRef);
                }
            }
        }
    }

    // add history to site panel.  Must use event queue because this proxylistener may not be run from event queue.
    final HistoryReference ref = historyRef;
    final HttpMessage finalMsg = msg;
    if (EventQueue.isDispatchThread()) {
        model.getSession().getSiteTree().addPath(ref, msg);
        if (isFirstAccess) {
            isFirstAccess = false;
            view.getSiteTreePanel().expandRoot();
        }

    } else {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    model.getSession().getSiteTree().addPath(ref, finalMsg);
                    if (isFirstAccess) {
                        isFirstAccess = false;
                        view.getSiteTreePanel().expandRoot();
                    }
                }
            });
        } catch (Exception e) {
        }

    }
}

From source file:org.parosproxy.paros.view.OutputPanel.java

public void append(final String msg) {
    if (EventQueue.isDispatchThread()) {
        doAppend(msg);//from   ww  w .j  a  v  a2s.  c om
        return;
    }
    try {
        EventQueue.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                doAppend(msg);
            }
        });
    } catch (Exception e) {
        // ZAP: Added logging.
        logger.error(e.getMessage(), e);
    }
}

From source file:org.zaproxy.zap.GuiBootstrap.java

/**
 * Initialises the {@code Control} and does post {@code View} initialisations.
 *
 * @throws Exception if an error occurs during initialisation
 * @see Control//from   w w  w .  ja  v a 2 s .c  o  m
 * @see View
 */
private void initControlAndPostViewInit() throws Exception {
    Control.initSingletonWithView(getControlOverrides());

    final Control control = Control.getSingleton();
    final View view = View.getSingleton();

    EventQueue.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            view.postInit();
            view.getMainFrame().setVisible(true);

            boolean createNewSession = true;
            if (getArgs().isEnabled(CommandLine.SESSION) && getArgs().isEnabled(CommandLine.NEW_SESSION)) {
                view.showWarningDialog(Constant.messages.getString("start.gui.cmdline.invalid.session.options",
                        CommandLine.SESSION, CommandLine.NEW_SESSION, Constant.getZapHome()));

            } else if (getArgs().isEnabled(CommandLine.SESSION)) {
                Path sessionPath = SessionUtils.getSessionPath(getArgs().getArgument(CommandLine.SESSION));
                if (!Files.exists(sessionPath)) {
                    view.showWarningDialog(Constant.messages
                            .getString("start.gui.cmdline.session.does.not.exist", Constant.getZapHome()));

                } else {
                    createNewSession = !control.getMenuFileControl()
                            .openSession(sessionPath.toAbsolutePath().toString());
                }

            } else if (getArgs().isEnabled(CommandLine.NEW_SESSION)) {
                Path sessionPath = SessionUtils.getSessionPath(getArgs().getArgument(CommandLine.NEW_SESSION));
                if (Files.exists(sessionPath)) {
                    view.showWarningDialog(Constant.messages
                            .getString("start.gui.cmdline.newsession.already.exist", Constant.getZapHome()));

                } else {
                    createNewSession = !control.getMenuFileControl()
                            .newSession(sessionPath.toAbsolutePath().toString());
                }
            }
            view.hideSplashScreen();

            if (createNewSession) {
                try {
                    control.getMenuFileControl().newSession(false);
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                    View.getSingleton()
                            .showWarningDialog(Constant.messages.getString("menu.file.newSession.error"));
                }
            }

        }
    });

    try {
        // Allow extensions to pick up command line args in GUI mode
        control.getExtensionLoader().hookCommandLineListener(getArgs());
        control.runCommandLine();

    } catch (final Exception e) {
        logger.error(e.getMessage(), e);
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                view.showWarningDialog(e.getMessage());
            }
        });
    }
}

From source file:org.parosproxy.paros.extension.history.ProxyListenerLog.java

private void addHistoryInEventQueue(final HistoryReference ref) {
    if (EventQueue.isDispatchThread()) {
        historyList.addElement(ref);//from   ww  w  . j av a 2s  .c o  m
    } else {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    historyList.addElement(ref);
                }

            });
        } catch (Exception e) {
        }
    }
}

From source file:org.zaproxy.zap.extension.brk.ProxyListenerBreak.java

private void clearAndDisableRequest() {
    if (EventQueue.isDispatchThread()) {
        getBreakPanel().clearAndDisableRequest();
    } else {//from   w  w  w. jav a 2 s.  c  o  m
        try {
            EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    getBreakPanel().clearAndDisableRequest();
                }
            });
        } catch (Exception e) {
            log.warn(e.getMessage(), e);
        }
    }
}

From source file:org.zaproxy.zap.extension.spider.ExtensionSpider.java

@Override
public void sessionChanged(final Session session) {
    if (EventQueue.isDispatchThread()) {
        sessionChangedEventHandler(session);
    } else {//from  ww w.java  2 s  .  co  m
        try {
            EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    sessionChangedEventHandler(session);
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.zaproxy.zap.extension.brk.ProxyListenerBreak.java

private void clearAndDisableResponse() {
    if (EventQueue.isDispatchThread()) {
        getBreakPanel().clearAndDisableResponse();
    } else {//from  ww  w  .  j av a 2 s  .  c om
        try {
            EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    getBreakPanel().clearAndDisableResponse();
                }
            });
        } catch (Exception e) {
            log.warn(e.getMessage(), e);
        }
    }
}

From source file:org.parosproxy.paros.extension.spider.ExtensionSpider.java

public void spiderComplete() {

    try {/*from   ww w . j av a 2 s.  c  om*/
        getModel().getDb().getTableHistory().deleteHistoryType(getModel().getSession().getSessionId(),
                HistoryReference.TYPE_SPIDER_SEED);
        getModel().getDb().getTableHistory().deleteHistoryType(getModel().getSession().getSessionId(),
                HistoryReference.TYPE_SPIDER_VISITED);

    } catch (SQLException e) {
        e.printStackTrace();
    }

    if (getView() != null) {
        getMenuItemSpider().setEnabled(true);
        getPopupMenuSpider().setEnabled(true);

    }

    try {
        Thread.sleep(3000);
    } catch (Exception e) {
    }

    if (getView() != null && dialog != null) {
        if (EventQueue.isDispatchThread()) {
            dialog.dispose();
            return;
        }
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    dialog.dispose();
                }
            });
        } catch (Exception e) {
        }
    }
}