Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrVect.java @ 1773

History | View | Annotate | Download (15.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import com.hardcode.driverManager.DriverLoadException;
44

    
45
import com.hardcode.gdbms.engine.data.DataSource;
46
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
47
import com.hardcode.gdbms.engine.values.DoubleValue;
48
import com.hardcode.gdbms.engine.values.FloatValue;
49
import com.hardcode.gdbms.engine.values.NullValue;
50
import com.hardcode.gdbms.engine.values.Value;
51

    
52
import com.iver.cit.gvsig.fmap.DriverException;
53
import com.iver.cit.gvsig.fmap.ViewPort;
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
56
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
57
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
58
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
59
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
60
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
61
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
62
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
63
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
64
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
66
import com.iver.cit.gvsig.fmap.operations.Cancellable;
67
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
68
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
69
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
70
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
71
import com.iver.cit.gvsig.fmap.rendering.Legend;
72
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
73
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
74
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
75

    
76
import com.iver.utiles.XMLEntity;
77

    
78
import org.apache.log4j.Logger;
79

    
80
import org.cresques.cts.ICoordTrans;
81

    
82
import java.awt.Graphics2D;
83
import java.awt.geom.Point2D;
84
import java.awt.geom.Rectangle2D;
85
import java.awt.image.BufferedImage;
86

    
87
import java.util.ArrayList;
88
import java.util.BitSet;
89

    
90

    
91
/**
92
 * Capa b?sica Vectorial.
93
 *
94
 * @author Fernando Gonz?lez Cort?s
95
 */
96

    
97
//TODO Cuando no sea para pruebas debe no ser public
98
public class FLyrVect extends FLyrDefault implements Labelable, Selectable,
99
        AlphanumericData, ClassifiableVectorial, SingleLayer, VectorialData,
100
        RandomVectorialData {
101
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
102

    
103
        /** Leyenda de la capa vectorial */
104
        private VectorialLegend legend;
105
        private int typeShape = -1;
106
        private SelectionSupport selectionSupport = new SelectionSupport();
107
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
108
        private VectorialAdapter source;
109
        private SelectableDataSource sds;
110

    
111
        /**
112
         * A?ade un SelectionListener a la lista de listeners.
113
         *
114
         * @param listener SelectionListener.
115
         */
116
        public void addSelectionListener(SelectionListener listener) {
117
                selectionSupport.addSelectionListener(listener);
118
        }
119

    
120
        /**
121
         * Borra un selectionListener de la lista de listeners.
122
         *
123
         * @param listener SelectionListener
124
         */
125
        public void removeSelectionListener(SelectionListener listener) {
126
                selectionSupport.removeSelectionListener(listener);
127
        }
128

    
129
        /**
130
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
131
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
132
         * eventos, se realiza la propagaci?n de manera manual al final de la
133
         * "r?faga" de eventos
134
         */
135
        public void fireSelectionEvents() {
136
                selectionSupport.fireSelectionEvents();
137
        }
138

    
139
        /**
140
         * Devuelve el VectorialAdapater de la capa.
141
         *
142
         * @return VectorialAdapter.
143
         */
144
        public VectorialAdapter getSource() {
145
                return source;
146
        }
147

    
148
        /**
149
         * Inserta el VectorialAdapter a la capa.
150
         *
151
         * @param va VectorialAdapter.
152
         */
153
        public void setSource(VectorialAdapter va) {
154
                source = va;
155
        }
156

    
157
        /**
158
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
159
         */
160
        public Rectangle2D getFullExtent() throws DriverException {
161
                try {
162
                        Rectangle2D rAux;
163
                        logger.debug("source.start()");
164
                        source.start();
165
                        rAux = source.getFullExtent();
166
                        logger.debug("source.stop()");
167
                        source.stop();
168

    
169
                        // Si existe reproyecci?n, reproyectar el extent
170
                        ICoordTrans ct = getCoordTrans();
171

    
172
                        if (ct != null) {
173
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
174
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
175
                                pt1 = ct.convert(pt1, null);
176
                                pt2 = ct.convert(pt2, null);
177
                                rAux = new Rectangle2D.Double();
178
                                rAux.setFrameFromDiagonal(pt1, pt2);
179
                        }
180

    
181
                        return rAux;
182
                } catch (DriverIOException e) {
183
                        throw new DriverException(e);
184
                }
185
        }
186

    
187
        /**
188
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
189
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
190
         */
191
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
192
                Cancellable cancel) throws DriverException {
193
            
194
            
195
            
196
                Strategy strategy = StrategyManager.getStrategy(this);
197

    
198
                strategy.draw(image, g, viewPort, cancel);
199

    
200
                if (getVirtualLayers() != null) {
201
                        getVirtualLayers().draw(image, g, viewPort, cancel);
202
                }
203

    
204
                if (getLayerText() != null) {
205
                        getLayerText().draw(image, g, viewPort, cancel);
206
                }
207
        }
208

    
209
        /**
210
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
211
         *                 com.iver.cit.gvsig.fmap.ViewPort,
212
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
213
         */
214
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
215
                throws DriverException {
216
                Strategy strategy = StrategyManager.getStrategy(this);
217

    
218
                strategy.print(g, viewPort, cancel);
219

    
220
                if (getLayerText() != null) {
221
                        getLayerText().draw(null, g, viewPort, cancel);
222
                }
223
        }
224

    
225
        /**
226
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
227
         */
228
        public FLayer createLabelLayer(int fieldId) {
229
                ArrayList labels = new ArrayList();
230

    
231
                try {
232
                        VectorialAdapter adapter = getSource();
233
                        DataSource ds = getRecordset();
234
                        logger.debug("adapter.start()");
235
                        adapter.start();
236
                        ds.start();
237

    
238
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
239
                        int sc;
240

    
241
                        sc = adapter.getShapeCount();
242

    
243
                        VectorialLegend l = (VectorialLegend) getLegend();
244
                        int idFieldHeightText = -1;
245
                        int idFieldRotationText = -1;
246
                        boolean bWithHeightText = false;
247

    
248
                        if (l.getLabelHeightField() != null) {
249
                                bWithHeightText = true;
250
                                idFieldHeightText = ds.getFieldIndexByName(l.getLabelHeightField());
251
                        }
252

    
253
                        boolean bWithRotationText = false;
254

    
255
                        if (l.getLabelRotationField() != null) {
256
                                bWithRotationText = true;
257
                                idFieldRotationText = ds.getFieldIndexByName(l.getLabelRotationField());
258
                        }
259

    
260
                        for (int i = 0; i < sc; i++) {
261
                                Value val = ds.getFieldValue(i, fieldId);
262

    
263
                                if ((val instanceof NullValue) || (val == null)) {
264
                                        continue;
265
                                }
266

    
267
                                IGeometry geom = adapter.getShape(i);
268

    
269
                                if (geom == null) {
270
                                        continue;
271
                                }
272

    
273
                                FSymbol symbol = l.getSymbol(i);
274

    
275
                                // TODO: El m?todo contenedor (createLabelLayer) debe recoger
276
                                // los par?metros de posicionamiento y de allowDuplicates
277
                                // if (i >= 328)
278
                                //         System.out.println("i= " + i + " " + val.toString());
279
                                FLabel[] lbls = geom.createLabels(0, true);
280

    
281
                                for (int j = 0; j < lbls.length; j++) {
282
                                        if (lbls[j] != null) {
283
                                                lbls[j].setString(val.toString());
284

    
285
                                                if (bWithHeightText) {
286
                                                        FloatValue height = (FloatValue) ds.getFieldValue(i,
287
                                                                        idFieldHeightText);
288
                                                        lbls[j].setHeight(height.getValue());
289
                                                } else {
290
                                                        lbls[j].setHeight(symbol.getFontSize());
291
                                                }
292

    
293
                                                if (bWithRotationText) {
294
                                                        DoubleValue rotation = (DoubleValue) ds.getFieldValue(i,
295
                                                                        idFieldRotationText);
296
                                                        lbls[j].setRotation(rotation.getValue());
297
                                                }
298
                                                labels.add(lbls[j]);        
299
                                        }
300

    
301
                                        
302
                                }
303
                        }
304

    
305
                        long t2 = System.currentTimeMillis();
306
                        logger.debug("adapter.stop()");
307
                        ds.stop();
308
                        adapter.stop();
309
                } catch (DriverIOException e) {
310
                        e.printStackTrace();
311
                } catch (DriverException e) {
312
                        e.printStackTrace();
313
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
314
                        // TODO Auto-generated catch block
315
                        e.printStackTrace();
316
                }
317

    
318
                FLyrText layerText = new FLyrText(labels);
319

    
320
                try {
321
                        layerText.setLegend((VectorialLegend) getLegend());
322
                } catch (FieldNotFoundException e1) {
323
                        // TODO Auto-generated catch block
324
                        e1.printStackTrace();
325
                } catch (DriverException e1) {
326
                        // TODO Auto-generated catch block
327
                        e1.printStackTrace();
328
                }
329

    
330
                setLayerText(layerText);
331

    
332
                return layerText;
333
        }
334

    
335
        /**
336
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
337
         */
338
        public void removeLabels() {
339
                setLayerText(null);
340
        }
341

    
342
        /**
343
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createIndex()
344
         */
345
        public void createIndex() {
346
        }
347

    
348
        /**
349
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
350
         *                 com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
351
         */
352
        public void process(FeatureVisitor visitor, BitSet subset)
353
                throws DriverException, VisitException {
354
                Strategy s = StrategyManager.getStrategy(this);
355
                s.process(visitor, subset);
356
        }
357

    
358
        /**
359
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
360
         */
361
        public void process(FeatureVisitor visitor)
362
                throws DriverException, VisitException {
363
                Strategy s = StrategyManager.getStrategy(this);
364
                s.process(visitor);
365
        }
366

    
367
        /**
368
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
369
         */
370
        public void setSelection(FBitSet selection) {
371
                selectionSupport.setSelection(selection);
372
                fireSelectionEvents();
373
        }
374

    
375
        /**
376
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
377
         */
378
        public boolean isSelected(int index) {
379
                return selectionSupport.isSelected(index);
380
        }
381

    
382
        /**
383
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
384
         */
385
        public FBitSet getSelection() {
386
                return selectionSupport.getSelection();
387
        }
388

    
389
        /**
390
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
391
         */
392
        public void clearSelection() {
393
                selectionSupport.clearSelection();
394
        }
395

    
396
        /**
397
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#queryByRect(java.awt.geom.Rectangle2D)
398
         */
399
        public BitSet queryByRect(Rectangle2D rect) throws DriverException {
400
                Strategy s = StrategyManager.getStrategy(this);
401

    
402
                return s.queryByRect(rect);
403
        }
404

    
405
        /**
406
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
407
         */
408
        public SelectableDataSource getRecordset() throws DriverException {
409
                if (sds == null) {
410
                        //Nombre en el GDBMS de la tabla
411
                        String name = this.toString();
412

    
413
                        try {
414
                                DataSource ds = source.getRecordset(name);
415

    
416
                                if (ds == null) {
417
                                        return null;
418
                                }
419

    
420
                                sds = new SelectableDataSource(ds);
421
                                sds.setSelectionSupport(selectionSupport);
422
                        } catch (DriverLoadException e) {
423
                                throw new DriverException(e);
424
                        }
425
                }
426

    
427
                return sds;
428
        }
429

    
430
        /**
431
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
432
         *                 com.iver.cit.gvsig.fmap.rendering.Legend)
433
         */
434
        public void setLegend(VectorialLegend r)
435
                throws DriverException, FieldNotFoundException {
436
                VectorialLegend oldLegend = legend;
437
                legend = r;
438

    
439
                try {
440
                        legend.setDataSource(getRecordset());
441

    
442
                        if (legend.getLabelField() != null) {
443
                            sds.start();
444
                                int idLabelField = getRecordset().getFieldIndexByName(legend.getLabelField());
445
                                createLabelLayer(idLabelField);
446
                                sds.stop();
447
                        }
448
                        else
449
                            removeLabels();
450
                } catch (DriverException e) {
451
                        throw new DriverException(e);
452
                } catch (FieldNotFoundException e) {
453
                        // TODO Auto-generated catch block
454
                        e.printStackTrace();
455
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
456
                        throw new DriverException(e);
457
        }
458

    
459
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, legend);
460
                callLegendChanged(e);
461
        }
