Example usage for javax.swing SwingWorker SwingWorker

List of usage examples for javax.swing SwingWorker SwingWorker

Introduction

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

Prototype

public SwingWorker() 

Source Link

Document

Constructs this SwingWorker .

Usage

From source file:de.atomfrede.tools.evalutation.evaluator.EvaluationController.java

public void startCo2AbsoluteOnlyEvaluation() {
    new SwingWorker<Void, Void>() {

        @Override/*from   w  w w  .  ja va2  s .co  m*/
        protected Void doInBackground() throws Exception {
            disableButtons();
            setupCO2OnlyEvaluation();
            return null;
        }

        @Override
        protected void done() {
            enableButtons();
            mainPanel.removeProgressPanel();
        }
    }.execute();
}

From source file:net.sf.dvstar.transmission.dialogs.SessionStatistic.java

/** Creates new form StatsDialog */
public SessionStatistic(TransmissionView parent, TransmissionWebClient webClient, boolean modal) {
    super(parent.getFrame(), modal);
    this.webClient = webClient;
    this.parent = parent;
    initComponents();/*from   w w  w  .ja  v  a2s  . c  om*/
    int messageTimeout = 5000;
    this.getRootPane().setDefaultButton(btClose);

    new SwingWorker() {

        @Override
        protected Object doInBackground() throws Exception {
            fillFormData();
            return null;
        }
    }.execute();

    updaterTimer = new Timer(messageTimeout, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            new SwingWorker() {

                @Override
                protected Object doInBackground() throws Exception {
                    updaterTimer.stop();
                    fillFormData();
                    updaterTimer.start();
                    return null;
                }
            }.execute();
        }
    });

    updaterTimer.setRepeats(true);
    updaterTimer.start();
}

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

public void doRefresh() {
    if (PlatformUI.MIRTH_FRAME.alertRefresh()) {
        return;//www  . ja  v  a2s . c o m
    }

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

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

        Map<String, ConfigurationProperty> configurationMap = null;

        public Void doInBackground() {
            try {
                configurationMap = getFrame().mirthClient.getConfigurationMap();
            } catch (ClientException e) {
                getFrame().alertThrowable(getFrame(), e);
            }
            return null;
        }

        @Override
        public void done() {
            // null if it failed to get the map settings or if confirmLeave returned false
            if (configurationMap != null) {
                updateConfigurationTable(configurationMap);
            }
            getFrame().stopWorking(workingId);
        }
    };

    worker.execute();
}

From source file:es.emergya.cliente.scheduler.jobs.UpdateAdminJob.java

@SuppressWarnings("unchecked")
@Override/*from   w w  w .j a v  a 2 s . co m*/
public void run() {
    try {
        for (final PluginListener o : (List<PluginListener>) Collections.synchronizedList(updatables))
            try {
                if (!(o instanceof Option) || ((Option) o).needsUpdating()) {
                    SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
                        @Override
                        protected Object doInBackground() throws Exception {
                            PluginEventHandler.fireChange(o);
                            return null;
                        }

                        @Override
                        protected void done() {
                            super.done();
                            o.refresh(null);
                        }
                    };
                    sw.execute();
                }
            } catch (Throwable t) {
                log.error("Error al actualizar " + o.toString() + " debido a " + t.toString());
            }
    } catch (Throwable e) {
        log.fatal("Error al ejecutar la actualizacin de adminjob " + e.toString(), e);
    }
}

From source file:Main.java

