Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_908 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrVect.java @ 11054

History | View | Annotate | Download (43.3 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.Color;
44
import java.awt.Font;
45
import java.awt.Graphics2D;
46
import java.awt.Point;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.image.BufferedImage;
50
import java.io.File;
51
import java.io.IOException;
52

    
53
import javax.print.attribute.PrintRequestAttributeSet;
54

    
55
import org.apache.log4j.Logger;
56
import org.cresques.cts.ICoordTrans;
57

    
58
import com.hardcode.driverManager.Driver;
59
import com.hardcode.driverManager.DriverLoadException;
60
import com.hardcode.gdbms.engine.data.DataSourceFactory;
61
import com.hardcode.gdbms.engine.data.NoSuchTableException;
62
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
63
import com.iver.cit.gvsig.fmap.DriverException;
64
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType;
65
import com.iver.cit.gvsig.fmap.ViewPort;
66
import com.iver.cit.gvsig.fmap.core.IGeometry;
67
import com.iver.cit.gvsig.fmap.core.ISymbol;
68
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
69
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
70
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
71
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
72
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
73
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
74
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
75
import com.iver.cit.gvsig.fmap.edition.AnnotationEditableAdapter;
76
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
77
import com.iver.cit.gvsig.fmap.edition.EditionException;
78
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
79
import com.iver.cit.gvsig.fmap.edition.IWriteable;
80
import com.iver.cit.gvsig.fmap.edition.IWriter;
81
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
82
import com.iver.cit.gvsig.fmap.edition.VectorialEditableDBAdapter;
83
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
84
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
85
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
86
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
87
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
88
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
89
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
90
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialXMLItem;
91
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
92
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
93
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
94
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
95
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
96
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
97
import com.iver.cit.gvsig.fmap.rendering.Legend;
98
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
99
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
100
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
101
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
102
import com.iver.cit.gvsig.fmap.spatialindex.IPersistentSpatialIndex;
103
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex;
104
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeGt2;
105
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
106
import com.iver.cit.gvsig.fmap.spatialindex.SpatialIndexException;
107
import com.iver.utiles.FileUtils;
108
import com.iver.utiles.IPersistance;
109
import com.iver.utiles.PostProcessSupport;
110
import com.iver.utiles.XMLEntity;
111
import com.iver.utiles.swing.threads.Cancellable;
112
import com.iver.utiles.swing.threads.CancellableMonitorable;
113

    
114
/**
115
 * Capa b?sica Vectorial.
116
 *
117
 * @author Fernando Gonz?lez Cort?s
118
 */
119

    
120
// TODO Cuando no sea para pruebas debe no ser public
121
public class FLyrVect extends FLyrDefault implements Labelable,
122
                ClassifiableVectorial, SingleLayer, VectorialData, RandomVectorialData,
123
                AlphanumericData, InfoByPoint {
124
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
125

    
126
        /** Leyenda de la capa vectorial */
127
        private VectorialLegend legend;
128

    
129
        private int typeShape = -1;
130

    
131
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
132

    
133
        private ReadableVectorial source;
134

    
135
        private SelectableDataSource sds;
136

    
137
        private SelectionSupport selectionSupport = new SelectionSupport();
138

    
139
        private SpatialCache spatialCache = new SpatialCache();
140
        private boolean spatialCacheEnabled = false;
141

    
142
        // protected SpatialIndex spatialIndex = null;
143
        /**
144
         * An implementation of gvSIG spatial index
145
         */
146
        protected ISpatialIndex spatialIndex = null;
147

    
148
        private boolean bHasJoin = false;
149

    
150
        private XMLEntity orgXMLEntity = null;
151

    
152
        private XMLEntity loadSelection = null;
153
        private VectorialLegend loadLegend = null;
154

    
155

    
156
        /**
157
         * A?ade un SelectionListener a la lista de listeners.
158
         *
159
         * @param listener
160
         *            SelectionListener.
161
         */
162
        /*
163
         * public void addSelectionListener(SelectionListener listener) { try {
164
         * getRecordset().addSelectionListener(listener); } catch (DriverException
165
         * e) { // TODO Auto-generated catch block e.printStackTrace(); } }
166
         */
167
        /**
168
         * Borra un selectionListener de la lista de listeners.
169
         *
170
         * @param listener
171
         *            SelectionListener
172
         */
173
        /*
174
         * public void removeSelectionListener(SelectionListener listener) { try {
175
         * getRecordset().removeSelectionListener(listener); } catch
176
         * (DriverException e) { // TODO Auto-generated catch block
177
         * e.printStackTrace(); } }
178
         */
179
        /**
180
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
181
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
182
         * eventos, se realiza la propagaci?n de manera manual al final de la
183
         * "r?faga" de eventos
184
         */
185
        /*
186
         * public void fireSelectionEvents() { try {
187
         * getRecordset().fireSelectionEvents(); } catch (DriverException e) { //
188
         * TODO Auto-generated catch block e.printStackTrace(); } }
189
         */
190
        /**
191
         * Devuelve el VectorialAdapater de la capa.
192
         *
193
         * @return VectorialAdapter.
194
         */
195
        public ReadableVectorial getSource() {
196
                if (!this.isAvailable()) return null;
197
                return source;
198
        }
199

    
200
        /**
201
         * If we use a persistent spatial index associated with this layer, and the
202
         * index is not intrisic to the layer (for example spatial databases) this
203
         * method looks for existent spatial index, and loads it.
204
         *
205
         */
206
        private void loadSpatialIndex() {
207
                //FIXME: Al abrir el indice en fichero...
208
                //?C?mo lo liberamos? un metodo Layer.shutdown()
209

    
210

    
211
                ReadableVectorial source = getSource();
212
                //REVISAR QUE PASA CON LOS DRIVERS DXF, DGN, etc.
213
                //PUES SON VECTORIALFILEADAPTER
214
                if (!(source instanceof VectorialFileAdapter)) {
215
                        // we are not interested in db adapters
216
                        return;
217
                }
218
                VectorialDriver driver = source.getDriver();
219
                if (!(driver instanceof BoundedShapes)) {
220
                        // we dont spatially index layers that are not bounded
221
                        return;
222
                }
223
                File file = ((VectorialFileAdapter) source).getFile();
224
                String fileName = file.getAbsolutePath();
225
                File sptFile = new File(fileName + ".qix");
226
                if (!sptFile.exists() || (!(sptFile.length() > 0))) {
227
                        // before to exit, look for it in temp path
228
                        String tempPath = System.getProperty("java.io.tmpdir");
229
                        fileName = tempPath + File.separator + sptFile.getName();
230
                        sptFile = new File(fileName);
231
                        // it doesnt exists, must to create
232
                        if (!sptFile.exists() || (!(sptFile.length() > 0))) {
233
                                return;
234
                        }// if
235
                }// if
236

    
237
                try {
238
                        source.start();
239
                        spatialIndex = new QuadtreeGt2(FileUtils.getFileWithoutExtension(sptFile),
240
                                        "NM", source.getFullExtent(), source.getShapeCount(), false);
241
                } catch (SpatialIndexException e) {
242
                        spatialIndex = null;
243
                        e.printStackTrace();
244
                        return;
245
                } catch (DriverIOException e) {
246
                        // TODO Auto-generated catch block
247
                        e.printStackTrace();
248
                }
249

    
250
        }
251

    
252
        /**
253
         * Checks if it has associated an external spatial index
254
         * (an spatial index file).
255
         *
256
         * It looks for it in main file path, or in temp system path.
257
         * If main file is rivers.shp, it looks for a file called
258
         * rivers.shp.qix.
259

260
         * @return
261
         */
262
        public boolean isExternallySpatiallyIndexed() {
263
                /*
264
                 * FIXME (AZABALA): Independizar del tipo de fichero de ?ndice
265
                  * con el que se trabaje (ahora mismo considera la extension .qix,
266
             * pero esto depender? del tipo de ?ndice)
267
                 * */
268
                ReadableVectorial source = getSource();
269
                if (!(source instanceof VectorialFileAdapter)) {
270
                        // we are not interested in db adapters.
271
                        // think in non spatial dbs, like HSQLDB
272
                        return false;
273
                }
274
                File file = ((VectorialFileAdapter) source).getFile();
275
                String fileName = file.getAbsolutePath();
276
                File sptFile = new File(fileName + ".qix");
277
                if (!sptFile.exists() || (!(sptFile.length() > 0))) {
278
                        // before to exit, look for it in temp path
279
                        // it doesnt exists, must to create
280
                        String tempPath = System.getProperty("java.io.tmpdir");
281
                        fileName = tempPath + File.separator + sptFile.getName();
282
                        sptFile = new File(fileName);
283
                        if (!sptFile.exists() || (!(sptFile.length() > 0))) {
284
                                return false;
285
                        }// if
286
                }// if
287
                return true;
288
        }
289

    
290
        /**
291
         * Inserta el VectorialAdapter a la capa.
292
         *
293
         * @param va
294
         *            VectorialAdapter.
295
         */
296
        public void setSource(ReadableVectorial rv) {
297
                source = rv;
298
                // azabala: we check if this layer could have a file spatial index
299
                // and load it if it exists
300
                loadSpatialIndex();
301
        }
302

    
303
        /**
304
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
305
         */
306
        public Rectangle2D getFullExtent() throws DriverException {
307
                try {
308
                        Rectangle2D rAux;
309
//                        logger.debug(getName() + "_source.start()");
310
                        source.start();
311
                        rAux = (Rectangle2D)source.getFullExtent().clone();
312
//                        logger.debug(getName() + "_source.stop()");
313
                        source.stop();
314

    
315
                        // Si existe reproyecci?n, reproyectar el extent
316
                        ICoordTrans ct = getCoordTrans();
317

    
318
                        if (ct != null) {
319
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
320
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
321
                                pt1 = ct.convert(pt1, null);
322
                                pt2 = ct.convert(pt2, null);
323
                                rAux = new Rectangle2D.Double();
324
                                rAux.setFrameFromDiagonal(pt1, pt2);
325
                        }
326

    
327
                        //Esto es para cuando se crea una capa nueva con el fullExtent de ancho y alto 0.
328
                        if (rAux.getWidth()==0 && rAux.getHeight()==0) {
329
                                rAux=new Rectangle2D.Double(0,0,100,100);
330
                        }
331

    
332
                        return rAux;
333
                } catch (DriverIOException e) {
334
                        throw new DriverException(e);
335
                }
336
        }
337

    
338
        /**
339
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
340
         *      java.awt.Graphics2D, ISymbol)
341
         */
342
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
343
                        Cancellable cancel, double scale) throws DriverException {
344

    
345
                if (isWithinScale(scale)) {
346
                        // Las que solo tienen etiquetado sin pintar el shape,
347
                        // no pasamos por ellas
348
                        boolean bDrawShapes = true;
349
                        if (legend instanceof SingleSymbolLegend) {
350
                                if (legend.getDefaultSymbol().isShapeVisible() == false)
351
                                        bDrawShapes = false;
352
                        }
353
                        if (bDrawShapes) {
354
                                Strategy strategy = StrategyManager.getStrategy(this);
355
                                try {
356
                                        prepareDrawing(image, g, viewPort);
357
                                        strategy.draw(image, g, viewPort, cancel);
358
                                } catch (DriverException e) {
359
                                        this.setVisible(false);
360
                                        this.setActive(false);
361
                                        throw e;
362
                                }
363
                        }
364
                        if (getVirtualLayers() != null) {
365
                                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
366
                        }
367

    
368
                        if (getLayerText() != null) {
369
                                getLayerText().draw(image, g, viewPort, cancel, scale);
370
                        }
371
                }
372
        }
373

    
374
        /**
375
         * Se llama antes de empezar a pintar.
376
         * Es ?til para preparar la cache a emplear, las leyendas, etc.
377
         * @param image
378
         * @param g
379
         * @param viewPort
380
         */
381
        private void prepareDrawing(BufferedImage image, Graphics2D g, ViewPort viewPort) {
382

    
383
        }
384

    
385
        /**
386
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
387
         *      com.iver.cit.gvsig.fmap.ViewPort,
388
         *      com.iver.utiles.swing.threads.Cancellable)
389
         */
390
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
391
                        double scale, PrintRequestAttributeSet properties) throws DriverException {
392
                if (isVisible() && isWithinScale(scale)) {
393
                        Strategy strategy = StrategyManager.getStrategy(this);
394

    
395
                        strategy.print(g, viewPort, cancel, properties);
396

    
397
                        if (getLayerText() != null) {
398
                                getLayerText().draw(null, g, viewPort, cancel, scale);
399
                        }
400
                }
401
        }
