root / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / incrementabletask / IncrementableProcess.java @ 34688
History | View | Annotate | Download (10.2 KB)
1 |
package org.gvsig.gui.beans.incrementabletask; |
---|---|
2 |
|
3 |
/* gvSIG. Geographic Information System of the Valencian Government
|
4 |
*
|
5 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
6 |
* of the Valencian Government (CIT)
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
21 |
* MA 02110-1301, USA.
|
22 |
*
|
23 |
*/
|
24 |
|
25 |
import java.awt.event.MouseAdapter; |
26 |
import java.awt.event.MouseEvent; |
27 |
|
28 |
import javax.swing.JButton; |
29 |
import javax.swing.JOptionPane; |
30 |
|
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
import org.gvsig.gui.beans.Messages; |
35 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
36 |
import org.gvsig.gui.beans.progresspanel.LogControl; |
37 |
|
38 |
/**
|
39 |
* Process to be executed by an {@link IncrementableTask IncrementableTask}.
|
40 |
*
|
41 |
* @version 20/08/2008
|
42 |
*
|
43 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
44 |
*/
|
45 |
public abstract class IncrementableProcess implements IIncrementable, IncrementableListener, Runnable { |
46 |
|
47 |
private static final Logger LOG = LoggerFactory |
48 |
.getLogger(IncrementableProcess.class); |
49 |
|
50 |
protected IncrementableTask iTask = null; |
51 |
protected LogControl log = new LogControl(); |
52 |
protected int percentage = 0; |
53 |
protected boolean ended = false; |
54 |
protected volatile boolean threadSuspended = false; |
55 |
protected volatile Thread blinker = null; |
56 |
protected long t0 = 0; |
57 |
protected static Cancellable cancelProcess = null; |
58 |
protected boolean isCancellable = true; |
59 |
protected boolean isPausable = false; |
60 |
|
61 |
|
62 |
protected String title = ""; |
63 |
protected String label = ""; |
64 |
|
65 |
/**
|
66 |
* Creates a new process.
|
67 |
*
|
68 |
* @param title title for the dialog that displays the evolution of the process
|
69 |
*/
|
70 |
public IncrementableProcess(String title) { |
71 |
super();
|
72 |
this.title = title;
|
73 |
this.cancelProcess = new Cancel(); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Creates a new process.
|
78 |
*
|
79 |
* @param title title for the dialog that displays the evolution of the process
|
80 |
* @param label brief of this process, that will be displayed in the dialog
|
81 |
*/
|
82 |
public IncrementableProcess(String title, String label) { |
83 |
super();
|
84 |
this.title = title;
|
85 |
this.label = label;
|
86 |
this.cancelProcess = new Cancel(); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* Creates a new process.
|
91 |
*
|
92 |
* @param title title for the dialog that displays the evolution of the process
|
93 |
* @param label brief of this process, that will be displayed in the dialog
|
94 |
* @param cancellable determines if this process can be canceled
|
95 |
* @param pausable determines if this process can be paused
|
96 |
*/
|
97 |
public IncrementableProcess(String title, String label, boolean cancellable, boolean pausable) { |
98 |
super();
|
99 |
this.title = title;
|
100 |
this.label = label;
|
101 |
this.cancelProcess = new Cancel(); |
102 |
this.isCancellable = cancellable;
|
103 |
this.isPausable = pausable;
|
104 |
} |
105 |
/*
|
106 |
* (non-Javadoc)
|
107 |
*
|
108 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
109 |
*/
|
110 |
public String getTitle() { |
111 |
return title;
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Adds a new line to the log.
|
116 |
*
|
117 |
* @param line
|
118 |
* the line to add
|
119 |
*/
|
120 |
protected void insertLineLog(String line) { |
121 |
log.addLine(line); |
122 |
} |
123 |
|
124 |
/*
|
125 |
* (non-Javadoc)
|
126 |
*
|
127 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
128 |
*/
|
129 |
public String getLabel() { |
130 |
return label;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* <p>Sets a brief of the current subprocess.</p>
|
135 |
*
|
136 |
* @param label brief of the current subprocess
|
137 |
*/
|
138 |
public void setLabel(String label) { |
139 |
this.label = label;
|
140 |
} |
141 |
|
142 |
/*
|
143 |
* (non-Javadoc)
|
144 |
*
|
145 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
|
146 |
*/
|
147 |
public String getLog() { |
148 |
return log.getText();
|
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
*
|
154 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
155 |
*/
|
156 |
public int getPercent() { |
157 |
if (percentage > 100) |
158 |
percentage = 100;
|
159 |
|
160 |
if (percentage == 100) |
161 |
ended = true;
|
162 |
|
163 |
return percentage;
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* Creates and starts a new thread to execute this importation process.
|
168 |
*
|
169 |
* @see Thread#start()
|
170 |
* @see #stop()
|
171 |
*/
|
172 |
public void start() { |
173 |
blinker = new Thread(this); |
174 |
blinker.start(); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* @see Thread#stop()
|
179 |
* @see #start()
|
180 |
*/
|
181 |
public synchronized void stop() { |
182 |
ended = true;
|
183 |
blinker = null;
|
184 |
notify(); |
185 |
} |
186 |
|
187 |
/**
|
188 |
* @see Thread#isAlive()
|
189 |
* @see #start()
|
190 |
*/
|
191 |
public boolean isAlive() { |
192 |
return blinker.isAlive();
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* Ends the thread that displays the progress dialog with the evolution of
|
197 |
* the loading process.
|
198 |
*
|
199 |
* @see IncrementableTask#processFinalize()
|
200 |
*/
|
201 |
protected void processFinalize() { |
202 |
iTask.processFinalize(); |
203 |
} |
204 |
|
205 |
/**
|
206 |
* Sets the object that will display the evolution of this loading process
|
207 |
* as a progress dialog.
|
208 |
*
|
209 |
* @param iTask
|
210 |
* the object that will display the evolution of this loading
|
211 |
* process
|
212 |
*/
|
213 |
public void setIncrementableTask(IncrementableTask iTask) { |
214 |
this.iTask = iTask;
|
215 |
iTask.setAskCancel(true);
|
216 |
iTask.getButtonsPanel().addAccept(); |
217 |
iTask.getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_ACCEPT, false);
|
218 |
|
219 |
JButton jButton = iTask.getButtonsPanel().getButton(
|
220 |
ButtonsPanel.BUTTON_ACCEPT); |
221 |
jButton.addMouseListener(new MouseAdapter() { |
222 |
/*
|
223 |
* (non-Javadoc)
|
224 |
*
|
225 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
226 |
*/
|
227 |
public void mouseClicked(MouseEvent e) { |
228 |
processFinalize(); |
229 |
} |
230 |
}); |
231 |
} |
232 |
|
233 |
/**
|
234 |
* Determines if this thread has been suspended.
|
235 |
*
|
236 |
* @return <code>true</code> if this thread has been suspended; otherwise <code>false</code>
|
237 |
*/
|
238 |
public boolean isSuspended() { |
239 |
return threadSuspended;
|
240 |
} |
241 |
|
242 |
/*
|
243 |
* (non-Javadoc)
|
244 |
* @see java.lang.Runnable#run()
|
245 |
*/
|
246 |
public void run() { |
247 |
String text = null; |
248 |
|
249 |
try {
|
250 |
process(); |
251 |
while (! ended) {
|
252 |
t0 += 500;
|
253 |
Thread.currentThread().sleep(150); |
254 |
} |
255 |
} catch (Exception ie) { |
256 |
if (! cancelProcess.isCanceled()) {
|
257 |
LOG.error("", ie);
|
258 |
label = Messages.getText("Process_failed");
|
259 |
iTask.getProgressPanel().setLabel(label); |
260 |
text = Messages.getText("Failed_the_process");
|
261 |
} |
262 |
else {
|
263 |
label = Messages.getText("Process_canceled");
|
264 |
iTask.getProgressPanel().setLabel(label); |
265 |
text = Messages.getText("Process_canceled");
|
266 |
} |
267 |
} |
268 |
finally {
|
269 |
iTask.setAskCancel(false);
|
270 |
iTask.getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_ACCEPT, true);
|
271 |
iTask.getButtonsPanel().setEnabled(ButtonsPanel.BUTTON_CANCEL, false);
|
272 |
|
273 |
if (text != null) { |
274 |
log.addLine(Messages.getText("Percent") + ": " + getPercent()); |
275 |
log.addLine(text); |
276 |
|
277 |
if (cancelProcess.isCanceled())
|
278 |
JOptionPane.showMessageDialog(iTask.getButtonsPanel(), text, Messages.getText("Information"), JOptionPane.INFORMATION_MESSAGE); |
279 |
else
|
280 |
JOptionPane.showMessageDialog(iTask.getButtonsPanel(), text, Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
281 |
} |
282 |
|
283 |
if (percentage == 100) { |
284 |
label = Messages.getText("Process_finished");
|
285 |
iTask.getProgressPanel().setLabel(label); |
286 |
// iTask.getProgressPanel().setPercent(100); // Forces setting the progress bar at 100 %
|
287 |
} |
288 |
|
289 |
// Ends this process
|
290 |
ended = true;
|
291 |
|
292 |
// Ends the progress panel
|
293 |
iTask.stop(); |
294 |
} |
295 |
} |
296 |
|
297 |
/*
|
298 |
* (non-Javadoc)
|
299 |
*
|
300 |
* @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionCanceled(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
301 |
*/
|
302 |
public void actionCanceled(IncrementableEvent e) { |
303 |
if (percentage < 100) { |
304 |
// If was cancelled
|
305 |
if (ended == true) |
306 |
processFinalize(); |
307 |
|
308 |
if (isCancellable)
|
309 |
cancelProcess.setCanceled(true);
|
310 |
else
|
311 |
JOptionPane.showMessageDialog(null, Messages.getText("The_process_cant_be_cancelled"), Messages.getText("Warning"), JOptionPane.WARNING_MESSAGE); |
312 |
} |
313 |
else {
|
314 |
LOG.warn(Messages.getText("Process_finished_wont_be_cancelled"));
|
315 |
|
316 |
processFinalize(); |
317 |
} |
318 |
} |
319 |
|
320 |
/*
|
321 |
* (non-Javadoc)
|
322 |
* @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionResumed(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
323 |
*/
|
324 |
public void actionResumed(IncrementableEvent e) { |
325 |
// if (isPausable)
|
326 |
// resume();
|
327 |
if ((isPausable) && (threadSuspended)) {
|
328 |
threadSuspended = false;
|
329 |
|
330 |
blinker.resume(); |
331 |
} |
332 |
} |
333 |
|
334 |
/*
|
335 |
* (non-Javadoc)
|
336 |
* @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionSuspended(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
337 |
*/
|
338 |
public void actionSuspended(IncrementableEvent e) { |
339 |
try {
|
340 |
if (isPausable) {
|
341 |
if ( ! threadSuspended) {
|
342 |
threadSuspended = true;
|
343 |
|
344 |
blinker.suspend(); |
345 |
} |
346 |
} |
347 |
|
348 |
|
349 |
// if (isPausable)
|
350 |
// suspend();
|
351 |
else
|
352 |
JOptionPane.showMessageDialog(null, Messages.getText("The_process_cant_be_paused"), Messages.getText("Warning"), JOptionPane.WARNING_MESSAGE); |
353 |
} |
354 |
catch (Exception iex) { |
355 |
LOG.error("", iex);
|
356 |
JOptionPane.showMessageDialog(null, Messages.getText("Failed_pausing_the_process"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
357 |
} |
358 |
} |
359 |
|
360 |
/*
|
361 |
* (non-Javadoc)
|
362 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#isCancelable()
|
363 |
*/
|
364 |
public boolean isCancelable() { |
365 |
return isCancellable;
|
366 |
} |
367 |
|
368 |
/**
|
369 |
* <p>Sets if this process can be canceled.</p>
|
370 |
*
|
371 |
* @param b <code>true</code> if this process can be canceled, otherwise <code>false</code>
|
372 |
*/
|
373 |
public void setCancelable(boolean b) { |
374 |
isCancellable = b; |
375 |
} |
376 |
|
377 |
/*
|
378 |
* (non-Javadoc)
|
379 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#isPausable()
|
380 |
*/
|
381 |
public boolean isPausable() { |
382 |
return isPausable;
|
383 |
} |
384 |
|
385 |
/**
|
386 |
* <p>Sets if this process can be paused.</p>
|
387 |
*
|
388 |
* @param b <code>true</code> if this process can be paused, otherwise <code>false</code>
|
389 |
*/
|
390 |
public void setPausable(boolean b) { |
391 |
isPausable = b; |
392 |
} |
393 |
/*
|
394 |
* (non-Javadoc)
|
395 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#process()
|
396 |
*/
|
397 |
public abstract void process() throws InterruptedException, Exception; |
398 |
} |