1 package com.explosion.utilities.dialog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.BorderLayout;
24 import java.awt.Canvas;
25 import java.awt.Dimension;
26 import java.awt.Graphics;
27 import java.awt.Image;
28 import java.awt.Toolkit;
29 import java.awt.event.ActionEvent;
30
31 import javax.swing.JButton;
32 import javax.swing.JDialog;
33 import javax.swing.JFrame;
34 import javax.swing.JOptionPane;
35 import javax.swing.JPanel;
36 import javax.swing.JToggleButton;
37
38 import com.explosion.utilities.GeneralUtils;
39 import com.explosion.utilities.Graphics.AnimatedImage;
40 import com.explosion.utilities.exception.ExceptionManagerFactory;
41 import com.explosion.utilities.process.threads.ProcessThread;
42 import com.explosion.utilities.process.threads.UnitisedProcessThread;
43
44 /***
45 * @author Stephen Cowx Date created:@12-Feb-2003
46 */
47 public class BusyDialog extends JDialog implements com.explosion.utilities.process.threads.UnitisedProcess
48 {
49
50
51 protected Image screenImage;
52
53 protected Graphics screenGraphics;
54
55 protected AnimatedImage image;
56
57 protected Canvas canvas;
58
59 protected Image[] images;
60
61
62 private JDialog dialog;
63
64 private JButton cancelButton;
65
66 private JToggleButton pauseButton;
67
68 protected JPanel controlPanel = new JPanel();
69
70
71 protected ProcessThread workerThread;
72
73 protected ProcessThread dialogThread;
74
75
76 protected int width = 75;
77
78 protected int height = 40;
79
80
81 private boolean processUnitComplete = false;
82
83 public BusyDialog(JFrame owner, String title, ProcessThread workerThread, Image[] images)
84 {
85 super(owner, title);
86
87
88 this.images = images;
89 this.workerThread = workerThread;
90
91 cancelButton = new JButton("Cancel");
92 pauseButton = new JToggleButton("Pause ");
93 cancelButton.addActionListener(new java.awt.event.ActionListener()
94 {
95
96 public void actionPerformed(ActionEvent e)
97 {
98 cancelButton_actionPerformed(e);
99 }
100 });
101 pauseButton.addActionListener(new java.awt.event.ActionListener()
102 {
103
104 public void actionPerformed(ActionEvent e)
105 {
106 pauseButton_actionPerformed(e);
107 }
108 });
109 controlPanel.add(cancelButton);
110 controlPanel.add(pauseButton);
111
112 canvas = new Canvas();
113 canvas.setSize(width, height);
114 canvas.setBackground(controlPanel.getBackground());
115
116 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
117
118 this.setResizable(false);
119 this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
120
121 this.getContentPane().setLayout(new BorderLayout());
122 this.getContentPane().add(canvas, BorderLayout.NORTH);
123 this.getContentPane().add(controlPanel, BorderLayout.SOUTH);
124 this.pack();
125
126
127
128
129
130 width = this.getWidth();
131 canvas.setSize(width, height);
132
133
134
135 image = new AnimatedImage((width / 2) - 16, (height / 2) - 16, images, 32, 32, width, height, false, true);
136
137 try
138 {
139 GeneralUtils.centreWindowInParent(this);
140 } catch (Exception e)
141 {}
142 this.setVisible(true);
143 }
144
145 public void start()
146 {
147 dialogThread = new UnitisedProcessThread();
148 dialogThread.setProcess(this);
149 dialogThread.setInterval(0);
150
151 workerThread.start();
152 dialogThread.start();
153 }
154
155 void cancelButton_actionPerformed(ActionEvent e)
156 {
157 try
158 {
159 int cancelDecision = -1;
160
161 cancelDecision = JOptionPane.showConfirmDialog(dialog, "Are you sure you want to cancel ?", "Cancel ?", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
162 if (cancelDecision == JOptionPane.YES_OPTION)
163 {
164 workerThread.stop();
165 dialogThread.stop();
166 }
167 } catch (Exception ex)
168 {
169 ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
170 }
171 }
172
173 void pauseButton_actionPerformed(ActionEvent e)
174 {
175 try
176 {
177 if (pauseButton.isSelected())
178 {
179 pauseButton.setText("Resume");
180 workerThread.suspend();
181 dialogThread.suspend();
182 } else
183 {
184 pauseButton.setText("Pause ");
185 workerThread.resume();
186 dialogThread.resume();
187 canvas.getGraphics().drawImage(screenImage, 0, 0, null);
188 }
189 } catch (Exception ex)
190 {
191 ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
192 }
193 }
194
195 /***
196 * @see com.explosion.utilities.process.threads.UnitisedProcess#processUnit()
197 */
198 public void processUnit() throws Exception
199 {
200
201 if (screenImage == null)
202 {
203 screenImage = canvas.createVolatileImage(width, height);
204 screenGraphics = screenImage.getGraphics();
205 }
206
207
208 screenGraphics.setColor(controlPanel.getBackground());
209 screenGraphics.fillRect(0, 0, width, height);
210
211
212 image.draw(screenGraphics);
213 image.cycle();
214
215
216 canvas.getGraphics().drawImage(screenImage, 0, 0, null);
217
218
219 try
220 {
221 Thread.sleep(100);
222 } catch (InterruptedException e)
223 {}
224 }
225
226 /***
227 * @see com.explosion.utilities.process.threads.UnitisedProcess#getPercentCompete()
228 */
229 public int getPercentComplete()
230 {
231 int status = workerThread.getStatus();
232 if (status == ProcessThread.THREAD_STOPPED || status == ProcessThread.THREAD_COMPLETED)
233 return 100;
234 else
235 return 0;
236 }
237
238 /***
239 * @see com.explosion.utilities.process.threads.UnitisedProcess#processUnitComplete()
240 */
241 public boolean processUnitComplete()
242 {
243 int status = workerThread.getStatus();
244 if (status == ProcessThread.THREAD_STOPPED || status == ProcessThread.THREAD_COMPLETED)
245 {
246 return true;
247 } else
248 return false;
249 }
250
251 public void finaliseProcessing() throws Exception
252 {
253 dialogThread.stop();
254 this.dispose();
255 }
256
257 /***
258 * Sets the value ofthestatusText for this process.
259 */
260 public void setPercentComplete(int percentComplete)
261 {}
262
263 public void setStatusText(String statusText)
264 {}
265
266 public void initialiseProcessing() throws Exception
267 {}
268
269 public void finaliseProcessing(Exception e)
270 {}
271
272 public String getStatusText()
273 {
274 return null;
275 }
276
277 public void beginProcessUnit() throws Exception
278 {}
279
280 public void endProcessUnit() throws Exception
281 {}
282
283 public ProcessThread getProcessControl()
284 {
285 return dialogThread;
286 }
287
288 public void setProcessControl(ProcessThread processThread)
289 {
290 dialogThread = processThread;
291 }
292
293 public final boolean isStopped()
294 {
295 return dialogThread.getStatus() == ProcessThread.THREAD_STOPPED;
296 }
297
298 public void log(String string)
299 {}
300
301 public void log(Exception exception, String message)
302 {}
303
304 private boolean isUserProcess = false;
305
306 /***
307 * Returns a boolean value indicating whther this is a user process or not
308 * @return
309 */
310 public boolean isUserProcess()
311 {
312 return this.isUserProcess;
313 }
314
315 /***
316 * Sets whtether or not this is a user process. True means that it is
317 * @param truth
318 */
319 public void setIsUserProcess(boolean truth)
320 {
321 this.isUserProcess = true;
322 }
323
324 }