Statistics
| Revision:

svn-gvsig-desktop / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrVect.java @ 1848

History | View | Annotate | Download (16.1 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 java.awt.Graphics2D;
44
import java.awt.geom.Point2D;
45
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47
import java.util.ArrayList;
48
import java.util.BitSet;
49

    
50
import org.apache.log4j.Logger;
51
import org.cresques.cts.ICoordTrans;
52

    
53
import com.hardcode.driverManager.DriverLoadException;
54
import com.hardcode.gdbms.engine.data.DataSource;
55
import com.hardcode.gdbms.engine.data.NoSuchTableException;
56
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
57
import com.hardcode.gdbms.engine.values.DoubleValue;
58
import com.hardcode.gdbms.engine.values.FloatValue;
59
import com.hardcode.gdbms.engine.values.NullValue;
60
import com.hardcode.gdbms.engine.values.Value;
61
import com.iver.cit.gvsig.fmap.DriverException;
62
import com.iver.cit.gvsig.fmap.ViewPort;
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
65
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
66
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
67
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
68
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
69
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
75
import com.iver.cit.gvsig.fmap.operations.Cancellable;
76
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
77
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
78
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
79
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
80
import com.iver.cit.gvsig.fmap.rendering.Legend;
81
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
82
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
83
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
84
import com.iver.utiles.XMLEntity;
85

    
86

    
87
/**
88
 * Capa b?sica Vectorial.
89
 *
90
 * @author Fernando Gonz?lez Cort?s
91
 */
92

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

    
99
        /** Leyenda de la capa vectorial */
100
        private VectorialLegend legend;
101
        private int typeShape = -1;
102
        private SelectionSupport selectionSupport = new SelectionSupport();
103
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
104
        private VectorialAdapter source;
105
        private SelectableDataSource sds;
106

    
107
        /**
108
         * A?ade un SelectionListener a la lista de listeners.
109
         *
110
         * @param listener SelectionListener.
111
         */
112
        public void addSelectionListener(SelectionListener listener) {
113
                selectionSupport.addSelectionListener(listener);
114
        }
115

    
116
        /**
117
         * Borra un selectionListener de la lista de listeners.
118
         *
119
         * @param listener SelectionListener
120
         */
121
        public void removeSelectionListener(SelectionListener listener) {
122
                selectionSupport.removeSelectionListener(listener);
123
        }
124

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

    
135
        /**
136
         * Devuelve el VectorialAdapater de la capa.
137
         *
138
         * @return VectorialAdapter.
139
         */
140
        public VectorialAdapter getSource() {
141
                return source;
142
        }
143

    
144
        /**
145
         * Inserta el VectorialAdapter a la capa.
146
         *
147
         * @param va VectorialAdapter.
148
         */
149
        public void setSource(VectorialAdapter va) {
150
                source = va;
151
        }
152

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

    
165
                        // Si existe reproyecci?n, reproyectar el extent
166
                        ICoordTrans ct = getCoordTrans();
167

    
168
                        if (ct != null) {
169
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
170
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
171
                                pt1 = ct.convert(pt1, null);
172
                                pt2 = ct.convert(pt2, null);
173
                                rAux = new Rectangle2D.Double();
174
                                rAux.setFrameFromDiagonal(pt1, pt2);
175
                        }
176

    
177
                        return rAux;
178
                } catch (DriverIOException e) {
179
                        throw new DriverException(e);
180
                }
181
        }
182

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

    
194
                strategy.draw(image, g, viewPort, cancel);
195

    
196
                if (getVirtualLayers() != null) {
197
                        getVirtualLayers().draw(image, g, viewPort, cancel);
198
                }
199

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

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

    
214
                strategy.print(g, viewPort, cancel);
215

    
216
                if (getLayerText() != null) {
217
                        getLayerText().draw(null, g, viewPort, cancel);
218
                }
219
        }
220

    
221
        /**
222
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
223
         */
