Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrVect.java @ 652

History | View | Annotate | Download (10.9 KB)

1
/* Generated by Together */
2
package com.iver.cit.gvsig.fmap.layers;
3

    
4
import java.awt.Graphics2D;
5
import java.awt.geom.Rectangle2D;
6
import java.awt.image.BufferedImage;
7
import java.util.BitSet;
8

    
9
import org.apache.log4j.Logger;
10

    
11
import com.hardcode.driverManager.DriverLoadException;
12
import com.hardcode.gdbms.engine.data.DataSource;
13
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
14
import com.iver.cit.gvsig.fmap.DriverException;
15
import com.iver.cit.gvsig.fmap.ViewPort;
16
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
17
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
18
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
19
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
20
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
21
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
22
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
23
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
24
import com.iver.cit.gvsig.fmap.operations.Cancellable;
25
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
26
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
27
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
28
import com.iver.cit.gvsig.fmap.rendering.Legend;
29
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
30
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
31
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
32
import com.iver.utiles.XMLEntity;
33

    
34

    
35
/**
36
 * DOCUMENT ME!
37
 *
38
 * @author Fernando Gonz?lez Cort?s
39
 */
40
//TODO Cuando no sea para pruebas debe no ser public
41
public class FLyrVect extends FLyrDefault implements Labelable, Selectable,
42
        AlphanumericData, ClassifiableVectorial, SingleLayer, VectorialData, RandomVectorialData {
43
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
44

    
45
        /** Leyenda de la capa vectorial */
46
        private VectorialLegend legend;
47
        private int typeShape = -1;
48
        private SelectionSupport selectionSupport = new SelectionSupport();
49
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
50
        private VectorialAdapter source;
51
        private SelectableDataSource sds;
52

    
53
        /**
54
         * DOCUMENT ME!
55
         *
56
         * @param listener DOCUMENT ME!
57
         */
58
        public void addSelectionListener(SelectionListener listener) {
59
                selectionSupport.addSelectionListener(listener);
60
        }
61

    
62
        /**
63
         * DOCUMENT ME!
64
         *
65
         * @param listener DOCUMENT ME!
66
         */
67
        public void removeSelectionListener(SelectionListener listener) {
68
                selectionSupport.removeSelectionListener(listener);
69
        }
70

    
71
        /**
72
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
73
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
74
         * eventos, se realiza la propagaci?n de manera manual al final de la
75
         * "r?faga" de eventos
76
         */
77
        public void fireSelectionEvents() {
78
                selectionSupport.fireSelectionEvents();
79
        }
80

    
81
        /**
82
         * DOCUMENT ME!
83
         *
84
         * @return DOCUMENT ME!
85
         */
86
        public VectorialAdapter getSource() {
87
                return source;
88
        }
89

    
90
        /**
91
         * DOCUMENT ME!
92
         *
93
         * @param va DOCUMENT ME!
94
         */
95
        public void setSource(VectorialAdapter va) {
96
                source = va;
97
        }
98

    
99
        /**
100
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
101
         */
102
        public Rectangle2D getFullExtent() throws DriverException {
103
                try{
104
                        Rectangle2D rAux;
105
                        logger.debug("source.start()");
106
                        source.start();
107
                        rAux = source.getFullExtent();
108
                        logger.debug("source.stop()");
109
                        source.stop();
110
                        return rAux;
111
                }catch(DriverIOException e){
112
                        throw new DriverException(e);
113
                }
114
        }
115

    
116
        /**
117
         * DOCUMENT ME!
118
         *
119
         * @param image DOCUMENT ME!
120
         * @param g DOCUMENT ME!
121
         * @param viewPort DOCUMENT ME!
122
         * @param cancel DOCUMENT ME!
123
         *
124
         * @throws DriverIOException
125
         * @throws DriverLoadException
126
         * @throws DriverException
127
         *
128
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
129
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
130
         */
131
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
132
                Cancellable cancel)
133
                throws DriverException {
134
                Strategy strategy = StrategyManager.getStrategy(this);
135

    
136
                strategy.draw(image, g, viewPort, cancel);
137
        }
