Example usage for javax.swing SwingWorker execute

List of usage examples for javax.swing SwingWorker execute

Introduction

In this page you can find the example usage for javax.swing SwingWorker execute.

Prototype

public final void execute() 

Source Link

Document

Schedules this SwingWorker for execution on a worker thread.

Usage

From source file:com.mirth.connect.manager.ManagerController.java

public void restartMirthWorker() {
    PlatformUI.MANAGER_DIALOG.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    setEnabledOptions(false, false, false, false);

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        private String errorMessage = null;

        public Void doInBackground() {
            errorMessage = restartMirth();
            return null;
        }//from   www .j a v  a 2  s  .  c om

        public void done() {
            if (errorMessage == null) {
                PlatformUI.MANAGER_TRAY.alertInfo("The Mirth Connect Service was restarted successfully.");
            } else {
                PlatformUI.MANAGER_TRAY.alertError(errorMessage);
            }

            updateMirthServiceStatus();
            PlatformUI.MANAGER_DIALOG.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    };

    worker.execute();
}

From source file:es.emergya.ui.gis.ControlPanel.java

public void setAvaliableIncidences(Collection<Object> res) {
    avaliableIncidences = new Vector<Object>(res);
    Collections.sort(avaliableIncidences, comparator);

    SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {

        @Override//  w w  w  .  j  a  v  a  2s .  c o m
        protected Object doInBackground() throws Exception {
            return null;
        }

        @Override
        protected void done() {
            super.done();
            if (!isComboIncidencesShowing) {
                Object selected = incidences.getSelectedItem();
                incidences.removeAllItems();
                for (Object o : avaliableIncidences) {
                    incidences.addItem(o);
                }
                incidences.setSelectedItem(selected);
            }
            if (!isComboIncidencesShowing)
                incidences.updateUI();
        }
    };
    sw.execute();
}

From source file:es.emergya.ui.gis.popups.GPSDialog.java

public GPSDialog(Recurso r) {
    super();//from   w  ww . j  a  v a2  s  .  c om
    setAlwaysOnTop(true);
    setResizable(false);
    iconTransparente = LogicConstants.getIcon("48x48_transparente");
    iconEnviando = LogicConstants.getIcon("anim_actualizando");
    target = r;
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setPreferredSize(new Dimension(400, 150));
    setTitle(i18n.getString("window.gps.titleBar") + " " + target.getIdentificador());
    try {
        setIconImage(((BasicWindow) GoClassLoader.getGoClassLoader().load(BasicWindow.class)).getFrame()
                .getIconImage());
    } catch (Throwable e) {
        LOG.error("There is no icon image", e);
    }

    JPanel base = new JPanel();

    base.setBackground(Color.WHITE);
    base.setLayout(new BoxLayout(base, BoxLayout.Y_AXIS));

    // Icono del titulo
    JPanel title = new JPanel(new FlowLayout(FlowLayout.LEADING));
    final JLabel titleLabel = new JLabel(i18n.getString("window.gps.title"),
            LogicConstants.getIcon("tittleventana_icon_actualizargps"), JLabel.LEFT);

    titleLabel.setFont(LogicConstants.deriveBoldFont(12f));
    title.add(titleLabel);
    title.setOpaque(false);
    base.add(title);

    // Area para mensajes
    JPanel notificationArea = new JPanel();

    notificationArea.setOpaque(false);
    notification = new JLabel("PLACEHOLDER");
    notification.setForeground(Color.WHITE);
    notificationArea.add(notification);
    base.add(notificationArea);

    JPanel buttons = new JPanel();

    buttons.setOpaque(false);
    buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
    actualizar = new JButton(i18n.getString("window.gps.button.actualizar"),
            LogicConstants.getIcon("ventanacontextual_button_solicitargps"));
    actualizar.addActionListener(this);
    buttons.add(actualizar);
    buttons.add(Box.createHorizontalGlue());
    progressIcon = new JLabel(iconTransparente);
    buttons.add(progressIcon);
    buttons.add(Box.createHorizontalGlue());

    JButton cancel = new JButton(i18n.getString("Buttons.cancel"), LogicConstants.getIcon("button_cancel"));

    cancel.addActionListener(this);
    buttons.add(cancel);
    base.add(buttons);
    getContentPane().add(base);
    pack();

    int x;
    int y;
    Container myParent;
    try {
        myParent = ((BasicWindow) GoClassLoader.getGoClassLoader().load(BasicWindow.class)).getFrame()
                .getContentPane();
        java.awt.Point topLeft = myParent.getLocationOnScreen();
        Dimension parentSize = myParent.getSize();

        Dimension mySize = getSize();

        if (parentSize.width > mySize.width)
            x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
        else
            x = topLeft.x;

        if (parentSize.height > mySize.height)
            y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
        else
            y = topLeft.y;

        setLocation(x, y);
    } catch (Throwable e1) {
        LOG.error("There is no basic window!", e1);
    }
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent arg0) {
            deleteErrorMessage();
        }

        @Override
        public void windowClosed(WindowEvent arg0) {
            deleteErrorMessage();
        }

        @Override
        public void windowClosing(WindowEvent arg0) {
            deleteErrorMessage();
        }

        private void deleteErrorMessage() {
            SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
                @Override
                protected Object doInBackground() throws Exception {
                    if (last_bandejaSalida != null) {
                        MessageGenerator.remove(last_bandejaSalida.getId());
                    }

                    return null;
                }

                @Override
                protected void done() {
                    super.done();
                    GPSDialog.this.progressIcon.setIcon(iconTransparente);
                    GPSDialog.this.progressIcon.repaint();
                    last_bandejaSalida = null;
                    GPSDialog.this.notification.setText("");
                    GPSDialog.this.notification.repaint();
                }
            };

            sw.execute();
        }
    });
}