402

    
403
        /**
404
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
405
         */
406
        // public FLayer createLabelLayer(int fieldId) {
407
        public FLayer createLabelLayer(SelectableDataSource ds) {
408
                FLyrText layerText = null;
409
                try {
410
                        layerText = new FLyrText();
411
                        layerText.setCoordTrans(getCoordTrans());
412
                        layerText.setLegend((VectorialLegend) getLegend());
413
                        layerText.createLabels(this);
414
                } catch (FieldNotFoundException e1) {
415
                        // TODO Auto-generated catch block
416
                        e1.printStackTrace();
417
                } catch (DriverException e1) {
418
                        // TODO Auto-generated catch block
419
                        e1.printStackTrace();
420
                }
421

    
422
                setLayerText(layerText);
423
                return layerText;
424
        }
425

    
426
        /**
427
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
428
         */
429
        public void removeLabels() {
430
                setLayerText(null);
431
        }
432

    
433
        /*
434
         * (non-Javadoc)
435
         *
436
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#deleteSpatialIndex()
437
         */
438
        public void deleteSpatialIndex() {
439
                //must we delete possible spatial indexes files?
440
                spatialIndex = null;
441
        }
442

    
443

    
444

    
445
   /**
446
    * <p>
447
    * Creates an spatial index associated to this layer.
448
    * The spatial index will used
449
    * the native projection of the layer, so if the layer is reprojected, it will
450
    * be ignored.
451
    * </p>
452
    * @param cancelMonitor instance of CancellableMonitorable that allows
453
    * to monitor progress of spatial index creation, and cancel the process
454
    */