138

    
139
        /**
140
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
141
         *                 com.iver.cit.gvsig.fmap.ViewPort,
142
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
143
         */
144
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
145
                throws DriverException {
146
                Strategy strategy = StrategyManager.getStrategy(this);
147

    
148
                strategy.print(g, viewPort, cancel);
149
        }
150

    
151
        /**
152
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
153
         */
154
        public FLayer createLabelLayer(int fieldId) {
155
                return null;
156
        }
157

    
158
        /**
159
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
160
         */
161
        public void removeLabels() {
162
        }
163

    
164
        /**
165
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createIndex()
166
         */
167
        public void createIndex() {
168
        }
169

    
170
        /**
171
         * @throws DriverIOException
172
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
173
         *                 com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
174
         */
175
        public void process(FeatureVisitor visitor, BitSet subset) throws DriverException {
176
                Strategy s = StrategyManager.getStrategy(this);
177
                s.process(visitor, subset);
178
        }
179

    
180
        /**
181
         * @throws DriverIOException
182
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
183
         */
184
        public void process(FeatureVisitor visitor) throws DriverException {
185
                Strategy s = StrategyManager.getStrategy(this);
186
                s.process(visitor);
187
        }
188

    
189
        /**
190
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
191
         */
192
        public void setSelection(BitSet selection) {
193
                selectionSupport.setSelection(selection);
194
                fireSelectionEvents();
195
        }
196

    
197
        /**
198
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
199
         */
200
        public boolean isSelected(int index) {
201
                return selectionSupport.isSelected(index);
202
        }
203

    
204
        /**
205
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
206
         */
207
        public BitSet getSelection() {
208
                return selectionSupport.getSelection();
209
        }
210

    
211
        /**
212
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
213
         */
214
        public void clearSelection() {
215
                selectionSupport.clearSelection();
216
        }
217

    
218
        /**
219
         * DOCUMENT ME!
220
         *
221
         * @param rect DOCUMENT ME!
222
         *
223
         * @return DOCUMENT ME!
224
         *
225
         * @throws DriverIOException
226
         *
227
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#queryByRect(java.awt.geom.Rectangle2D)
228
         */
229
        public BitSet queryByRect(Rectangle2D rect) throws DriverException {
230
                Strategy s = StrategyManager.getStrategy(this);
231

    
232
                return s.queryByRect(rect);
233
        }
234

    
235
        /**
236
         * DOCUMENT ME!
237
         *
238
         * @return DOCUMENT ME!
239
         *
240
         * @throws DriverLoadException
241
         *
242
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
243
         */
244
        public SelectableDataSource getRecordset() throws DriverException {
245
                if (sds == null) {
246
                        String name = getName();
247

    
248
                        if (name == null) {
249
                                return null;
250
                        }
251

    
252
                        try{
253
                                DataSource ds = source.getRecordset(name);
254
        
255
                                if (ds == null) {
256
                                        return null;
257
                                }
258
        
259
                                sds = new SelectableDataSource(getName(), ds);
260
                                sds.setSelectionSupport(selectionSupport);
261
                        }catch (DriverLoadException e){
262
                                throw new DriverException(e);
263
                        }
264
                }
265

    
266
                return sds;
267
        }
268

    
269
        /**
270
         * DOCUMENT ME!
271
         *
272
         * @param r DOCUMENT ME!
273
         *
274
         * @throws FieldNotFoundException
275
         * @throws DriverException
276
         * @throws DriverLoadException
277
         *
278
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
279
         *                 com.iver.cit.gvsig.fmap.rendering.Legend)
280
         */
281
        public void setLegend(VectorialLegend r)
282
                throws DriverException, FieldNotFoundException {
283
                VectorialLegend oldLegend = legend;
284
                legend = r;
285
                try{
286
                        legend.setDataSource(getRecordset());
287
                } catch(DriverException e){
288
                        throw new DriverException(e);
289
                }
290

    
291
                LegendChangedEvent e = new LegendChangedEvent(oldLegend, legend);
292
                callLegendChanged(e);
293
        }
294

    
295
        /**
296
         * DOCUMENT ME!
297
         *
298
         * @return DOCUMENT ME!
299
         */
