Statistics
| Revision:

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

History | View | Annotate | Download (14.5 KB)

1
package com.iver.cit.gvsig.fmap.layers;
2

    
3
import com.hardcode.driverManager.DriverLoadException;
4

    
5
import com.hardcode.gdbms.engine.data.DataSource;
6
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
7
import com.hardcode.gdbms.engine.values.DoubleValue;
8
import com.hardcode.gdbms.engine.values.FloatValue;
9
import com.hardcode.gdbms.engine.values.NullValue;
10
import com.hardcode.gdbms.engine.values.Value;
11

    
12
import com.iver.cit.gvsig.fmap.DriverException;
13
import com.iver.cit.gvsig.fmap.ViewPort;
14
import com.iver.cit.gvsig.fmap.core.IGeometry;
15
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
16
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
17
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
18
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
19
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
20
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
21
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
22
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
23
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
24
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
25
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
26
import com.iver.cit.gvsig.fmap.operations.Cancellable;
27
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
28
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
29
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
30
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
31
import com.iver.cit.gvsig.fmap.rendering.Legend;
32
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
33
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
34
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
35

    
36
import com.iver.utiles.XMLEntity;
37

    
38
import org.apache.log4j.Logger;
39

    
40
import org.cresques.cts.ICoordTrans;
41

    
42
import java.awt.Graphics2D;
43
import java.awt.geom.Point2D;
44
import java.awt.geom.Rectangle2D;
45
import java.awt.image.BufferedImage;
46

    
47
import java.util.ArrayList;
48
import java.util.BitSet;
49

    
50

    
51
/**
52
 * Capa b?sica Vectorial.
53
 *
54
 * @author Fernando Gonz?lez Cort?s
55
 */
56

    
57
//TODO Cuando no sea para pruebas debe no ser public
58
public class FLyrVect extends FLyrDefault implements Labelable, Selectable,
59
        AlphanumericData, ClassifiableVectorial, SingleLayer, VectorialData,
60
        RandomVectorialData {
61
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
62

    
63
        /** Leyenda de la capa vectorial */
64
        private VectorialLegend legend;
65
        private int typeShape = -1;
66
        private SelectionSupport selectionSupport = new SelectionSupport();
67
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
68
        private VectorialAdapter source;
69
        private SelectableDataSource sds;
70

    
71
        /**
72
         * A?ade un SelectionListener a la lista de listeners.
73
         *
74
         * @param listener SelectionListener.
75
         */
76
        public void addSelectionListener(SelectionListener listener) {
77
                selectionSupport.addSelectionListener(listener);
78
        }
79

    
80
        /**
81
         * Borra un selectionListener de la lista de listeners.
82
         *
83
         * @param listener SelectionListener
84
         */
85
        public void removeSelectionListener(SelectionListener listener) {
86
                selectionSupport.removeSelectionListener(listener);
87
        }
88

    
89
        /**
90
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
91
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
92
         * eventos, se realiza la propagaci?n de manera manual al final de la
93
         * "r?faga" de eventos
94
         */
95
        public void fireSelectionEvents() {
96
                selectionSupport.fireSelectionEvents();
97
        }
98

    
99
        /**
100
         * Devuelve el VectorialAdapater de la capa.
101
         *
102
         * @return VectorialAdapter.
103
         */
104
        public VectorialAdapter getSource() {
105
                return source;
106
        }
107

    
108
        /**
109
         * Inserta el VectorialAdapter a la capa.
110
         *
111
         * @param va VectorialAdapter.
112
         */
113
        public void setSource(VectorialAdapter va) {
114
                source = va;
115
        }
116

    
117
        /**
118
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
119
         */
120
        public Rectangle2D getFullExtent() throws DriverException {
121
                try {
122
                        Rectangle2D rAux;
123
                        logger.debug("source.start()");
124
                        source.start();
125
                        rAux = source.getFullExtent();
126
                        logger.debug("source.stop()");
127
                        source.stop();
128

    
129
                        // Si existe reproyecci?n, reproyectar el extent
130
                        ICoordTrans ct = getCoordTrans();
131

    
132
                        if (ct != null) {
133
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
134
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
135
                                pt1 = ct.convert(pt1, null);
136
                                pt2 = ct.convert(pt2, null);
137
                                rAux = new Rectangle2D.Double();
138
                                rAux.setFrameFromDiagonal(pt1, pt2);
139
                        }
140

    
141
                        return rAux;
142
                } catch (DriverIOException e) {
143
                        throw new DriverException(e);
144
                }
145
        }
146

    
147
        /**
148
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
149
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
150
         */
151
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
152
                Cancellable cancel) throws DriverException {
153
                Strategy strategy = StrategyManager.getStrategy(this);
154

    
155
                strategy.draw(image, g, viewPort, cancel);
156

    
157
                if (getVirtualLayers() != null) {
158
                        getVirtualLayers().draw(image, g, viewPort, cancel);
159
                }
160

    
161
                if (getLayerText() != null) {
162
                        getLayerText().draw(image, g, viewPort, cancel);
163
                }
164
        }
