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

History | View | Annotate | Download (6.51 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.Date;
25

    
26
import org.gvsig.tools.ToolsLocator;
27
import org.gvsig.tools.observer.ObservableHelper;
28
import org.gvsig.tools.observer.Observer;
29
import org.gvsig.tools.task.SimpleTaskStatus;
30
import org.gvsig.tools.task.TaskStatusManager;
31

    
32

    
33
/**
34
 * @author gvSIG Team
35
 * @version $Id$
36
 *
37
 */
38
public class BaseTaskStatus implements SimpleTaskStatus {
39
    protected Date lastModification = null;
40

    
41
    protected long minValue = 0;
42
    protected long maxValue = 0;
43
    protected long curValue = 0;
44
    
45
    protected String title = null;
46
    protected String code = null;
47
    
48
    protected String message = null;
49

    
50
    protected boolean isCancelled = false;
51
    protected boolean isAbortedByError = false;
52
    protected boolean isRunning = true;
53
    
54
    protected TaskStatusManager manager = null;
55

    
56
        protected boolean isCancellable;
57

    
58
        protected boolean isCancellationRequested;
59

    
60
        protected ObservableHelper observers = null;
61

    
62
        protected boolean autoremove;
63
    
64
    public BaseTaskStatus(String tittle) {
65
            this.manager =  ToolsLocator.getTaskStatusManager();
66
            this.autoremove = true;
67
        this.title = tittle;
68
        this.minValue = 0;
69
        this.maxValue = 0;
70
        this.isCancelled = false;
71
        this.isAbortedByError = false;
72
        this.isRunning = true;
73
        this.message = null;
74
        this.isCancellable = false;
75
        this.isCancellationRequested = false;
76
        this.code = this.manager.getNewCode();
77
        this.observers = new ObservableHelper();
78
        this.touch();
79
    }
80
    
81
    public BaseTaskStatus(String tittle, long minValue, long maxValue) {
82
            this(tittle);
83
        this.minValue = minValue;
84
        this.maxValue = maxValue;
85
    }
86
    
87
    public void setRangeOfValues(long min, long max) {
88
        this.minValue = min;
89
        this.maxValue = max;
90
    }
91
    
92
    protected void touch() {
93
            Date now = new Date(); 
94
            if( this.lastModification!=null && (now.getTime() - this.lastModification.getTime()) > 2000 ) {
95
                    this.message = null;
96
            }
97
        this.lastModification = now;
98
        this.manager.update(this);
99
        this.observers.notifyObservers(this, null);
100
    }
101
    
102
    public Date getLastModification() {
103
            return this.lastModification;
104
    }
105
    
106
    public void message(String message) {
107
            this.touch();
108
            this.message = message;
109
    }
110
    
111
    public String getTitle() {
112
        return this.title;
113
    }
114

    
115
    public String getCode() {
116
        return this.code;
117
    }
118

    
119
    public void setCurValue(long value) {
120
        this.curValue = value;
121
        this.touch();
122
    }
123
    
124
    public int getCompleted() {
125
        if( !this.isRunning ) {
126
            return 100;
127
        }
128
        if( this.maxValue == this.minValue ) {
129
                // No se puede calcular ya que no se ha indicado el numero de elementos
130
                // sobre los que se esta trabajando, asi que devoobemos el 100%.
131
                return 100;
132
        }
133
        try {
134
            return (int) (((this.curValue-this.minValue)*100)/(this.maxValue-this.minValue));
135
        } catch( Exception e) {
136
            return 100;
137
        }
138
    }
139

    
140
    public String getLabel() {
141
            String progress;
142
            
143
        if( !this.isRunning ) {
144
            return "" ;
145
        }
146
        if( this.maxValue == this.minValue ) {
147
                // No se puede calcular ya que no se ha indicado el numero de elementos
148
                // sobre los que se esta trabajando.
149
                if( this.curValue > 0 ) {
150
                        progress = " (" + this.curValue + ")";
151
                } else {
152
                        progress = "";
153
                }
154
        } else {
155
            progress = " (" + (this.curValue-this.minValue) + " / " + (this.maxValue-this.minValue) + ")" ;
156
        }
157
            if( this.message == null ) {
158
            return progress;
159
            } else {
160
            return this.message + progress;
161
            }
162
    }
163

    
164
    public void terminate() {
165
            if( !isRunning ) {
166
                    return;
167
            }
168
        this.isRunning = false;
169
        this.touch();
170
    }
171
    
172
    public void cancel() {
173
            if( !isRunning ) {
174
                    return;
175
            }
176
        this.isCancelled = true;
177
        this.isRunning = false;
178
        this.touch();
179
    }
180

    
181
    public void abort() {
182
            if( !isRunning ) {
183
                    return;
184
            }
185
        this.isAbortedByError = true;
186
        this.isRunning = false;
187
        this.touch();
188
    }
189
    
190
    public boolean isCancelled() {
191
        return this.isCancelled;
192
    }
193

    
194
    public boolean isAborted() {
195
        return this.isAbortedByError;
196
    }
197

    
198
    public boolean isRunning() {
199
        return this.isRunning;
200
    }
201

    
202
        public TaskStatusManager getManager() {
203
                return this.manager;
204
        }
205

    
206
        public boolean isIndeterminate() {
207
                return this.minValue == this.maxValue;
208
        }
209

    
210
        public void add() {
211
                this.manager.add(this);
212
        }
213

    
214
        public void remove() {
215
                this.manager.remove(this);
216
        }
217

    
218
        public boolean isCancellable() {
219
                return this.isCancellable;
220
        }
221

    
222
        public void setCancellable(boolean cancellable) {
223
                this.isCancellable = cancellable;
224
        }
225

    
226
        synchronized public boolean isCancellationRequested() {
227
                return this.isCancellationRequested;
228
        }
229

    
230
        synchronized public void cancelRequest() {
231
                this.isCancellationRequested = true;
232
        }
233

    
234
        synchronized public void addObserver(Observer o) {
235
                this.observers.addObserver(o);
236
        }
237

    
238
        synchronized public void deleteObserver(Observer o) {
239
                this.observers.deleteObserver(o);
240
        }
241

    
242
        synchronized public void deleteObservers() {
243
                this.observers.deleteObservers();
244
        }
245

    
246
        public void setTittle(String tittle) {
247
        setTitle(tittle);
248
    }
249

    
250
    public void setTitle(String title) {
251
        if (this.title != null) {
252
            return;
253
        }
254
        this.title = title;
255
    }
256

    
257
        public void setAutoremove(boolean autoremove) {
258
                this.autoremove = autoremove;
259
        }
260

    
261
        public boolean getAutoRemove() {
262
                return this.autoremove;
263
        }
264

    
265

    
266
}