Statistics
| Revision:

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

History | View | Annotate | Download (17.2 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.DriverAttributes;
67
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
68
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
69
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
76
import com.iver.cit.gvsig.fmap.operations.Cancellable;
77
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
78
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
79
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
80
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
81
import com.iver.cit.gvsig.fmap.rendering.Legend;
82
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
83
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
84
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
85
import com.iver.utiles.XMLEntity;
86

    
87

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
235
                        //VectorialDriver driver = (VectorialDriver) adapter.getDriver();
236
                        int sc;
237

    
238
                        sc = adapter.getShapeCount();
239

    
240
            DriverAttributes attr = adapter.getDriverAttributes();
241
            boolean bMustClone = false;
242
            if (attr != null)
243
            {
244
                if (attr.isLoadedInMemory())
245
                {
246
                    bMustClone = attr.isLoadedInMemory();               
247
                }
248
            }
249
            ICoordTrans ct = getCoordTrans();
250
            
251
                        VectorialLegend l = (VectorialLegend) getLegend();
252
                        int idFieldHeightText = -1;
253
                        int idFieldRotationText = -1;
254
                        boolean bWithHeightText = false;
255

    
256
                        if (l.getLabelHeightField() != null) {
257
                                bWithHeightText = true;
258
                                idFieldHeightText = ds.getFieldIndexByName(l.getLabelHeightField());
259
                        }
260

    
261
                        boolean bWithRotationText = false;
262

    
263
                        if (l.getLabelRotationField() != null) {
264
                                bWithRotationText = true;
265
                                idFieldRotationText = ds.getFieldIndexByName(l.getLabelRotationField());
266
                        }
267

    
268
                        for (int i = 0; i < sc; i++) {
269
                                Value val = ds.getFieldValue(i, fieldId);
270

    
271
                                if ((val instanceof NullValue) || (val == null)) {
272
                                        continue;
273
                                }
274

    
275
                                IGeometry geom = adapter.getShape(i);
276

    
277
                                if (geom == null) {
278
                                        continue;
279
                                }
280
                if (ct != null) {
281
                    if (bMustClone)
282
                        geom = geom.cloneGeometry();
283
                    geom.reProject(ct);
284
                }
285

    
286
                                FSymbol symbol = l.getSymbol(i);
287

    
288
                                // TODO: El m?todo contenedor (createLabelLayer) debe recoger
289
                                // los par?metros de posicionamiento y de allowDuplicates
290
                                // if (i >= 328)
291
                                //         System.out.println("i= " + i + " " + val.toString());
292
                                FLabel[] lbls = geom.createLabels(0, true);
293

    
294
                                for (int j = 0; j < lbls.length; j++) {
295
                                        if (lbls[j] != null) {
296
                                                lbls[j].setString(val.toString());
297

    
298
                                                if (bWithHeightText) {
299
                                                        FloatValue height = (FloatValue) ds.getFieldValue(i,
300
                                                                        idFieldHeightText);
301
                                                        lbls[j].setHeight(height.getValue());
302
                                                } else {
303
                                                        lbls[j].setHeight(symbol.getFontSize());
304
                                                }
305

    
306
                                                if (bWithRotationText) {
307
                                                        DoubleValue rotation = (DoubleValue) ds.getFieldValue(i,
308
                                                                        idFieldRotationText);
309
                                                        lbls[j].setRotation(rotation.getValue());
310
                                                }
311
                                                labels.add(lbls[j]);        
312
                                        }
313

    
314
                                        
315
                                }
316
                        }
317

    
318
                        long t2 = System.currentTimeMillis();
319
                        logger.debug("adapter.stop()");
320
                        ds.stop();
321
                        adapter.stop();
322
                } catch (DriverIOException e) {
323
                        e.printStackTrace();
324
                } catch (DriverException e) {
325
                        e.printStackTrace();
326
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
327
                        // TODO Auto-generated catch block
328
                        e.printStackTrace();
329
                }
330

    
331
                FLyrText layerText = new FLyrText(labels);
332

    
333
                try {
334
                        layerText.setLegend((VectorialLegend) getLegend());
335
                } catch (FieldNotFoundException e1) {
336
                        // TODO Auto-generated catch block
337
                        e1.printStackTrace();
338
                } catch (DriverException e1) {
339
                        // TODO Auto-generated catch block
340
                        e1.printStackTrace();
341
                }