224
        public FLayer createLabelLayer(int fieldId) {
225
                ArrayList labels = new ArrayList();
226

    
227
                try {
228
                        VectorialAdapter adapter = getSource();
229
                        DataSource ds = getRecordset();
230
                        logger.debug("adapter.start()");
231
                        adapter.start();
232
                        ds.start();
233

    
234
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
235
                        int sc;
236

    
237
                        sc = adapter.getShapeCount();
238

    
239
                        VectorialLegend l = (VectorialLegend) getLegend();
240
                        int idFieldHeightText = -1;
241
                        int idFieldRotationText = -1;
242
                        boolean bWithHeightText = false;
243

    
244
                        if (l.getLabelHeightField() != null) {
245
                                bWithHeightText = true;
246
                                idFieldHeightText = ds.getFieldIndexByName(l.getLabelHeightField());
247
                        }
248

    
249
                        boolean bWithRotationText = false;
250

    
251
                        if (l.getLabelRotationField() != null) {
252
                                bWithRotationText = true;
253
                                idFieldRotationText = ds.getFieldIndexByName(l.getLabelRotationField());
254
                        }
255

    
256
                        for (int i = 0; i < sc; i++) {
257
                                Value val = ds.getFieldValue(i, fieldId);
258

    
259
                                if ((val instanceof NullValue) || (val == null)) {
260
                                        continue;
261
                                }
262

    
263
                                IGeometry geom = adapter.getShape(i);
264

    
265
                                if (geom == null) {
266
                                        continue;
267
                                }
268

    
269
                                FSymbol symbol = l.getSymbol(i);
270

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

    
277
                                for (int j = 0; j < lbls.length; j++) {
278
                                        if (lbls[j] != null) {
279
                                                lbls[j].setString(val.toString());
280

    
281
                                                if (bWithHeightText) {
282
                                                        FloatValue height = (FloatValue) ds.getFieldValue(i,
283
                                                                        idFieldHeightText);
284
                                                        lbls[j].setHeight(height.getValue());
285
                                                } else {
286
                                                        lbls[j].setHeight(symbol.getFontSize());
287
                                                }
288

    
289
                                                if (bWithRotationText) {
290
                                                        DoubleValue rotation = (DoubleValue) ds.getFieldValue(i,
291
                                                                        idFieldRotationText);
292
                                                        lbls[j].setRotation(rotation.getValue());
293
                                                }
294
                                                labels.add(lbls[j]);        
295
                                        }
296

    
297
                                        
298
                                }
299
                        }
300

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

    
314
                FLyrText layerText = new FLyrText(labels);
315

    
316
                try {
317
                        layerText.setLegend((VectorialLegend) getLegend());
318
                } catch (FieldNotFoundException e1) {
319
                        // TODO Auto-generated catch block
320
                        e1.printStackTrace();
321
                } catch (DriverException e1) {
322
                        // TODO Auto-generated catch block
323
                        e1.printStackTrace();
324
                }
325

    
326
                setLayerText(layerText);
327

    
328
                return layerText;
329
        }
330

    
331
        /**
332
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
333
         */
334
        public void removeLabels() {
335
                setLayerText(null);
336
        }
337

    
338
        /**
339
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createIndex()
340
         */
341
        public void createIndex() {
342
        }
343

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

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

    
363
        /**
364
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
365
         */
366
        public void setSelection(FBitSet selection) {
367
                selectionSupport.setSelection(selection);
368
                fireSelectionEvents();
369
        }
370

    
371
        /**
372
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
373
         */
374
        public boolean isSelected(int index) {
375
                return selectionSupport.isSelected(index);
376
        }
377

    
378
        /**
379
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
380
         */
381
        public FBitSet getSelection() {
382
                return selectionSupport.getSelection();
383
        }
384

    
385
        /**
386
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
387
         */
388
        public void clearSelection() {
389
                selectionSupport.clearSelection();
390
        }
391

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

    
398
                return s.queryByRect(rect);
399
        }
400

    
401
        /**
402
         * @throws DriverException
403
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
404
         */
