Search in sources :

Example 1 with SwingWorker

use of javax.swing.SwingWorker in project zookeeper by apache.

the class NodeViewerMetaData method nodeSelectionChanged.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer#
     * nodeSelectionChanged(java.util.Set)
     */
@Override
public void nodeSelectionChanged(List<String> selectedNodes) {
    this.metaDataPanel.removeAll();
    if (selectedNodes.size() > 0) {
        this.selectedNode = selectedNodes.get(0);
        SwingWorker<Map<String, String>, Void> worker = new SwingWorker<Map<String, String>, Void>() {

            @Override
            protected Map<String, String> doInBackground() throws Exception {
                return NodeViewerMetaData.this.zooInspectorManager.getNodeMeta(NodeViewerMetaData.this.selectedNode);
            }

            @Override
            protected void done() {
                Map<String, String> data = null;
                try {
                    data = get();
                } catch (InterruptedException e) {
                    data = new HashMap<String, String>();
                    LoggerFactory.getLogger().error("Error retrieving meta data for node: " + NodeViewerMetaData.this.selectedNode, e);
                } catch (ExecutionException e) {
                    data = new HashMap<String, String>();
                    LoggerFactory.getLogger().error("Error retrieving meta data for node: " + NodeViewerMetaData.this.selectedNode, e);
                }
                NodeViewerMetaData.this.metaDataPanel.setLayout(new GridBagLayout());
                JPanel infoPanel = new JPanel();
                infoPanel.setBackground(Color.WHITE);
                infoPanel.setLayout(new GridBagLayout());
                int i = 0;
                int rowPos = 0;
                for (Map.Entry<String, String> entry : data.entrySet()) {
                    rowPos = 2 * i + 1;
                    JLabel label = new JLabel(entry.getKey());
                    JTextField text = new JTextField(entry.getValue());
                    text.setEditable(false);
                    GridBagConstraints c1 = new GridBagConstraints();
                    c1.gridx = 0;
                    c1.gridy = rowPos;
                    c1.gridwidth = 1;
                    c1.gridheight = 1;
                    c1.weightx = 0;
                    c1.weighty = 0;
                    c1.anchor = GridBagConstraints.WEST;
                    c1.fill = GridBagConstraints.HORIZONTAL;
                    c1.insets = new Insets(5, 5, 5, 5);
                    c1.ipadx = 0;
                    c1.ipady = 0;
                    infoPanel.add(label, c1);
                    GridBagConstraints c2 = new GridBagConstraints();
                    c2.gridx = 2;
                    c2.gridy = rowPos;
                    c2.gridwidth = 1;
                    c2.gridheight = 1;
                    c2.weightx = 0;
                    c2.weighty = 0;
                    c2.anchor = GridBagConstraints.WEST;
                    c2.fill = GridBagConstraints.HORIZONTAL;
                    c2.insets = new Insets(5, 5, 5, 5);
                    c2.ipadx = 0;
                    c2.ipady = 0;
                    infoPanel.add(text, c2);
                    i++;
                }
                GridBagConstraints c = new GridBagConstraints();
                c.gridx = 1;
                c.gridy = rowPos;
                c.gridwidth = 1;
                c.gridheight = 1;
                c.weightx = 1;
                c.weighty = 1;
                c.anchor = GridBagConstraints.NORTHWEST;
                c.fill = GridBagConstraints.NONE;
                c.insets = new Insets(5, 5, 5, 5);
                c.ipadx = 0;
                c.ipady = 0;
                NodeViewerMetaData.this.metaDataPanel.add(infoPanel, c);
                NodeViewerMetaData.this.metaDataPanel.revalidate();
                NodeViewerMetaData.this.metaDataPanel.repaint();
            }
        };
        worker.execute();
    }
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) HashMap(java.util.HashMap) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) SwingWorker(javax.swing.SwingWorker) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SwingWorker

use of javax.swing.SwingWorker in project Openfire by igniterealtime.

the class Launcher method startApplication.