342

    
343
                setLayerText(layerText);
344

    
345
                return layerText;
346
        }
347

    
348
        /**
349
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
350
         */
351
        public void removeLabels() {
352
                setLayerText(null);
353
        }
354

    
355
        /**
356
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createIndex()
357
         */
358
        public void createIndex() {
359
        }
360

    
361
        /**
362
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
363
         *                 com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
364
         */
365
        public void process(FeatureVisitor visitor, BitSet subset)
366
                throws DriverException, VisitException {
367
                Strategy s = StrategyManager.getStrategy(this);
368
                s.process(visitor, subset);
369
        }
370

    
371
        /**
372
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
373
         */
374
        public void process(FeatureVisitor visitor)
375
                throws DriverException, VisitException {
376
                Strategy s = StrategyManager.getStrategy(this);
377
                s.process(visitor);
378
        }
379

    
380
        /**
381
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
382
         */
383
        public void setSelection(FBitSet selection) {
384
                selectionSupport.setSelection(selection);
385
                fireSelectionEvents();
386
        }
387

    
388
        /**
389
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
390
         */
391
        public boolean isSelected(int index) {
392
                return selectionSupport.isSelected(index);
393
        }
394

    
395
        /**
396
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
397
         */
398
        public FBitSet getSelection() {
399
                return selectionSupport.getSelection();
400
        }
401

    
402
        /**
403
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
404
         */
405
        public void clearSelection() {
406
                selectionSupport.clearSelection();
407
        }
408

    
409
        /**
410
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#queryByRect(java.awt.geom.Rectangle2D)
411
         */
412
        public BitSet queryByRect(Rectangle2D rect) throws DriverException {
413
                Strategy s = StrategyManager.getStrategy(this);
414

    
415
                return s.queryByRect(rect);
416
        }
417

    
418
        /**
419
         * @throws DriverException
420
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
421
         */
422
        public SelectableDataSource getRecordset() throws DriverException {
423
                if (sds == null){
424
                        try {
425
                                DataSource ds = source.getRecordset();
426

    
427
                                if (ds == null) {
428
                                        return null;
429
                                }
430

    
431
                                sds = new SelectableDataSource(ds);
432
                                sds.setSelectionSupport(selectionSupport);
433
                                
434
                                return sds;
435
                        } catch (DriverLoadException e) {
436
                                throw new DriverException(e);
437
                        }
438
                }
439
                return sds;
440
        }
441

    
442
        /**
443
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
444
         *                 com.iver.cit.gvsig.fmap.rendering.Legend)
445
         */
446
        public void setLegend(VectorialLegend r)
447
                throws DriverException, FieldNotFoundException {
448
                VectorialLegend oldLegend = legend;
449
                legend = r;
450

    
451
                try {
452
                        legend.setDataSource(getRecordset());
453

    
454
                        if (legend.getLabelField() != null) {
455
                            sds.start();
456
                                int idLabelField = getRecordset().getFieldIndexByName(legend.getLabelField());
457
                                createLabelLayer(idLabelField);
458
                                sds.stop();
459
                        }
460
                        else
461
                            removeLabels();
462
                } catch (DriverException e) {
463
                        throw new DriverException(e);
464
                } catch (FieldNotFoundException e) {
465
                        // TODO Auto-generated catch block
466
                        e.printStackTrace();
467
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
468
                        throw new DriverException(e);
469
        }
470

    
471
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, legend);
472
                callLegendChanged(e);
473
        }
474

    
475
        /**
476
         * Devuelve la Leyenda de la capa.
477
         *
478
         * @return Leyenda.
479
         */
480
        public Legend getLegend() {
481
                return legend;
482
        }
483

    
484
        /**
485
         * Devuelve el tipo de shape que contiene la capa.
486
         *
487
         * @return tipo de shape.
488
         *
489
         * @throws DriverException
490
         */