455
    public void createSpatialIndex(CancellableMonitorable cancelMonitor){
456
             // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
457
        // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
458
        // para que acepten recorrer sin geometria, solo con rectangulos.
459

    
460
            //If this vectorial layer is based in a spatial database, the spatial
461
                //index is already implicit. We only will index file drivers
462
            ReadableVectorial va = getSource();
463
            //We must think in non spatial databases, like HSQLDB
464
                if(!(va instanceof VectorialFileAdapter)){
465
                        return;
466
                }
467
                if (!(va.getDriver() instanceof BoundedShapes)) {
468
                        return;
469
                }
470
                File file = ((VectorialFileAdapter) va).getFile();
471
                String fileName = file.getAbsolutePath();
472
                ISpatialIndex localCopy = null;
473
                try {
474
                        va.start();
475
                        localCopy = new QuadtreeGt2(fileName, "NM", va.getFullExtent(),
476
                                        va.getShapeCount(), true);
477

    
478
                } catch (SpatialIndexException e1) {
479
                        // Probably we dont have writing permissions
480
                        String directoryName = System.getProperty("java.io.tmpdir");
481
                        File newFile = new File(directoryName +
482
                                        File.separator +
483
                                        file.getName());
484
                        String newFileName = newFile.getName();
485
                        try {
486
                                localCopy = new QuadtreeGt2(newFileName, "NM", va.getFullExtent(),
487
                                                va.getShapeCount(), true);
488
                        } catch (SpatialIndexException e) {
489
                                // if we cant build a file based spatial index, we'll build
490
                                // a pure memory spatial index
491
                                localCopy = new QuadtreeJts();
492
                        } catch (DriverIOException e) {
493
                                // TODO Auto-generated catch block
494
                                e.printStackTrace();
495
                        }
496

    
497
                }catch(Exception e){
498
                        e.printStackTrace();
499
                }//try
500
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
501
        try {
502
            for (int i=0; i < va.getShapeCount(); i++)
503
            {
504
                    if(cancelMonitor != null){
505
                            if(cancelMonitor.isCanceled())
506
                                    return;
507
                            cancelMonitor.reportStep();
508
                    }
509
                Rectangle2D r = shapeBounds.getShapeBounds(i);
510
                if(r != null)
511
                        localCopy.insert(r, i);
512
            } // for
513
            va.stop();
514
            if(localCopy instanceof IPersistentSpatialIndex)
515
                    ((IPersistentSpatialIndex) localCopy).flush();
516
            spatialIndex = localCopy;
517
        } catch (DriverIOException e) {
518
            // TODO Auto-generated catch block
519
            e.printStackTrace();
520
        } catch (IOException e) {
521
            // TODO Auto-generated catch block
522
            e.printStackTrace();
523
        }
524
    }
525

    
526
        /**
527
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
528
         */
529
        public void createSpatialIndex() {
530
                createSpatialIndex(null);
531
        }
532

    
533
        /**
534
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
535
         *      FBitSet)
536
         */
537
        public void process(FeatureVisitor visitor, FBitSet subset)
538
                        throws DriverException, VisitException {
539
                Strategy s = StrategyManager.getStrategy(this);
540
                s.process(visitor, subset);
541
        }
542

    
543
        /**
544
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
545
         */
546
        public void process(FeatureVisitor visitor) throws DriverException,
547
                        VisitException {
548
                Strategy s = StrategyManager.getStrategy(this);
549
                s.process(visitor);
550
        }
551

    
552
        /**
553
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
554
         *      Rectangle2D)
555
         */
556
        public void process(FeatureVisitor visitor, Rectangle2D rect)
557
                        throws DriverException, VisitException {
558
                Strategy s = StrategyManager.getStrategy(this);
559
                s.process(visitor, rect);
560
        }
561

    
562
        /**
563
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
564
         */
565
        /*
566
         * public void setSelection(FBitSet selection) { try {
567
         * getRecordset().setSelection(selection); } catch (DriverException e) { //
568
         * TODO Auto-generated catch block e.printStackTrace(); }
569
         * fireSelectionEvents(); }
570
         */
571
        /**
572
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
573
         */
574
        /*
575
         * public boolean isSelected(int index) { try { return
576
         * getRecordset().isSelected(index); } catch (DriverException e) { // TODO
577
         * Auto-generated catch block e.printStackTrace(); } return false; }
578
         */
579
        /**
580
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
581
         */
582
        /*
583
         * public FBitSet getSelection() { try { return
584
         * getRecordset().getSelection(); } catch (DriverException e) { // TODO
585
         * Auto-generated catch block e.printStackTrace(); } return new FBitSet(); }
586
         */
587
        /**
588
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
589
         */
590
        /*
591
         * public void clearSelection() { try { getRecordset().clearSelection(); }
592
         * catch (DriverException e) { // TODO Auto-generated catch block
593
         * e.printStackTrace(); } }
594
         */
595

    
596
        /*
597
         * (non-Javadoc)
598
         *
599
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
600
         */
