1 package com.explosion.utilities.process;
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.Component;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28
29 import javax.swing.BorderFactory;
30 import javax.swing.JButton;
31 import javax.swing.JLabel;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.JProgressBar;
35 import javax.swing.JToggleButton;
36 import javax.swing.SwingUtilities;
37
38 import com.explosion.utilities.exception.ExceptionManagerFactory;
39 import com.explosion.utilities.process.threads.Finishable;
40 import com.explosion.utilities.process.threads.ProcessThread;
41 import com.explosion.utilities.process.threads.SimpleProcess;
42 import com.explosion.utilities.process.threads.SimpleProcessThread;
43
44 /***
45 * @author Stephen Cowx Date created:@17-Feb-2003
46 */
47 public class ProcessMonitoringStatusBar extends JPanel implements SimpleProcess, Finishable
48 {
49
50 private JButton cancelButton = new JButton("Stop");
51
52 private JToggleButton pauseButton = new JToggleButton("Pause");
53
54 private JPanel buttonPanel = new JPanel();
55
56 private JPanel progressPanel = new JPanel();
57
58 private JLabel statusLabel = new JLabel();
59
60 private JProgressBar progressBar;
61
62 private ProcessThread processThread;
63
64 private ProcessThread workerThread;
65
66 private Component parent;
67
68 private boolean progressBarEnabled = false;
69
70 private boolean isUserProcess = false;
71
72 /***
73 * Returns a boolean value indicating whther this is a user process or not
74 * @return
75 */
76 public boolean isUserProcess()
77 {
78 return this.isUserProcess;
79 }
80
81 /***
82 * Sets whtether or not this is a user process. True means that it is
83 * @param truth
84 */
85 public void setIsUserProcess(boolean truth)
86 {
87 this.isUserProcess = true;
88 }
89
90 public ProcessMonitoringStatusBar(Component parent)
91 {
92 try
93 {
94 this.parent = parent;
95 init();
96 } catch (Exception e)
97 {
98 ExceptionManagerFactory.getExceptionManager().manageException(e, null);
99 }
100 }
101
102 public void init() throws Exception
103 {
104 buttonPanel.setLayout(new GridBagLayout());
105 buttonPanel.add(cancelButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
106 buttonPanel.add(pauseButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
107
108 progressPanel.setLayout(new GridBagLayout());
109 progressPanel.add(statusLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 3, 0, 0), 0, 0));
110
111 cancelButton.addActionListener(new java.awt.event.ActionListener()
112 {
113
114 public void actionPerformed(ActionEvent e)
115 {
116 cancelButton_actionPerformed(e);
117 }
118 });
119 pauseButton.addActionListener(new java.awt.event.ActionListener()
120 {
121
122 public void actionPerformed(ActionEvent e)
123 {
124 pauseButton_actionPerformed(e);
125 }
126 });
127
128 this.setLayout(new GridBagLayout());
129 this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Status"));
130 this.add(progressPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
131 this.add(buttonPanel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
132
133 cancelButton.setVisible(false);
134 pauseButton.setVisible(false);
135 }
136
137 public void setProcessToMonitor(com.explosion.utilities.process.threads.Process processToMonitor) throws Exception
138 {
139 if (this.workerThread != null && workerThread.getRunThread() != null && workerThread.getRunThread().isAlive())
140 throw new Exception("Worker thread is currently busy. Cannot start another process yet.");
141
142 this.workerThread = processToMonitor.getProcessControl();
143 }
144
145 public void setProgressBarEnabled(boolean progressBarEnabled)
146 {
147 this.progressBarEnabled = progressBarEnabled;
148 }
149
150 public void startProcess() throws Exception
151 {
152 if (this.workerThread == null) throw new Exception("Worker thread is null. Cannot initiaite processing.");
153
154 processThread = new SimpleProcessThread();
155 processThread.setInterval(75);
156 processThread.setProcess(this);
157
158 this.workerThread.start();
159 this.processThread.start();
160
161 pauseButton.setText("Pause");
162 pauseButton.setSelected(false);
163
164 cancelButton.setVisible(true);
165 pauseButton.setVisible(true);
166
167 }
168
169 public void finish()
170 {
171 cancelButton.setVisible(false);
172 pauseButton.setVisible(false);
173
174 pauseButton.setText("Pause");
175 pauseButton.setSelected(false);
176
177 statusLabel.setText(workerThread.getProcess().getStatusText());
178 }
179
180 /***
181 * Stops the process but asks the user to confirm, first
182 *
183 */
184 public void stopProcess()
185 {
186 cancelButton_actionPerformed(null);
187 }
188
189 /***
190 * Stops the process without asking for confirmation
191 *
192 */
193 public void stopNoConfirm() throws InterruptedException
194 {
195 workerThread.stop();
196 }
197
198 void cancelButton_actionPerformed(ActionEvent ed)
199 {
200 try
201 {
202 int cancelDecision = -1;
203 boolean alreadySuspended = false;
204 if (workerThread.getStatus() != ProcessThread.THREAD_SUSPENDED)
205 workerThread.suspend();
206 else
207 alreadySuspended = true;
208
209
210 cancelDecision = JOptionPane.showConfirmDialog(parent, "Are you sure you want to stop ?", "Stop ?", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
211 if (cancelDecision == JOptionPane.YES_OPTION)
212 {
213 workerThread.stop();
214 } else
215 {
216 if (!alreadySuspended)
217 workerThread.resume();
218 }
219 } catch (Exception ex)
220 {
221 ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
222 }
223 }
224
225 void pauseButton_actionPerformed(ActionEvent e)
226 {
227 try
228 {
229 if (pauseButton.isSelected())
230 {
231 pauseButton.setText("Resume");
232 workerThread.suspend();
233
234 } else
235 {
236 pauseButton.setText("Pause");
237 workerThread.resume();
238 }
239 } catch (Exception ex)
240 {
241 ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
242 }
243 }
244
245 /***
246 * @see com.explosion.utilities.process.threads.SimpleProcess#process()
247 */
248 public void process() throws Exception
249 {
250 Runnable updateProgress = new Runnable()
251 {
252
253 public void run()
254 {
255 if (progressBarEnabled)
256 {
257 if (progressBar != null)
258 {
259 if (workerThread != null)
260 {
261 progressBar.setValue(workerThread.getProcess().getPercentComplete());
262 } else
263 {
264 progressBar.setValue(0);
265 }
266 }
267 } else
268 statusLabel.setText(workerThread.getProcess().getStatusText());
269 }
270 };
271
272 SwingUtilities.invokeLater(updateProgress);
273
274 }
275
276 public void updateStatus()
277 {
278 statusLabel.setText(workerThread.getProcess().getStatusText());
279 }
280
281 /***
282 * @see com.explosion.utilities.process.threads.Process#getPercentComplete()
283 */
284 public int getPercentComplete()
285 {
286 int status = workerThread.getStatus();
287 if (status == ProcessThread.THREAD_STOPPED || status == ProcessThread.THREAD_COMPLETED)
288 return 100;
289 else
290 return 0;
291 }
292
293 /***
294 * @see com.explosion.utilities.process.threads.Process#getProcessControl()
295 */
296 public ProcessThread getProcessControl()
297 {
298 return null;
299 }
300
301 /***
302 * @see com.explosion.utilities.process.threads.Process#getStatusText()
303 */
304 public String getStatusText()
305 {
306 return "Updating progress bar.";
307 }
308
309 /***
310 * @see com.explosion.utilities.process.threads.Process#setPercentComplete(int)
311 */
312 public void setPercentComplete(int percentComplete)
313 {}
314
315 /***
316 * @see com.explosion.utilities.process.threads.Process#setProcessControl(ProcessThread)
317 */
318 public void setProcessControl(ProcessThread processThread)
319 {}
320
321 /***
322 * @see com.explosion.utilities.process.threads.Process#setStatusText(String)
323 */
324 public void setStatusText(String statusText)
325 {}
326
327 public void initialise() throws Exception
328 {}
329
330 public final void finalise() throws Exception
331 {
332 finishIt();
333 }
334
335 public final void finalise(Exception e)
336 {
337 finishIt();
338 }
339
340 private final void finishIt()
341 {
342 Runnable doFinished = new Runnable()
343 {
344
345 public void run()
346 {
347 finish();
348 }
349 };
350 SwingUtilities.invokeLater(doFinished);
351 }
352
353 public final boolean isStopped()
354 {
355 return processThread.getStatus() == ProcessThread.THREAD_STOPPED;
356 }
357
358 public void log(String string)
359 {}
360
361 public void log(Exception exception, String message)
362 {}
363
364 }