001: package org.dbbrowser.ui.widget;
002:
003: import java.awt.FlowLayout;
004: import java.awt.event.WindowAdapter;
005: import java.awt.event.WindowEvent;
006: import javax.swing.JDialog;
007: import javax.swing.JFrame;
008: import javax.swing.JLabel;
009: import javax.swing.JProgressBar;
010: import javax.swing.event.ChangeEvent;
011: import javax.swing.event.ChangeListener;
012: import org.dbbrowser.ui.UIControllerForQueries;
013:
014: /**
015: * A dialog which shows the progress of a task and some label
016: */
017: public class ProgressDialog implements ChangeListener {
018: private static final long serialVersionUID = UIControllerForQueries.version;
019:
020: private String title = "";
021: private String textForLabel = "";
022: private JDialog frame = null;
023: private JProgressBar aProgressBar = null;
024: private JLabel label = null;
025: private Integer minValue = null;
026: private Integer maxValue = null;
027:
028: /**
029: * Constructer
030: * @param title
031: * @param textForLabel
032: * @param minValue
033: * @param maxValue
034: */
035: public ProgressDialog(String title, String textForLabel,
036: Integer minValue, Integer maxValue) {
037: this .title = title;
038: this .textForLabel = textForLabel;
039: this .minValue = minValue;
040: this .maxValue = maxValue;
041: initialize();
042: }
043:
044: private void initialize() {
045: //Setup the frame
046: this .frame = new JDialog();
047: this .frame.setTitle(this .title);
048: this .frame.setResizable(false);
049: this .frame.getContentPane().setLayout(new FlowLayout());
050: this .frame.setSize(300, 100);
051: this .frame.setLocationRelativeTo(null);
052: this .frame
053: .addWindowFocusListener(new ProgressDialogWindowAdapter());
054: this .frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
055:
056: //Setup the progress bar
057: aProgressBar = new JProgressBar(this .minValue.intValue(),
058: this .maxValue.intValue());
059: aProgressBar.setValue(this .minValue.intValue());
060: aProgressBar.setStringPainted(true);
061:
062: //Set the label
063: this .label = new JLabel(this .textForLabel);
064:
065: //Add the widgets
066: this .frame.getContentPane().add(this .aProgressBar);
067: this .frame.getContentPane().add(this .label);
068: }
069:
070: /**
071: * Returns the maximum value for the progress of the task
072: * @return
073: */
074: public Integer getMaxValue() {
075: return this .maxValue;
076: }
077:
078: /**
079: * Returns the starting(min) value
080: * @return
081: */
082: public Integer getMinValue() {
083: return this .minValue;
084: }
085:
086: /**
087: * Call this method to set the value during the progress of the task.
088: * @param newValue
089: */
090: public void setValue(Integer newValue) {
091: this .aProgressBar.setValue(newValue.intValue());
092: }
093:
094: /**
095: * Set to true to make it an indeterminate progress bar
096: * @param b
097: */
098: public void setIndeterminate(boolean b) {
099: this .aProgressBar.setIndeterminate(b);
100: }
101:
102: /**
103: * Set true if you want to see the labels
104: * @param b
105: */
106: public void setStringPainted(boolean b) {
107: this .aProgressBar.setStringPainted(b);
108: }
109:
110: /**
111: * Call this during the progress of the task. This calls setValue as well. Call this method if it is a determinate progress bar
112: * @param e
113: */
114: public void stateChanged(ChangeEvent e) {
115: int progress = ((Integer) e.getSource()).intValue();
116: this .aProgressBar.setValue(progress);
117: }
118:
119: /**
120: * Shows the progress bar
121: */
122: public void show() {
123: this .frame.show();
124: }
125:
126: /**
127: * Call this method to close the progress bar when the task is completed
128: */
129: public void close() {
130: this .frame.pack();
131: this .frame.dispose();
132: }
133:
134: private class ProgressDialogWindowAdapter extends WindowAdapter {
135: public void windowLostFocus(WindowEvent e) {
136: frame.toFront();
137: }
138: }
139: }
|