Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / task / impl / BaseTaskStatus.java @ 1719

History | View | Annotate | Download (9.79 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 int getCompleted() {
180
        if (!this.isRunning) {
181
            return 100;
182
        }
183
        if (this.values.maxValue == this.values.minValue) {
184
            // No se puede calcular ya que no se ha indicado el numero de elementos
185
            // sobre los que se esta trabajando, asi que devoobemos el 100%.
186
            return 100;
187
        }
188
        try {
189
            return (int) (((this.values.curValue - this.values.minValue) * 100) / (this.values.maxValue - this.values.minValue));
190
        } catch (Exception e) {
191
            return 100;
192
        }
193
    }
194

    
195
    @Override
196
    public String getLabel() {
197
        String progress;
198

    
199
        if (this.values.maxValue == this.values.minValue) {
200
            // No se puede calcular ya que no se ha indicado el numero de elementos
201
            // sobre los que se esta trabajando.
202
            if (this.values.curValue > 0) {
203
                progress = " (" + this.values.curValue + ")";
204
            } else {
205
                progress = "";
206
            }
207
        } else {
208
            progress = " (" + (this.values.curValue - this.values.minValue) + " / " + (this.values.maxValue - this.values.minValue) + ")";
209
        }
210
        if (this.values.message == null) {
211
            return progress;
212
        } else {
213
            return this.values.message + progress;
214
        }
215
    }
216

    
217
    @Override
218
    public void terminate() {
219
        if (!isRunning) {
220
            return;
221
        }
222
        this.isRunning = false;
223
        if (LOG.isDebugEnabled()) {
224
            long endMillis = System.currentTimeMillis();
225
            LOG.debug("Terminated status {} execution in {} ms", getTitle(), endMillis - startMillis);
226
        }
227
        this.touch();
228
        if (this.autoremove) {
229
            this.remove();
230
        }
231
    }
232

    
233
    @Override
234
    public void cancel() {
235
        if (!isRunning) {
236
            return;
237
        }
238
        this.isCancelled = true;
239
        this.isRunning = false;
240
        this.touch();
241
        if (this.autoremove) {
242
            this.remove();
243
        }
244
    }
245

    
246
    @Override
247
    public void abort() {
248
        if (!isRunning) {
249
            return;
250
        }
251
        this.isAbortedByError = true;
252
        this.isRunning = false;
253
        this.touch();
254
        if (this.autoremove) {
255
            this.remove();
256
        }
257
    }
258

    
259
    @Override
260
    public boolean isCancelled() {
261
        return this.isCancelled;
262
    }
263

    
264
    @Override
265
    public boolean isAborted() {
266
        return this.isAbortedByError;
267
    }
268

    
269
    @Override
270
    public boolean isRunning() {
271
        return this.isRunning;
272
    }
273

    
274
    @Override
275
    public TaskStatusManager getManager() {
276
        return this.manager;
277
    }
278

    
279
    @Override
280
    public boolean isIndeterminate() {
281
        return this.values.minValue == this.values.maxValue;
282
    }
283

    
284
    @Override
285
    public void add() {
286
        this.manager.add(this);
287
        if (LOG.isDebugEnabled()) {
288
            startMillis = System.currentTimeMillis();
289
        }
290
    }
291

    
292
    @Override
293
    public void remove() {
294
        this.manager.remove(this);
295
    }
296

    
297
    @Override
298
    public boolean isCancellable() {
299
        return this.isCancellable;
300
    }
301

    
302
    @Override
303
    public void setCancellable(boolean cancellable) {
304
        this.isCancellable = cancellable;
305
    }
306

    
307
    @Override
308
    synchronized public boolean isCancellationRequested() {
309
        return this.isCancellationRequested;
310
    }
311

    
312
    @Override
313
    synchronized public void cancelRequest() {
314
        this.isCancellationRequested = true;
315
    }
316

    
317
    @Override
318
    synchronized public void addObserver(Observer o) {
319
        this.observers.addObserver(o);
320
    }
321

    
322
    @Override
323
    synchronized public void deleteObserver(Observer o) {
324
        this.observers.deleteObserver(o);
325
    }
326

    
327
    @Override
328
    synchronized public void deleteObservers() {
329
        this.observers.deleteObservers();
330
    }
331

    
332
    @Override
333
    public void setTittle(String tittle) {
334
        setTitle(tittle);
335
    }
336

    
337
    @Override
338
    public void setTitle(String title) {
339
        if (this.title != null) {
340
            return;
341
        }
342
        this.title = title;
343
    }
344

    
345
    @Override
346
    public void setAutoremove(boolean autoremove) {
347
        this.autoremove = autoremove;
348
    }
349

    
350
    @Override
351
    public boolean getAutoRemove() {
352
        return this.autoremove;
353
    }
354

    
355
    @Override
356
    public void push() {
357
        if (this.subtaskStack == null) {
358
            this.subtaskStack = new ArrayList();
359
        }
360
        this.subtaskStack.add(0, this.values);
361
        try {
362
            this.values = (SubtaskValues) this.values.clone();
363
        } catch (CloneNotSupportedException ex) {
364
        }
365
    }
366

    
367
    @Override
368
    public void pop() {
369
        if (this.subtaskStack == null && this.subtaskStack.isEmpty()) {
370
            return;
371
        }
372
        this.values = (SubtaskValues) this.subtaskStack.get(0);
373
        this.subtaskStack.remove(0);
374
        touch();
375
    }
376

    
377
    @Override
378
    public void setIndeterminate() {
379
        this.values.maxValue = 0;
380
        this.values.minValue = 0;
381
        this.values.curValue = 0;
382
    }
383

    
384
}