Statistics
| Revision:

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

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

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

    
54
import com.hardcode.driverManager.Driver;
55
import com.hardcode.driverManager.DriverLoadException;
56
import com.hardcode.gdbms.engine.data.DataSource;
57
import com.hardcode.gdbms.engine.data.DataSourceFactory;
58
import com.hardcode.gdbms.engine.data.NoSuchTableException;
59
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
60
import com.hardcode.gdbms.engine.values.DoubleValue;
61
import com.hardcode.gdbms.engine.values.FloatValue;
62
import com.hardcode.gdbms.engine.values.NullValue;
63
import com.hardcode.gdbms.engine.values.NumericValue;
64
import com.hardcode.gdbms.engine.values.Value;
65
import com.iver.cit.gvsig.fmap.DriverException;
66
import com.iver.cit.gvsig.fmap.ViewPort;
67
import com.iver.cit.gvsig.fmap.core.IGeometry;
68
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
69
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
70
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
71
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
72
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
73
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
74
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
75
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
76
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
77
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
78
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
81
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
82
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
83
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
84
import com.iver.cit.gvsig.fmap.operations.Cancellable;
85
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
86
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
87
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
88
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
89
import com.iver.cit.gvsig.fmap.rendering.Legend;
90
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
91
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
92
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
93
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
94
import com.iver.utiles.IPersistance;
95
import com.iver.utiles.PostProcessSupport;
96
import com.iver.utiles.XMLEntity;
97
import com.vividsolutions.jts.geom.Coordinate;
98
import com.vividsolutions.jts.geom.Envelope;
99
import com.vividsolutions.jts.index.SpatialIndex;
100
import com.vividsolutions.jts.index.quadtree.Quadtree;
101
import com.vividsolutions.jts.index.strtree.STRtree;
102

    
103

    
104
/**
105
 * Capa b?sica Vectorial.
106
 *
107
 * @author Fernando Gonz?lez Cort?s
108
 */
109

    
110
//TODO Cuando no sea para pruebas debe no ser public
111
public class FLyrVect extends FLyrDefault implements Labelable, ClassifiableVectorial, SingleLayer, VectorialData,
112
        RandomVectorialData,AlphanumericData {
113
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
114

    
115
        /** Leyenda de la capa vectorial */
116
        private VectorialLegend legend;
117
        private int typeShape = -1;
118

    
119
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
120
        private ReadableVectorial source;
121
        //private SelectableDataSource sds;
122

    
123
    private SpatialIndex spatialIndex = null;
124

    
125
    private boolean bHasJoin = false;
126

    
127
        /**
128
         * A?ade un SelectionListener a la lista de listeners.
129
         *
130
         * @param listener SelectionListener.
131
         */
132
        /*public void addSelectionListener(SelectionListener listener) {
133
                try {
134
                        getRecordset().addSelectionListener(listener);
135
                } catch (DriverException e) {
136
                        // TODO Auto-generated catch block
137
                        e.printStackTrace();
138
                }
139
        }
140
*/
141
        /**
142
         * Borra un selectionListener de la lista de listeners.
143
         *
144
         * @param listener SelectionListener
145
         */
146
        /*public void removeSelectionListener(SelectionListener listener) {
147
                try {
148
                        getRecordset().removeSelectionListener(listener);
149
                } catch (DriverException e) {
150
                        // TODO Auto-generated catch block
151
                        e.printStackTrace();
152
                }
153
        }
154
*/
155
        /**
156
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
157
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
158
         * eventos, se realiza la propagaci?n de manera manual al final de la
159
         * "r?faga" de eventos
160
         */
161
        /*public void fireSelectionEvents() {
162
                try {
163
                        getRecordset().fireSelectionEvents();
164
                } catch (DriverException e) {
165
                        // TODO Auto-generated catch block
166
                        e.printStackTrace();
167
                }
168
        }
169
*/
170
        /**
171
         * Devuelve el VectorialAdapater de la capa.
172
         *
173
         * @return VectorialAdapter.
174
         */
175
        public ReadableVectorial getSource() {
176
                return source;
177
        }
178

    
179
        /**
180
         * Inserta el VectorialAdapter a la capa.
181
         *
182
         * @param va VectorialAdapter.
183
         */
184
        public void setSource(ReadableVectorial rv) {
185
                source = rv;
186
        }
187

    
188
        /**
189
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
190
         */
191
        public Rectangle2D getFullExtent() throws DriverException {
192
                try {
193
                        Rectangle2D rAux;
194
                        logger.debug("source.start()");
195
                        source.start();
196
                        rAux = source.getFullExtent();
197
                        logger.debug("source.stop()");
198
                        source.stop();
199

    
200
                        // Si existe reproyecci?n, reproyectar el extent
201
                        ICoordTrans ct = getCoordTrans();
202

    
203
                        if (ct != null) {
204
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
205
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
206
                                pt1 = ct.convert(pt1, null);
207
                                pt2 = ct.convert(pt2, null);
208
                                rAux = new Rectangle2D.Double();
209
                                rAux.setFrameFromDiagonal(pt1, pt2);
210
                        }
211

    
212
                        return rAux;
213
                } catch (DriverIOException e) {
214
                        throw new DriverException(e);
215
                }
216
        }
217

    
218
        /**
219
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
220
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
221
         */
222
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
223
                Cancellable cancel,double scale) throws DriverException {
224

    
225
                if (isWithinScale(scale)){
226
            // Las que solo tienen etiquetado sin pintar el shape,
227
            // no pasamos por ellas
228
            boolean bDrawShapes = true;
229
            if (legend instanceof SingleSymbolLegend)
230
            {
231
                if (legend.getDefaultSymbol().isShapeVisible() == false)
232
                    bDrawShapes = false;
233
            }
234
            if (bDrawShapes)
235
            {
236
                Strategy strategy = StrategyManager.getStrategy(this);
237
                try{
238
                strategy.draw(image, g, viewPort, cancel);
239
                }catch (DriverException e) {
240
                                        this.setVisible(false);
241
                                        this.setActive(false);
242
                                        throw e;
243
                                }
244
            }
245

    
246
                    if (getVirtualLayers() != null) {
247
                            getVirtualLayers().draw(image, g, viewPort, cancel,scale);
248
                    }
249

    
250
                    if (getLayerText() != null) {
251
                            getLayerText().draw(image, g, viewPort, cancel,scale);
252
                    }
253
                }
254
        }