462

    
463
        /**
464
         * Devuelve la Leyenda de la capa.
465
         *
466
         * @return Leyenda.
467
         */
468
        public Legend getLegend() {
469
                return legend;
470
        }
471

    
472
        /**
473
         * Devuelve el tipo de shape que contiene la capa.
474
         *
475
         * @return tipo de shape.
476
         *
477
         * @throws DriverException
478
         */
479
        public int getShapeType() throws DriverException {
480
                if (typeShape == -1) {
481
                        try {
482
                                logger.debug("source.start()");
483
                                source.start();
484
                                typeShape = source.getShapeType();
485
                                logger.debug("source.stop()");
486
                                source.stop();
487
                        } catch (DriverIOException e) {
488
                                throw new DriverException(e);
489
                        }
490
                }
491

    
492
                return typeShape;
493
        }
494

    
495
        /**
496
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
497
         */
498
        public XMLEntity getXMLEntity() {
499
                XMLEntity xml = super.getXMLEntity();
500
                xml.addChild(legend.getXMLEntity());
501
                xml.addChild(selectionSupport.getXMLEntity());
502

    
503
                if (source instanceof VectorialFileAdapter) {
504
                        xml.putProperty("file", ((VectorialFileAdapter) source).getFile());
505
                } else if (source instanceof VectorialDBAdapter) {
506
                } else if (source instanceof WFSAdapter) {
507
                }
508

    
509
                xml.putProperty("driverName", getSource().getDriver().getName());
510

    
511
                return xml;
512
        }
