/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dlect.ui;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.SwingWorker;
import javax.swing.Timer;
/**
*
* @author lee
*/
public class SwingWorkerProgressDialog extends javax.swing.JDialog {
private static final long serialVersionUID = 1L;
private final Worker w;
/**
* Creates new form SwingWorkerProgressDialog
*
* @param parent
* @param w
*/
public SwingWorkerProgressDialog(Window parent, Worker w) {
super(parent, ModalityType.APPLICATION_MODAL);
initComponents();
this.setLocationRelativeTo(parent);
this.w = w;
this.descLabel.setText(w.getDescription());
this.w.setDialog(this);
}
@Override
public void setVisible(boolean visible) {
w.execute();
super.setVisible(visible);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
progressBar = new javax.swing.JProgressBar();
descLabel = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
closeButton = new javax.swing.JButton();
setMinimumSize(new java.awt.Dimension(300, 100));
getContentPane().setLayout(new java.awt.GridBagLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
getContentPane().add(progressBar, gridBagConstraints);
descLabel.setText("TODO");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
getContentPane().add(descLabel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
getContentPane().add(jSeparator1, gridBagConstraints);
closeButton.setText("Close");
closeButton.setEnabled(false);
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(closeButton, gridBagConstraints);
pack();
}// </editor-fold>//GEN-END:initComponents
private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
this.setVisible(false);
this.dispose();
}//GEN-LAST:event_closeButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton closeButton;
private javax.swing.JLabel descLabel;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JProgressBar progressBar;
// End of variables declaration//GEN-END:variables
public static abstract class Worker extends SwingWorker<Void, ProgressData> {
private SwingWorkerProgressDialog d;
public Worker() {
}
private void setDialog(SwingWorkerProgressDialog d) {
this.d = d;
d.progressBar.setStringPainted(true);
d.closeButton.setEnabled(false);
}
protected void setMax(int max) {
if (d != null) {
d.progressBar.setMaximum(max);
}
}
@Override
protected final void done() {
d.progressBar.setValue(d.progressBar.getMaximum());
d.closeButton.setEnabled(true);
d.progressBar.setValue(d.progressBar.getMaximum());
Timer t = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
d.dispose();
}
});
t.setRepeats(false);
t.start();
}
@Override
protected final void process(List<ProgressData> chunks) {
ProgressData get = chunks.get(chunks.size() - 1);
d.progressBar.setValue(get.getProgress());
d.progressBar.setString(get.getLabel());
}
protected final ProgressData of(String s, Integer i) {
return new ProgressData(s, i);
}
@Override
protected final Void doInBackground() throws Exception {
doAction();
return null;
}
protected abstract String getDescription();
protected abstract void doAction() throws Exception;
}
public static class ProgressData {
private final String label;
private final int progress;
public ProgressData(String label, int progress) {
this.label = label;
this.progress = progress;
}
public String getLabel() {
return label;
}
public int getProgress() {
return progress;
}
}
}