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

History | View | Annotate | Download (8.02 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.tools.task.impl;
23

    
24
import java.util.ArrayList;
25
import java.util.Date;
26
import java.util.List;
27

    
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.observer.ObservableHelper;
30
import org.gvsig.tools.observer.Observer;
31
import org.gvsig.tools.task.SimpleTaskStatus;
32
import org.gvsig.tools.task.TaskStatusManager;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36

    
37
/**
38
 * @author gvSIG Team
39
 * @version $Id$
40
 *
41
 */
42
public class BaseTaskStatus implements SimpleTaskStatus {
43

    
44
    private static final Logger LOG = LoggerFactory
45
        .getLogger(BaseTaskStatus.class);
46

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

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

    
77
        protected boolean isCancellable;
78

    
79
        protected boolean isCancellationRequested;
80

    
81
        protected ObservableHelper observers = null;
82

    
83
        protected boolean autoremove;
84

    
85
    private long startMillis;
86
    
87
    public BaseTaskStatus(String title) {
88
            this.manager =  ToolsLocator.getTaskStatusManager();
89
            this.autoremove = true;
90
        this.title = title;
91
        this.values = new SubtaskValues();
92
        this.isCancelled = false;
93
        this.isAbortedByError = false;
94
        this.isRunning = true;
95
        this.isCancellable = false;
96
        this.isCancellationRequested = false;
97
        this.code = this.manager.getNewCode();
98
        this.observers = new ObservableHelper();
99
        this.touch();
100
    }
101
    
102
    public BaseTaskStatus(String tittle, long minValue, long maxValue) {
103
            this(tittle);
104
        this.values.minValue = minValue;
105
        this.values.maxValue = maxValue;
106
    }
107
    
108
    public void setRangeOfValues(long min, long max) {
109
        this.values.minValue = min;
110
        this.values.maxValue = max;
111
    }
112
    
113
    protected void touch() {
114
            Date now = new Date(); 
115
            if( this.lastModification!=null && (now.getTime() - this.lastModification.getTime()) > 2000 ) {
116
                    this.values.message = null;
117
            }
118
        this.lastModification = now;
119
        this.manager.update(this);
120
        this.observers.notifyObservers(this, null);
121
    }
122
    
123
    public Date getLastModification() {
124
            return this.lastModification;
125
    }
126
    
127
    public void message(String message) {
128
            this.values.message = message;
129
            this.touch();
130
    }
131
    
132
    public String getTitle() {
133
        return this.title;
134
    }
135

    
136
    public String getCode() {
137
        return this.code;
138
    }
139

    
140
    public void setCurValue(long value) {
141
        this.values.curValue = value;
142
        this.touch();
143
    }
144
    
145
    public int getCompleted() {
146
        if( !this.isRunning ) {
147
            return 100;
148
        }
149
        if( this.values.maxValue == this.values.minValue ) {
150
                // No se puede calcular ya que no se ha indicado el numero de elementos
151
                // sobre los que se esta trabajando, asi que devoobemos el 100%.
152
                return 100;
153
        }
154
        try {
155
            return (int) (((this.values.curValue-this.values.minValue)*100)/(this.values.maxValue-this.values.minValue));
156
        } catch( Exception e) {
157
            return 100;
158
        }
159
    }
160

    
161
    public String getLabel() {
162
            String progress;
163
            
164
        if( this.values.maxValue == this.values.minValue ) {
165
                // No se puede calcular ya que no se ha indicado el numero de elementos
166
                // sobre los que se esta trabajando.
167
                if( this.values.curValue > 0 ) {
168
                        progress = " (" + this.values.curValue + ")";
169
                } else {
170
                        progress = "";
171
                }
172
        } else {
173
            progress = " (" + (this.values.curValue-this.values.minValue) + " / " + (this.values.maxValue-this.values.minValue) + ")" ;
174
        }
175
            if( this.values.message == null ) {
176
            return progress;
177
            } else {
178
            return this.values.message + progress;
179
            }
180
    }
181

    
182
    public void terminate() {
183
            if( !isRunning ) {
184
                    return;
185
            }
186
        this.isRunning = false;
187
        if (LOG.isDebugEnabled()) {
188
            long endMillis = System.currentTimeMillis();
189
            LOG.debug("Terminated status {} execution in {} ms", getTitle(),
190
                new Long(endMillis - startMillis));
191
        }
192
        this.touch();
193
    }
194
    
195
    public void cancel() {
196
            if( !isRunning ) {
197
                    return;
198
            }
199
        this.isCancelled = true;
200
        this.isRunning = false;
201
        this.touch();
202
    }
203

    
204
    public void abort() {
205
            if( !isRunning ) {
206
                    return;
207
            }
208
        this.isAbortedByError = true;
209
        this.isRunning = false;
210
        this.touch();
211
    }
212
    
213
    public boolean isCancelled() {
214
        return this.isCancelled;
215
    }
216

    
217
    public boolean isAborted() {
218
        return this.isAbortedByError;
219
    }
220

    
221
    public boolean isRunning() {
222
        return this.isRunning;
223
    }
224

    
225
        public TaskStatusManager getManager() {
226
                return this.manager;
227
        }
228

    
229
        public boolean isIndeterminate() {
230
                return this.values.minValue == this.values.maxValue;
231
        }
232

    
233
        public void add() {
234
                this.manager.add(this);
235
        if (LOG.isDebugEnabled()) {
236
            startMillis = System.currentTimeMillis();
237
        }
238
        }
239

    
240
        public void remove() {
241
                this.manager.remove(this);
242
        }
243

    
244
        public boolean isCancellable() {
245
                return this.isCancellable;
246
        }
247

    
248
        public void setCancellable(boolean cancellable) {
249
                this.isCancellable = cancellable;
250
        }
251

    
252
        synchronized public boolean isCancellationRequested() {
253
                return this.isCancellationRequested;
254
        }
255

    
256
        synchronized public void cancelRequest() {
257
                this.isCancellationRequested = true;
258
        }
259

    
260
        synchronized public void addObserver(Observer o) {
261
                this.observers.addObserver(o);
262
        }
263

    
264
        synchronized public void deleteObserver(Observer o) {
265
                this.observers.deleteObserver(o);
266
        }
267

    
268
        synchronized public void deleteObservers() {
269
                this.observers.deleteObservers();
270
        }
271

    
272
        public void setTittle(String tittle) {
273
        setTitle(tittle);
274
    }
275

    
276
    public void setTitle(String title) {
277
        if (this.title != null) {
278
            return;
279
        }
280
        this.title = title;
281
    }
282

    
283
        public void setAutoremove(boolean autoremove) {
284
                this.autoremove = autoremove;
285
        }
286

    
287
        public boolean getAutoRemove() {
288
                return this.autoremove;
289
        }
290

    
291
        public void push() {
292
                if( this.subtaskStack == null ) {
293
                        this.subtaskStack = new ArrayList();
294
                }
295
                this.subtaskStack.add(0, this.values);
296
                this.values = (SubtaskValues) this.values.clone();
297
        }
298

    
299
        public void pop() {
300
                if( this.subtaskStack == null && this.subtaskStack.isEmpty() ) {
301
                        return;
302
                }
303
                this.values = (SubtaskValues) this.subtaskStack.get(0);
304
                this.subtaskStack.remove(0);
305
                touch();
306
        }
307

    
308
        public void setIndeterminate() {
309
                this.values.maxValue = 0;
310
                this.values.minValue = 0;
311
                this.values.curValue = 0;
312
        }
313

    
314
}