255

    
256
        /**
257
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
258
         *                 com.iver.cit.gvsig.fmap.ViewPort,
259
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
260
         */
261
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
262
                throws DriverException {
263
                if (isVisible() && isWithinScale(scale)){
264
                Strategy strategy = StrategyManager.getStrategy(this);
265

    
266
                strategy.print(g, viewPort, cancel);
267

    
268
                if (getLayerText() != null) {
269
                        getLayerText().draw(null, g, viewPort, cancel,scale);
270
                }
271
                }
272
        }
273

    
274
        /**
275
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
276
         */
277
        // public FLayer createLabelLayer(int fieldId) {
278
    public FLayer createLabelLayer(SelectableDataSource ds) {
279
                FLyrText layerText = null;
280
        try {
281
            layerText = new FLyrText();
282
                        layerText.setLegend((VectorialLegend) getLegend());
283
            layerText.createLabels(this);
284
                } catch (FieldNotFoundException e1) {
285
                        // TODO Auto-generated catch block
286
                        e1.printStackTrace();
287
                } catch (DriverException e1) {
288
                        // TODO Auto-generated catch block
289
                        e1.printStackTrace();
290
        }
291

    
292

    
293
                setLayerText(layerText);
294
                layerText.setCoordTrans(getCoordTrans());
295
                return layerText;
296
        }
297

    
298
        /**
299
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
300
         */
301
        public void removeLabels() {
302
                setLayerText(null);
303
        }
304

    
305
    /* (non-Javadoc)
306
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#deleteSpatialIndex()
307
     */
308
    public void deleteSpatialIndex()
309
    {
310
        spatialIndex = null;
311
    }
312
        /* (non-Javadoc)
313
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
314
         */
315
        public void createSpatialIndex() {
316
        // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
317
        // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
318
        // para que acepten recorrer sin geometria, solo con rectangulos.
319
        spatialIndex = new Quadtree();
320
        ReadableVectorial va = getSource();
321
        ICoordTrans ct = getCoordTrans();
322
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
323
        try {
324
            va.start();
325

    
326
            for (int i=0; i < va.getShapeCount(); i++)
327
            {
328
                Rectangle2D r = shapeBounds.getShapeBounds(i);
329
                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
330
                if (ct != null) {
331
                    r = ct.convert(r);
332
                }
333
                Coordinate c1 = new Coordinate(r.getMinX(), r.getMinY());
334
                Coordinate c2 = new Coordinate(r.getMaxX(), r.getMaxY());
335
                Envelope env = new Envelope(c1, c2);
336
                spatialIndex.insert(env, new Integer(i));
337
            } // for
338
            va.stop();
339
        } catch (DriverIOException e) {
340
            // TODO Auto-generated catch block
341
            e.printStackTrace();
342
        } catch (IOException e) {
343
            // TODO Auto-generated catch block
344
            e.printStackTrace();
345
        }
346

    
347
        }