405
        public SelectableDataSource getRecordset() throws DriverException {
406
                if (sds == null){
407
                        try {
408
                                DataSource ds = source.getRecordset();
409

    
410
                                if (ds == null) {
411
                                        return null;
412
                                }
413

    
414
                                sds = new SelectableDataSource(ds);
415
                                sds.setSelectionSupport(selectionSupport);
416
                                
417
                                return sds;
418
                        } catch (DriverLoadException e) {
419
                                throw new DriverException(e);
420
                        }
421
                }
422
                return sds;
423
        }
424

    
425
        /**
426
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
427
         *                 com.iver.cit.gvsig.fmap.rendering.Legend)
428
         */
429
        public void setLegend(VectorialLegend r)
430
                throws DriverException, FieldNotFoundException {
431
                VectorialLegend oldLegend = legend;
432
                legend = r;
433

    
434
                try {
435
                        legend.setDataSource(getRecordset());
436

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

    
454
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, legend);
455
                callLegendChanged(e);
456
        }
457

    
458
        /**
459
         * Devuelve la Leyenda de la capa.
460
         *
461
         * @return Leyenda.
462
         */
463
        public Legend getLegend() {
464
                return legend;
465
        }
466

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

    
487
                return typeShape;
488
        }
489

    
490
        /**
491
         * @throws XMLException
492
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
493
         */
494
        public XMLEntity getXMLEntity() throws XMLException {
495
                XMLEntity xml = super.getXMLEntity();
496
                xml.addChild(legend.getXMLEntity());
497
                xml.addChild(selectionSupport.getXMLEntity());
498

    
499
                if (source instanceof VectorialFileAdapter) {
500
                        xml.putProperty("type", "vectorial");
501
                        xml.putProperty("file", ((VectorialFileAdapter) source).getFile());
502
                        try {
503
                                xml.putProperty("recordset-name", getRecordset().getName());
504
                        } catch (DriverException e) {
505
                                throw new XMLException(e);
506
                        }
507
                } else if (source instanceof VectorialDBAdapter) {
508
                } else if (source instanceof WFSAdapter) {
509
                }
510

    
511
                xml.putProperty("driverName", getSource().getDriver().getName());
512

    
513
                return xml;
514
        }
515

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

    
525
                try {
526
                        // legend.setDataSource(getRecordset());
527
                        setLegend(legend);
528
                } catch (FieldNotFoundException e) {
529
                        throw new XMLException(e);
530
                } catch (DriverException e) {
531
                        throw new XMLException(e);
532
                }        
533
                
534
                selectionSupport.setXMLEntity(xml.getChild(1));
535

    
536
                String recordsetName = xml.getStringProperty("recordset-name");
537
                try {
538
                        LayerFactory.getDataSourceFactory().changeDataSourceName(
539
                                        getRecordset().getName(),
540
                                        recordsetName);
541
                } catch (NoSuchTableException e1) {
542
                        throw new XMLException(e1);
543
                } catch (DriverException e1) {
544
                        throw new XMLException(e1);
545
                }
546
        }
547

    
548
        /**
549
         * A?ade un LegendListener a la lista de Listeners.
550
         *
551
         * @param listener LegendListener.
552
         */
553
        public void addLegendListener(LegendListener listener) {
554
                layerChangeSupport.addLayerListener(listener);
555
        }
556

    
557
        /**
558
         * Llamada al m?todo callLegendChanged de los listener.
559
         *
560
         * @param e Evento.
561
         */
562
        private void callLegendChanged(LegendChangedEvent e) {
563
                layerChangeSupport.callLegendChanged(e);
564
        }
565

    
566
        /**
567
         * Borra un LegendListener de la lista de Listeners
568
         *
569
         * @param listener LegendListener.
570
         */
571
        public void removeLegendListener(LegendListener listener) {
572
                layerChangeSupport.removeLayerListener(listener);
573
        }
574

    
575
        /**
576
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
577
         * identifiquen la capa.
578
         *
579
         * @return DOCUMENT ME!
580
         */
581
        public String toString() {
582
                /*
583
                 * Se usa internamente para que la parte de datos
584
                 * identifique de forma un?voca las tablas
585
                 */
586
                String ret = super.toString();
587

    
588
                return "layer" + ret.substring(ret.indexOf('@') + 1);
589
        }
590
}