601
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
602
                Strategy s = StrategyManager.getStrategy(this);
603

    
604
                return s.queryByRect(rect);
605
        }
606

    
607
        public FBitSet queryByPoint(Point2D p, double tolerance)
608
                        throws DriverException {
609
                Strategy s = StrategyManager.getStrategy(this);
610
                return s.queryByPoint(p, tolerance);
611
        }
612

    
613
        public FBitSet queryByShape(IGeometry g, int relationship)
614
                        throws DriverException, VisitException {
615
                Strategy s = StrategyManager.getStrategy(this);
616
                return s.queryByShape(g, relationship);
617
        }
618

    
619
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws DriverException {
620
                Point2D pReal = this.getMapContext().getViewPort().toMapPoint(p);
621
                FBitSet bs = queryByPoint(pReal, tolerance);
622
                VectorialXMLItem[] item = new VectorialXMLItem[1];
623
                item[0] = new VectorialXMLItem(bs, this);
624

    
625
                return item;
626
        }
627

    
628
        /**
629
         * @throws DriverException
630
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
631
         */
632
        /*
633
         * public SelectableDataSource getRecordset() throws DriverException { if
634
         * (sds == null){ try { DataSource ds = getSource().getRecordset();
635
         *
636
         * if (ds == null) { return null; }
637
         *
638
         * sds = new SelectableDataSource(ds);
639
         * //sds.setSelectionSupport(selectionSupport);
640
         *
641
         * return sds; } catch (DriverLoadException e) { throw new
642
         * DriverException(e); } catch
643
         * (com.hardcode.gdbms.engine.data.driver.DriverException e) { throw new
644
         * DriverException(e); } } return sds; }
645
         */
646
        /**
647
         * Para cuando haces una uni?n, sustituyes el recorset por el nuevo. De esta
648
         * forma, podr?s poner leyendas basadas en el nuevo recordset
649
         *
650
         * @param newSds
651
         * @throws DriverException
652
         * @throws FieldNotFoundException
653
         * @throws FieldNotFoundException
654
         */
655
        /*
656
         * public void setRecordset(SelectableDataSource newSds) throws
657
         * DriverException, FieldNotFoundException { sds = newSds;
658
         * //sds.setSelectionSupport(selectionSupport); legend.setDataSource(sds);
659
         * logger.debug("Recordset cambiado a " + sds.getName()); }
660
         */
661
        /**
662
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
663
         *      com.iver.cit.gvsig.fmap.rendering.Legend)
664
         */
665
        public void setLegend(VectorialLegend r) throws DriverException,
666
                        FieldNotFoundException {
667
                VectorialLegend oldLegend = legend;
668
                legend = r;
669

    
670
                try {
671
                        legend.setDataSource(getRecordset());
672

    
673
                        if (legend.getLabelField() != null) {
674
                                // sds.start();
675
                                // int idLabelField =
676
                                // getRecordset().getFieldIndexByName(legend.getLabelField());
677
                                createLabelLayer(getSource().getRecordset());
678
                                // sds.stop();
679
                        } else
680
                                removeLabels();
681
                } catch (DriverException e) {
682
                        throw new DriverException(e);
683
                } catch (FieldNotFoundException e) {
684
                        // TODO Auto-generated catch block
685
                        e.printStackTrace();
686
                } catch (DriverLoadException e) {
687
                        // TODO Auto-generated catch block
688
                        e.printStackTrace();
689
                }
690

    
691
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(
692
                                oldLegend, legend);
693
                callLegendChanged(e);
694
        }
695

    
696
        /**
697
         * Devuelve la Leyenda de la capa.
698
         *
699
         * @return Leyenda.
700
         */
701
        public Legend getLegend() {
702
                return legend;
703
        }
704

    
705
        /**
706
         * Devuelve el tipo de shape que contiene la capa.
707
         *
708
         * @return tipo de shape.
709
         *
710
         * @throws DriverException
711
         */
712
        public int getShapeType() throws DriverException {
713
                if (typeShape == -1) {
714
                        try {
715
//                                logger.debug("source.start()");
716
                                getSource().start();
717
                                typeShape = getSource().getShapeType();
718
//                                logger.debug("source.stop()");
719
                                getSource().stop();
720
                        } catch (DriverIOException e) {
721
                                throw new DriverException(e);
722
                        }
723
                }
724

    
725
                return typeShape;
726
        }
727

    
728
        /**
729
         * @throws XMLException
730
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
731
         */
732
        public XMLEntity getXMLEntity() throws XMLException {
733
                if (!this.isAvailable() && this.orgXMLEntity != null) {
734
                        return this.orgXMLEntity;
735
                }
736
                XMLEntity xml = super.getXMLEntity();
737
                xml.addChild(getLegend().getXMLEntity());
738
                try {
739
                        xml.addChild(getRecordset().getSelectionSupport().getXMLEntity());
740

    
741
                } catch (DriverException e1) {
742
                        e1.printStackTrace();
743
                        throw new XMLException(e1);
744
                }
745
                //Repongo el mismo ReadableVectorial m?s abajo para cuando se guarda el proyecto.
746
                ReadableVectorial rv=getSource();
747
                if (getSource() instanceof VectorialEditableAdapter) {
748
                        setSource(((VectorialEditableAdapter) source).getOriginalAdapter());
749
                }
750
                if (getSource() instanceof VectorialFileAdapter) {
751
                        xml.putProperty("type", "vectorial");
752
                        xml.putProperty("file", ((VectorialFileAdapter) getSource())
753
                                        .getFile());
754
                        // try {
755
                        try {
756
                                xml.putProperty("recordset-name", getSource().getRecordset()
757
                                                .getName());
758
                        } catch (DriverLoadException e) {
759
                                // TODO Auto-generated catch block
760
                                e.printStackTrace();
761
                        }
762
                        // } catch (DriverException e) {
763
                        // throw new XMLException(e);
764
                        // }
765
                } else if (source instanceof VectorialDBAdapter) {
766
                        xml.putProperty("type", "vectorial");
767

    
768
                        VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) getSource()
769
                                        .getDriver();
770

    
771
                        // Guardamos el nombre del driver para poder recuperarlo
772
                        // con el DriverManager de Fernando.
773
                        xml.putProperty("db", dbDriver.getName());
774
                        // try {
775
                        try {
776
                                xml.putProperty("recordset-name", getSource().getRecordset()
777
                                                .getName());
778
                        } catch (DriverLoadException e) {
779
                                // TODO Auto-generated catch block
780
                                e.printStackTrace();
781
                        }
782
                        // } catch (DriverException e) {
783
                        // throw new XMLException(e);
784
                        // }
785
                        xml.addChild(dbDriver.getXMLEntity()); // Tercer child. Antes hemos
786
                                                                                                        // metido la leyenda y el
787
                                                                                                        // selection support
788
                } else if (source instanceof VectorialAdapter) {
789
                        // Se supone que hemos hecho algo gen?rico.
790
                        xml.putProperty("type", "vectorial");
791

    
792
                        VectorialDriver driver = (VectorialDriver) getSource().getDriver();
793

    
794
                        // Guardamos el nombre del driver para poder recuperarlo
795
                        // con el DriverManager de Fernando.
796
                        xml.putProperty("other", driver.getName());
797
                        // try {
798
                        try {
799
                                xml.putProperty("recordset-name", getSource().getRecordset()
800
                                                .getName());
801
                        } catch (DriverLoadException e) {
802
                                // TODO Auto-generated catch block
803
                                e.printStackTrace();
804
                        }
805
                        // } catch (DriverException e) {
806
                        // throw new XMLException(e);
807
                        // }
808
                        if (driver instanceof IPersistance) {
809
                                // xml.putProperty("className", driver.getClass().getName());
810
                                IPersistance persist = (IPersistance) driver;
811
                                xml.addChild(persist.getXMLEntity()); // Tercer child. Antes
812
                                                                                                                // hemos metido la
813
                                                                                                                // leyenda y el
814
                                                                                                                // selection support
815
                        }
816
                }