165

    
166
        /**
167
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
168
         *                 com.iver.cit.gvsig.fmap.ViewPort,
169
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
170
         */
171
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
172
                throws DriverException {
173
                Strategy strategy = StrategyManager.getStrategy(this);
174

    
175
                strategy.print(g, viewPort, cancel);
176

    
177
                if (getLayerText() != null) {
178
                        getLayerText().draw(null, g, viewPort, cancel);
179
                }
180
        }
181

    
182
        /**
183
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
184
         */
185
        public FLayer createLabelLayer(int fieldId) {
186
                ArrayList labels = new ArrayList();
187

    
188
                try {
189
                        VectorialAdapter adapter = getSource();
190
                        DataSource ds = getRecordset();
191
                        logger.debug("adapter.start()");
192
                        adapter.start();
193
                        ds.start();
194

    
195
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
196
                        int sc;
197

    
198
                        sc = adapter.getShapeCount();
199

    
200
                        VectorialLegend l = (VectorialLegend) getLegend();
201
                        int idFieldHeightText = -1;
202
                        int idFieldRotationText = -1;
203
                        boolean bWithHeightText = false;
204

    
205
                        if (l.getLabelHeightField() != null) {
206
                                bWithHeightText = true;
207
                                idFieldHeightText = ds.getFieldIndexByName(l.getLabelHeightField());
208
                        }
209

    
210
                        boolean bWithRotationText = false;
211

    
212
                        if (l.getLabelRotationField() != null) {
213
                                bWithRotationText = true;
214
                                idFieldRotationText = ds.getFieldIndexByName(l.getLabelRotationField());
215
                        }
216

    
217
                        for (int i = 0; i < sc; i++) {
218
                                Value val = ds.getFieldValue(i, fieldId);
219

    
220
                                if (val instanceof NullValue) {
221
                                        continue;
222
                                }
223

    
224
                                IGeometry geom = adapter.getShape(i);
225

    
226
                                if (geom == null) {
227
                                        continue;
228
                                }
229

    
230
                                FSymbol symbol = l.getSymbol(i);
231

    
232
                                // TODO: El m?todo contenedor (createLabelLayer) debe recoger
233
                                // los par?metros de posicionamiento y de allowDuplicates
234
                                // if (i >= 328)
235
                                //         System.out.println("i= " + i + " " + val.toString());
236
                                FLabel[] lbls = geom.createLabels(0, true);
237

    
238
                                for (int j = 0; j < lbls.length; j++) {
239
                                        if (lbls[j] != null) {
240
                                                lbls[j].setString(val.toString());
241

    
242
                                                if (bWithHeightText) {
243
                                                        FloatValue height = (FloatValue) ds.getFieldValue(i,
244
                                                                        idFieldHeightText);
245
                                                        lbls[j].setHeight(height.getValue());
246
                                                } else {
247
                                                        lbls[j].setHeight(symbol.getFontSize());
248
                                                }
249

    
250
                                                if (bWithRotationText) {
251
                                                        DoubleValue rotation = (DoubleValue) ds.getFieldValue(i,
252
                                                                        idFieldRotationText);
253
                                                        lbls[j].setRotation(rotation.getValue());
254
                                                }
255
                                        }
256

    
257
                                        labels.add(lbls[j]);
258
                                }
259
                        }
260

    
261
                        long t2 = System.currentTimeMillis();
262
                        logger.debug("adapter.stop()");
263
                        ds.stop();
264
                        adapter.stop();
265
                } catch (DriverIOException e) {
266
                        e.printStackTrace();
267
                } catch (DriverException e) {
268
                        e.printStackTrace();
269
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
270
                        // TODO Auto-generated catch block
271
                        e.printStackTrace();
272
                } catch (FieldNotFoundException e) {
273
                        // TODO Auto-generated catch block
274
                        e.printStackTrace();
275
                }
276

    
277
                FLyrText layerText = new FLyrText(labels);
278

    
279
                try {
280
                        layerText.setLegend((VectorialLegend) getLegend());
281
                } catch (FieldNotFoundException e1) {
282
                        // TODO Auto-generated catch block
283
                        e1.printStackTrace();
284
                } catch (DriverException e1) {
285
                        // TODO Auto-generated catch block
286
                        e1.printStackTrace();
287
                }
288

    
289
                setLayerText(layerText);
290

    
291
                return layerText;
292
        }
