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

History | View | Annotate | Download (5.76 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
    private Date lastModification = null;
40

    
41
    protected long minValue = 0;
42
    protected long maxValue = 0;
43
    protected long curValue = 0;
44
    
45
    protected String tittle = 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
        private boolean isCancellable;
57

    
58
        private boolean isCancellationRequested;
59

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

    
112
    public String getCode() {
113
        return this.code;
114
    }
115

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

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

    
157
    public void teminate() {
158
        this.isRunning = false;
159
        this.touch();
160
    }
161
    
162
    public void cancel() {
163
        this.isCancelled = true;
164
        this.isRunning = false;
165
        this.touch();
166
    }
167

    
168
    public void abort() {
169
        this.isAbortedByError = true;
170
        this.isRunning = false;
171
        this.touch();
172
    }
173
    
174
    public boolean isCancelled() {
175
        return this.isCancelled;
176
    }
177

    
178
    public boolean isAborted() {
179
        return this.isAbortedByError;
180
    }
181

    
182
    public boolean isRunning() {
183
        return this.isRunning;
184
    }
185

    
186
        public TaskStatusManager getManager() {
187
                return this.manager;
188
        }
189

    
190
        public boolean isIndeterminate() {
191
                return this.minValue == this.maxValue;
192
        }
193

    
194
        public void remove() {
195
                this.manager.remove(this);
196
        }
197

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

    
202
        public void setCancellable(boolean cancellable) {
203
                this.isCancellable = cancellable;
204
        }
205

    
206
        public boolean isCancellationRequested() {
207
                return this.isCancellationRequested;
208
        }
209

    
210
        public void cancelRequest() {
211
                this.isCancellationRequested = true;
212
        }
213

    
214
        public void addObserver(Observer o) {
215
                this.observers.addObserver(o);
216
        }
217

    
218
        public void deleteObserver(Observer o) {
219
                this.observers.deleteObserver(o);
220
        }
221

    
222
        public void deleteObservers() {
223
                this.observers.deleteObservers();
224
        }
225

    
226
}