From source file:com.mirth.connect.client.ui.SettingsPanelDatabaseTasks.java

public void doRunDatabaseTask() {
    DatabaseTask databaseTask = (DatabaseTask) taskTable.getValueAt(taskTable.getSelectedRow(), 1);

    if (databaseTask.getConfirmationMessage() != null
            && !getFrame().alertOption(getFrame(), databaseTask.getConfirmationMessage())) {
        return;//from  w ww  .j  ava 2  s. c o  m
    }

    final String workingId = getFrame().startWorking("Running database task...");
    final String taskId = databaseTask.getId();

    SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {

        @Override
        public String doInBackground() throws ClientException {
            return getFrame().mirthClient.runDatabaseTask(taskId);
        }

        @Override
        public void done() {
            try {
                String result = get();
                if (StringUtils.isNotBlank(result)) {
                    getFrame().alertInformation(getFrame(), result);
                }
            } catch (Throwable t) {
                if (t instanceof ExecutionException) {
                    t = t.getCause();
                }
                getFrame().alertThrowable(getFrame(), t, "Error running database task: " + t.getMessage());
            } finally {
                getFrame().stopWorking(workingId);
                doRefresh();
            }
        }
    };

    worker.execute();
    doRefresh();
}

From source file:Main.java

public void loadChildren(final DefaultTreeModel model, final PropertyChangeListener progressListener) {
    if (loaded) {
        return;//ww w . j a v a  2 s  .  c o  m
    }
    SwingWorker<List<MyTreeNode>, Void> worker = new SwingWorker<List<MyTreeNode>, Void>() {
        @Override
        protected List<MyTreeNode> doInBackground() throws Exception {
            setProgress(0);
            List<MyTreeNode> children = new ArrayList<MyTreeNode>();
            if (depth < 5) {
                for (int i = 0; i < 5; i++) {
                    Thread.sleep(300);
                    children.add(new MyTreeNode(i + 1, depth + 1));
                    setProgress((i + 1) * 20);
                }
            } else {
                Thread.sleep(1000);
            }
            setProgress(0);
            return children;
        }

        @Override
        protected void done() {
            try {
                setChildren(get());
                model.nodeStructureChanged(MyTreeNode.this);
            } catch (Exception e) {
                e.printStackTrace();
            }
            super.done();
        }
    };
    if (progressListener != null) {
        worker.getPropertyChangeSupport().addPropertyChangeListener("progress", progressListener);
    }
    worker.execute();
}