private synchronized void startApplication() {
    if (openfired == null) {
        try {
            File windowsExe = new File(binDir, "openfired.exe");
            File unixExe = new File(binDir, "openfired");
            if (windowsExe.exists()) {
                openfired = Runtime.getRuntime().exec(new String[] { windowsExe.toString() });
            } else if (unixExe.exists()) {
                openfired = Runtime.getRuntime().exec(new String[] { unixExe.toString() });
            } else {
                throw new FileNotFoundException();
            }
        } catch (Exception e) {
            // Try one more time using the jar and hope java is on the path
            try {
                File libDir = new File(binDir.getParentFile(), "lib").getAbsoluteFile();
                openfired = Runtime.getRuntime().exec(new String[] { "java", "-jar", new File(libDir, "startup.jar").toString() });
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Launcher could not start,\n" + appName, "File not found", JOptionPane.ERROR_MESSAGE);
            }
        }
        final SimpleAttributeSet styles = new SimpleAttributeSet();
        SwingWorker<String, Void> inputWorker = new SwingWorker<String, Void>() {

            @Override
            public String doInBackground() {
                if (openfired != null) {
                    // Get the input stream and read from it
                    try (InputStream in = openfired.getInputStream()) {
                        int c;
                        while ((c = in.read()) != -1) {
                            try {
                                StyleConstants.setFontFamily(styles, "courier new");
                                pane.getDocument().insertString(pane.getDocument().getLength(), "" + (char) c, styles);
                            } catch (BadLocationException e) {
                            // Ignore.
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return "ok";
            }
        };
        inputWorker.execute();
        SwingWorker<String, Void> errorWorker = new SwingWorker<String, Void>() {

            @Override
            public String doInBackground() {
                if (openfired != null) {
                    // Get the input stream and read from it
                    try (InputStream in = openfired.getErrorStream()) {
                        int c;
                        while ((c = in.read()) != -1) {
                            try {
                                StyleConstants.setForeground(styles, Color.red);
                                pane.getDocument().insertString(pane.getDocument().getLength(), "" + (char) c, styles);
                            } catch (BadLocationException e) {
                            // Ignore.
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return "ok";
            }
        };
        errorWorker.execute();
        if (freshStart) {
            try {
                Thread.sleep(1000);
                cardLayout.show(cardPanel, "running");
            } catch (Exception ex) {
            // Ignore.
            }
            freshStart = false;
        } else {
            // Clear Text
            pane.setText("");
            cardLayout.show(cardPanel, "running");
        }
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SwingWorker(javax.swing.SwingWorker) IOException(java.io.IOException) File(java.io.File) BadLocationException(javax.swing.text.BadLocationException) FileNotFoundException(java.io.FileNotFoundException) AWTException(java.awt.AWTException) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 3 with SwingWorker

use of javax.swing.SwingWorker in project processing by processing.

the class ContributionManager method cleanup.

/**
   * Called by Base to clean up entries previously marked for deletion
   * and remove any "requires restart" flags.
   * Also updates all entries previously marked for update.
   */
private static void cleanup(final Base base) throws Exception {
    deleteTemp(Base.getSketchbookModesFolder());
    deleteTemp(Base.getSketchbookToolsFolder());
    deleteFlagged(Base.getSketchbookLibrariesFolder());
    deleteFlagged(Base.getSketchbookModesFolder());
    deleteFlagged(Base.getSketchbookToolsFolder());
    installPreviouslyFailed(base, Base.getSketchbookModesFolder());
    updateFlagged(base, Base.getSketchbookModesFolder());
    updateFlagged(base, Base.getSketchbookToolsFolder());
    SwingWorker s = new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() throws Exception {
            try {
                // TODO: pls explain the sleep and why this runs on a worker thread,
                //   but a couple of lines above on EDT [jv]
                Thread.sleep(1000);
                installPreviouslyFailed(base, Base.getSketchbookToolsFolder());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    s.execute();
    clearRestartFlags(Base.getSketchbookModesFolder());
    clearRestartFlags(Base.getSketchbookToolsFolder());
}
Also used : SwingWorker(javax.swing.SwingWorker)

Example 4 with SwingWorker

use of javax.swing.SwingWorker in project gitblit by gitblit.

the class GitblitManager method login.

@Override
public void login(GitblitRegistration reg) {
    if (!reg.savePassword && (reg.password == null || reg.password.length == 0)) {
        // prompt for password
        EditRegistrationDialog dialog = new EditRegistrationDialog(this, reg, true);
        dialog.setLocationRelativeTo(GitblitManager.this);
        dialog.setVisible(true);
        GitblitRegistration newReg = dialog.getRegistration();
        if (newReg == null) {
            // user canceled
            return;
        }
        // preserve feeds
        newReg.feeds.addAll(reg.feeds);
        // use new reg
        reg = newReg;
    }
    // login
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    final GitblitRegistration registration = reg;
    final GitblitPanel panel = new GitblitPanel(registration, this);
    SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {

        @Override
        protected Boolean doInBackground() throws IOException {
            panel.login();
            return true;
        }

        @Override
        protected void done() {
            try {
                boolean success = get();
                serverTabs.addTab(registration.name, panel);
                int idx = serverTabs.getTabCount() - 1;
                serverTabs.setSelectedIndex(idx);
                serverTabs.setTabComponentAt(idx, new ClosableTabComponent(registration.name, null, serverTabs, panel));
                registration.lastLogin = new Date();
                saveRegistration(registration.name, registration);
                registrations.put(registration.name, registration);
                rebuildRecentMenu();
                if (!registration.savePassword) {
                    // clear password
                    registration.password = null;
                }
            } catch (Throwable t) {
                Throwable cause = t.getCause();
                if (cause instanceof ConnectException) {
                    JOptionPane.showMessageDialog(GitblitManager.this, cause.getMessage(), Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
                } else if (cause instanceof ForbiddenException) {
                    JOptionPane.showMessageDialog(GitblitManager.this, "This Gitblit server does not allow RPC Management or Administration", Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
                } else {
                    Utils.showException(GitblitManager.this, t);
                }
            } finally {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        }
    };
    worker.execute();
}
Also used : ForbiddenException(com.gitblit.GitBlitException.ForbiddenException) Point(java.awt.Point) Date(java.util.Date) SwingWorker(javax.swing.SwingWorker) ConnectException(java.net.ConnectException)

Example 5 with SwingWorker

use of javax.swing.SwingWorker in project android-classyshark by google.

the class ClassySharkPanel method readArchiveAndFillDisplayArea.

private void readArchiveAndFillDisplayArea(final String className) {
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() throws Exception {
            silverGhost.readContents();
            return null;
        }

        @Override
        protected void done() {
            if (silverGhost.isArchiveError()) {
                filesTree.fillArchive(new File("ERROR"), new ArrayList<String>(), silverGhost.getComponents());
                displayArea.displayError();
                return;
            }
            filesTree.fillArchive(silverGhost.getBinaryArchive(), silverGhost.getAllClassNames(), silverGhost.getComponents());
            if (className != null) {
                onSelectedClassName(className);
            } else {
                displayArea.displaySharkey();
            }
            isDataLoaded = true;
        }
    };
    worker.execute();
}
Also used : SwingWorker(javax.swing.SwingWorker) File(java.io.File)

Aggregations

SwingWorker (javax.swing.SwingWorker)41 ExecutionException (java.util.concurrent.ExecutionException)15 IOException (java.io.IOException)14 List (java.util.List)10 ResourceBundle (java.util.ResourceBundle)10 ArrayList (java.util.ArrayList)9 Preferences (java.util.prefs.Preferences)8 File (java.io.File)6 JFileChooser (javax.swing.JFileChooser)6 JPanel (javax.swing.JPanel)6 BorderLayout (java.awt.BorderLayout)5 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)5 Engine (jgnash.engine.Engine)5 FileNotFoundException (java.io.FileNotFoundException)4 InstanceNotFoundException (javax.management.InstanceNotFoundException)4 IntrospectionException (javax.management.IntrospectionException)4 MBeanInfo (javax.management.MBeanInfo)4 ReflectionException (javax.management.ReflectionException)4 JScrollPane (javax.swing.JScrollPane)4 ResourceAccessException (org.springframework.web.client.ResourceAccessException)4