293

    
294
        /**
295
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
296
         */
297
        public void removeLabels() {
298
                setLayerText(null);
299
        }
300

    
301
        /**
302
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createIndex()
303
         */
304
        public void createIndex() {
305
        }
306

    
307
        /**
308
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
309
         *                 com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
310
         */
311
        public void process(FeatureVisitor visitor, BitSet subset)
312
                throws DriverException, VisitException {
313
                Strategy s = StrategyManager.getStrategy(this);
314
                s.process(visitor, subset);
315
        }
316

    
317
        /**
318
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
319
         */
320
        public void process(FeatureVisitor visitor)
321
                throws DriverException, VisitException {
322
                Strategy s = StrategyManager.getStrategy(this);
323
                s.process(visitor);
324
        }
325

    
326
        /**
327
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
328
         */
329
        public void setSelection(FBitSet selection) {
330
                selectionSupport.setSelection(selection);
331
                fireSelectionEvents();
332
        }
333

    
334
        /**
335
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
336
         */
337
        public boolean isSelected(int index) {
338
                return selectionSupport.isSelected(index);
339
        }
340

    
341
        /**
342
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
343
         */
344
        public FBitSet getSelection() {
345
                return selectionSupport.getSelection();
346
        }
347

    
348
        /**
349
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
350
         */
351
        public void clearSelection() {
352
                selectionSupport.clearSelection();
353
        }
354

    
355
        /**
356
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#queryByRect(java.awt.geom.Rectangle2D)
357
         */
358
        public BitSet queryByRect(Rectangle2D rect) throws DriverException {
359
                Strategy s = StrategyManager.getStrategy(this);
360

    
361
                return s.queryByRect(rect);
362
        }
363

    
364
        /**
365
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
366
         */
367
        public SelectableDataSource getRecordset() throws DriverException {
368
                if (sds == null) {
369
                        //Nombre en el GDBMS de la tabla
370
                        String name = this.toString();
371

    
372
                        try {
373
                                DataSource ds = source.getRecordset(name);
374

    
375
                                if (ds == null) {
376
                                        return null;
377
                                }
378

    
379
                                sds = new SelectableDataSource(ds);
380
                                sds.setSelectionSupport(selectionSupport);
381
                        } catch (DriverLoadException e) {
382
                                throw new DriverException(e);
383
                        }
384
                }
385

    
386
                return sds;
387
        }
388

    
389
        /**
390
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
391
         *                 com.iver.cit.gvsig.fmap.rendering.Legend)
392
         */
393
        public void setLegend(VectorialLegend r)
