Statistics
| Revision:

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

History | View | Annotate | Download (24.5 KB)

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

    
43
import 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

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

    
52
import com.hardcode.driverManager.DriverLoadException;
53
import com.hardcode.gdbms.engine.data.DataSourceFactory;
54
import com.hardcode.gdbms.engine.data.NoSuchTableException;
55
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
56
import com.iver.cit.gvsig.fmap.DriverException;
57
import com.iver.cit.gvsig.fmap.ViewPort;
58
import com.iver.cit.gvsig.fmap.MapControl.CancelDraw;
59
import com.iver.cit.gvsig.fmap.core.IGeometry;
60
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
63
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
64
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
65
import com.iver.cit.gvsig.fmap.edition.EditionException;
66
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
67
import com.iver.cit.gvsig.fmap.edition.VectorialEditableDBAdapter;
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.SingleLayer;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
74
import com.iver.cit.gvsig.fmap.operations.Cancellable;
75
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
76
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
77
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
78
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
79
import com.iver.cit.gvsig.fmap.rendering.Legend;
80
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
81
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
82
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
83
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
84
import com.iver.utiles.IPersistance;
85
import com.iver.utiles.PostProcessSupport;
86
import com.iver.utiles.XMLEntity;
87
import com.vividsolutions.jts.geom.Coordinate;
88
import com.vividsolutions.jts.geom.Envelope;
89
import com.vividsolutions.jts.index.SpatialIndex;
90
import com.vividsolutions.jts.index.quadtree.Quadtree;
91

    
92

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

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

    
104
        /** Leyenda de la capa vectorial */
105
        private VectorialLegend legend;
106
        private int typeShape = -1;
107

    
108
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
109
        private ReadableVectorial source;
110
        //private SelectableDataSource sds;
111

    
112
    protected SpatialIndex spatialIndex = null;
113

    
114
    private boolean bHasJoin = false;
115

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

    
168
        /**
169
         * Inserta el VectorialAdapter a la capa.
170
         *
171
         * @param va VectorialAdapter.
172
         */
173
        public void setSource(ReadableVectorial rv) {
174
                source = rv;
175
        }
176

    
177
        /**
178
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
179
         */
180
        public Rectangle2D getFullExtent() throws DriverException {
181
                try {
182
                        Rectangle2D rAux;
183
                        logger.debug("source.start()");
184
                        source.start();
185
                        rAux = source.getFullExtent();
186
                        logger.debug("source.stop()");
187
                        source.stop();
188

    
189
                        // Si existe reproyecci?n, reproyectar el extent
190
                        ICoordTrans ct = getCoordTrans();
191

    
192
                        if (ct != null) {
193
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
194
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
195
                                pt1 = ct.convert(pt1, null);
196
                                pt2 = ct.convert(pt2, null);
197
                                rAux = new Rectangle2D.Double();
198
                                rAux.setFrameFromDiagonal(pt1, pt2);
199
                        }
200

    
201
                        return rAux;
202
                } catch (DriverIOException e) {
203
                        throw new DriverException(e);
204
                }
205
        }
206

    
207
        /**
208
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
209
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
210
         */
211
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
212
                Cancellable cancel,double scale) throws DriverException {
213

    
214
                if (isWithinScale(scale)){
215
            // Las que solo tienen etiquetado sin pintar el shape,
216
            // no pasamos por ellas
217
            boolean bDrawShapes = true;
218
            if (legend instanceof SingleSymbolLegend)
219
            {
220
                if (legend.getDefaultSymbol().isShapeVisible() == false)
221
                    bDrawShapes = false;
222
            }
223
            if (bDrawShapes)
224
            {
225
                Strategy strategy = StrategyManager.getStrategy(this);
226
                try{
227
                strategy.draw(image, g, viewPort, cancel);
228
                }catch (DriverException e) {
229
                                        this.setVisible(false);
230
                                        this.setActive(false);
231
                                        throw e;
232
                                }
233
            }
234

    
235
                    if (getVirtualLayers() != null) {
236
                            getVirtualLayers().draw(image, g, viewPort, cancel,scale);
237
                    }
238

    
239
                    if (getLayerText() != null) {
240
                            getLayerText().draw(image, g, viewPort, cancel,scale);
241
                    }
242
                }
243
        }
244

    
245
        /**
246
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
247
         *                 com.iver.cit.gvsig.fmap.ViewPort,
248
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
249
         */
250
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
251
                throws DriverException {
252
                if (isVisible() && isWithinScale(scale)){
253
                Strategy strategy = StrategyManager.getStrategy(this);
254

    
255
                strategy.print(g, viewPort, cancel);
256

    
257
                if (getLayerText() != null) {
258
                        getLayerText().draw(null, g, viewPort, cancel,scale);
259
                }
260
                }
261
        }
262

    
263
        /**
264
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
265
         */