348

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

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

    
368
        /**
369
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
370
         */
371
        /*public void setSelection(FBitSet selection) {
372
                try {
373
                        getRecordset().setSelection(selection);
374
                } catch (DriverException e) {
375
                        // TODO Auto-generated catch block
376
                        e.printStackTrace();
377
                }
378
                fireSelectionEvents();
379
        }
380
*/
381
        /**
382
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
383
         */
384
        /*public boolean isSelected(int index) {
385
                try {
386
                        return getRecordset().isSelected(index);
387
                } catch (DriverException e) {
388
                        // TODO Auto-generated catch block
389
                        e.printStackTrace();
390
                }
391
                return false;
392
        }
393
*/
394
        /**
395
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
396
         */
397
        /*public FBitSet getSelection() {
398
                try {
399
                        return getRecordset().getSelection();
400
                } catch (DriverException e) {
401
                        // TODO Auto-generated catch block
402
                        e.printStackTrace();
403
                }
404
                return new FBitSet();
405
        }
406
*/
407
        /**
408
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
409
         */
410
        /*public void clearSelection() {
411
                try {
412
                        getRecordset().clearSelection();
413
                } catch (DriverException e) {
414
                        // TODO Auto-generated catch block
415
                        e.printStackTrace();
416
                }
417
        }*/
418

    
419
        /* (non-Javadoc)
420
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
421
         */
422
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
423
                Strategy s = StrategyManager.getStrategy(this);
424

    
425
                return s.queryByRect(rect);
426
        }
427

    
428
    public FBitSet queryByPoint(Point2D p, double tolerance) throws DriverException
429
    {
430
        Strategy s = StrategyManager.getStrategy(this);
431
        return s.queryByPoint(p, tolerance);
432
    }
433

    
434
    public FBitSet queryByShape(IGeometry g, int relationship) throws DriverException, VisitException
435
    {
436
        Strategy s = StrategyManager.getStrategy(this);
437
        return s.queryByShape(g, relationship);
438
    }
439
        /**
440
         * @throws DriverException
441
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
442
         */
443
        /*public SelectableDataSource getRecordset() throws DriverException {
444
                if (sds == null){
445
                        try {
446
                                DataSource ds = getSource().getRecordset();
447

448
                                if (ds == null) {
449
                                        return null;
450
                                }
451

452
                                sds = new SelectableDataSource(ds);
453
                                //sds.setSelectionSupport(selectionSupport);
454

455
                                return sds;
456
                        } catch (DriverLoadException e) {
457
                                throw new DriverException(e);
458
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
459
                                throw new DriverException(e);
460
            }
461
                }
462
                return sds;
463
        }
464
*/
465
    /**
466
     * Para cuando haces una uni?n, sustituyes el recorset por el nuevo.
467
     * De esta forma, podr?s poner leyendas basadas en el nuevo
468
     * recordset
469
     * @param newSds
470
     * @throws DriverException
471
     * @throws FieldNotFoundException
472
     * @throws FieldNotFoundException
473
     */
474
 /*   public void setRecordset(SelectableDataSource newSds) throws DriverException, FieldNotFoundException
475
    {
476
        sds = newSds;
477
        //sds.setSelectionSupport(selectionSupport);
478
        legend.setDataSource(sds);
479
        logger.debug("Recordset cambiado a " + sds.getName());
480
    }
481
*/
482
        /**
483
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
484
         *                 com.iver.cit.gvsig.fmap.rendering.Legend)
485
         */
486
        public void setLegend(VectorialLegend r)
487
                throws DriverException, FieldNotFoundException {
488
                VectorialLegend oldLegend = legend;
489
                legend = r;
490

    
491
                try {
492
                        legend.setDataSource(getRecordset());
493

    
494
                        if (legend.getLabelField() != null) {
495
                            // sds.start();
496
                                // int idLabelField = getRecordset().getFieldIndexByName(legend.getLabelField());
497
                                createLabelLayer(getSource().getRecordset());
498
                                // sds.stop();
499
                        }
500
                        else
501
                            removeLabels();
502
                } catch (DriverException e) {
503
                        throw new DriverException(e);
504
                } catch (FieldNotFoundException e) {
505
                        // TODO Auto-generated catch block
506
                        e.printStackTrace();
507
        } catch (DriverLoadException e) {
508
                        // TODO Auto-generated catch block
509
                        e.printStackTrace();
510
                }
511

    
512
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, legend);
513
                callLegendChanged(e);
514
        }