513

    
514
        /**
515
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
516
         */
517
        public void setXMLEntity(XMLEntity xml)
518
                throws XMLException {
519
                
520
                super.setXMLEntity(xml);
521
                legend = LegendFactory.createFromXML(xml.getChild(0));
522

    
523
                try {
524
                        // legend.setDataSource(getRecordset());
525
                        setLegend(legend);
526
                } catch (FieldNotFoundException e) {
527
                        throw new XMLException(e);
528
                } catch (DriverException e) {
529
                        throw new XMLException(e);
530
                }        
531

    
532
                selectionSupport.setXMLEntity(xml.getChild(1));
533
        }
534

    
535
        /**
536
         * A?ade un LegendListener a la lista de Listeners.
537
         *
538
         * @param listener LegendListener.
539
         */
540
        public void addLegendListener(LegendListener listener) {
541
                layerChangeSupport.addLayerListener(listener);
542
        }
543

    
544
        /**
545
         * Llamada al m?todo callLegendChanged de los listener.
546
         *
547
         * @param e Evento.
548
         */
549
        private void callLegendChanged(LegendChangedEvent e) {
550
                layerChangeSupport.callLegendChanged(e);
551
        }
552

    
553
        /**
554
         * Borra un LegendListener de la lista de Listeners
555
         *
556
         * @param listener LegendListener.
557
         */
558
        public void removeLegendListener(LegendListener listener) {
559
                layerChangeSupport.removeLayerListener(listener);
560
        }
561

    
562
        /**
563
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
564
         * identifiquen la capa.
565
         *
566
         * @return DOCUMENT ME!
567
         */
568
        public String toString() {
569
                /*
570
                 * Se usa internamente para que la parte de datos
571
                 * identifique de forma un?voca las tablas
572
                 */
573
                String ret = super.toString();
574

    
575
                return "layer" + ret.substring(ret.indexOf('@') + 1);
576
        }
577
}