300
        public Legend getLegend() {
301
                return legend;
302
        }
303

    
304
        /**
305
         * DOCUMENT ME!
306
         *
307
         * @return DOCUMENT ME!
308
         *
309
         * @throws DriverIOException DOCUMENT ME!
310
         */
311
        public int getShapeType() throws DriverException {
312
                if (typeShape == -1) {
313
                        try{
314
                                logger.debug("source.start()");
315
                                source.start();
316
                                typeShape = source.getShapeType();
317
                                logger.debug("source.stop()");
318
                                source.stop();
319
                        }catch(DriverIOException e){
320
                                throw new DriverException(e);
321
                        }
322
                }
323

    
324
                return typeShape;
325
        }
326

    
327
        /**
328
         * DOCUMENT ME!
329
         *
330
         * @return DOCUMENT ME!
331
         *
332
         * @throws DriverException
333
         *
334
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
335
         */
336
        public XMLEntity getXMLEntity() {
337
                BitSet selected = selectionSupport.getSelection();
338
                XMLEntity xml = super.getXMLEntity();
339
                xml.addChild(legend.getXMLEntity());
340
                if (selected!=null){
341
                xml.putProperty("numBitSet",selected.cardinality());
342
                int n=0;
343
                for (int i=0;i<selected.length();i++){
344
                        if (selected.get(i)){
345
                                xml.putProperty(String.valueOf(n),i);
346
                        n++;
347
                        }
348
                }
349
                }
350

    
351
                if (source instanceof VectorialFileAdapter) {
352
                        xml.putProperty("file", ((VectorialFileAdapter) source).getFile());
353
                } else if (source instanceof VectorialDBAdapter) {
354
                } else if (source instanceof WFSAdapter) {
355
                }
356

    
357
                xml.putProperty("driverName", getSource().getDriver().getName());
358

    
359
                return xml;
360
        }
361

    
362
        /**
363
         * DOCUMENT ME!
364
         *
365
         * @param xml DOCUMENT ME!
366
         * @throws XMLException
367
         * @throws DriverException
368
         *
369
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
370
         */
371
        public void setXMLEntity(XMLEntity xml) throws XMLException, DriverException{
372
                super.setXMLEntity(xml);
373
                legend=LegendFactory.createFromXML(xml.getChild(0));
374
                try{
375
                        legend.setDataSource(getRecordset());
376
                } catch (FieldNotFoundException e) {
377
                        throw new XMLException(e);
378
                } catch (DriverException e) {
379
                        throw new XMLException(e);
380
                }
381
                int numBitSet=xml.getIntProperty("numBitSet");
382
                if (numBitSet!=0){
383
                BitSet selected = new BitSet();
384
                selected=new BitSet();
385
                for(int i=1;i<=numBitSet;i++){
386
                        selected.set(xml.getIntProperty(String.valueOf(i)));
387
                }
388
                selectionSupport.setSelection(selected);
389
                //sds.setSelectionSupport(selectionSupport);
390
                }
391
                //TODO source.setDriver(nameDriver);
392
                
393
        }
394

    
395
        /**
396
         * DOCUMENT ME!
397
         *
398
         * @param listener DOCUMENT ME!
399
         */
400
        public void addLegendListener(LegendListener listener) {
401
                layerChangeSupport.addLayerListener(listener);
402
        }
403

    
404
        /**
405
         * DOCUMENT ME!
406
         *
407
         * @param e DOCUMENT ME!
408
         */
409
        private void callLegendChanged(LegendChangedEvent e) {
410
                layerChangeSupport.callLegendChanged(e);
411
        }
412

    
413
        /**
414
         * DOCUMENT ME!
415
         *
416
         * @param listener DOCUMENT ME!
417
         */
418
        public void removeLegendListener(LegendListener listener) {
419
                layerChangeSupport.removeLayerListener(listener);
420
        }
421

    
422
        /**
423
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#changeRecordsetName()
424
         */
425
        public void changeRecordsetName(String newName) throws DriverException {
426
                source.changeRecordsetName(newName);
427
        }
428
}