817
                setSource(rv);
818
                xml.putProperty("driverName", getSource().getDriver().getName());
819
                if (bHasJoin)
820
                        xml.putProperty("hasJoin", "true");
821

    
822
                return xml;
823
        }
824

    
825
        /**
826
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
827
         */
828
        public void setXMLEntity03(XMLEntity xml) throws XMLException {
829

    
830
                super.setXMLEntity(xml);
831
                legend = LegendFactory.createFromXML03(xml.getChild(0));
832

    
833
                try {
834
                        // legend.setDataSource(getRecordset());
835
                        setLegend(legend);
836
                } catch (FieldNotFoundException e) {
837
                        throw new XMLException(e);
838
                } catch (DriverException e) {
839
                        throw new XMLException(e);
840
                }
841

    
842
                try {
843
                        getRecordset().getSelectionSupport()
844
                                        .setXMLEntity03(xml.getChild(1));
845
                } catch (DriverException e) {
846
                        e.printStackTrace();
847
                }
848
        }
849

    
850
        /**
851
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
852
         */
853
        public void setXMLEntity(XMLEntity xml) throws XMLException {
854
                try {
855
                        super.setXMLEntity(xml);
856

    
857
                        VectorialLegend leg = LegendFactory.createFromXML(xml.getChild(0));
858
                        try {
859
                                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
860
                                /* JMVIVO
861
                                this.setLoadSelection(xml.getChild(1));
862
                                */
863
                                // JMVIVO: Esto sirve para algo????
864
                                String recordsetName = xml.getStringProperty("recordset-name");
865

    
866
                                LayerFactory.getDataSourceFactory().changeDataSourceName(
867
                                                getSource().getRecordset().getName(), recordsetName);
868
                                // Hacemos que el recordset que hemos creado antes (los
869
                                // recordset se crean antes que todo) sea el recordset
870
                                // de esta capa. Y del que se crea automaticamente
871
                                // en el setLegend del createLayer, nos olvidamos
872
//                                 CHEMA: AUTOMATIC DATA SOURCE
873
                                /*
874
                                SelectableDataSource sds = new SelectableDataSource(LayerFactory
875
                                                .getDataSourceFactory().createRandomDataSource(
876
                                                                recordsetName, DataSourceFactory.MANUAL_OPENING));
877
                                */
878
                                SelectableDataSource sds = new SelectableDataSource(LayerFactory
879
                                                .getDataSourceFactory().createRandomDataSource(
880
                                                                recordsetName, DataSourceFactory.AUTOMATIC_OPENING));
881
                                // sds.setSelectionSupport(selectionSupport);
882
                                // ((EditableAdapter)getSource()).setRecordSet(sds);
883
                        } catch (NoSuchTableException e1) {
884
                                this.setAvailable(false);
885
                                throw new XMLException(e1);
886
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
887
                                this.setAvailable(false);
888
                                throw new XMLException(e1);
889
                        } catch (DriverLoadException e1) {
890
                                this.setAvailable(false);
891
                                throw new XMLException(e1);
892
                        } catch (DriverException e1) {
893
                                this.setAvailable(false);
894
                                throw new XMLException(e1);
895
                        }
896
                        // Si tiene una uni?n, lo marcamos para que no se cree la leyenda hasta
897
                        // el final
898
                        // de la lectura del proyecto
899
                        if (xml.contains("hasJoin")) {
900
                                setIsJoined(true);
901
                                PostProcessSupport.addToPostProcess(this, "setLegend", leg, 1);
902
                        } else {
903
                                /*
904
                                JMVIVO
905
                                this.setLoadLegend(leg);
906
                                */
907
                                try {
908
                                        // legend.setDataSource(getRecordset());
909
                                        setLegend(leg);
910
                                } catch (FieldNotFoundException e) {
911
                                        throw new XMLException(e);
912
                                } catch (DriverException e) {
913
                                        throw new XMLException(e);
914
                                }
915
                        }
916

    
917
                } catch (XMLException e) {
918
                        this.setAvailable(false);
919
                        this.orgXMLEntity = xml;
920
                        //throw e;
921
                } catch (Exception e) {
922
                        this.setAvailable(false);
923
                        this.orgXMLEntity = xml;
924
                        //throw new XMLException(e);
925
                }
926

    
927

    
928
        }
