Search in sources :

Example 6 with SwingWorker

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

the class ExtensionAutoUpdate method uninstallAddOnsWithView.

boolean uninstallAddOnsWithView(final Window caller, final Set<AddOn> addOns, final boolean updates, final Set<AddOn> failedUninstallations) {
    if (addOns == null || addOns.isEmpty()) {
        return true;
    }
    if (!EventQueue.isDispatchThread()) {
        try {
            EventQueue.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    uninstallAddOnsWithView(caller, addOns, updates, failedUninstallations);
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            logger.error("Failed to uninstall add-ons:", e);
            return false;
        }
        return failedUninstallations.isEmpty();
    }
    final Window parent = getWindowParent(caller);
    final UninstallationProgressDialogue waitDialogue = new UninstallationProgressDialogue(parent, addOns);
    waitDialogue.addAddOnUninstallListener(new AddOnUninstallListener() {

        @Override
        public void uninstallingAddOn(AddOn addOn, boolean updating) {
            if (updating) {
                String message = MessageFormat.format(Constant.messages.getString("cfu.output.replacing") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                getView().getOutputPanel().append(message);
            }
        }

        @Override
        public void addOnUninstalled(AddOn addOn, boolean update, boolean uninstalled) {
            if (uninstalled) {
                if (!update && addonsDialog != null) {
                    addonsDialog.notifyAddOnUninstalled(addOn);
                }
                String message = MessageFormat.format(Constant.messages.getString("cfu.output.uninstalled") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                getView().getOutputPanel().append(message);
            } else {
                if (addonsDialog != null) {
                    addonsDialog.notifyAddOnFailedUninstallation(addOn);
                }
                String message;
                if (update) {
                    message = MessageFormat.format(Constant.messages.getString("cfu.output.replace.failed") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                } else {
                    message = MessageFormat.format(Constant.messages.getString("cfu.output.uninstall.failed") + "\n", addOn.getName(), Integer.valueOf(addOn.getFileVersion()));
                }
                getView().getOutputPanel().append(message);
            }
        }
    });
    SwingWorker<Void, UninstallationProgressEvent> a = new SwingWorker<Void, UninstallationProgressEvent>() {

        @Override
        protected void process(List<UninstallationProgressEvent> events) {
            waitDialogue.update(events);
        }

        @Override
        protected Void doInBackground() {
            UninstallationProgressHandler progressHandler = new UninstallationProgressHandler() {

                @Override
                protected void publishEvent(UninstallationProgressEvent event) {
                    publish(event);
                }
            };
            for (AddOn addOn : addOns) {
                if (!uninstall(addOn, updates, progressHandler)) {
                    failedUninstallations.add(addOn);
                }
            }
            if (!failedUninstallations.isEmpty()) {
                logger.warn("Not all add-ons were successfully uninstalled: " + failedUninstallations);
            }
            return null;
        }
    };
    waitDialogue.bind(a);
    a.execute();
    waitDialogue.setSynchronous(updates);
    waitDialogue.setVisible(true);
    return failedUninstallations.isEmpty();
}
Also used : Window(java.awt.Window) AddOn(org.zaproxy.zap.control.AddOn) UninstallationProgressHandler(org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.UninstallationProgressHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) UninstallationProgressEvent(org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.UninstallationProgressEvent) AddOnUninstallListener(org.zaproxy.zap.extension.autoupdate.UninstallationProgressDialogue.AddOnUninstallListener) SwingWorker(javax.swing.SwingWorker) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with SwingWorker

use of javax.swing.SwingWorker in project jdk8u_jdk by JetBrains.

the class XSheet method displayMBeanAttributesNode.

// Call on EDT
private void displayMBeanAttributesNode(final DefaultMutableTreeNode node) {
    final XNodeInfo uo = (XNodeInfo) node.getUserObject();
    if (!uo.getType().equals(Type.ATTRIBUTES)) {
        return;
    }
    mbean = (XMBean) uo.getData();
    final XMBean xmb = mbean;
    SwingWorker<MBeanInfo, Void> sw = new SwingWorker<MBeanInfo, Void>() {

        @Override
        public MBeanInfo doInBackground() throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
            MBeanInfo mbi = xmb.getMBeanInfo();
            return mbi;
        }

        @Override
        protected void done() {
            try {
                MBeanInfo mbi = get();
                if (mbi != null && mbi.getAttributes() != null && mbi.getAttributes().length > 0) {
                    mbeanAttributes.loadAttributes(xmb, mbi);
                    if (!isSelectedNode(node, currentNode)) {
                        return;
                    }
                    invalidate();
                    mainPanel.removeAll();
                    JPanel borderPanel = new JPanel(new BorderLayout());
                    borderPanel.setBorder(BorderFactory.createTitledBorder(Messages.ATTRIBUTE_VALUES));
                    borderPanel.add(new JScrollPane(mbeanAttributes));
                    mainPanel.add(borderPanel, BorderLayout.CENTER);
                    // add the refresh button to the south panel
                    southPanel.removeAll();
                    southPanel.add(refreshButton, BorderLayout.SOUTH);
                    southPanel.setVisible(true);
                    refreshButton.setEnabled(true);
                    validate();
                    repaint();
                }
            } catch (Exception e) {
                Throwable t = Utils.getActualException(e);
                if (JConsole.isDebug()) {
                    System.err.println("Problem displaying MBean " + "attributes for MBean [" + mbean.getObjectName() + "]");
                    t.printStackTrace();
                }
                showErrorDialog(t.toString(), Messages.PROBLEM_DISPLAYING_MBEAN);
            }
        }
    };
    sw.execute();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) MBeanInfo(javax.management.MBeanInfo) BorderLayout(java.awt.BorderLayout) SwingWorker(javax.swing.SwingWorker) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) IOException(java.io.IOException)

Example 8 with SwingWorker

use of javax.swing.SwingWorker in project jdk8u_jdk by JetBrains.

the class XSheet method displayMBeanNode.

// Call on EDT
private void displayMBeanNode(final DefaultMutableTreeNode node) {
    final XNodeInfo uo = (XNodeInfo) node.getUserObject();
    if (!uo.getType().equals(Type.MBEAN)) {
        return;
    }
    mbean = (XMBean) uo.getData();
    SwingWorker<MBeanInfo, Void> sw = new SwingWorker<MBeanInfo, Void>() {

        @Override
        public MBeanInfo doInBackground() throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
            return mbean.getMBeanInfo();
        }

        @Override
        protected void done() {
            try {
                MBeanInfo mbi = get();
                if (mbi != null) {
                    if (!isSelectedNode(node, currentNode)) {
                        return;
                    }
                    mbeanInfo.addMBeanInfo(mbean, mbi);
                    invalidate();
                    mainPanel.removeAll();
                    mainPanel.add(mbeanInfo, BorderLayout.CENTER);
                    southPanel.setVisible(false);
                    southPanel.removeAll();
                    validate();
                    repaint();
                }
            } catch (Exception e) {
                Throwable t = Utils.getActualException(e);
                if (JConsole.isDebug()) {
                    System.err.println("Couldn't get MBeanInfo for MBean [" + mbean.getObjectName() + "]");
                    t.printStackTrace();
                }
                showErrorDialog(t.toString(), Messages.PROBLEM_DISPLAYING_MBEAN);
            }
        }
    };
    sw.execute();
}
Also used : MBeanInfo(javax.management.MBeanInfo) SwingWorker(javax.swing.SwingWorker) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) IOException(java.io.IOException)

