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