gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / task / impl / BaseTaskStatus.java @ 2505
History | View | Annotate | Download (10.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 2
|
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.tools.task.impl; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Date; |
28 |
import java.util.List; |
29 |
|
30 |
import org.gvsig.tools.ToolsLocator; |
31 |
import org.gvsig.tools.observer.ObservableHelper; |
32 |
import org.gvsig.tools.observer.Observer; |
33 |
import org.gvsig.tools.task.SimpleTaskStatus; |
34 |
import org.gvsig.tools.task.TaskStatusManager; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
/**
|
39 |
* @author gvSIG Team
|
40 |
* @version $Id$
|
41 |
*
|
42 |
*/
|
43 |
public class BaseTaskStatus implements SimpleTaskStatus { |
44 |
|
45 |
private static final Logger LOG = LoggerFactory |
46 |
.getLogger(BaseTaskStatus.class); |
47 |
|
48 |
protected class SubtaskValues { |
49 |
|
50 |
long minValue = 0; |
51 |
long maxValue = 0; |
52 |
long curValue = 0; |
53 |
String message = null; |
54 |
|
55 |
@Override
|
56 |
public Object clone() throws CloneNotSupportedException { |
57 |
SubtaskValues other = new SubtaskValues();
|
58 |
other.curValue = this.curValue;
|
59 |
other.maxValue = this.maxValue;
|
60 |
other.minValue = this.minValue;
|
61 |
other.message = this.message;
|
62 |
return other;
|
63 |
} |
64 |
} |
65 |
|
66 |
protected Date lastModification = null; |
67 |
|
68 |
protected SubtaskValues values;
|
69 |
protected List subtaskStack = null; |
70 |
|
71 |
protected String title = null; |
72 |
protected String code = null; |
73 |
|
74 |
protected boolean isCancelled = false; |
75 |
protected boolean isAbortedByError = false; |
76 |
protected boolean isRunning = true; |
77 |
|
78 |
protected TaskStatusManager manager = null; |
79 |
|
80 |
protected boolean isCancellable; |
81 |
|
82 |
protected boolean isCancellationRequested; |
83 |
|
84 |
protected ObservableHelper observers = null; |
85 |
|
86 |
protected boolean autoremove; |
87 |
|
88 |
private long startMillis; |
89 |
|
90 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
91 |
public BaseTaskStatus(String title) { |
92 |
this.manager = ToolsLocator.getTaskStatusManager();
|
93 |
this.autoremove = true; |
94 |
this.title = title;
|
95 |
this.values = new SubtaskValues(); |
96 |
this.isCancelled = false; |
97 |
this.isAbortedByError = false; |
98 |
this.isRunning = true; |
99 |
this.isCancellable = false; |
100 |
this.isCancellationRequested = false; |
101 |
this.code = this.manager.getNewCode(); |
102 |
this.observers = new ObservableHelper(); |
103 |
this.touch();
|
104 |
} |
105 |
|
106 |
public BaseTaskStatus(String tittle, long minValue, long maxValue) { |
107 |
this(tittle);
|
108 |
this.values.minValue = minValue;
|
109 |
this.values.maxValue = maxValue;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public void restart() { |
114 |
this.autoremove = true; |
115 |
this.values = new SubtaskValues(); |
116 |
this.isCancelled = false; |
117 |
this.isAbortedByError = false; |
118 |
this.isRunning = true; |
119 |
this.isCancellable = false; |
120 |
this.isCancellationRequested = false; |
121 |
this.touch();
|
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void setRangeOfValues(long min, long max) { |
126 |
this.values.minValue = min;
|
127 |
this.values.maxValue = max;
|
128 |
} |
129 |
|
130 |
protected void touch() { |
131 |
touch(true);
|
132 |
} |
133 |
|
134 |
protected void touch(boolean clearmsg) { |
135 |
Date now = new Date(); |
136 |
if (clearmsg) {
|
137 |
if (this.lastModification != null && (now.getTime() - this.lastModification.getTime()) > 4000) { |
138 |
this.values.message = null; |
139 |
} |
140 |
} |
141 |
this.lastModification = now;
|
142 |
this.notifyObservers();
|
143 |
} |
144 |
|
145 |
protected void notifyObservers() { |
146 |
this.manager.update(this); |
147 |
this.observers.notifyObservers(this, null); |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public Date getLastModification() { |
152 |
return this.lastModification; |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void message(String message) { |
157 |
this.values.message = message;
|
158 |
this.touch(false); |
159 |
} |
160 |
|
161 |
@Override
|
162 |
public String getTitle() { |
163 |
return this.title; |
164 |
} |
165 |
|
166 |
@Override
|
167 |
public String getCode() { |
168 |
return this.code; |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public void setCurValue(long value) { |
173 |
//LOG.debug("setCurValue: '"+value+"'.");
|
174 |
this.values.curValue = value;
|
175 |
this.touch(false); |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public void incrementCurrentValue() { |
180 |
this.values.curValue++;
|
181 |
this.touch(false); |
182 |
} |
183 |
|
184 |
@Override
|
185 |
public int getCompleted() { |
186 |
if (!this.isRunning) { |
187 |
return 100; |
188 |
} |
189 |
if (this.values.maxValue == this.values.minValue) { |
190 |
// No se puede calcular ya que no se ha indicado el numero de elementos
|
191 |
// sobre los que se esta trabajando, asi que devoobemos el 100%.
|
192 |
return 100; |
193 |
} |
194 |
try {
|
195 |
return (int) (((this.values.curValue - this.values.minValue) * 100) / (this.values.maxValue - this.values.minValue)); |
196 |
} catch (Exception e) { |
197 |
return 100; |
198 |
} |
199 |
} |
200 |
|
201 |
@Override
|
202 |
public String getLabel() { |
203 |
String progress;
|
204 |
|
205 |
if (this.values.maxValue == this.values.minValue) { |
206 |
// No se puede calcular ya que no se ha indicado el numero de elementos
|
207 |
// sobre los que se esta trabajando.
|
208 |
if (this.values.curValue > 0) { |
209 |
progress = " (" + this.values.curValue + ")"; |
210 |
} else {
|
211 |
progress = "";
|
212 |
} |
213 |
} else {
|
214 |
progress = " (" + (this.values.curValue - this.values.minValue) + " / " + (this.values.maxValue - this.values.minValue) + ")"; |
215 |
} |
216 |
if (this.values.message == null) { |
217 |
return progress;
|
218 |
} else {
|
219 |
return this.values.message + progress; |
220 |
} |
221 |
} |
222 |
|
223 |
public String getMessage() { |
224 |
return this.values.message; |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void terminate() { |
229 |
if (!isRunning) {
|
230 |
return;
|
231 |
} |
232 |
this.isRunning = false; |
233 |
if (LOG.isDebugEnabled()) {
|
234 |
long endMillis = System.currentTimeMillis(); |
235 |
LOG.debug("Terminated status {} execution in {} ms", getTitle(), endMillis - startMillis);
|
236 |
} |
237 |
this.touch();
|
238 |
if (this.autoremove) { |
239 |
this.remove();
|
240 |
} |
241 |
} |
242 |
|
243 |
@Override
|
244 |
public void cancel() { |
245 |
if (!isRunning) {
|
246 |
return;
|
247 |
} |
248 |
this.isCancelled = true; |
249 |
this.isRunning = false; |
250 |
this.touch();
|
251 |
if (this.autoremove) { |
252 |
this.remove();
|
253 |
} |
254 |
} |
255 |
|
256 |
@Override
|
257 |
public void abort() { |
258 |
if (!isRunning) {
|
259 |
return;
|
260 |
} |
261 |
this.isAbortedByError = true; |
262 |
this.isRunning = false; |
263 |
this.touch(false); |
264 |
if (this.autoremove) { |
265 |
this.remove();
|
266 |
} |
267 |
} |
268 |
|
269 |
@Override
|
270 |
public boolean isCancelled() { |
271 |
return this.isCancelled; |
272 |
} |
273 |
|
274 |
@Override
|
275 |
public boolean isAborted() { |
276 |
return this.isAbortedByError; |
277 |
} |
278 |
|
279 |
@Override
|
280 |
public boolean isRunning() { |
281 |
return this.isRunning; |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public TaskStatusManager getManager() {
|
286 |
return this.manager; |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public boolean isIndeterminate() { |
291 |
return this.values.maxValue <= this.values.minValue; |
292 |
} |
293 |
|
294 |
@Override
|
295 |
public void add() { |
296 |
this.manager.add(this); |
297 |
if (LOG.isDebugEnabled()) {
|
298 |
startMillis = System.currentTimeMillis();
|
299 |
} |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void remove() { |
304 |
if(!this.isRunning() && !this.isAborted() && !this.isCancelled()){ |
305 |
this.values.message = ""; |
306 |
} |
307 |
this.manager.remove(this); |
308 |
} |
309 |
|
310 |
@Override
|
311 |
public boolean isCancellable() { |
312 |
return this.isCancellable; |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public void setCancellable(boolean cancellable) { |
317 |
this.isCancellable = cancellable;
|
318 |
} |
319 |
|
320 |
@Override
|
321 |
synchronized public boolean isCancellationRequested() { |
322 |
return this.isCancellationRequested; |
323 |
} |
324 |
|
325 |
@Override
|
326 |
synchronized public void cancelRequest() { |
327 |
this.isCancellationRequested = true; |
328 |
} |
329 |
|
330 |
@Override
|
331 |
synchronized public void addObserver(Observer o) { |
332 |
this.observers.addObserver(o);
|
333 |
} |
334 |
|
335 |
@Override
|
336 |
synchronized public void deleteObserver(Observer o) { |
337 |
this.observers.deleteObserver(o);
|
338 |
} |
339 |
|
340 |
@Override
|
341 |
synchronized public void deleteObservers() { |
342 |
this.observers.deleteObservers();
|
343 |
} |
344 |
|
345 |
@Override
|
346 |
public void setTittle(String tittle) { |
347 |
setTitle(tittle); |
348 |
} |
349 |
|
350 |
@Override
|
351 |
public void setTitle(String title) { |
352 |
if (this.title != null) { |
353 |
return;
|
354 |
} |
355 |
this.title = title;
|
356 |
} |
357 |
|
358 |
@Override
|
359 |
public void setAutoremove(boolean autoremove) { |
360 |
this.autoremove = autoremove;
|
361 |
} |
362 |
|
363 |
@Override
|
364 |
public boolean getAutoRemove() { |
365 |
return this.autoremove; |
366 |
} |
367 |
|
368 |
@Override
|
369 |
public void push() { |
370 |
if (this.subtaskStack == null) { |
371 |
this.subtaskStack = new ArrayList(); |
372 |
} |
373 |
this.subtaskStack.add(0, this.values); |
374 |
try {
|
375 |
this.values = (SubtaskValues) this.values.clone(); |
376 |
} catch (CloneNotSupportedException ex) { |
377 |
} |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public void pop() { |
382 |
if (this.subtaskStack == null || this.subtaskStack.isEmpty()) { |
383 |
return;
|
384 |
} |
385 |
this.values = (SubtaskValues) this.subtaskStack.get(0); |
386 |
this.subtaskStack.remove(0); |
387 |
touch(); |
388 |
} |
389 |
|
390 |
@Override
|
391 |
public void setIndeterminate() { |
392 |
this.values.maxValue = 0; |
393 |
this.values.minValue = 0; |
394 |
this.values.curValue = 0; |
395 |
} |
396 |
|
397 |
} |