929

    
930
        public void setXMLEntityNew(XMLEntity xml) throws XMLException {
931
                try {
932
                        super.setXMLEntity(xml);
933

    
934
                        VectorialLegend leg = LegendFactory.createFromXML(xml.getChild(0));
935
                        try {
936
                                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
937

    
938
                                this.setLoadSelection(xml.getChild(1));
939
                                /*
940
                                // JMVIVO: Esto sirve para algo????
941
                                String recordsetName = xml.getStringProperty("recordset-name");
942

943
                                LayerFactory.getDataSourceFactory().changeDataSourceName(
944
                                                getSource().getRecordset().getName(), recordsetName);
945
                                // Hacemos que el recordset que hemos creado antes (los
946
                                // recordset se crean antes que todo) sea el recordset
947
                                // de esta capa. Y del que se crea automaticamente
948
                                // en el setLegend del createLayer, nos olvidamos
949
                                SelectableDataSource sds = new SelectableDataSource(LayerFactory
950
                                                .getDataSourceFactory().createRandomDataSource(
951
                                                                recordsetName, DataSourceFactory.AUTOMATIC_OPENING));
952
                                // sds.setSelectionSupport(selectionSupport);
953
                                // ((EditableAdapter)getSource()).setRecordSet(sds);
954

955

956
                        } catch (NoSuchTableException e1) {
957
                                this.setAvailable(false);
958
                                throw new XMLException(e1);
959
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
960
                                this.setAvailable(false);
961
                                throw new XMLException(e1);
962
                        */
963
                        } catch (DriverLoadException e1) {
964
                                this.setAvailable(false);
965
                                throw new XMLException(e1);
966
                        } catch (DriverException e1) {
967
                                this.setAvailable(false);
968
                                throw new XMLException(e1);
969
                        }
970
                        // Si tiene una uni?n, lo marcamos para que no se cree la leyenda hasta
971
                        // el final
972
                        // de la lectura del proyecto
973
                        if (xml.contains("hasJoin")) {
974
                                setIsJoined(true);
975
                                PostProcessSupport.addToPostProcess(this, "setLegend", leg, 1);
976
                        } else {
977

    
978

    
979
                                this.setLoadLegend(leg);
980
                                /*
981
                                try {
982
                                        // legend.setDataSource(getRecordset());
983
                                        setLegend(leg);
984
                                } catch (FieldNotFoundException e) {
985
                                        throw new XMLException(e);
986
                                } catch (DriverException e) {
987
                                        throw new XMLException(e);
988
                                }
989
                                */
990
                        }
991

    
992
                } catch (XMLException e) {
993
                        this.setAvailable(false);
994
                        this.orgXMLEntity = xml;
995
                        //throw e;
996
                } catch (Exception e) {
997
                        this.setAvailable(false);
998
                        this.orgXMLEntity = xml;
999
                        //throw new XMLException(e);
1000
                }
1001

    
1002

    
1003
        }
1004

    
1005

    
1006

    
1007
        /**
1008
         * A?ade un LegendListener a la lista de Listeners.
1009
         *
1010
         * @param listener
1011
         *            LegendListener.
1012
         */
1013
        public void addLegendListener(LegendListener listener) {
1014
                layerChangeSupport.addLayerListener(listener);
1015
        }
1016

    
1017
        /**
1018
         * Llamada al m?todo callLegendChanged de los listener.
1019
         *
1020
         * @param e
1021
         *            Evento.
1022
         */
1023
        private void callLegendChanged(LegendChangedEvent e) {
1024
                layerChangeSupport.callLegendChanged(e);
1025
        }
1026

    
1027
        /**
1028
         * Borra un LegendListener de la lista de Listeners
1029
         *
1030
         * @param listener
1031
         *            LegendListener.
1032
         */
1033
        public void removeLegendListener(LegendListener listener) {
1034
                layerChangeSupport.removeLayerListener(listener);
1035
        }
1036

    
1037
        /**
1038
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
1039
         * identifiquen la capa.
1040
         *
1041
         * @return DOCUMENT ME!
1042
         */
1043
        public String toString() {
1044
                /*
1045
                 * Se usa internamente para que la parte de datos identifique de forma
1046
                 * un?voca las tablas
1047
                 */
1048
                String ret = super.toString();
1049

    
1050
                return "layer" + ret.substring(ret.indexOf('@') + 1);
1051
        }
1052

    
1053
        public boolean isJoined() {
1054
                return bHasJoin;
1055
        }
1056

    
1057
        /**
1058
         * Returns if a layer is spatially indexed
1059
         *
1060
         * @return if this layer has the ability to proces spatial queries without
1061
         *         secuential scans.
1062
         */
1063
        public boolean isSpatiallyIndexed() {
1064
                ReadableVectorial source = getSource();
1065
                if (source instanceof ISpatialDB)
1066
                        return true;
1067

    
1068
//FIXME azabala
1069
/*
1070
 * Esto es muy dudoso, y puede cambiar.
1071
 * Estoy diciendo que las que no son fichero o no son
1072
 * BoundedShapes estan indexadas. Esto es mentira, pero
1073
 * as? quien pregunte no querr? generar el indice.
1074
 * Esta por ver si interesa generar el indice para capas
1075
 * HSQLDB, WFS, etc.
1076
 */
1077
                if(!(source instanceof VectorialFileAdapter)){
1078
                        return true;
1079
                }
1080
                if (!(source.getDriver() instanceof BoundedShapes)) {
1081
                        return true;
1082
                }
1083

    
1084
                if (getISpatialIndex() != null)
1085
                        return true;
1086
                return false;
1087
        }
1088

    
1089
        public void setIsJoined(boolean hasJoin) {
1090
                bHasJoin = hasJoin;
1091
        }
1092

    
1093
        /**
1094
         * @return Returns the spatialIndex.
1095
         */
1096
        public ISpatialIndex getISpatialIndex() {
1097
                return spatialIndex;
1098
        }
1099
        /**
1100
         * Sets the spatial index. This could be useful if, for some
1101
         * reasons, you want to work with a distinct spatial index
1102
         * (for example, a spatial index which could makes nearest
1103
         * neighbour querys)
1104
         * @param spatialIndex
1105
         */
1106
        public void setISpatialIndex(ISpatialIndex spatialIndex){
1107
                this.spatialIndex = spatialIndex;
1108
        }
1109

    
1110

    
1111
        /*
1112
         * public SelectableDataSource getRecordset() { try { return
1113
         * getSource().getRecordset(); } catch (DriverLoadException e) { // TODO
1114
         * Auto-generated catch block e.printStackTrace(); } return null; }
1115
         */
1116

    
1117
        /**
1118
         * @throws DriverException
1119
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
1120
         */