515

    
516
        /**
517
         * Devuelve la Leyenda de la capa.
518
         *
519
         * @return Leyenda.
520
         */
521
        public Legend getLegend() {
522
                return legend;
523
        }
524

    
525
        /**
526
         * Devuelve el tipo de shape que contiene la capa.
527
         *
528
         * @return tipo de shape.
529
         *
530
         * @throws DriverException
531
         */
532
        public int getShapeType() throws DriverException {
533
                if (typeShape == -1) {
534
                        try {
535
                                logger.debug("source.start()");
536
                                source.start();
537
                                typeShape = source.getShapeType();
538
                                logger.debug("source.stop()");
539
                                source.stop();
540
                        } catch (DriverIOException e) {
541
                                throw new DriverException(e);
542
                        }
543
                }
544

    
545
                return typeShape;
546
        }
547

    
548
        /**
549
         * @throws XMLException
550
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
551
         */
552
        public XMLEntity getXMLEntity() throws XMLException {
553
                XMLEntity xml = super.getXMLEntity();
554
        xml.addChild(legend.getXMLEntity());
555
        //try {
556
                        xml.addChild(getRecordset().getSelectionSupport().getXMLEntity());
557

    
558
                //} catch (DriverException e1) {
559
                        // TODO Auto-generated catch block
560
                //        e1.printStackTrace();
561
                //}
562
                if (source instanceof VectorialEditableAdapter){
563
                        source=((VectorialEditableAdapter)source).getOriginalAdapter();
564
                }
565
                if (source instanceof VectorialFileAdapter) {
566
                        xml.putProperty("type", "vectorial");
567
                        xml.putProperty("file", ((VectorialFileAdapter) source).getFile());
568
                        //try {
569
                                try {
570
                                        xml.putProperty("recordset-name", getSource().getRecordset().getName());
571
                                } catch (DriverLoadException e) {
572
                                        // TODO Auto-generated catch block
573
                                        e.printStackTrace();
574
                                }
575
                        //} catch (DriverException e) {
576
                        //        throw new XMLException(e);
577
                        //}
578
                } else if (source instanceof VectorialDBAdapter) {
579
            xml.putProperty("type", "vectorial");
580

    
581
            VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) source.getDriver();
582

    
583
            // Guardamos el nombre del driver para poder recuperarlo
584
            // con el DriverManager de Fernando.
585
            xml.putProperty("db", dbDriver.getName());
586
            //try {
587
                try {
588
                                        xml.putProperty("recordset-name", getSource().getRecordset().getName());
589
                                } catch (DriverLoadException e) {
590
                                        // TODO Auto-generated catch block
591
                                        e.printStackTrace();
592
                                }
593
            //} catch (DriverException e) {
594
            //    throw new XMLException(e);
595
            //}
596
            xml.addChild(dbDriver.getXMLEntity()); // Tercer child. Antes hemos metido la leyenda y el selection support
597
        } else if (source instanceof VectorialAdapter) {
598
            // Se supone que hemos hecho algo gen?rico.
599
            xml.putProperty("type", "vectorial");
600

    
601
            VectorialDriver driver = (VectorialDriver) source.getDriver();
602

    
603
            // Guardamos el nombre del driver para poder recuperarlo
604
            // con el DriverManager de Fernando.
605
            xml.putProperty("other", driver.getName());
606
            //try {
607
                try {
608
                                        xml.putProperty("recordset-name", getSource().getRecordset().getName());
609
                                } catch (DriverLoadException e) {
610
                                        // TODO Auto-generated catch block
611
                                        e.printStackTrace();
612
                                }
613
            //} catch (DriverException e) {
614
            //    throw new XMLException(e);
615
            //}
616
            if (driver instanceof IPersistance)
617
            {
618
                // xml.putProperty("className", driver.getClass().getName());
619
                IPersistance persist = (IPersistance) driver;
620
                xml.addChild(persist.getXMLEntity()); // Tercer child. Antes hemos metido la leyenda y el selection support
621
            }
622
                }
623
                xml.putProperty("driverName", getSource().getDriver().getName());
624
        if (bHasJoin)
625
            xml.putProperty("hasJoin", "true");
626

    
627

    
628
                return xml;
629
        }
630

    
631
        /**
632
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
633
         */
634
        public void setXMLEntity03(XMLEntity xml)