From source file:es.emergya.ui.gis.ControlPanel.java

public void setAvaliableResources(Collection<Object> res) {
    avaliableResources = new Vector<Object>(res);
    Collections.sort(avaliableResources, comparator);

    SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {

        @Override//from w  w w .  j av a 2s.co m
        protected Object doInBackground() throws Exception {
            return null;
        }

        @Override
        protected void done() {
            super.done();
            // actualizamos la lista
            if (!isComboResourcesShowing) {
                Object selected = resources.getSelectedItem();
                resources.removeAllItems();
                for (Object o : avaliableResources) {
                    resources.addItem(o);
                }
                resources.setSelectedItem(selected);
            }
            if (!isComboResourcesShowing)
                resources.updateUI();
        }
    };
    sw.execute();
}

From source file:com.mirth.connect.plugins.datapruner.DataPrunerPanel.java

public void doStop() {
    setStopTaskVisible(false);/*from w  w w . ja v a 2 s .  c  o m*/
    final String workingId = parent.startWorking("Stopping the data pruner...");

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() {
            try {
                parent.mirthClient.getServlet(DataPrunerServletInterface.class).stop();
            } catch (Exception e) {
                parent.alertThrowable(parent, e, "An error occurred while attempting to stop the data pruner.");
                return null;
            }

            return null;
        }

        @Override
        public void done() {
            parent.stopWorking(workingId);
            updateStatus();
        }
    };

    worker.execute();
}

From source file:coreferenceresolver.gui.MarkupGUI.java