394
                throws DriverException, FieldNotFoundException {
395
                VectorialLegend oldLegend = legend;
396
                legend = r;
397

    
398
                try {
399
                        legend.setDataSource(getRecordset());
400

    
401
                        if (legend.getLabelField() != null) {
402
                                int idLabelField = getRecordset().getFieldIndexByName(legend.getLabelField());
403
                                createLabelLayer(idLabelField);
404
                        }
405
                } catch (DriverException e) {
406
                        throw new DriverException(e);
407
                } catch (FieldNotFoundException e) {
408
                        // TODO Auto-generated catch block
409
                        e.printStackTrace();
410
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
411
                        throw new DriverException(e);
412
                }
413

    
414
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, legend);
415
                callLegendChanged(e);
416
        }
417

    
418
        /**
419
         * Devuelve la Leyenda de la capa.
420
         *
421
         * @return Leyenda.
422
         */
423
        public Legend getLegend() {
424
                return legend;
425
        }
426

    
427
        /**
428
         * Devuelve el tipo de shape que contiene la capa.
429
         *
430
         * @return tipo de shape.
431
         *
432
         * @throws DriverException
433
         */
434
        public int getShapeType() throws DriverException {
435
                if (typeShape == -1) {
436
                        try {
437
                                logger.debug("source.start()");
438
                                source.start();
439
                                typeShape = source.getShapeType();
440
                                logger.debug("source.stop()");
441
                                source.stop();
442
                        } catch (DriverIOException e) {
443
                                throw new DriverException(e);
444
                        }
445
                }
446

    
447
                return typeShape;
448
        }
449

    
450
        /**
451
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
452
         */
453
        public XMLEntity getXMLEntity() {
454
                XMLEntity xml = super.getXMLEntity();
455
                xml.addChild(legend.getXMLEntity());
456
                xml.addChild(selectionSupport.getXMLEntity());
457

    
458
                if (source instanceof VectorialFileAdapter) {
459
                        xml.putProperty("file", ((VectorialFileAdapter) source).getFile());
460
                } else if (source instanceof VectorialDBAdapter) {
461
                } else if (source instanceof WFSAdapter) {
462
                }
463

    
464
                xml.putProperty("driverName", getSource().getDriver().getName());
465

    
466
                return xml;
467
        }
468

    
469
        /**
470
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
471
         */
472
        public void setXMLEntity(XMLEntity xml)
473
                throws XMLException {
474
                
475
                super.setXMLEntity(xml);
476
                legend = LegendFactory.createFromXML(xml.getChild(0));
477

    
478
                try {
479
                        legend.setDataSource(getRecordset());
480
                } catch (FieldNotFoundException e) {
481
                        throw new XMLException(e);
482
                } catch (DriverException e) {
483
                        throw new XMLException(e);
484
                }        
485

    
486
                selectionSupport.setXMLEntity(xml.getChild(1));
487
        }
488

    
489
        /**
490
         * A?ade un LegendListener a la lista de Listeners.
491
         *
492
         * @param listener LegendListener.
493
         */
494
        public void addLegendListener(LegendListener listener) {
495
                layerChangeSupport.addLayerListener(listener);
496
        }
497

    
498
        /**
499
         * Llamada al m?todo callLegendChanged de los listener.
500
         *
501
         * @param e Evento.
502
         */
503
        private void callLegendChanged(LegendChangedEvent e) {
504
                layerChangeSupport.callLegendChanged(e);
505
        }
506

    
507
        /**
508
         * Borra un LegendListener de la lista de Listeners
509
         *
510
         * @param listener LegendListener.
511
         */
512
        public void removeLegendListener(LegendListener listener) {
513
                layerChangeSupport.removeLayerListener(listener);
514
        }
515

    
516
        /**
517
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#changeRecordsetName()
518
         */
519
        public void changeRecordsetName(String newName) throws DriverException {
520
                source.changeRecordsetName(newName);
521
        }
522

    
523
        /**
524
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
525
         * identifiquen la capa.
526
         *
527
         * @return DOCUMENT ME!
528
         */
529
        public String toString() {
530
                /*
531
                 * Se usa internamente para que la parte de datos
532
                 * identifique de forma un?voca las tablas
533
                 */
534
                String ret = super.toString();
535

    
536
                return "layer" + ret.substring(ret.indexOf('@') + 1);
537
        }
538
}