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 @ 1666

History | View | Annotate | Download (8.67 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
/**
40
 * @author gvSIG Team
41
 * @version $Id$
42
 *
43
 */
44
public class BaseTaskStatus implements SimpleTaskStatus {
45

    
46
    private static final Logger LOG = LoggerFactory
47
        .getLogger(BaseTaskStatus.class);
48

    
49
    protected class SubtaskValues {
50
                long minValue = 0;
51
        long maxValue = 0;
52
        long curValue = 0;
53
        String message = null;
54
        
55
        public Object clone() {
56
                SubtaskValues other = new SubtaskValues();
57
                other.curValue = this.curValue;
58
                other.maxValue = this.maxValue;
59
                other.minValue = this.minValue;
60
                other.message = this.message;
61
                return other;
62
        }
63
    }
64
    
65
    protected Date lastModification = null;
66

    
67
    protected SubtaskValues values;
68
    protected List subtaskStack = null ;
69
    
70
    protected String title = null;
71
    protected String code = null;
72
    
73
    protected boolean isCancelled = false;
74
    protected boolean isAbortedByError = false;
75
    protected boolean isRunning = true;
76
    
77
    protected TaskStatusManager manager = null;
78

    
79
        protected boolean isCancellable;
80

    
81
        protected boolean isCancellationRequested;
82

    
83
        protected ObservableHelper observers = null;
84

    
85
        protected boolean autoremove;
86

    
87
    private long startMillis;
88
    
89
    public BaseTaskStatus(String title) {
90
            this.manager =  ToolsLocator.getTaskStatusManager();
91
            this.autoremove = true;
92
        this.title = title;
93
        this.values = new SubtaskValues();
94
        this.isCancelled = false;
95
        this.isAbortedByError = false;
96
        this.isRunning = true;
97
        this.isCancellable = false;
98
        this.isCancellationRequested = false;
99
        this.code = this.manager.getNewCode();
100
        this.observers = new ObservableHelper();
101
        this.touch();
102
    }
103
    
104
    public BaseTaskStatus(String tittle, long minValue, long maxValue) {
105
            this(tittle);
106
        this.values.minValue = minValue;
107
        this.values.maxValue = maxValue;
108
    }
109

    
110
    @Override
111
    public void restart() {
112
            this.autoremove = true;
113
        this.values = new SubtaskValues();
114
        this.isCancelled = false;
115
        this.isAbortedByError = false;
116
        this.isRunning = true;
117
        this.isCancellable = false;
118
        this.isCancellationRequested = false;
119
        this.touch();
120
    }
121
    
122
    @Override
123
    public void setRangeOfValues(long min, long max) {
124
        this.values.minValue = min;
125
        this.values.maxValue = max;
126
    }
127
    
128
    protected void touch() {
129
        touch(true);
130
    }
131

    
132
    protected void touch(boolean clearmsg) {
133
            Date now = new Date();
134
        if( clearmsg ) {
135
            if( this.lastModification!=null && (now.getTime() - this.lastModification.getTime()) > 4000 ) {
136
                    this.values.message = null;
137
            }
138
        }
139
        this.lastModification = now;
140
        this.notifyObservers();
141
    }
142
    
143
    protected void notifyObservers() {
144
        this.manager.update(this);
145
        this.observers.notifyObservers(this, null);
146
    }
147
    
148
    public Date getLastModification() {
149
            return this.lastModification;
150
    }
151
    
152
    public void message(String message) {
153
            this.values.message = message;
154
            this.touch(false);
155
    }
156
    
157
    public String getTitle() {
158
        return this.title;
159
    }
160

    
161
    public String getCode() {
162
        return this.code;
163
    }
164

    
165
    public void setCurValue(long value) {
166
        //LOG.debug("setCurValue: '"+value+"'.");
167
        this.values.curValue = value;
168
        this.touch();
169
    }
170
    
171
    public int getCompleted() {
172
        if( !this.isRunning ) {
173
            return 100;
174
        }
175
        if( this.values.maxValue == this.values.minValue ) {
176
                // No se puede calcular ya que no se ha indicado el numero de elementos
177
                // sobre los que se esta trabajando, asi que devoobemos el 100%.
178
                return 100;
179
        }
180
        try {
181
            return (int) (((this.values.curValue-this.values.minValue)*100)/(this.values.maxValue-this.values.minValue));
182
        } catch( Exception e) {
183
            return 100;
184
        }
185
    }
186

    
187
    public String getLabel() {
188
            String progress;
189
            
190
        if( this.values.maxValue == this.values.minValue ) {
191
                // No se puede calcular ya que no se ha indicado el numero de elementos
192
                // sobre los que se esta trabajando.
193
                if( this.values.curValue > 0 ) {
194
                        progress = " (" + this.values.curValue + ")";
195
                } else {
196
                        progress = "";
197
                }
198
        } else {
199
            progress = " (" + (this.values.curValue-this.values.minValue) + " / " + (this.values.maxValue-this.values.minValue) + ")" ;
200
        }
201
            if( this.values.message == null ) {
202
            return progress;
203
            } else {
204
            return this.values.message + progress;
205
            }
206
    }
207

    
208
    public void terminate() {
209
            if( !isRunning ) {
210
                    return;
211
            }
212
        this.isRunning = false;
213
        if (LOG.isDebugEnabled()) {
214
            long endMillis = System.currentTimeMillis();
215
            LOG.debug("Terminated status {} execution in {} ms", getTitle(),
216
                new Long(endMillis - startMillis));
217
        }
218
        this.touch();
219
    }
220
    
221
    public void cancel() {
222
            if( !isRunning ) {
223
                    return;
224
            }
225
        this.isCancelled = true;
226
        this.isRunning = false;
227
        this.touch();
228
    }
229

    
230
    public void abort() {
231
            if( !isRunning ) {
232
                    return;
233
            }
234
        this.isAbortedByError = true;
235
        this.isRunning = false;
236
        this.touch();
237
    }
238
    
239
    public boolean isCancelled() {
240
        return this.isCancelled;
241
    }
242

    
243
    public boolean isAborted() {
244
        return this.isAbortedByError;
245
    }
246

    
247
    public boolean isRunning() {
248
        return this.isRunning;
249
    }
250

    
251
        public TaskStatusManager getManager() {
252
                return this.manager;
253
        }
254

    
255
        public boolean isIndeterminate() {
256
                return this.values.minValue == this.values.maxValue;
257
        }
258

    
259
        public void add() {
260
                this.manager.add(this);
261
        if (LOG.isDebugEnabled()) {
262
            startMillis = System.currentTimeMillis();
263
        }
264
        }
265

    
266
        public void remove() {
267
                this.manager.remove(this);
268
        }
269

    
270
        public boolean isCancellable() {
271
                return this.isCancellable;
272
        }
273

    
274
        public void setCancellable(boolean cancellable) {
275
                this.isCancellable = cancellable;
276
        }
277

    
278
        synchronized public boolean isCancellationRequested() {
279
                return this.isCancellationRequested;
280
        }
281

    
282
        synchronized public void cancelRequest() {
283
                this.isCancellationRequested = true;
284
        }
285

    
286
        synchronized public void addObserver(Observer o) {
287
                this.observers.addObserver(o);
288
        }
289

    
290
        synchronized public void deleteObserver(Observer o) {
291
                this.observers.deleteObserver(o);
292
        }
293

    
294
        synchronized public void deleteObservers() {
295
                this.observers.deleteObservers();
296
        }
297

    
298
        public void setTittle(String tittle) {
299
        setTitle(tittle);
300
    }
301

    
302
    public void setTitle(String title) {
303
        if (this.title != null) {
304
            return;
305
        }
306
        this.title = title;
307
    }
308

    
309
        public void setAutoremove(boolean autoremove) {
310
                this.autoremove = autoremove;
311
        }
312

    
313
        public boolean getAutoRemove() {
314
                return this.autoremove;
315
        }
316

    
317
        public void push() {
318
                if( this.subtaskStack == null ) {
319
                        this.subtaskStack = new ArrayList();
320
                }
321
                this.subtaskStack.add(0, this.values);
322
                this.values = (SubtaskValues) this.values.clone();
323
        }
324

    
325
        public void pop() {
326
                if( this.subtaskStack == null && this.subtaskStack.isEmpty() ) {
327
                        return;
328
                }
329
                this.values = (SubtaskValues) this.subtaskStack.get(0);
330
                this.subtaskStack.remove(0);
331
                touch();
332
        }
333

    
334
        public void setIndeterminate() {
335
                this.values.maxValue = 0;
336
                this.values.minValue = 0;
337
                this.values.curValue = 0;
338
        }
339

    
340
}