Example 9 with SwingWorker

use of javax.swing.SwingWorker in project jabref by JabRef.

the class PreviewPrefsTab method setValues.

@Override
public void setValues() {
    PreviewPreferences previewPreferences = Globals.prefs.getPreviewPreferences();
    chosenModel.clear();
    boolean isPreviewChosen = false;
    for (String style : previewPreferences.getPreviewCycle()) {
        if (CitationStyle.isCitationStyleFile(style)) {
            chosenModel.addElement(CitationStyle.createCitationStyleFromFile(style));
        } else {
            if (isPreviewChosen) {
                LOGGER.error("Preview is already in the list, something went wrong");
                continue;
            }
            isPreviewChosen = true;
            chosenModel.addElement(Localization.lang("Preview"));
        }
    }
    availableModel.clear();
    if (!isPreviewChosen) {
        availableModel.addElement(Localization.lang("Preview"));
    }
    btnLeft.setEnabled(!chosen.isSelectionEmpty());
    btnRight.setEnabled(!available.isSelectionEmpty());
    btnUp.setEnabled(!chosen.isSelectionEmpty());
    btnDown.setEnabled(!chosen.isSelectionEmpty());
    if (discoverCitationStyleWorker != null) {
        discoverCitationStyleWorker.cancel(true);
    }
    discoverCitationStyleWorker = new SwingWorker<List<CitationStyle>, Void>() {

        @Override
        protected List<CitationStyle> doInBackground() throws Exception {
            return CitationStyle.discoverCitationStyles();
        }

        @Override
        public void done() {
            if (this.isCancelled()) {
                return;
            }
            try {
                get().stream().filter(style -> !previewPreferences.getPreviewCycle().contains(style.getFilepath())).sorted((style0, style1) -> style0.getTitle().compareTo(style1.getTitle())).forEach(availableModel::addElement);
                btnRight.setEnabled(!availableModel.isEmpty());
            } catch (InterruptedException | ExecutionException e) {
                LOGGER.error("something went wrong while adding the discovered CitationStyles to the list ");
            }
        }
    };
    discoverCitationStyleWorker.execute();
    layout.setText(Globals.prefs.getPreviewPreferences().getPreviewStyle().replace("__NEWLINE__", "\n"));
}
Also used : ListSelectionModel(javax.swing.ListSelectionModel) TestEntry(org.jabref.logic.util.TestEntry) PreviewPanel(org.jabref.gui.PreviewPanel) Enumeration(java.util.Enumeration) ArrayUtils(org.apache.commons.lang3.ArrayUtils) BasePanel(org.jabref.gui.BasePanel) ArrayList(java.util.ArrayList) SwingWorker(javax.swing.SwingWorker) Localization(org.jabref.logic.l10n.Localization) BorderLayout(java.awt.BorderLayout) BoxLayout(javax.swing.BoxLayout) CitationStyle(org.jabref.logic.citationstyle.CitationStyle) FormBuilder(com.jgoodies.forms.builder.FormBuilder) JButton(javax.swing.JButton) Paddings(com.jgoodies.forms.factories.Paddings) JList(javax.swing.JList) JabRefGUI(org.jabref.JabRefGUI) JOptionPane(javax.swing.JOptionPane) Globals(org.jabref.Globals) ExecutionException(java.util.concurrent.ExecutionException) JScrollPane(javax.swing.JScrollPane) Dimension(java.awt.Dimension) List(java.util.List) DefaultListModel(javax.swing.DefaultListModel) Log(org.apache.commons.logging.Log) JTextArea(javax.swing.JTextArea) LogFactory(org.apache.commons.logging.LogFactory) JPanel(javax.swing.JPanel) PreviewPreferences(org.jabref.preferences.PreviewPreferences) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List) PreviewPreferences(org.jabref.preferences.PreviewPreferences) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with SwingWorker

use of javax.swing.SwingWorker in project beast-mcmc by beast-dev.

the class TreesTableModel method loadTreeFile.

// END: doLoadTreeFile
private void loadTreeFile(final File file, final int row) {
    frame.setBusy();
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        // Executed in background thread
        public Void doInBackground() {
            try {
                Tree tree = Utils.importTreeFromFile(file);
                tree.setId(file.getName());
                setRow(row, new TreesTableRecord(tree.getId(), tree));
            } catch (Exception e) {
                Utils.handleException(e);
            }
            return null;
        }

        // END: doInBackground()
        // Executed in event dispatch thread
        public void done() {
            frame.setIdle();
            frame.fireTaxaChanged();
        }
    };
    worker.execute();
}
Also used : SwingWorker(javax.swing.SwingWorker) Tree(dr.evolution.tree.Tree)

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