public void loadChildren(final DefaultTreeModel model, final PropertyChangeListener progressListener) {
    if (loaded) {
        return;//from w  w  w  .  j  a v a2 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:edu.ku.brc.specify.ui.AttachmentIconMapper.java

/**
 * Sends notification on GUI thread//from  w w w .j  av a 2  s . c o m
 * @param listener
 * @param imgIcon
 */
private ImageIcon notifyListener(final ChangeListener listener, final ImageIcon imgIcon) {
    if (listener != null) {
        SwingWorker<Boolean, Boolean> worker = new SwingWorker<Boolean, Boolean>() {
            @Override
            protected Boolean doInBackground() throws Exception {
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {
                }
                return null;
            }

            @Override
            protected void done() {
                listener.stateChanged(new ChangeEvent(imgIcon));
            }

        };
        worker.execute();
    }
    return imgIcon;
}

From source file:com.clough.android.adbv.view.UpdateTableDialog.java

private synchronized void processResult() {
    try {//from w ww  . j  ava  2s.  com
        final JSONArray jsonArray = new JSONArray(outputResult);
        final int jsonObjectLength = jsonArray.length();

        new SwingWorker<Void, Void>() {

            boolean columnsFound = false;

            @Override
            protected Void doInBackground() {
                rowList = new ArrayList<Row>();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                }
                try {
                    for (int i = 0; i < jsonObjectLength; i++) {
                        waitingDialog.incrementProgressBar();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        Object[] rowData = new Object[defaultTableModel.getColumnCount()];
                        for (int j = 0; j < resultTable.getColumnCount(); j++) {
                            String cellValue = String.valueOf(jsonObject.get(resultTable.getColumnName(j)))
                                    .replaceAll("\n", "").replaceAll("\t", " ");
                            rowData[j] = cellValue;
                        }
                        defaultTableModel.addRow(rowData);
                        rowList.add(new Row(i, rowData, Row.DEFAULT));
                    }
                } catch (Exception e) {
                    return null;
                }
                return null;
            }

            @Override
            protected void done() {
                tableColumnAdjuster.adjustColumns();
                rowController = RowController.getPreparedRowController(rowList);
                closeProgressDialog();
                resultTable.setRowSelectionAllowed(false);
                rowsToChange = new ArrayList<Row>();
                rowsToChange.addAll(rowList);
            }

        }.execute();
        showProgressDialog(false, jsonObjectLength, "Processing " + jsonObjectLength + " fields");
    } catch (JSONException ex) {
        JOptionPane.showMessageDialog(null, outputResult, "Result error", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:de.atomfrede.tools.evalutation.evaluator.EvaluationController.java

public void startIngoEvaluation() {
    new SwingWorker<Void, Void>() {

        @Override/*w ww  .  j  av a  2 s .  c  o m*/
        protected Void doInBackground() throws Exception {
            disableButtons();
            setupIngoEvaluation();
            return null;
        }

        @Override
        protected void done() {
            enableButtons();
            mainPanel.removeProgressPanel();
        }
    }.execute();
}

From source file:MailTest.java

public MailTestFrame() {
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    setTitle("MailTest");

    setLayout(new GridBagLayout());

    // we use the GBC convenience class of Core Java Volume 1 Chapter 9
    add(new JLabel("From:"), new GBC(0, 0).setFill(GBC.HORIZONTAL));

    from = new JTextField(20);
    add(from, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0));

    add(new JLabel("To:"), new GBC(0, 1).setFill(GBC.HORIZONTAL));

    to = new JTextField(20);
    add(to, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0));

    add(new JLabel("SMTP server:"), new GBC(0, 2).setFill(GBC.HORIZONTAL));

    smtpServer = new JTextField(20);
    add(smtpServer, new GBC(1, 2).setFill(GBC.HORIZONTAL).setWeight(100, 0));

    message = new JTextArea();
    add(new JScrollPane(message), new GBC(0, 3, 2, 1).setFill(GBC.BOTH).setWeight(100, 100));

    comm = new JTextArea();
    add(new JScrollPane(comm), new GBC(0, 4, 2, 1).setFill(GBC.BOTH).setWeight(100, 100));

    JPanel buttonPanel = new JPanel();
    add(buttonPanel, new GBC(0, 5, 2, 1));

    JButton sendButton = new JButton("Send");
    buttonPanel.add(sendButton);/*  w  w w  . j  a va 2s  . com*/
    sendButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            new SwingWorker<Void, Void>() {
                protected Void doInBackground() throws Exception {
                    comm.setText("");
                    sendMail();
                    return null;
                }
            }.execute();
        }
    });
}

From source file:edu.ku.brc.specify.web.HttpLargeFileTransfer.java

/**
 * @param infileName/* w  w w.  j av a  2 s .  c  o m*/
 * @param outFileName
 * @param changeListener
 * @return
 */
public boolean compressFile(final String infileName, final String outFileName,
        final PropertyChangeListener propChgListener) {
    final File file = new File(infileName);
    if (file.exists()) {
        long fileSize = file.length();
        if (fileSize > 0) {
            SwingWorker<Integer, Integer> backupWorker = new SwingWorker<Integer, Integer>() {
                protected String errorMsg = null;
                protected FileInputStream fis = null;
                protected GZIPOutputStream fos = null;

                /* (non-Javadoc)
                 * @see javax.swing.SwingWorker#doInBackground()
                 */
                @Override
                protected Integer doInBackground() throws Exception {
                    try {
                        Thread.sleep(100);

                        long totalSize = file.length();
                        long bytesCnt = 0;

                        FileInputStream fis = new FileInputStream(infileName);
                        GZIPOutputStream fos = new GZIPOutputStream(new FileOutputStream(outFileName));

                        byte[] bytes = new byte[BUFFER_SIZE * 10];

                        while (true) {
                            int len = fis.read(bytes);
                            if (len > 0) {
                                fos.write(bytes, 0, len);
                                bytesCnt += len;
                                firePropertyChange("MEGS", 0,
                                        (int) (((double) bytesCnt / (double) totalSize) * 100.0));
                            } else {
                                break;
                            }
                        }

                        fis.close();
                        fos.close();

                    } catch (Exception ex) {
                        ex.printStackTrace();
                        errorMsg = ex.toString();

                    } finally {
                        try {
                            if (fis != null) {
                                fis.close();
                            }
                            if (fos != null) {
                                fos.close();
                            }
                        } catch (IOException ex) {
                            errorMsg = ex.toString();
                        }
                    }
                    firePropertyChange("MEGS", 0, 100);
                    return null;
                }

                @Override
                protected void done() {
                    super.done();

                    UIRegistry.getStatusBar().setProgressDone(HttpLargeFileTransfer.class.toString());

                    //UIRegistry.clearSimpleGlassPaneMsg();

                    if (StringUtils.isNotEmpty(errorMsg)) {
                        UIRegistry.showError(errorMsg);
                    }

                    if (propChgListener != null) {
                        propChgListener.propertyChange(
                                new PropertyChangeEvent(HttpLargeFileTransfer.this, "Done", 0, 1));
                    }
                }
            };

            final JStatusBar statusBar = UIRegistry.getStatusBar();
            statusBar.setIndeterminate(HttpLargeFileTransfer.class.toString(), true);

            UIRegistry.writeSimpleGlassPaneMsg(getLocalizedMessage("Compressing Backup..."), 24);

            backupWorker.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(final PropertyChangeEvent evt) {
                    if ("MEGS".equals(evt.getPropertyName())) {
                        Integer value = (Integer) evt.getNewValue();
                        double val = value / 10.0;
                        statusBar
                                .setText(UIRegistry.getLocalizedMessage("MySQLBackupService.BACKUP_MEGS", val));
                    }
                }
            });
            backupWorker.execute();

        } else {
            // file doesn't exist
        }
    } else {
        // file doesn't exist
    }
    return false;
}