Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / FLayerStatus.java @ 43215

History | View | Annotate | Download (10.6 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 3
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.fmap.mapcontext.layers;
25

    
26
import java.util.ArrayList;
27
import java.util.List;
28

    
29
import org.gvsig.tools.ToolsLocator;
30
import org.gvsig.tools.dynobject.DynStruct;
31
import org.gvsig.tools.exception.BaseException;
32
import org.gvsig.tools.persistence.PersistenceManager;
33
import org.gvsig.tools.persistence.PersistentState;
34
import org.gvsig.tools.persistence.exception.PersistenceException;
35
import org.gvsig.tools.util.Callable;
36

    
37

    
38
/**
39
 * <p>All layers in <i>libFMap</i> can be in a set of <i>states</i>, that their combination represent the <i>status</i> of a layer.</p>
40
 * 
41
 * <p><code>FLayerStatus</code> class supports the common status for all kind of {@link FLyrDefault FLyrDefault} layers.</p>
42
 * 
43
 * <p>This means that not necessarily all layers can stay in all possible status, it depends on its particular
44
 * nature.</p>
45
 * 
46
 * <p>Layer status states defined in <code>FLayerStatus</code> are:
47
 *  <ul>
48
 *  <li><i><b>Available</b></i>: the layer is available if the source of its data is on-line.</li>
49
 *  <li><i><b>Visible</b></i>: one layer is visible if it's selected its <i>check box</i> associated in TOC. This implies that 
50
 *  layer will be called to be painted unless the necessary data would be unavailable; this situation is more frequent with layers of
51
 *  remote services.</li>
52
 *  <li><i><b>Active</b></i>: is active if it's selected at the TOC.</li>
53
 *  <li><i><b>Dirty</b></i>: is dirty if needs to be refreshed.</li>
54
 *  <li><i><b>In TOC</b></i>: if it's being listed in a TOC GUI.</li>
55
 *  <li><i><b>Editing</b></i>: if the layer it's being edited now.</li>
56
 *  <li><i><b>Writable</b></i>: if can be modified and saved the changes.</li>
57
 *  <li><i><b>Cache drawn layers</b></i>: if stores previous draws. This is useful to accelerate the draw of the layer.</li>
58
 *  <li><i><b>Driver loaded</b></i>: if driver for managing the layer has been loaded successfully.</li>
59
 *  </ul>
60
 * </p>
61
 * 
62
 * @see IFLayerStatus
63
 * 
64
 * @author azabala
65
 */
66
public class FLayerStatus implements IFLayerStatus {
67
        /**
68
         * <p>Layer's availability: the layer is available if the source of its data is on-line.</p>
69
         */
70
        public boolean available;
71
        
72
        /**
73
         * <p>Layer's visibility: the layer is visible if it's selected its <i>check box</i> associated in TOC. This implies that 
74
         *  layer will tried to be painted unless the necessary data would be unavailable; this situation is more frequent with layers of
75
         *  remote services.</p>
76
         */
77
        public boolean visible;
78
        
79
        /**
80
         * <p>The layer is active if it's selected at the TOC.</p>
81
         */
82
        public boolean active;
83
        
84
        /**
85
         * <p>One layer is dirty if needs to be refreshed.</p>
86
         */
87
        public boolean dirty;
88
        
89
        /**
90
         * <p>The layer is in TOC if it's being listed in a TOC GUI.</p>
91
         */
92
        public boolean inTOC;
93
        
94
        /**
95
         * <p>This parameter reports if the layer it's being edited now.</p>
96
         */
97
        public boolean editing;
98
        
99
        /**
100
         * <p>This parameter reports if the layer can be modified and saved the changes.</p>
101
         */
102
        public boolean writable;
103
        
104
        /**
105
         * <p>This state reports if the layer stores previous draws. That's useful to accelerate the draw of the layer.</p>
106
         */
107
        public boolean cacheDrawnLayers;
108

    
109
//        private boolean temporal = false;
110
        /**
111
         * <p>List with the information of the driver errors produced working this layer.</p>
112
         */
113
        private final List<Throwable> layerErrors;
114
                
115
        /**
116
         * <p>This state reports if the driver for managing the layer has been loaded successfully.</p>
117
         */
118
        private boolean driverLoaded=true;
119

    
120
        /**
121
         * <p>Creates an instance with the default status of a <code>FLyrDefault</code> layer:
122
         * <br>
123
         *  <ul>
124
         *  <li><b><i>Visible</i></b>: <code>true</code> .</li>
125
         *  <li><b><i>Active</i></b>: <code>false</code> .</li>
126
         *  <li><b><i>In TOC</i></b>: <code>true</code> .</li>
127
         *  <li><b><i>Editing</i></b>: <code>false</code> .</li>
128
         *  <li><b><i>Cache drawn layers</i></b>: <code>false</code> .</li>
129
         *  <li><b><i>Dirty</i></b>: <code>false</code> .</li>
130
         *  <li><b><i>Available</i></b>: <code>true</code> .</li>
131
         *  <li><b><i>Driver loaded</i></b>: <code>true</code> .</li>
132
         *  <li><b><i>Writable</i></b>: <code>false</code> .</li>
133
         *  <li>Without driver errors.</li>
134
         *  </ul>
135
         * </p>
136
         */
137
        public FLayerStatus(){
138
                this.layerErrors = new ArrayList<>();
139
                visible = true;
140
                active = false;
141
                inTOC = true;
142
                editing = false;
143
                cacheDrawnLayers = false;
144
                dirty = false;
145
                available = true;
146

    
147
                writable = false;
148

    
149
        }
150

    
151
        /**
152
         * <p>Stores information of an exception produced working with a driver of the layer.</p>
153
         * 
154
         * @param error the driver exception
155
         */
156
        public void addLayerError(Throwable error){
157
                layerErrors.add(error);
158
        }
159

    
160
        public void setLayerError(Throwable ex) {
161
            layerErrors.clear();
162
            layerErrors.add(ex);
163
        }
164
        
165
        public FLayerStatus cloneStatus(){
166
                FLayerStatus newStatus = new FLayerStatus();
167
                newStatus.layerErrors.addAll(this.layerErrors);
168
                newStatus.visible = this.visible;
169
                newStatus.active = this.active;
170
                newStatus.inTOC = this.inTOC;
171
                newStatus.editing = this.editing;
172
                newStatus.cacheDrawnLayers = this.cacheDrawnLayers;
173
                newStatus.dirty = this.dirty;
174
                newStatus.available = this.available;
175
                newStatus.driverLoaded = this.driverLoaded;        
176
                newStatus.writable = this.writable;
177
        
178
                return newStatus;
179
        }
180

    
181
        @Override
182
        public boolean equals(Object obj) {
183
                FLayerStatus other;
184
                if (obj instanceof FLayerStatus) {
185
                        other = (FLayerStatus) obj;
186
                } else{
187
                        return false;
188
                }
189

    
190
                if (other.visible != this.visible 
191
                                || other.active != this.active
192
                                || other.inTOC != this.inTOC || other.editing != this.editing
193
                                || other.cacheDrawnLayers != this.cacheDrawnLayers
194
                                || other.dirty != this.dirty
195
                                || other.available != this.available
196
                                || other.driverLoaded != this.driverLoaded
197
                                || other.writable != this.writable) {
198
                        return false;
199
                }
200
                
201

    
202
                
203
                if ((!other.layerErrors.containsAll(this.layerErrors)) || other.layerErrors.size() != this.layerErrors.size()){
204
                        return false;
205
                }
206
                return super.equals(obj);
207
        }
208

    
209
        /**
210
         * <p>Gets the number of driver exceptions produced working with the layer.</p>
211
         * 
212
         * @return number of driver exceptions produced
213
         */
214
        public int getNumErrors(){
215
                return layerErrors.size();
216
        }
217

    
218
        /**
219
         * <p>Gets the information of the <i>nth</i> layer driver exception registered in the status.</p>
220
         * 
221
         * @param i ith layer exception registered
222
         * 
223
         * @return the <i>nth</i> layer driver exception registered
224
         */
225
        public Throwable getError(int i){
226
                return layerErrors.get(i);
227
        }
228

    
229
        /**
230
         * <p>Returns if there have been driver errors working with the layer.</p>
231
         * 
232
         * @return <code>true</code> if there have driver errors working with the layer; otherwise <code>false</code>
233
         */
234
        public boolean isOk(){
235
                return layerErrors.isEmpty();
236
        }
237

    
238
        /**
239
         * <p>Returns a list of errors produced in a layer working with its driver.</p>
240
         * 
241
         * @return list errors produced in a layer working with its driver
242
         */
243
        public List<Throwable> getErrors() {
244
                return layerErrors;
245
        }
246
        
247
        /**
248
         * <p>Returns if has been loaded successfully the driver for managing the layer.</p>
249
         * 
250
         * @return <code>true</code> if has been loaded successfully the driver for managing the layer; otherwise <code>false</code>
251
         */
252
        public boolean isDriverLoaded() {
253
                return driverLoaded;
254
        }
255

    
256
        /**
257
         * <p>Sets if has been loaded successfully the driver for managing the layer.</p>
258
         * 
259
         * @param driverLoaded <code>true</code> if has been loaded successfully the driver for managing the layer; otherwise <code>false</code>
260
         */
261
        public void setDriverLoaded(boolean driverLoaded) {
262
                this.driverLoaded = driverLoaded;
263
        }
264

    
265
        @Override
266
        public void loadFromState(PersistentState state)
267
                        throws PersistenceException {
268

    
269
                visible = state.getBoolean("visible");
270
                active = state.getBoolean("active");
271
                inTOC = state.getBoolean("inTOC");
272
                editing = state.getBoolean("editing");
273
                cacheDrawnLayers = state.getBoolean("cacheDrawnLayers");
274
                dirty = state.getBoolean("dirty");
275
                available = state.getBoolean("available");
276
                writable = state.getBoolean("writable");
277
                driverLoaded = state.getBoolean("driverLoaded");
278
        }
279

    
280
        @Override
281
        public void saveToState(PersistentState state) throws PersistenceException {
282
                
283
                state.set("visible", visible);
284
                state.set("active", active);
285
                state.set("inTOC", inTOC);
286
                state.set("editing", editing);
287
                state.set("cacheDrawnLayers", cacheDrawnLayers);
288
                state.set("dirty", dirty);
289
                state.set("available", available);
290
                state.set("writable", writable);
291
                state.set("driverLoaded", driverLoaded);
292
        }
293
        
294
    public static class RegisterPersistence implements Callable {
295

    
296
                @Override
297
        public Object call() {
298
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
299
                        DynStruct definition = manager.addDefinition(
300
                                FLayerStatus.class,
301
                                "FLayerStatus",
302
                                "FLayerStatus Persistence definition",
303
                                null,
304
                                null
305
                        );
306
                        definition.addDynFieldBoolean("visible").setMandatory(true);
307
                        definition.addDynFieldBoolean("active").setMandatory(true);
308
                        definition.addDynFieldBoolean("inTOC").setMandatory(true);
309
                        definition.addDynFieldBoolean("editing").setMandatory(true);
310
                        definition.addDynFieldBoolean("cacheDrawnLayers").setMandatory(true);
311
                        definition.addDynFieldBoolean("dirty").setMandatory(true);
312
                        definition.addDynFieldBoolean("available").setMandatory(true);
313
                        definition.addDynFieldBoolean("writable").setMandatory(true);
314
                        definition.addDynFieldBoolean("driverLoaded").setMandatory(true);
315
                        //definition.addDynFieldBoolean("locked").setMandatory(true);
316

    
317
                        return Boolean.TRUE;
318
                }
319
    }
320
}
321