1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.xnap.gui.component;
20
21 import java.awt.GridBagLayout;
22
23 import javax.swing.JLabel;
24 import javax.swing.JPanel;
25 import javax.swing.JProgressBar;
26 import javax.swing.SwingUtilities;
27 import javax.swing.border.EmptyBorder;
28
29 import org.xnap.gui.util.GUIHelper;
30 import org.xnap.gui.util.GridBagHelper;
31
32 public class ProgressDialog extends DefaultDialog implements ProgressMonitor {
33
34
35
36
37
38 private boolean isCancelled = false;
39 private JPanel panel;
40 private JProgressBar progressBar;
41 private JLabel statusLabel;
42 private Thread thread;
43 private boolean disposed = false;
44
45
46
47 public ProgressDialog(int buttons, String title)
48 {
49 super(buttons);
50
51 setModal(true);
52 setTitle(title);
53
54 panel = new JPanel(new GridBagLayout());
55
56
57 statusLabel = new JLabel("");
58 statusLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
59 GridBagHelper.add(panel, statusLabel);
60
61
62 progressBar = new JProgressBar();
63 progressBar.setMinimum(0);
64 progressBar.setMaximum(100);
65 progressBar.setValue(0);
66 GridBagHelper.add(panel, progressBar);
67
68 setMainComponent(panel);
69
70 pack();
71 }
72
73 public ProgressDialog(String title)
74 {
75 this(BUTTON_CANCEL, title);
76 }
77
78
79
80 /***
81 * Sets the cancelled status to true.
82 */
83 public void close()
84 {
85 getCancelAction().setEnabled(false);
86 isCancelled = true;
87 if (thread != null) {
88 thread.interrupt();
89 }
90 }
91
92 /***
93 * Disposes the dialog.
94 */
95 public void done()
96 {
97 Runnable r = new Runnable()
98 {
99 public void run()
100 {
101 if (isVisible()) {
102 dispose();
103 }
104 else {
105 disposed = true;
106 }
107 }
108 };
109 SwingUtilities.invokeLater(r);
110 }
111
112 public JPanel getPanel()
113 {
114 return panel;
115 }
116
117 public boolean isCancelled()
118 {
119 return isCancelled;
120 }
121
122 public void setCancelEnabled(final boolean enabled)
123 {
124 Runnable r = new Runnable()
125 {
126 public void run()
127 {
128 getCancelAction().setEnabled(enabled);
129 }
130 };
131 SwingUtilities.invokeLater(r);
132 }
133
134 public void setMaximum(final int max)
135 {
136 Runnable r = new Runnable()
137 {
138 public void run()
139 {
140 progressBar.setMaximum(max);
141 }
142 };
143 SwingUtilities.invokeLater(r);
144 }
145
146 public void setMinimum(final int min)
147 {
148 Runnable r = new Runnable()
149 {
150 public void run()
151 {
152 progressBar.setMinimum(min);
153 }
154 };
155 SwingUtilities.invokeLater(r);
156 }
157
158 public void setPercent(final double percent)
159 {
160 Runnable r = new Runnable()
161 {
162 public void run()
163 {
164 int value = (int)((progressBar.getMaximum()
165 - progressBar.getMinimum()) * percent);
166 progressBar.setValue(value);
167 }
168 };
169 SwingUtilities.invokeLater(r);
170 }
171
172 public void setValue(final int value)
173 {
174 Runnable r = new Runnable()
175 {
176 public void run()
177 {
178 progressBar.setValue(value);
179 }
180 };
181 SwingUtilities.invokeLater(r);
182 }
183
184 public void setText(final String text)
185 {
186 Runnable r = new Runnable()
187 {
188 public void run()
189 {
190 statusLabel.setText(GUIHelper.tt(text));
191 pack();
192 }
193 };
194 SwingUtilities.invokeLater(r);
195 }
196
197 public void setThread(Thread thread)
198 {
199 this.thread = thread;
200 }
201
202 public void show()
203 {
204 if (disposed) {
205 disposed = false;
206 return;
207 }
208
209 super.show();
210 }
211
212
213
214 }