1121
        public SelectableDataSource getRecordset() throws DriverException {
1122
                if (!this.isAvailable()) return null;
1123
                if (sds == null) {
1124
                        try {
1125
                                SelectableDataSource ds = source.getRecordset();
1126

    
1127
                                if (ds == null) {
1128
                                        return null;
1129
                                }
1130

    
1131
                                sds = ds;
1132
                                sds.setSelectionSupport(selectionSupport);
1133

    
1134
                        } catch (DriverLoadException e) {
1135
                                DriverNotLoadedExceptionType exceptionType =
1136
                                        new DriverNotLoadedExceptionType();
1137
                                exceptionType.setDriverName(source.getDriver().getName());
1138
                                throw new DriverException(e, exceptionType);
1139
                        }
1140
                }
1141
                return sds;
1142
        }
1143

    
1144
        /*
1145
         * (non-Javadoc)
1146
         *
1147
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setEditing(boolean)
1148
         */
1149
        public void setEditing(boolean b) throws EditionException {
1150
                super.setEditing(b);
1151
                try {
1152
                        if (b) {
1153
                                VectorialEditableAdapter vea = null;
1154
                                // TODO: Qu? pasa si hay m?s tipos de adapters?
1155
                                // FJP: Se podr?a pasar como argumento el
1156
                                // VectorialEditableAdapter
1157
                                // que se quiera usar para evitar meter c?digo aqu? de este
1158
                                // estilo.
1159
                                if (getSource() instanceof VectorialDBAdapter) {
1160
                                        vea = new VectorialEditableDBAdapter();
1161
                                } else if (this instanceof FLyrAnnotation) {
1162
                                        vea = new AnnotationEditableAdapter(
1163
                                                        (FLyrAnnotation) this);
1164
                                } else {
1165
                                        vea = new VectorialEditableAdapter();
1166
                                }
1167
                                vea.setOriginalVectorialAdapter(getSource());
1168

    
1169
                                // /vea.setSpatialIndex(getSpatialIndex());
1170
                                // /vea.setFullExtent(getFullExtent());
1171
                                //CHEMA
1172
                                vea.start();
1173
                                //CHEMA
1174
                                vea.startEdition(EditionEvent.GRAPHIC);
1175
                                setSource(vea);
1176
                                getRecordset().setSelectionSupport(
1177
                                                vea.getOriginalAdapter().getRecordset()
1178
                                                                .getSelectionSupport());
1179

    
1180
                        } else {
1181
                                VectorialEditableAdapter vea = (VectorialEditableAdapter) getSource();
1182
                                //CHEMA
1183
                                vea.stop();
1184
                                //CHEMA                                
1185
                                setSource(vea.getOriginalAdapter());
1186
                        }
1187
                        // Si tenemos una leyenda, hay que pegarle el cambiazo a su
1188
                        // recordset
1189
                        setRecordset(getSource().getRecordset());
1190
                        if (getLegend() instanceof VectorialLegend) {
1191
                                VectorialLegend ley = (VectorialLegend) getLegend();
1192
                                ley.setDataSource(getSource().getRecordset());
1193
                                // Esto lo pongo para evitar que al dibujar sobre un
1194
                                // dxf, dwg, o dgn no veamos nada. Es debido al checkbox
1195
                                // de la leyenda de textos "dibujar solo textos".
1196
                                if (!(getSource().getDriver() instanceof IndexedShpDriver)){
1197
                                        if (ley.getDefaultSymbol()==null){
1198
                                                FSymbol symbol=new FSymbol(getShapeType());
1199
                                                symbol.setFontSizeInPixels(false);
1200
                                                symbol.setFont(new Font("SansSerif", Font.PLAIN, 9));
1201
        //                                        Color color=symbol.getColor();
1202
        //                                        int alpha=symbol.getColor().getAlpha();
1203
        //                                        if (alpha>250) {
1204
        //                                                symbol.setColor(new Color(color.getRed(),color.getGreen(),color.getBlue(),100));
1205
//                                        }
1206
                                        ley.setDefaultSymbol(symbol);
1207
                                        }
1208
                                }
1209
                                ley.useDefaultSymbol(true);
1210
                        }
1211
                } catch (DriverLoadException e) {
1212
                        e.printStackTrace();
1213
                        throw new EditionException(e);
1214
                } catch (DriverException e) {
1215
                        e.printStackTrace();
1216
                        throw new EditionException(e);
1217
                } catch (FieldNotFoundException e) {
1218
                        e.printStackTrace();
1219
                        throw new EditionException(e);
1220
                //CHEMA        
1221
                } catch (DriverIOException e) {
1222
                        e.printStackTrace();
1223
                        throw new EditionException(e);
1224
                //CHEMA
1225
                }
1226

    
1227
                setSpatialCacheEnabled(b);
1228
                callEditionChanged(LayerEvent
1229
                                .createEditionChangedEvent(this, "edition"));
1230

    
1231
        }
1232

    
1233
        /**
1234
         * Para cuando haces una uni?n, sustituyes el recorset por el nuevo. De esta
1235
         * forma, podr?s poner leyendas basadas en el nuevo recordset
1236
         *
1237
         * @param newSds
1238
         */
1239
        public void setRecordset(SelectableDataSource newSds) {
1240
                sds = newSds;
1241
                sds.setSelectionSupport(selectionSupport);
1242
        }
1243
        /*
1244
         * public SelectionSupport getSelectionSupport() { return selectionSupport; }
1245
         *
1246
         * public void setSelectionSupport(SelectionSupport selectionSupport) {
1247
         * this.selectionSupport = selectionSupport; }
1248
         */
1249

    
1250
//        public SpatialCache createSpatialCache() {
1251
//               spatialCache = new SpatialCache();
1252
//                return spatialCache;
1253
//        }
1254
        public void clearSpatialCache()
1255
        {
1256
                spatialCache.clearAll();
1257
        }
1258

    
1259
//        public void setSpatialCache(SpatialCache spatialCache) {
1260
//                this.spatialCache = spatialCache;
1261
//        }
1262

    
1263
        public boolean isSpatialCacheEnabled() {
1264
                return spatialCacheEnabled;
1265
        }
1266

    
1267
        public void setSpatialCacheEnabled(boolean spatialCacheEnabled) {
1268
                this.spatialCacheEnabled = spatialCacheEnabled;
1269
        }
1270

    
1271
        public SpatialCache getSpatialCache() {
1272
                return spatialCache;
1273
        }
1274

    
1275
        /**
1276
         * Siempre es un numero mayor de 1000
1277
         * @param maxFeatures
1278
         */
1279
        public void setMaxFeaturesInEditionCache(int maxFeatures) {
1280
                if (maxFeatures > spatialCache.maxFeatures)
1281
                        spatialCache.setMaxFeatures(maxFeatures);
1282

    
1283
        }