266
        // public FLayer createLabelLayer(int fieldId) {
267
    public FLayer createLabelLayer(SelectableDataSource ds) {
268
                FLyrText layerText = null;
269
        try {
270
            layerText = new FLyrText();
271
                        layerText.setLegend((VectorialLegend) getLegend());
272
            layerText.createLabels(this);
273
                } catch (FieldNotFoundException e1) {
274
                        // TODO Auto-generated catch block
275
                        e1.printStackTrace();
276
                } catch (DriverException e1) {
277
                        // TODO Auto-generated catch block
278
                        e1.printStackTrace();
279
        }
280

    
281

    
282
                setLayerText(layerText);
283
                layerText.setCoordTrans(getCoordTrans());
284
                return layerText;
285
        }
286

    
287
        /**
288
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
289
         */
290
        public void removeLabels() {
291
                setLayerText(null);
292
        }
293

    
294
    /* (non-Javadoc)
295
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#deleteSpatialIndex()
296
     */
297
    public void deleteSpatialIndex()
298
    {
299
        spatialIndex = null;
300
    }
301
        /* (non-Javadoc)
302
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
303
         */
304
        public void createSpatialIndex() {
305
        // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
306
        // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
307
        // para que acepten recorrer sin geometria, solo con rectangulos.
308
        spatialIndex = new Quadtree();
309
        ReadableVectorial va = getSource();
310
        ICoordTrans ct = getCoordTrans();
311
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
312
        try {
313
            va.start();
314

    
315
            for (int i=0; i < va.getShapeCount(); i++)
316
            {
317
                Rectangle2D r = shapeBounds.getShapeBounds(i);
318
                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
319
                if (ct != null) {
320
                    r = ct.convert(r);
321
                }
322
                Coordinate c1 = new Coordinate(r.getMinX(), r.getMinY());
323
                Coordinate c2 = new Coordinate(r.getMaxX(), r.getMaxY());
324
                Envelope env = new Envelope(c1, c2);
325
                spatialIndex.insert(env, new Integer(i));
326
            } // for
327
            va.stop();
328
        } catch (DriverIOException e) {
329
            // TODO Auto-generated catch block
330
            e.printStackTrace();
331
        } catch (IOException e) {
332
            // TODO Auto-generated catch block
333
            e.printStackTrace();
334
        }
335

    
336
        }
337

    
338
        /**
339
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
340
         *                 FBitSet)
341
         */
342
        public void process(FeatureVisitor visitor, FBitSet subset)
343
                throws DriverException, VisitException {
344
                Strategy s = StrategyManager.getStrategy(this);
345
                s.process(visitor, subset);
346
        }
347

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

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

    
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
                                getSource().start();
537
                                typeShape = getSource().getShapeType();
538
                                logger.debug("source.stop()");
539
                                getSource().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(getLegend().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 (getSource() instanceof VectorialEditableAdapter){
563
                        setSource(((VectorialEditableAdapter)source).getOriginalAdapter());
564
                }
565
                if (getSource() instanceof VectorialFileAdapter) {
566
                        xml.putProperty("type", "vectorial");
567
                        xml.putProperty("file", ((VectorialFileAdapter) getSource()).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) getSource().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) getSource().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
                VectorialLegend leg = 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", leg, 1);
694
        }
695
        else
696
        {
697
            try {
698
                // legend.setDataSource(getRecordset());
699
                setLegend(leg);
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
        /* (non-Javadoc)
778
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setEditing(boolean)
779
         */
780
        public void setEditing(boolean b) {
781
                super.setEditing(b);
782
                if (b)
783
                {
784
                        VectorialEditableAdapter vea = null;
785
                        // TODO: Qu? pasa si hay m?s tipos de adapters?
786
                        if (getSource() instanceof VectorialDBAdapter)
787
                        {
788
                                vea = new VectorialEditableDBAdapter();
789
                        }
790
                        else
791
                        {
792
                                vea = new VectorialEditableAdapter();
793
                        }
794
            vea.setOriginalVectorialAdapter(getSource());
795
            try{
796
                    vea.startEdition();
797
            }catch (EditionException e) {
798
                                e.printStackTrace();
799
                        }
800
            setSource(vea);
801
            getRecordset().setSelectionSupport(vea.getOriginalAdapter().getRecordset().getSelectionSupport());
802

    
803
                }
804
                else
805
                {
806
                        VectorialEditableAdapter vea = (VectorialEditableAdapter) getSource();
807
                        setSource(vea.getOriginalAdapter());
808
                }
809
                callEditionChanged(LayerEvent.createEditionChangedEvent(this, "edition"));
810

    
811
        }
812

    
813
        /*public SelectionSupport getSelectionSupport() {
814
                return selectionSupport;
815
        }
816

817
        public void setSelectionSupport(SelectionSupport selectionSupport) {
818
                this.selectionSupport = selectionSupport;
819
        }*/
820
}