Statistics
| Revision:

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

History | View | Annotate | Download (15.7 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
                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
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
236
                        int sc;
237

    
238
                        sc = adapter.getShapeCount();
239

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

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

    
250
                        boolean bWithRotationText = false;
251

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

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

    
260
                                if (val instanceof NullValue) {
261
                                        continue;
262
                                }
263

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

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

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

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

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

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

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

    
297
                                        labels.add(lbls[j]);
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.DriverException e) {
310
                        // TODO Auto-generated catch block
311
                        e.printStackTrace();
312
                } catch (FieldNotFoundException e) {
313
                        // TODO Auto-generated catch block
314
                        e.printStackTrace();
315
                }
316

    
317
                FLyrText layerText = new FLyrText(labels);
318

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

    
329
                setLayerText(layerText);
330

    
331
                return layerText;
332
        }
333

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
426
                return sds;
427
        }
428

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

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

    
441
                        if (legend.getLabelField() != null) {
442
                                int idLabelField = getRecordset().getFieldIndexByName(legend.getLabelField());
443
                                createLabelLayer(idLabelField);
444
                        }
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.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
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
492
         */
493
        public XMLEntity getXMLEntity() {
494
                XMLEntity xml = super.getXMLEntity();
495
                xml.addChild(legend.getXMLEntity());
496
                xml.addChild(selectionSupport.getXMLEntity());
497

    
498
                if (source instanceof VectorialFileAdapter) {
499
                        xml.putProperty("file", ((VectorialFileAdapter) source).getFile());
500
                } else if (source instanceof VectorialDBAdapter) {
501
                } else if (source instanceof WFSAdapter) {
502
                }
503

    
504
                xml.putProperty("driverName", getSource().getDriver().getName());
505

    
506
                return xml;
507
        }
508

    
509
        /**
510
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
511
         */
512
        public void setXMLEntity(XMLEntity xml)
513
                throws XMLException {
514
                
515
                super.setXMLEntity(xml);
516
                legend = LegendFactory.createFromXML(xml.getChild(0));
517

    
518
                try {
519
                        legend.setDataSource(getRecordset());
520
                } catch (FieldNotFoundException e) {
521
                        throw new XMLException(e);
522
                } catch (DriverException e) {
523
                        throw new XMLException(e);
524
                }        
525

    
526
                selectionSupport.setXMLEntity(xml.getChild(1));
527
        }
528

    
529
        /**
530
         * A?ade un LegendListener a la lista de Listeners.
531
         *
532
         * @param listener LegendListener.
533
         */
534
        public void addLegendListener(LegendListener listener) {
535
                layerChangeSupport.addLayerListener(listener);
536
        }
537

    
538
        /**
539
         * Llamada al m?todo callLegendChanged de los listener.
540
         *
541
         * @param e Evento.
542
         */
543
        private void callLegendChanged(LegendChangedEvent e) {
544
                layerChangeSupport.callLegendChanged(e);
545
        }
546

    
547
        /**
548
         * Borra un LegendListener de la lista de Listeners
549
         *
550
         * @param listener LegendListener.
551
         */
552
        public void removeLegendListener(LegendListener listener) {
553
                layerChangeSupport.removeLayerListener(listener);
554
        }
555

    
556
        /**
557
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#changeRecordsetName()
558
         */
559
        public void changeRecordsetName(String newName) throws DriverException {
560
                source.changeRecordsetName(newName);
561
        }
562

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

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