1284

    
1285
        /**
1286
         * This method returns a boolean that is used by the FPopMenu
1287
         * to make visible the properties menu or not. It is visible by
1288
         * default, and if a later don't have to show this menu only
1289
         * has to override this method.
1290
         * @return
1291
         * If the properties menu is visible (or not)
1292
         */
1293
        public boolean isPropertiesMenuVisible(){
1294
                return true;
1295
        }
1296

    
1297
        public void reload() throws DriverIOException {
1298
                super.reload();
1299
                try {
1300
                        this.source.getDriver().reload();
1301
                        //this.getRecordset().reload();
1302
                        if (this.getLegend() == null) {
1303
                    if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
1304
                        WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
1305
                        this.setLegend((VectorialLegend) aux.getDefaultLegend());
1306
                    } else {
1307
                        this.setLegend(LegendFactory.createSingleSymbolLegend(
1308
                                this.getShapeType()));
1309
                    }
1310
                        }
1311

    
1312
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1313
                        this.setAvailable(false);
1314
                        throw new DriverIOException(e);
1315
                } catch (IOException e) {
1316
                        this.setAvailable(false);
1317
                        throw new DriverIOException(e);
1318
                } catch (DriverException e) {
1319
                        this.setAvailable(false);
1320
                        throw new DriverIOException(e);
1321
                } catch (Exception e) {
1322
                        this.setAvailable(false);
1323
                        throw new DriverIOException(e);
1324
                }
1325

    
1326
        }
1327

    
1328
        protected void setLoadSelection(XMLEntity xml) {
1329
                this.loadSelection = xml;
1330
        }
1331

    
1332
        protected void setLoadLegend(VectorialLegend legend) {
1333
                this.loadLegend = legend;
1334
        }
1335

    
1336
        protected void putLoadSelection() throws XMLException {
1337
                if (this.loadSelection == null) return;
1338
                try {
1339
                        this.getRecordset().getSelectionSupport().setXMLEntity(this.loadSelection);
1340
                } catch (DriverException e) {
1341
                        throw new XMLException(e);
1342
                }
1343
                this.loadSelection = null;
1344

    
1345
        }
1346
        protected void putLoadLegend() throws FieldNotFoundException, DriverException {
1347
                if (this.loadLegend == null) return;
1348

    
1349
                this.setLegend(this.loadLegend);
1350

    
1351
                this.loadLegend = null;
1352

    
1353
        }
1354

    
1355
        protected void cleanLoadOptions() {
1356
                this.loadLegend = null;
1357
                this.loadSelection = null;
1358
        }
1359

    
1360
        public boolean isWritable() {
1361
                VectorialDriver drv = getSource().getDriver();
1362
                if (!drv.isWritable())
1363
                        return false;
1364
//                VectorialEditableAdapter vea = (VectorialEditableAdapter) lyrVect
1365
//                                .getSource();
1366
                if (drv instanceof IWriteable)
1367
                {
1368
                        IWriter writer = ((IWriteable)drv).getWriter();
1369
                        if (writer != null)
1370
                        {
1371
                                if (writer instanceof ISpatialWriter)
1372
                                        return true;
1373
                        }
1374
                }
1375
                return false;
1376

    
1377
        }
1378

    
1379
        public void beforePrinting(PrintRequestAttributeSet properties) {
1380
                VectorialLegend vl=(VectorialLegend)getLegend();
1381
                if (vl instanceof ClassifiedLegendInfo) {
1382
                        ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) vl;
1383
                        ISymbol[] symbols=clsfLegend.getSymbols();
1384
                        for (int i=0;i<symbols.length;i++) {
1385
                                symbols[i].setPrintingProperties(properties);
1386
                        }
1387
                }
1388
                if (vl.getDefaultSymbol()!=null) {
1389
                        vl.getDefaultSymbol().setPrintingProperties(properties);
1390
                }
1391
        }
1392

    
1393
        public void afterPrinting() {
1394
                VectorialLegend vl=(VectorialLegend)getLegend();
1395
                if (vl instanceof ClassifiedLegendInfo) {
1396
                        ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) vl;
1397
                        ISymbol[] symbols=clsfLegend.getSymbols();
1398
                        for (int i=0;i<symbols.length;i++) {
1399
                                symbols[i].setPrintingProperties(null);
1400
                        }
1401
                }
1402
                if (vl.getDefaultSymbol()!=null) {
1403
                        vl.getDefaultSymbol().setPrintingProperties(null);
1404
                }
1405

    
1406
        }
1407

    
1408
        public FLayer cloneLayer() throws Exception {
1409
                FLyrVect clonedLayer = new FLyrVect();
1410
                clonedLayer.setSource(getSource());
1411
                clonedLayer.setVisible(isVisible());
1412
                clonedLayer.setISpatialIndex(getISpatialIndex());
1413
                clonedLayer.setName(getName());
1414
                clonedLayer.setCoordTrans(getCoordTrans());
1415

    
1416
                if (legend.getLabelField() != null) // Hay etiquetas
1417
                {
1418
                        FLyrText lyrText = getLayerText();
1419
                        String antLabelField = legend.getLabelField();
1420
                        VectorialLegend clonedLegend = (VectorialLegend) legend.cloneLegend();
1421
                        clonedLegend.setLabelField(null);
1422
                        clonedLayer.setLegend(clonedLegend);
1423
                        clonedLayer.setLayerText(lyrText);
1424
                        clonedLegend.setLabelField(antLabelField);
1425

    
1426
                }
1427
                else
1428
                        clonedLayer.setLegend((VectorialLegend)getLegend());
1429
                return clonedLayer;
1430
        }
1431

    
1432
        /**
1433
         * This method tells the driver that the layer is going to be removed
1434
         * from the view, if it is a VectorialDatabaseDriver, because those
1435
         * drivers can have problems if that event is not notified.
1436
         * This overwrites FLyrDefault.removingThisLayer().
1437
         */
1438
        public void removingThisLayer() {
1439

    
1440
                Driver drv = sds.getDriver();
1441
                if (drv != null) {
1442
                        if (drv instanceof VectorialDatabaseDriver) {
1443
                                ((VectorialDatabaseDriver) drv).remove();
1444
                        }
1445
                } else {
1446
                        logger.warn("Unable to send removing event to driver (driver is null)");
1447
                }
1448

    
1449
        }
1450

    
1451
}