public MarkupGUI() throws IOException {
    highlightPainters = new ArrayList<>();

    for (int i = 0; i < COLORS.length; ++i) {
        DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(
                COLORS[i]);/*from   w  w  w .  j  ava2 s . c o m*/
        highlightPainters.add(highlightPainter);
    }

    defaulPath = FileUtils.readFileToString(new File(".\\src\\coreferenceresolver\\gui\\defaultpath"));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());

    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    //create menu items
    JMenuItem importMenuItem = new JMenuItem("Import");

    JMenuItem exportMenuItem = new JMenuItem("Export");

    fileMenu.add(importMenuItem);
    fileMenu.add(exportMenuItem);

    menuBar.add(fileMenu);

    this.setJMenuBar(menuBar);

    ScrollablePanel mainPanel = new ScrollablePanel();
    mainPanel.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.NONE);
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    //IMPORT BUTTON
    importMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //                MarkupGUI.reviewElements.clear();
            //                MarkupGUI.markupReviews.clear();                
            JFileChooser markupFileChooser = new JFileChooser(defaulPath);
            markupFileChooser.setDialogTitle("Choose your markup file");
            markupFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

            if (markupFileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                final JDialog d = new JDialog();
                JPanel p1 = new JPanel(new GridBagLayout());
                p1.add(new JLabel("Please Wait..."), new GridBagConstraints());
                d.getContentPane().add(p1);
                d.setSize(100, 100);
                d.setLocationRelativeTo(null);
                d.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                d.setModal(true);

                SwingWorker<?, ?> worker = new SwingWorker<Void, Void>() {
                    protected Void doInBackground() throws IOException, BadLocationException {
                        readMarkupFile(markupFileChooser.getSelectedFile().getAbsolutePath());
                        for (int i = 0; i < markupReviews.size(); ++i) {
                            mainPanel.add(newReviewPanel(markupReviews.get(i), i));
                        }
                        return null;
                    }

                    protected void done() {
                        MarkupGUI.this.revalidate();
                        d.dispose();
                    }
                };
                worker.execute();
                d.setVisible(true);
            } else {
                return;
            }
        }
    });

    //EXPORT BUTTON: GET NEW VALUE (REF, TYPE) OF NPs      
    exportMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser markupFileChooser = new JFileChooser(defaulPath);
            markupFileChooser.setDialogTitle("Choose where your markup file saved");
            markupFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            if (markupFileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                final JDialog d = new JDialog();
                JPanel p1 = new JPanel(new GridBagLayout());
                p1.add(new JLabel("Please Wait..."), new GridBagConstraints());
                d.getContentPane().add(p1);
                d.setSize(100, 100);
                d.setLocationRelativeTo(null);
                d.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                d.setModal(true);

                SwingWorker<?, ?> worker = new SwingWorker<Void, Void>() {
                    protected Void doInBackground() throws IOException {
                        for (Review review : markupReviews) {
                            generateNPsRef(review);
                        }
                        int i = 0;
                        for (ReviewElement reviewElement : reviewElements) {
                            int j = 0;
                            for (Element element : reviewElement.elements) {
                                String newType = element.typeSpinner.getValue().toString();
                                if (newType.equals("Object")) {
                                    markupReviews.get(i).getNounPhrases().get(j).setType(0);
                                } else if (newType.equals("Attribute")) {
                                    markupReviews.get(i).getNounPhrases().get(j).setType(3);
                                } else if (newType.equals("Other")) {
                                    markupReviews.get(i).getNounPhrases().get(j).setType(1);
                                } else if (newType.equals("Candidate")) {
                                    markupReviews.get(i).getNounPhrases().get(j).setType(2);
                                }
                                ++j;
                            }
                            ++i;
                        }
                        initMarkupFile(markupFileChooser.getSelectedFile().getAbsolutePath() + File.separator
                                + "markup.out.txt");
                        return null;
                    }

                    protected void done() {
                        d.dispose();
                        try {
                            Desktop.getDesktop()
                                    .open(new File(markupFileChooser.getSelectedFile().getAbsolutePath()));
                        } catch (IOException ex) {
                            Logger.getLogger(MarkupGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                };
                worker.execute();
                d.setVisible(true);
            } else {
                return;
            }
        }
    });

    JScrollPane scrollMainPane = new JScrollPane(mainPanel);
    scrollMainPane.getVerticalScrollBar().setUnitIncrement(16);
    scrollMainPane.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
    scrollMainPane.setSize(this.getWidth(), this.getHeight());
    this.setResizable(false);
    this.add(scrollMainPane, BorderLayout.CENTER);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.pack();
}

From source file:com.mirth.connect.plugins.datapruner.DataPrunerPanel.java

@Override
public void doRefresh() {
    if (PlatformUI.MIRTH_FRAME.alertRefresh()) {
        return;//  w w w .ja  va 2s .c om
    }

    final String workingId = getFrame().startWorking("Loading " + getTabName() + " properties...");

    final Properties serverProperties = new Properties();

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        public Void doInBackground() {
            try {
                Properties propertiesFromServer = plugin.getPropertiesFromServer();

                if (propertiesFromServer != null) {
                    serverProperties.putAll(propertiesFromServer);
                }
            } catch (Exception e) {
                getFrame().alertThrowable(getFrame(), e);
            }
            return null;
        }

        @Override
        public void done() {
            setProperties(serverProperties);
            getFrame().stopWorking(workingId);
        }
    };

    worker.execute();
}

From source file:org.eumetsat.metop.visat.SounderInfoView.java

private void updateEarthLocationFields(final Ifov selectedIfov, final EpsFile epsFile) {
    final SwingWorker<GeoPos, Object> worker = new SwingWorker<GeoPos, Object>() {
        @Override/* w ww .  java 2  s .c  o  m*/
        protected GeoPos doInBackground() throws Exception {
            return readEarthLocation(epsFile, selectedIfov);
        }

        @Override
        protected void done() {
            try {
                final GeoPos geoPos = get();
                lonTextField.setText(geoPos.getLonString());
                latTextField.setText(geoPos.getLatString());
            } catch (InterruptedException e) {
                clearEarthLocationFields(IO_ERROR);
            } catch (ExecutionException e) {
                clearEarthLocationFields(IO_ERROR);
            }
        }
    };
    worker.execute();
}