491
        public int getShapeType() throws DriverException {
492
                if (typeShape == -1) {
493
                        try {
494
                                logger.debug("source.start()");
495
                                source.start();
496
                                typeShape = source.getShapeType();
497
                                logger.debug("source.stop()");
498
                                source.stop();
499
                        } catch (DriverIOException e) {
500
                                throw new DriverException(e);
501
                        }
502
                }
503

    
504
                return typeShape;
505
        }
506

    
507
        /**
508
         * @throws XMLException
509
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
510
         */
511
        public XMLEntity getXMLEntity() throws XMLException {
512
                XMLEntity xml = super.getXMLEntity();
513
                xml.addChild(legend.getXMLEntity());
514
                xml.addChild(selectionSupport.getXMLEntity());
515

    
516
                if (source instanceof VectorialFileAdapter) {
517
                        xml.putProperty("type", "vectorial");
518
                        xml.putProperty("file", ((VectorialFileAdapter) source).getFile());
519
                        try {
520
                                xml.putProperty("recordset-name", getRecordset().getName());
521
                        } catch (DriverException e) {
522
                                throw new XMLException(e);
523
                        }
524
                } else if (source instanceof VectorialDBAdapter) {
525
                } else if (source instanceof WFSAdapter) {
526
                }
527

    
528
                xml.putProperty("driverName", getSource().getDriver().getName());
529

    
530
                return xml;
531
        }
532

    
533
        /**
534
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
535
         */
536
        public void setXMLEntity03(XMLEntity xml)
537
                throws XMLException {
538
                
539
                super.setXMLEntity(xml);
540
                legend = LegendFactory.createFromXML03(xml.getChild(0));
541

    
542
                try {
543
                        // legend.setDataSource(getRecordset());
544
                        setLegend(legend);
545
                } catch (FieldNotFoundException e) {
546
                        throw new XMLException(e);
547
                } catch (DriverException e) {
548
                        throw new XMLException(e);
549
                }        
550

    
551
                selectionSupport.setXMLEntity03(xml.getChild(1));
552
        }
553

    
554
        /**
555
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
556
         */
557
        public void setXMLEntity(XMLEntity xml)
558
                throws XMLException {
559
                
560
                super.setXMLEntity(xml);
561
                legend = LegendFactory.createFromXML(xml.getChild(0));
562

    
563
                try {
564
                        // legend.setDataSource(getRecordset());
565
                        setLegend(legend);
566
                } catch (FieldNotFoundException e) {
567
                        throw new XMLException(e);
568
                } catch (DriverException e) {
569
                        throw new XMLException(e);
570
                }        
571
                
572
                selectionSupport.setXMLEntity(xml.getChild(1));
573

    
574
                String recordsetName = xml.getStringProperty("recordset-name");
575
                try {
576
                        LayerFactory.getDataSourceFactory().changeDataSourceName(
577
                                        getRecordset().getName(),
578
                                        recordsetName);
579
                } catch (NoSuchTableException e1) {
580
                        throw new XMLException(e1);
581
                } catch (DriverException e1) {
582
                        throw new XMLException(e1);
583
                }
584
        }
585

    
586
        /**
587
         * A?ade un LegendListener a la lista de Listeners.
588
         *
589
         * @param listener LegendListener.
590
         */
591
        public void addLegendListener(LegendListener listener) {
592
                layerChangeSupport.addLayerListener(listener);
593
        }
594

    
595
        /**
596
         * Llamada al m?todo callLegendChanged de los listener.
597
         *
598
         * @param e Evento.
599
         */
600
        private void callLegendChanged(LegendChangedEvent e) {
601
                layerChangeSupport.callLegendChanged(e);
602
        }
603

    
604
        /**
605
         * Borra un LegendListener de la lista de Listeners
606
         *
607
         * @param listener LegendListener.
608
         */
609
        public void removeLegendListener(LegendListener listener) {
610
                layerChangeSupport.removeLayerListener(listener);
611
        }
612

    
613
        /**
614
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
615
         * identifiquen la capa.
616
         *
617
         * @return DOCUMENT ME!
618
         */
619
        public String toString() {
620
                /*
621
                 * Se usa internamente para que la parte de datos
622
                 * identifique de forma un?voca las tablas
623
                 */
624
                String ret = super.toString();
625

    
626
                return "layer" + ret.substring(ret.indexOf('@') + 1);
627
        }
628
}