KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > ProgressDialogImpl


1 package net.suberic.pooka.gui;
2 import javax.swing.*;
3 import java.awt.event.*;
4 import java.util.List JavaDoc;
5
6 import net.suberic.util.swing.*;
7
8
9 /**
10  * A simple implementation of ProgressDialog. This creates and shows a
11  * dialog with a JProgressBar and a cancel button.
12  */

13 public class ProgressDialogImpl implements ProgressDialog {
14
15   JProgressBar progressBar;
16   JDialog dialog;
17   boolean cancelled = false;
18   int mCurrentValue;
19   String JavaDoc mTitle;
20   String JavaDoc mMessage;
21
22   JLabel nameLabel;
23   JPanel buttonPanel;
24   List JavaDoc listenerList = new java.util.ArrayList JavaDoc();
25
26   /**
27    * Creates a ProgressDialogImpl with the given minimum, maximum, and
28    * current values.
29    */

30   public ProgressDialogImpl(int min, int max, int current, String JavaDoc title, String JavaDoc message) {
31     initDialog(min, max, current,title, message);
32   }
33   
34   /**
35    * For subclasses.
36    */

37   protected ProgressDialogImpl() {
38
39   }
40
41   protected void initDialog(int min, int max, int current, String JavaDoc title, String JavaDoc message) {
42     progressBar = new JProgressBar(min, max);
43     
44     mTitle = title;
45     mMessage = message;
46     
47     nameLabel = new JLabel(mTitle);
48     buttonPanel = new JPanel();
49     JButton cancelButton = new JButton(net.suberic.pooka.Pooka.getProperty("button.cancel", "Cancel"));
50     cancelButton.addActionListener(new ActionListener() {
51     public void actionPerformed(ActionEvent e) {
52       cancelAction();
53     }
54       });
55     buttonPanel.add(cancelButton);
56     
57     createDialog();
58     
59     setValue(current);
60   }
61
62   /**
63    * Creates the Dialog in which the ProgressBar will be shown.
64    */

65   protected void createDialog() {
66     dialog = new JDialog();
67     dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
68
69     dialog.getContentPane().add(nameLabel);
70     dialog.getContentPane().add(progressBar);
71     dialog.getContentPane().add(buttonPanel);
72     
73     dialog.pack();
74     
75   }
76
77   /**
78    * Sets the minimum value for the progress dialog.
79    */

80   public void setMinimumValue(int minimum) {
81     progressBar.setMinimum(minimum);
82   }
83
84   /**
85    * Gets the minimum value for the progress dialog.
86    */

87   public int getMinimumValue() {
88     return progressBar.getMinimum();
89   }
90
91   /**
92    * Sets the maximum value for the progress dialog.
93    */

94   public void setMaximumValue(int maximum) {
95     progressBar.setMaximum(maximum);
96   }
97
98   /**
99    * Gets the maximum value for the progress dialog.
100    */

101   public int getMaximumValue() {
102     return progressBar.getMaximum();
103   }
104
105   /**
106    * Sets the current value for the progress dialog.
107    */

108   public void setValue(int current) {
109     mCurrentValue = current;
110     if (SwingUtilities.isEventDispatchThread()) {
111       if (mCurrentValue != getBarValue())
112     progressBar.setValue(mCurrentValue);
113     } else
114       SwingUtilities.invokeLater(new Runnable JavaDoc() {
115       public void run() {
116         if (mCurrentValue != getBarValue())
117           progressBar.setValue(mCurrentValue);
118       }
119     });
120   }
121
122   /**
123    * Gets the configured current value for the progress dialog.
124    */

125   public int getValue() {
126     return mCurrentValue;
127   }
128
129   /**
130    * Gets the actual value for the progress dialog.
131    */

132   public int getBarValue() {
133     return progressBar.getValue();
134   }
135
136   /**
137    * Gets the title for the progress dialog.
138    */

139   public String JavaDoc getTitle() {
140     return mTitle;
141   }
142
143   /**
144    * Gets the display message for the progress dialog.
145    */

146   public String JavaDoc getMessage() {
147     return mMessage;
148   }
149   
150   /**
151    * Cancels the current action.
152    */

153   public void cancelAction() {
154     cancelled = true;
155     for (int i = 0; i < listenerList.size(); i++) {
156       ProgressDialogListener current = (ProgressDialogListener) listenerList.get(i);
157       current.dialogCancelled();
158     }
159   }
160
161   /**
162    * Adds a ProgressDialogListener.
163    */

164   public void addCancelListener(ProgressDialogListener listener) {
165     listenerList.add(listener);
166   }
167
168   /**
169    * Returns whether or not this action has been cancelled.
170    */

171   public boolean isCancelled() {
172     return cancelled;
173   }
174
175   /**
176    * Shows the dialog.
177    */

178   public void show() {
179     if (SwingUtilities.isEventDispatchThread())
180       dialog.setVisible(true);
181     else
182       SwingUtilities.invokeLater(new Runnable JavaDoc() {
183       public void run() {
184         dialog.setVisible(true);
185       }
186     });
187   }
188
189   /**
190    * Disposes of the dialog.
191    */

192   public void dispose() {
193     if (SwingUtilities.isEventDispatchThread())
194       dialog.dispose();
195     else
196       SwingUtilities.invokeLater(new Runnable JavaDoc() {
197       public void run() {
198         dialog.dispose();
199       }
200     });
201   }
202 }
203
Popular Tags