635
                throws XMLException {
636

    
637
                super.setXMLEntity(xml);
638
                legend = LegendFactory.createFromXML03(xml.getChild(0));
639

    
640
                try {
641
                        // legend.setDataSource(getRecordset());
642
                        setLegend(legend);
643
                } catch (FieldNotFoundException e) {
644
                        throw new XMLException(e);
645
                } catch (DriverException e) {
646
                        throw new XMLException(e);
647
                }
648

    
649
                //try {
650
                                getRecordset().getSelectionSupport().setXMLEntity03(xml.getChild(1));
651
                //} catch (DriverException e) {
652
                        // TODO Auto-generated catch block
653
                //        e.printStackTrace();
654
                //}
655
        }
656

    
657
        /**
658
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
659
         */
660
        public void setXMLEntity(XMLEntity xml)
661
                throws XMLException {
662
        super.setXMLEntity(xml);
663

    
664
                legend = LegendFactory.createFromXML(xml.getChild(0));
665
                                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
666

    
667

    
668
                String recordsetName = xml.getStringProperty("recordset-name");
669
                try {
670
                        LayerFactory.getDataSourceFactory().changeDataSourceName(
671
                   getSource().getRecordset().getName(), recordsetName
672
                                        );
673
            // Hacemos que el recordset que hemos creado antes (los
674
            // recordset se crean antes que todo) sea el recordset
675
            // de esta capa. Y del que se crea automaticamente
676
            // en el setLegend del createLayer, nos olvidamos
677
                        SelectableDataSource sds=new SelectableDataSource(LayerFactory.getDataSourceFactory()
678
                    .createRandomDataSource(recordsetName, DataSourceFactory.AUTOMATIC_OPENING));
679
                        //sds.setSelectionSupport(selectionSupport);
680
            //((EditableAdapter)getSource()).setRecordSet(sds);
681
                } catch (NoSuchTableException e1) {
682
                        throw new XMLException(e1);
683
                }  catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
684
            throw new XMLException(e1);
685
        } catch (DriverLoadException e1) {
686
            throw new XMLException(e1);
687
        }
688
        // Si tiene una uni?n, lo marcamos para que no se cree la leyenda hasta el final
689
        // de la lectura del proyecto
690
        if (xml.contains("hasJoin"))
691
        {
692
            setIsJoined(true);
693
            PostProcessSupport.addToPostProcess(this,"setLegend", legend, 1);
694
        }
695
        else
696
        {
697
            try {
698
                // legend.setDataSource(getRecordset());
699
                setLegend(legend);
700
            } catch (FieldNotFoundException e) {
701
                throw new XMLException(e);
702
            } catch (DriverException e) {
703
                throw new XMLException(e);
704
            }
705
        }
706

    
707
        }
708

    
709
        /**
710
         * A?ade un LegendListener a la lista de Listeners.
711
         *
712
         * @param listener LegendListener.
713
         */
714
        public void addLegendListener(LegendListener listener) {
715
                layerChangeSupport.addLayerListener(listener);
716
        }
717

    
718
        /**
719
         * Llamada al m?todo callLegendChanged de los listener.
720
         *
721
         * @param e Evento.
722
         */
723
        private void callLegendChanged(LegendChangedEvent e) {
724
                layerChangeSupport.callLegendChanged(e);
725
        }
726

    
727
        /**
728
         * Borra un LegendListener de la lista de Listeners
729
         *
730
         * @param listener LegendListener.
731
         */
732
        public void removeLegendListener(LegendListener listener) {
733
                layerChangeSupport.removeLayerListener(listener);
734
        }
735

    
736
        /**
737
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
738
         * identifiquen la capa.
739
         *
740
         * @return DOCUMENT ME!
741
         */
742
        public String toString() {
743
                /*
744
                 * Se usa internamente para que la parte de datos
745
                 * identifique de forma un?voca las tablas
746
                 */
747
                String ret = super.toString();
748

    
749
                return "layer" + ret.substring(ret.indexOf('@') + 1);
750
        }
751

    
752
    public boolean isJoined() {
753
        return bHasJoin;
754
    }
755

    
756
    public void setIsJoined(boolean hasJoin) {
757
        bHasJoin = hasJoin;
758
    }
759

    
760
    /**
761
     * @return Returns the spatialIndex.
762
     */
763
    public SpatialIndex getSpatialIndex() {
764
        return spatialIndex;
765
    }
766

    
767
        public SelectableDataSource getRecordset() {
768
                try {
769
                        return getSource().getRecordset();
770
                } catch (DriverLoadException e) {
771
                        // TODO Auto-generated catch block
772
                        e.printStackTrace();
773
                }
774
                return null;
775
        }
776

    
777
        /*public SelectionSupport getSelectionSupport() {
778
                return selectionSupport;
779
        }
780

781
        public void setSelectionSupport(SelectionSupport selectionSupport) {
782
                this.selectionSupport = selectionSupport;
783
        }*/
784
}