Statistics
| Revision:

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

History | View | Annotate | Download (42.9 KB)

1 1100 fjp
/* 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 214 fernando
package com.iver.cit.gvsig.fmap.layers;
42
43 6311 caballero
import java.awt.Color;
44 6312 caballero
import java.awt.Font;
45 1828 fernando
import java.awt.Graphics2D;
46 5048 ldiaz
import java.awt.Point;
47 1828 fernando
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.image.BufferedImage;
50 4977 azabala
import java.io.File;
51 2978 fjp
import java.io.IOException;
52 1828 fernando
53 9013 caballero
import javax.print.attribute.PrintRequestAttributeSet;
54
55 1828 fernando
import org.apache.log4j.Logger;
56
import org.cresques.cts.ICoordTrans;
57
58 10048 fjp
import com.hardcode.driverManager.Driver;
59 408 fernando
import com.hardcode.driverManager.DriverLoadException;
60 2694 fjp
import com.hardcode.gdbms.engine.data.DataSourceFactory;
61 1828 fernando
import com.hardcode.gdbms.engine.data.NoSuchTableException;
62 470 fjp
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
63 652 fernando
import com.iver.cit.gvsig.fmap.DriverException;
64 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType;
65 214 fernando
import com.iver.cit.gvsig.fmap.ViewPort;
66 757 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
67 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
68 5152 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
69 2978 fjp
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
70 214 fernando
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
71 2778 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
72 3301 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
73 6534 jmvivo
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
74 5390 caballero
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
75 4256 caballero
import com.iver.cit.gvsig.fmap.edition.AnnotationEditableAdapter;
76 5184 caballero
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
77 4147 fjp
import com.iver.cit.gvsig.fmap.edition.EditionException;
78 6856 fjp
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 3981 caballero
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
82 4159 fjp
import com.iver.cit.gvsig.fmap.edition.VectorialEditableDBAdapter;
83 562 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
84
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
85 5048 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
86 562 fernando
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 5057 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialXMLItem;
91 5201 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
92 214 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
93 231 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
94
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
95 926 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
96 9066 caballero
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
97 461 fernando
import com.iver.cit.gvsig.fmap.rendering.Legend;
98
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
99 214 fernando
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
100 3035 fjp
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
101 321 fernando
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
102 4977 azabala
import com.iver.cit.gvsig.fmap.spatialindex.IPersistentSpatialIndex;
103
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex;
104 5414 azabala
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeGt2;
105 4977 azabala
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
106
import com.iver.cit.gvsig.fmap.spatialindex.SpatialIndexException;
107
import com.iver.utiles.FileUtils;
108 3301 fjp
import com.iver.utiles.IPersistance;
109 2672 fjp
import com.iver.utiles.PostProcessSupport;
110 415 fernando
import com.iver.utiles.XMLEntity;
111 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
112
import com.iver.utiles.swing.threads.CancellableMonitorable;
113 214 fernando
114 562 fernando
/**
115 1034 vcaballero
 * Capa b?sica Vectorial.
116 5184 caballero
 *
117 562 fernando
 * @author Fernando Gonz?lez Cort?s
118
 */
119 885 fjp
120 5152 fjp
// 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 526 vcaballero
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
125 562 fernando
126 526 vcaballero
        /** Leyenda de la capa vectorial */
127 562 fernando
        private VectorialLegend legend;
128 5152 fjp
129 562 fernando
        private int typeShape = -1;
130 3963 caballero
131 562 fernando
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
132 5152 fjp
133 3940 caballero
        private ReadableVectorial source;
134 5152 fjp
135 4455 fjp
        private SelectableDataSource sds;
136 5152 fjp
137 4455 fjp
        private SelectionSupport selectionSupport = new SelectionSupport();
138 5390 caballero
139 5813 fjp
        private SpatialCache spatialCache = new SpatialCache();
140 5574 fjp
        private boolean spatialCacheEnabled = false;
141 3940 caballero
142 5152 fjp
        // protected SpatialIndex spatialIndex = null;
143 4977 azabala
        /**
144
         * An implementation of gvSIG spatial index
145
         */
146 5152 fjp
        protected ISpatialIndex spatialIndex = null;
147 5115 caballero
148 5152 fjp
        private boolean bHasJoin = false;
149 6476 caballero
150 6400 jmvivo
        private XMLEntity orgXMLEntity = null;
151 9066 caballero
152 6761 jmvivo
        private XMLEntity loadSelection = null;
153
        private VectorialLegend loadLegend = null;
154 461 fernando
155 9066 caballero
156 562 fernando
        /**
157 1034 vcaballero
         * A?ade un SelectionListener a la lista de listeners.
158 5184 caballero
         *
159 5152 fjp
         * @param listener
160
         *            SelectionListener.
161 562 fernando
         */
162 5152 fjp
        /*
163
         * public void addSelectionListener(SelectionListener listener) { try {
164
         * getRecordset().addSelectionListener(listener); } catch (DriverException
165
         * e) { // TODO Auto-generated catch block e.printStackTrace(); } }
166
         */
167 562 fernando
        /**
168 1034 vcaballero
         * Borra un selectionListener de la lista de listeners.
169 5184 caballero
         *
170 5152 fjp
         * @param listener
171
         *            SelectionListener
172 562 fernando
         */
173 5152 fjp
        /*
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 562 fernando
        /**
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 5152 fjp
        /*
186
         * public void fireSelectionEvents() { try {
187
         * getRecordset().fireSelectionEvents(); } catch (DriverException e) { //
188
         * TODO Auto-generated catch block e.printStackTrace(); } }
189
         */
190 562 fernando
        /**
191 1034 vcaballero
         * Devuelve el VectorialAdapater de la capa.
192 5184 caballero
         *
193 1034 vcaballero
         * @return VectorialAdapter.
194 562 fernando
         */
195 3940 caballero
        public ReadableVectorial getSource() {
196 6543 jmvivo
                if (!this.isAvailable()) return null;
197 562 fernando
                return source;
198
        }
199 5115 caballero
200 4977 azabala
        /**
201 5152 fjp
         * 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 5184 caballero
         *
205 4977 azabala
         */
206 5152 fjp
        private void loadSpatialIndex() {
207 5414 azabala
                //FIXME: Al abrir el indice en fichero...
208
                //?C?mo lo liberamos? un metodo Layer.shutdown()
209 5484 caballero
210
211 4977 azabala
                ReadableVectorial source = getSource();
212 5414 azabala
                //REVISAR QUE PASA CON LOS DRIVERS DXF, DGN, etc.
213
                //PUES SON VECTORIALFILEADAPTER
214 5152 fjp
                if (!(source instanceof VectorialFileAdapter)) {
215
                        // we are not interested in db adapters
216 4977 azabala
                        return;
217
                }
218
                VectorialDriver driver = source.getDriver();
219 5152 fjp
                if (!(driver instanceof BoundedShapes)) {
220
                        // we dont spatially index layers that are not bounded
221 4977 azabala
                        return;
222
                }
223 5152 fjp
                File file = ((VectorialFileAdapter) source).getFile();
224 5414 azabala
                String fileName = file.getAbsolutePath();
225
                File sptFile = new File(fileName + ".qix");
226 5152 fjp
                if (!sptFile.exists() || (!(sptFile.length() > 0))) {
227
                        // before to exit, look for it in temp path
228 4977 azabala
                        String tempPath = System.getProperty("java.io.tmpdir");
229
                        fileName = tempPath + File.separator + sptFile.getName();
230
                        sptFile = new File(fileName);
231 5152 fjp
                        // it doesnt exists, must to create
232
                        if (!sptFile.exists() || (!(sptFile.length() > 0))) {
233 4977 azabala
                                return;
234 5152 fjp
                        }// if
235
                }// if
236 5484 caballero
237 4977 azabala
                try {
238 5414 azabala
                        source.start();
239
                        spatialIndex = new QuadtreeGt2(FileUtils.getFileWithoutExtension(sptFile),
240
                                        "NM", source.getFullExtent(), source.getShapeCount(), false);
241 4977 azabala
                } catch (SpatialIndexException e) {
242
                        spatialIndex = null;
243
                        e.printStackTrace();
244
                        return;
245 5414 azabala
                } catch (DriverIOException e) {
246
                        // TODO Auto-generated catch block
247
                        e.printStackTrace();
248 5484 caballero
                }
249
250 4977 azabala
        }
251 5115 caballero
252 4977 azabala
        /**
253
         * Checks if it has associated an external spatial index
254 5414 azabala
         * (an spatial index file).
255 5484 caballero
         *
256 5414 azabala
         * 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 5484 caballero

260 4977 azabala
         * @return
261
         */
262 5152 fjp
        public boolean isExternallySpatiallyIndexed() {
263 5414 azabala
                /*
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 4977 azabala
                ReadableVectorial source = getSource();
269 5152 fjp
                if (!(source instanceof VectorialFileAdapter)) {
270 5414 azabala
                        // we are not interested in db adapters.
271
                        // think in non spatial dbs, like HSQLDB
272 4977 azabala
                        return false;
273
                }
274 5152 fjp
                File file = ((VectorialFileAdapter) source).getFile();
275 5414 azabala
                String fileName = file.getAbsolutePath();
276
                File sptFile = new File(fileName + ".qix");
277 5152 fjp
                if (!sptFile.exists() || (!(sptFile.length() > 0))) {
278
                        // before to exit, look for it in temp path
279
                        // it doesnt exists, must to create
280 4977 azabala
                        String tempPath = System.getProperty("java.io.tmpdir");
281
                        fileName = tempPath + File.separator + sptFile.getName();
282
                        sptFile = new File(fileName);
283 5152 fjp
                        if (!sptFile.exists() || (!(sptFile.length() > 0))) {
284 4977 azabala
                                return false;
285 5152 fjp
                        }// if
286
                }// if
287 4977 azabala
                return true;
288
        }
289 472 fernando
290 562 fernando
        /**
291 1034 vcaballero
         * Inserta el VectorialAdapter a la capa.
292 5184 caballero
         *
293 5152 fjp
         * @param va
294
         *            VectorialAdapter.
295 562 fernando
         */
296 3940 caballero
        public void setSource(ReadableVectorial rv) {
297
                source = rv;
298 5152 fjp
                // azabala: we check if this layer could have a file spatial index
299 5414 azabala
                // and load it if it exists
300 4977 azabala
                loadSpatialIndex();
301 562 fernando
        }
302 214 fernando
303
        /**
304 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
305 214 fernando
         */
306 652 fernando
        public Rectangle2D getFullExtent() throws DriverException {
307 885 fjp
                try {
308 652 fernando
                        Rectangle2D rAux;
309 8765 jjdelcerro
//                        logger.debug(getName() + "_source.start()");
310 652 fernando
                        source.start();
311 6246 caballero
                        rAux = (Rectangle2D)source.getFullExtent().clone();
312 8765 jjdelcerro
//                        logger.debug(getName() + "_source.stop()");
313 652 fernando
                        source.stop();
314 885 fjp
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 6476 caballero
                        //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 652 fernando
                        return rAux;
333 885 fjp
                } catch (DriverIOException e) {
334 652 fernando
                        throw new DriverException(e);
335
                }
336 214 fernando
        }
337
338
        /**
339 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
340 8765 jjdelcerro
         *      java.awt.Graphics2D, ISymbol)
341 214 fernando
         */
342 562 fernando
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
343 5152 fjp
                        Cancellable cancel, double scale) throws DriverException {
344 6246 caballero
345 5152 fjp
                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 5813 fjp
                                        prepareDrawing(image, g, viewPort);
357 5152 fjp
                                        strategy.draw(image, g, viewPort, cancel);
358
                                } catch (DriverException e) {
359 3462 caballero
                                        this.setVisible(false);
360
                                        this.setActive(false);
361
                                        throw e;
362
                                }
363 5152 fjp
                        }
364
                        if (getVirtualLayers() != null) {
365
                                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
366
                        }
367 3940 caballero
368 5152 fjp
                        if (getLayerText() != null) {
369
                                getLayerText().draw(image, g, viewPort, cancel, scale);
370
                        }
371 885 fjp
                }
372 214 fernando
        }
373
374
        /**
375 5813 fjp
         * 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 6246 caballero
383 5813 fjp
        }
384
385
        /**
386 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
387 5152 fjp
         *      com.iver.cit.gvsig.fmap.ViewPort,
388 5317 fjp
         *      com.iver.utiles.swing.threads.Cancellable)
389 562 fernando
         */
390 5152 fjp
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
391 9013 caballero
                        double scale, PrintRequestAttributeSet properties) throws DriverException {
392 5152 fjp
                if (isVisible() && isWithinScale(scale)) {
393
                        Strategy strategy = StrategyManager.getStrategy(this);
394 650 vcaballero
395 9013 caballero
                        strategy.print(g, viewPort, cancel, properties);
396 757 fjp
397 5152 fjp
                        if (getLayerText() != null) {
398
                                getLayerText().draw(null, g, viewPort, cancel, scale);
399
                        }
400 885 fjp
                }
401 562 fernando
        }
402
403
        /**
404 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
405 214 fernando
         */
406 2629 fjp
        // public FLayer createLabelLayer(int fieldId) {
407 5152 fjp
        public FLayer createLabelLayer(SelectableDataSource ds) {
408 3247 fjp
                FLyrText layerText = null;
409 5152 fjp
                try {
410
                        layerText = new FLyrText();
411 763 fjp
                        layerText.setLegend((VectorialLegend) getLegend());
412 5152 fjp
                        layerText.createLabels(this);
413 763 fjp
                } catch (FieldNotFoundException e1) {
414
                        // TODO Auto-generated catch block
415
                        e1.printStackTrace();
416
                } catch (DriverException e1) {
417
                        // TODO Auto-generated catch block
418
                        e1.printStackTrace();
419 5152 fjp
                }
420 885 fjp
421 757 fjp
                setLayerText(layerText);
422 3438 caballero
                layerText.setCoordTrans(getCoordTrans());
423 757 fjp
                return layerText;
424 214 fernando
        }
425
426
        /**
427 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
428 214 fernando
         */
429 228 fernando
        public void removeLabels() {
430 757 fjp
                setLayerText(null);
431 214 fernando
        }
432
433 5152 fjp
        /*
434
         * (non-Javadoc)
435 5184 caballero
         *
436 5152 fjp
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#deleteSpatialIndex()
437
         */
438
        public void deleteSpatialIndex() {
439 5414 azabala
                //must we delete possible spatial indexes files?
440 5152 fjp
                spatialIndex = null;
441
        }
442 5484 caballero
443
444
445 5414 azabala
   /**
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 5484 caballero
    */
455 5414 azabala
    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 5484 caballero
460 5414 azabala
            //If this vectorial layer is based in a spatial database, the spatial
461 5484 caballero
                //index is already implicit. We only will index file drivers
462 5414 azabala
            ReadableVectorial va = getSource();
463
            //We must think in non spatial databases, like HSQLDB
464
                if(!(va instanceof VectorialFileAdapter)){
465 4977 azabala
                        return;
466
                }
467 5152 fjp
                if (!(va.getDriver() instanceof BoundedShapes)) {
468 4977 azabala
                        return;
469
                }
470 5152 fjp
                File file = ((VectorialFileAdapter) va).getFile();
471 5414 azabala
                String fileName = file.getAbsolutePath();
472
                ISpatialIndex localCopy = null;
473 5152 fjp
                try {
474 5414 azabala
                        va.start();
475
                        localCopy = new QuadtreeGt2(fileName, "NM", va.getFullExtent(),
476
                                        va.getShapeCount(), true);
477 5484 caballero
478 4977 azabala
                } catch (SpatialIndexException e1) {
479 5152 fjp
                        // Probably we dont have writing permissions
480 4977 azabala
                        String directoryName = System.getProperty("java.io.tmpdir");
481 5484 caballero
                        File newFile = new File(directoryName +
482
                                        File.separator +
483 5414 azabala
                                        file.getName());
484 4977 azabala
                        String newFileName = newFile.getName();
485
                        try {
486 5414 azabala
                                localCopy = new QuadtreeGt2(newFileName, "NM", va.getFullExtent(),
487
                                                va.getShapeCount(), true);
488 4977 azabala
                        } catch (SpatialIndexException e) {
489 5414 azabala
                                // 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 4977 azabala
                        }
496 5115 caballero
497 5414 azabala
                }catch(Exception e){
498 5152 fjp
                        e.printStackTrace();
499 5414 azabala
                }//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 5916 azabala
                if(r != null)
511
                        localCopy.insert(r, i);
512 5414 azabala
            } // 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 5484 caballero
526 5414 azabala
        /**
527 4977 azabala
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
528
         */
529
        public void createSpatialIndex() {
530
                createSpatialIndex(null);
531 214 fernando
        }
532
533
        /**
534 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
535 5152 fjp
         *      FBitSet)
536 214 fernando
         */
537 2969 fjp
        public void process(FeatureVisitor visitor, FBitSet subset)
538 5152 fjp
                        throws DriverException, VisitException {
539 562 fernando
                Strategy s = StrategyManager.getStrategy(this);
540
                s.process(visitor, subset);
541 228 fernando
        }
542
543
        /**
544 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
545
         */
546 5152 fjp
        public void process(FeatureVisitor visitor) throws DriverException,
547
                        VisitException {
548 562 fernando
                Strategy s = StrategyManager.getStrategy(this);
549
                s.process(visitor);
550
        }
551 4223 caballero
552 4200 azabala
        /**
553 5152 fjp
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
554
         *      Rectangle2D)
555 4200 azabala
         */
556
        public void process(FeatureVisitor visitor, Rectangle2D rect)
557 5152 fjp
                        throws DriverException, VisitException {
558 4200 azabala
                Strategy s = StrategyManager.getStrategy(this);
559
                s.process(visitor, rect);
560
        }
561 562 fernando
562
        /**
563 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
564
         */
565 5152 fjp
        /*
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 228 fernando
        /**
572
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
573
         */
574 5152 fjp
        /*
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 228 fernando
        /**
580 456 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
581
         */
582 5152 fjp
        /*
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 456 fernando
        /**
588 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
589
         */
590 5152 fjp
        /*
591
         * public void clearSelection() { try { getRecordset().clearSelection(); }
592
         * catch (DriverException e) { // TODO Auto-generated catch block
593
         * e.printStackTrace(); } }
594
         */
595 228 fernando
596 5152 fjp
        /*
597
         * (non-Javadoc)
598 5184 caballero
         *
599 2978 fjp
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
600 214 fernando
         */
601 2978 fjp
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
602 488 vcaballero
                Strategy s = StrategyManager.getStrategy(this);
603 562 fernando
604 488 vcaballero
                return s.queryByRect(rect);
605 214 fernando
        }
606
607 5152 fjp
        public FBitSet queryByPoint(Point2D p, double tolerance)
608
                        throws DriverException {
609
                Strategy s = StrategyManager.getStrategy(this);
610
                return s.queryByPoint(p, tolerance);
611
        }
612 3940 caballero
613 5152 fjp
        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 5048 ldiaz
619 8765 jjdelcerro
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws DriverException {
620 6881 cesar
                Point2D pReal = this.getMapContext().getViewPort().toMapPoint(p);
621 5152 fjp
                FBitSet bs = queryByPoint(pReal, tolerance);
622
                VectorialXMLItem[] item = new VectorialXMLItem[1];
623
                item[0] = new VectorialXMLItem(bs, this);
624 5115 caballero
625 5152 fjp
                return item;
626
        }
627
628 214 fernando
        /**
629 1828 fernando
         * @throws DriverException
630 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
631
         */
632 5152 fjp
        /*
633
         * public SelectableDataSource getRecordset() throws DriverException { if
634
         * (sds == null){ try { DataSource ds = getSource().getRecordset();
635 5184 caballero
         *
636 5152 fjp
         * if (ds == null) { return null; }
637 5184 caballero
         *
638 5152 fjp
         * sds = new SelectableDataSource(ds);
639
         * //sds.setSelectionSupport(selectionSupport);
640 5184 caballero
         *
641 5152 fjp
         * 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 214 fernando
        /**
647 5152 fjp
         * 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 5184 caballero
         *
650 5152 fjp
         * @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 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
663 5152 fjp
         *      com.iver.cit.gvsig.fmap.rendering.Legend)
664 214 fernando
         */
665 5152 fjp
        public void setLegend(VectorialLegend r) throws DriverException,
666
                        FieldNotFoundException {
667 486 fjp
                VectorialLegend oldLegend = legend;
668 264 fernando
                legend = r;
669 885 fjp
670
                try {
671 652 fernando
                        legend.setDataSource(getRecordset());
672 885 fjp
673
                        if (legend.getLabelField() != null) {
674 5152 fjp
                                // sds.start();
675
                                // int idLabelField =
676
                                // getRecordset().getFieldIndexByName(legend.getLabelField());
677 3963 caballero
                                createLabelLayer(getSource().getRecordset());
678 2629 fjp
                                // sds.stop();
679 5152 fjp
                        } else
680
                                removeLabels();
681 885 fjp
                } catch (DriverException e) {
682 652 fernando
                        throw new DriverException(e);
683 757 fjp
                } catch (FieldNotFoundException e) {
684
                        // TODO Auto-generated catch block
685
                        e.printStackTrace();
686 5152 fjp
                } catch (DriverLoadException e) {
687 3963 caballero
                        // TODO Auto-generated catch block
688
                        e.printStackTrace();
689
                }
690 562 fernando
691 5152 fjp
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(
692
                                oldLegend, legend);
693 486 fjp
                callLegendChanged(e);
694 214 fernando
        }
695 303 fernando
696 321 fernando
        /**
697 1034 vcaballero
         * Devuelve la Leyenda de la capa.
698 5184 caballero
         *
699 1034 vcaballero
         * @return Leyenda.
700 321 fernando
         */
701 562 fernando
        public Legend getLegend() {
702
                return legend;
703 321 fernando
        }
704 373 fernando
705
        /**
706 1034 vcaballero
         * Devuelve el tipo de shape que contiene la capa.
707 5184 caballero
         *
708 1034 vcaballero
         * @return tipo de shape.
709 5184 caballero
         *
710 1034 vcaballero
         * @throws DriverException
711 373 fernando
         */
712 652 fernando
        public int getShapeType() throws DriverException {
713 562 fernando
                if (typeShape == -1) {
714 885 fjp
                        try {
715 8765 jjdelcerro
//                                logger.debug("source.start()");
716 4213 caballero
                                getSource().start();
717
                                typeShape = getSource().getShapeType();
718 8765 jjdelcerro
//                                logger.debug("source.stop()");
719 4213 caballero
                                getSource().stop();
720 885 fjp
                        } catch (DriverIOException e) {
721 652 fernando
                                throw new DriverException(e);
722
                        }
723 526 vcaballero
                }
724 562 fernando
725 526 vcaballero
                return typeShape;
726 373 fernando
        }
727 435 vcaballero
728
        /**
729 1828 fernando
         * @throws XMLException
730 435 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
731
         */
732 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
733 6761 jmvivo
                if (!this.isAvailable() && this.orgXMLEntity != null) {
734 6400 jmvivo
                        return this.orgXMLEntity;
735
                }
736 562 fernando
                XMLEntity xml = super.getXMLEntity();
737 5152 fjp
                xml.addChild(getLegend().getXMLEntity());
738
                try {
739 3963 caballero
                        xml.addChild(getRecordset().getSelectionSupport().getXMLEntity());
740 562 fernando
741 4455 fjp
                } catch (DriverException e1) {
742
                        e1.printStackTrace();
743
                        throw new XMLException(e1);
744
                }
745 5484 caballero
                //Repongo el mismo ReadableVectorial m?s abajo para cuando se guarda el proyecto.
746
                ReadableVectorial rv=getSource();
747 5152 fjp
                if (getSource() instanceof VectorialEditableAdapter) {
748
                        setSource(((VectorialEditableAdapter) source).getOriginalAdapter());
749 3981 caballero
                }
750 4213 caballero
                if (getSource() instanceof VectorialFileAdapter) {
751 1828 fernando
                        xml.putProperty("type", "vectorial");
752 5152 fjp
                        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 562 fernando
                } else if (source instanceof VectorialDBAdapter) {
766 5152 fjp
                        xml.putProperty("type", "vectorial");
767 3940 caballero
768 5152 fjp
                        VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) getSource()
769
                                        .getDriver();
770 3940 caballero
771 5152 fjp
                        // 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 3940 caballero
792 5152 fjp
                        VectorialDriver driver = (VectorialDriver) getSource().getDriver();
793 3940 caballero
794 5152 fjp
                        // 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 3940 caballero
                }
817 5484 caballero
                setSource(rv);
818 562 fernando
                xml.putProperty("driverName", getSource().getDriver().getName());
819 5152 fjp
                if (bHasJoin)
820
                        xml.putProperty("hasJoin", "true");
821 3940 caballero
822 435 vcaballero
                return xml;
823
        }
824
825
        /**
826
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
827
         */
828 5152 fjp
        public void setXMLEntity03(XMLEntity xml) throws XMLException {
829 3940 caballero
830 2183 fernando
                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 3940 caballero
                }
841 2183 fernando
842 4455 fjp
                try {
843 5152 fjp
                        getRecordset().getSelectionSupport()
844
                                        .setXMLEntity03(xml.getChild(1));
845 4455 fjp
                } catch (DriverException e) {
846
                        e.printStackTrace();
847
                }
848 2183 fernando
        }
849
850
        /**
851
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
852
         */
853 5152 fjp
        public void setXMLEntity(XMLEntity xml) throws XMLException {
854 4455 fjp
                try {
855 6400 jmvivo
                        super.setXMLEntity(xml);
856 6476 caballero
857 6400 jmvivo
                        VectorialLegend leg = LegendFactory.createFromXML(xml.getChild(0));
858 5152 fjp
                        try {
859 6400 jmvivo
                                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
860 6761 jmvivo
                                /* JMVIVO
861
                                this.setLoadSelection(xml.getChild(1));
862
                                */
863
                                // JMVIVO: Esto sirve para algo????
864 6400 jmvivo
                                String recordsetName = xml.getStringProperty("recordset-name");
865 6476 caballero
866 6400 jmvivo
                                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
                                SelectableDataSource sds = new SelectableDataSource(LayerFactory
873
                                                .getDataSourceFactory().createRandomDataSource(
874 10171 fjp
                                                                recordsetName, DataSourceFactory.MANUAL_OPENING));
875 6400 jmvivo
                                // sds.setSelectionSupport(selectionSupport);
876
                                // ((EditableAdapter)getSource()).setRecordSet(sds);
877
                        } catch (NoSuchTableException e1) {
878 6534 jmvivo
                                this.setAvailable(false);
879 6400 jmvivo
                                throw new XMLException(e1);
880
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
881 6534 jmvivo
                                this.setAvailable(false);
882 6400 jmvivo
                                throw new XMLException(e1);
883
                        } catch (DriverLoadException e1) {
884 6534 jmvivo
                                this.setAvailable(false);
885 6400 jmvivo
                                throw new XMLException(e1);
886
                        } catch (DriverException e1) {
887 6534 jmvivo
                                this.setAvailable(false);
888 6400 jmvivo
                                throw new XMLException(e1);
889 5152 fjp
                        }
890 6400 jmvivo
                        // Si tiene una uni?n, lo marcamos para que no se cree la leyenda hasta
891
                        // el final
892
                        // de la lectura del proyecto
893
                        if (xml.contains("hasJoin")) {
894
                                setIsJoined(true);
895
                                PostProcessSupport.addToPostProcess(this, "setLegend", leg, 1);
896
                        } else {
897 6761 jmvivo
                                /*
898 9066 caballero
                                JMVIVO
899 6761 jmvivo
                                this.setLoadLegend(leg);
900
                                */
901 6400 jmvivo
                                try {
902
                                        // legend.setDataSource(getRecordset());
903
                                        setLegend(leg);
904
                                } catch (FieldNotFoundException e) {
905
                                        throw new XMLException(e);
906
                                } catch (DriverException e) {
907
                                        throw new XMLException(e);
908
                                }
909
                        }
910 6476 caballero
911 6400 jmvivo
                } catch (XMLException e) {
912 6534 jmvivo
                        this.setAvailable(false);
913 6400 jmvivo
                        this.orgXMLEntity = xml;
914
                        //throw e;
915
                } catch (Exception e) {
916 6534 jmvivo
                        this.setAvailable(false);
917 6400 jmvivo
                        this.orgXMLEntity = xml;
918
                        //throw new XMLException(e);
919 5152 fjp
                }
920 3940 caballero
921 6476 caballero
922 435 vcaballero
        }
923 562 fernando
924 6768 jmvivo
        public void setXMLEntityNew(XMLEntity xml) throws XMLException {
925
                try {
926
                        super.setXMLEntity(xml);
927
928
                        VectorialLegend leg = LegendFactory.createFromXML(xml.getChild(0));
929
                        try {
930
                                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
931 9066 caballero
932 6768 jmvivo
                                this.setLoadSelection(xml.getChild(1));
933
                                /*
934
                                // JMVIVO: Esto sirve para algo????
935
                                String recordsetName = xml.getStringProperty("recordset-name");
936

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

949 9066 caballero

950 6768 jmvivo
                        } catch (NoSuchTableException e1) {
951
                                this.setAvailable(false);
952
                                throw new XMLException(e1);
953
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
954
                                this.setAvailable(false);
955
                                throw new XMLException(e1);
956
                        */
957
                        } catch (DriverLoadException e1) {
958
                                this.setAvailable(false);
959
                                throw new XMLException(e1);
960
                        } catch (DriverException e1) {
961
                                this.setAvailable(false);
962
                                throw new XMLException(e1);
963
                        }
964
                        // Si tiene una uni?n, lo marcamos para que no se cree la leyenda hasta
965
                        // el final
966
                        // de la lectura del proyecto
967
                        if (xml.contains("hasJoin")) {
968
                                setIsJoined(true);
969
                                PostProcessSupport.addToPostProcess(this, "setLegend", leg, 1);
970
                        } else {
971 9066 caballero
972
973
                                this.setLoadLegend(leg);
974 6768 jmvivo
                                /*
975
                                try {
976
                                        // legend.setDataSource(getRecordset());
977
                                        setLegend(leg);
978
                                } catch (FieldNotFoundException e) {
979
                                        throw new XMLException(e);
980
                                } catch (DriverException e) {
981
                                        throw new XMLException(e);
982
                                }
983
                                */
984
                        }
985
986
                } catch (XMLException e) {
987
                        this.setAvailable(false);
988
                        this.orgXMLEntity = xml;
989
                        //throw e;
990
                } catch (Exception e) {
991
                        this.setAvailable(false);
992
                        this.orgXMLEntity = xml;
993
                        //throw new XMLException(e);
994
                }
995
996
997
        }
998 9066 caballero
999
1000
1001 562 fernando
        /**
1002 1034 vcaballero
         * A?ade un LegendListener a la lista de Listeners.
1003 5184 caballero
         *
1004 5152 fjp
         * @param listener
1005
         *            LegendListener.
1006 562 fernando
         */
1007
        public void addLegendListener(LegendListener listener) {
1008 472 fernando
                layerChangeSupport.addLayerListener(listener);
1009
        }
1010 562 fernando
1011
        /**
1012 1034 vcaballero
         * Llamada al m?todo callLegendChanged de los listener.
1013 5184 caballero
         *
1014 5152 fjp
         * @param e
1015
         *            Evento.
1016 562 fernando
         */
1017 486 fjp
        private void callLegendChanged(LegendChangedEvent e) {
1018 472 fernando
                layerChangeSupport.callLegendChanged(e);
1019
        }
1020 562 fernando
1021
        /**
1022 1034 vcaballero
         * Borra un LegendListener de la lista de Listeners
1023 5184 caballero
         *
1024 5152 fjp
         * @param listener
1025
         *            LegendListener.
1026 562 fernando
         */
1027
        public void removeLegendListener(LegendListener listener) {
1028 472 fernando
                layerChangeSupport.removeLayerListener(listener);
1029
        }
1030 544 fernando
1031
        /**
1032 1034 vcaballero
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
1033
         * identifiquen la capa.
1034 5184 caballero
         *
1035 911 fernando
         * @return DOCUMENT ME!
1036
         */
1037
        public String toString() {
1038
                /*
1039 5152 fjp
                 * Se usa internamente para que la parte de datos identifique de forma
1040
                 * un?voca las tablas
1041 911 fernando
                 */
1042
                String ret = super.toString();
1043
1044
                return "layer" + ret.substring(ret.indexOf('@') + 1);
1045
        }
1046 2672 fjp
1047 5152 fjp
        public boolean isJoined() {
1048
                return bHasJoin;
1049
        }
1050 5115 caballero
1051 5152 fjp
        /**
1052 4977 azabala
         * Returns if a layer is spatially indexed
1053 5184 caballero
         *
1054 5152 fjp
         * @return if this layer has the ability to proces spatial queries without
1055
         *         secuential scans.
1056 4977 azabala
         */
1057 5152 fjp
        public boolean isSpatiallyIndexed() {
1058 4977 azabala
                ReadableVectorial source = getSource();
1059 5152 fjp
                if (source instanceof ISpatialDB)
1060 4977 azabala
                        return true;
1061 6246 caballero
1062 5794 azabala
//FIXME azabala
1063
/*
1064
 * Esto es muy dudoso, y puede cambiar.
1065
 * Estoy diciendo que las que no son fichero o no son
1066
 * BoundedShapes estan indexadas. Esto es mentira, pero
1067
 * as? quien pregunte no querr? generar el indice.
1068
 * Esta por ver si interesa generar el indice para capas
1069
 * HSQLDB, WFS, etc.
1070 6246 caballero
 */
1071 5794 azabala
                if(!(source instanceof VectorialFileAdapter)){
1072
                        return true;
1073
                }
1074
                if (!(source.getDriver() instanceof BoundedShapes)) {
1075
                        return true;
1076
                }
1077 6246 caballero
1078 5152 fjp
                if (getISpatialIndex() != null)
1079 4977 azabala
                        return true;
1080
                return false;
1081
        }
1082 2672 fjp
1083 5152 fjp
        public void setIsJoined(boolean hasJoin) {
1084
                bHasJoin = hasJoin;
1085
        }
1086 2978 fjp
1087 5152 fjp
        /**
1088
         * @return Returns the spatialIndex.
1089
         */
1090
        public ISpatialIndex getISpatialIndex() {
1091
                return spatialIndex;
1092
        }
1093 5679 azabala
        /**
1094
         * Sets the spatial index. This could be useful if, for some
1095
         * reasons, you want to work with a distinct spatial index
1096 6246 caballero
         * (for example, a spatial index which could makes nearest
1097 5679 azabala
         * neighbour querys)
1098
         * @param spatialIndex
1099
         */
1100
        public void setISpatialIndex(ISpatialIndex spatialIndex){
1101
                this.spatialIndex = spatialIndex;
1102
        }
1103 3366 caballero
1104 6246 caballero
1105 5152 fjp
        /*
1106
         * public SelectableDataSource getRecordset() { try { return
1107
         * getSource().getRecordset(); } catch (DriverLoadException e) { // TODO
1108
         * Auto-generated catch block e.printStackTrace(); } return null; }
1109
         */
1110 4977 azabala
1111 4455 fjp
        /**
1112
         * @throws DriverException
1113
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
1114
         */
1115
        public SelectableDataSource getRecordset() throws DriverException {
1116 6543 jmvivo
                if (!this.isAvailable()) return null;
1117 5152 fjp
                if (sds == null) {
1118 4455 fjp
                        try {
1119
                                SelectableDataSource ds = source.getRecordset();
1120
1121
                                if (ds == null) {
1122
                                        return null;
1123
                                }
1124
1125
                                sds = ds;
1126
                                sds.setSelectionSupport(selectionSupport);
1127 4523 caballero
1128 4455 fjp
                        } catch (DriverLoadException e) {
1129 8765 jjdelcerro
                                DriverNotLoadedExceptionType exceptionType =
1130
                                        new DriverNotLoadedExceptionType();
1131
                                exceptionType.setDriverName(source.getDriver().getName());
1132
                                throw new DriverException(e, exceptionType);
1133 5152 fjp
                        }
1134 4455 fjp
                }
1135
                return sds;
1136 4523 caballero
        }
1137
1138 5152 fjp
        /*
1139
         * (non-Javadoc)
1140 5184 caballero
         *
1141 4147 fjp
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setEditing(boolean)
1142
         */
1143 5152 fjp
        public void setEditing(boolean b) throws EditionException {
1144 4147 fjp
                super.setEditing(b);
1145 5152 fjp
                try {
1146
                        if (b) {
1147
                                VectorialEditableAdapter vea = null;
1148
                                // TODO: Qu? pasa si hay m?s tipos de adapters?
1149
                                // FJP: Se podr?a pasar como argumento el
1150
                                // VectorialEditableAdapter
1151
                                // que se quiera usar para evitar meter c?digo aqu? de este
1152
                                // estilo.
1153
                                if (getSource() instanceof VectorialDBAdapter) {
1154
                                        vea = new VectorialEditableDBAdapter();
1155
                                } else if (this instanceof FLyrAnnotation) {
1156 5390 caballero
                                        vea = new AnnotationEditableAdapter(
1157
                                                        (FLyrAnnotation) this);
1158
                                } else {
1159 5152 fjp
                                        vea = new VectorialEditableAdapter();
1160 5115 caballero
                                }
1161 5152 fjp
                                vea.setOriginalVectorialAdapter(getSource());
1162 5115 caballero
1163 5152 fjp
                                // /vea.setSpatialIndex(getSpatialIndex());
1164
                                // /vea.setFullExtent(getFullExtent());
1165 5184 caballero
                                vea.startEdition(EditionEvent.GRAPHIC);
1166 5152 fjp
                                setSource(vea);
1167
                                getRecordset().setSelectionSupport(
1168
                                                vea.getOriginalAdapter().getRecordset()
1169
                                                                .getSelectionSupport());
1170
1171
                        } else {
1172
                                VectorialEditableAdapter vea = (VectorialEditableAdapter) getSource();
1173
                                setSource(vea.getOriginalAdapter());
1174 4159 fjp
                        }
1175 5152 fjp
                        // Si tenemos una leyenda, hay que pegarle el cambiazo a su
1176
                        // recordset
1177
                        setRecordset(getSource().getRecordset());
1178
                        if (getLegend() instanceof VectorialLegend) {
1179
                                VectorialLegend ley = (VectorialLegend) getLegend();
1180
                                ley.setDataSource(getSource().getRecordset());
1181 5184 caballero
                                // Esto lo pongo para evitar que al dibujar sobre un
1182 5152 fjp
                                // dxf, dwg, o dgn no veamos nada. Es debido al checkbox
1183
                                // de la leyenda de textos "dibujar solo textos".
1184 5390 caballero
                                if (!(getSource().getDriver() instanceof IndexedShpDriver)){
1185 6312 caballero
                                        FSymbol symbol=new FSymbol(getShapeType());
1186
                                        symbol.setFontSizeInPixels(false);
1187
                                        symbol.setFont(new Font("SansSerif", Font.PLAIN, 9));
1188
                                        Color color=symbol.getColor();
1189
                                        int alpha=symbol.getColor().getAlpha();
1190 6311 caballero
                                        if (alpha>250) {
1191 6312 caballero
                                                symbol.setColor(new Color(color.getRed(),color.getGreen(),color.getBlue(),100));
1192 6311 caballero
                                        }
1193 6312 caballero
                                        ley.setDefaultSymbol(symbol);
1194 5152 fjp
                                }
1195 5390 caballero
                                ley.useDefaultSymbol(true);
1196 4147 fjp
                        }
1197 5152 fjp
                } catch (DriverLoadException e) {
1198
                        e.printStackTrace();
1199
                        throw new EditionException(e);
1200
                } catch (DriverException e) {
1201
                        e.printStackTrace();
1202
                        throw new EditionException(e);
1203
                } catch (FieldNotFoundException e) {
1204
                        e.printStackTrace();
1205
                        throw new EditionException(e);
1206 4147 fjp
                }
1207 4223 caballero
1208 5576 fjp
                setSpatialCacheEnabled(b);
1209 5152 fjp
                callEditionChanged(LayerEvent
1210
                                .createEditionChangedEvent(this, "edition"));
1211
1212 4147 fjp
        }
1213
1214 5152 fjp
        /**
1215
         * Para cuando haces una uni?n, sustituyes el recorset por el nuevo. De esta
1216
         * forma, podr?s poner leyendas basadas en el nuevo recordset
1217 5184 caballero
         *
1218 5152 fjp
         * @param newSds
1219
         */
1220
        public void setRecordset(SelectableDataSource newSds) {
1221
                sds = newSds;
1222
                sds.setSelectionSupport(selectionSupport);
1223 3366 caballero
        }
1224 5152 fjp
        /*
1225
         * public SelectionSupport getSelectionSupport() { return selectionSupport; }
1226 5184 caballero
         *
1227 5152 fjp
         * public void setSelectionSupport(SelectionSupport selectionSupport) {
1228
         * this.selectionSupport = selectionSupport; }
1229
         */
1230 3366 caballero
1231 5813 fjp
//        public SpatialCache createSpatialCache() {
1232
//               spatialCache = new SpatialCache();
1233
//                return spatialCache;
1234
//        }
1235
        public void clearSpatialCache()
1236
        {
1237
                spatialCache.clearAll();
1238 5223 fjp
        }
1239
1240 5226 fjp
//        public void setSpatialCache(SpatialCache spatialCache) {
1241
//                this.spatialCache = spatialCache;
1242
//        }
1243 5223 fjp
1244
        public boolean isSpatialCacheEnabled() {
1245
                return spatialCacheEnabled;
1246
        }
1247
1248
        public void setSpatialCacheEnabled(boolean spatialCacheEnabled) {
1249
                this.spatialCacheEnabled = spatialCacheEnabled;
1250
        }
1251
1252 5226 fjp
        public SpatialCache getSpatialCache() {
1253
                return spatialCache;
1254
        }
1255
1256 5813 fjp
        /**
1257
         * Siempre es un numero mayor de 1000
1258
         * @param maxFeatures
1259
         */
1260 5793 fjp
        public void setMaxFeaturesInEditionCache(int maxFeatures) {
1261 5813 fjp
                if (maxFeatures > spatialCache.maxFeatures)
1262
                        spatialCache.setMaxFeatures(maxFeatures);
1263 6246 caballero
1264 5793 fjp
        }
1265 6246 caballero
1266 6197 jorpiell
        /**
1267
         * This method returns a boolean that is used by the FPopMenu
1268
         * to make visible the properties menu or not. It is visible by
1269
         * default, and if a later don't have to show this menu only
1270
         * has to override this method.
1271
         * @return
1272
         * If the properties menu is visible (or not)
1273
         */
1274
        public boolean isPropertiesMenuVisible(){
1275
                return true;
1276
        }
1277 9066 caballero
1278 6534 jmvivo
        public void reload() throws DriverIOException {
1279
                super.reload();
1280
                try {
1281
                        this.source.getDriver().reload();
1282
                        //this.getRecordset().reload();
1283
                        if (this.getLegend() == null) {
1284
                    if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
1285
                        WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
1286 9066 caballero
                        this.setLegend((VectorialLegend) aux.getDefaultLegend());
1287 6534 jmvivo
                    } else {
1288
                        this.setLegend(LegendFactory.createSingleSymbolLegend(
1289
                                this.getShapeType()));
1290
                    }
1291
                        }
1292 9066 caballero
1293 6534 jmvivo
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1294 9066 caballero
                        this.setAvailable(false);
1295
                        throw new DriverIOException(e);
1296 6534 jmvivo
                } catch (IOException e) {
1297 9066 caballero
                        this.setAvailable(false);
1298 6534 jmvivo
                        throw new DriverIOException(e);
1299
                } catch (DriverException e) {
1300 9066 caballero
                        this.setAvailable(false);
1301 6534 jmvivo
                        throw new DriverIOException(e);
1302
                } catch (Exception e) {
1303 9066 caballero
                        this.setAvailable(false);
1304 6534 jmvivo
                        throw new DriverIOException(e);
1305
                }
1306 9066 caballero
1307 6534 jmvivo
        }
1308 9066 caballero
1309 6761 jmvivo
        protected void setLoadSelection(XMLEntity xml) {
1310
                this.loadSelection = xml;
1311
        }
1312 5793 fjp
1313 6761 jmvivo
        protected void setLoadLegend(VectorialLegend legend) {
1314
                this.loadLegend = legend;
1315
        }
1316 6534 jmvivo
1317 6761 jmvivo
        protected void putLoadSelection() throws XMLException {
1318
                if (this.loadSelection == null) return;
1319
                try {
1320
                        this.getRecordset().getSelectionSupport().setXMLEntity(this.loadSelection);
1321 9066 caballero
                } catch (DriverException e) {
1322 6761 jmvivo
                        throw new XMLException(e);
1323
                }
1324 9066 caballero
                this.loadSelection = null;
1325
1326 6761 jmvivo
        }
1327
        protected void putLoadLegend() throws FieldNotFoundException, DriverException {
1328
                if (this.loadLegend == null) return;
1329 9066 caballero
1330 6761 jmvivo
                this.setLegend(this.loadLegend);
1331 9066 caballero
1332
                this.loadLegend = null;
1333
1334 6761 jmvivo
        }
1335
1336
        protected void cleanLoadOptions() {
1337 9066 caballero
                this.loadLegend = null;
1338 6761 jmvivo
                this.loadSelection = null;
1339
        }
1340
1341 6856 fjp
        public boolean isWritable() {
1342 9066 caballero
                VectorialDriver drv = getSource().getDriver();
1343 6856 fjp
                if (!drv.isWritable())
1344
                        return false;
1345
//                VectorialEditableAdapter vea = (VectorialEditableAdapter) lyrVect
1346
//                                .getSource();
1347
                if (drv instanceof IWriteable)
1348
                {
1349
                        IWriter writer = ((IWriteable)drv).getWriter();
1350
                        if (writer != null)
1351
                        {
1352
                                if (writer instanceof ISpatialWriter)
1353
                                        return true;
1354
                        }
1355
                }
1356
                return false;
1357
1358
        }
1359
1360 9066 caballero
        public void beforePrinting(PrintRequestAttributeSet properties) {
1361
                VectorialLegend vl=(VectorialLegend)getLegend();
1362
                if (vl instanceof ClassifiedLegendInfo) {
1363
                        ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) vl;
1364
                        ISymbol[] symbols=clsfLegend.getSymbols();
1365
                        for (int i=0;i<symbols.length;i++) {
1366
                                symbols[i].setPrintingProperties(properties);
1367
                        }
1368
                }
1369
                if (vl.getDefaultSymbol()!=null) {
1370
                        vl.getDefaultSymbol().setPrintingProperties(properties);
1371
                }
1372
        }
1373
1374
        public void afterPrinting() {
1375
                VectorialLegend vl=(VectorialLegend)getLegend();
1376
                if (vl instanceof ClassifiedLegendInfo) {
1377
                        ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) vl;
1378
                        ISymbol[] symbols=clsfLegend.getSymbols();
1379
                        for (int i=0;i<symbols.length;i++) {
1380
                                symbols[i].setPrintingProperties(null);
1381
                        }
1382
                }
1383
                if (vl.getDefaultSymbol()!=null) {
1384
                        vl.getDefaultSymbol().setPrintingProperties(null);
1385
                }
1386
1387
        }
1388
1389 9225 fjp
        public FLayer cloneLayer() throws Exception {
1390
                FLyrVect clonedLayer = new FLyrVect();
1391
                clonedLayer.setSource(getSource());
1392
                clonedLayer.setVisible(isVisible());
1393
                clonedLayer.setISpatialIndex(getISpatialIndex());
1394
                clonedLayer.setName(getName());
1395
                clonedLayer.setCoordTrans(getCoordTrans());
1396
1397
                if (legend.getLabelField() != null) // Hay etiquetas
1398
                {
1399
                        FLyrText lyrText = getLayerText();
1400
                        String antLabelField = legend.getLabelField();
1401
                        VectorialLegend clonedLegend = (VectorialLegend) legend.cloneLegend();
1402
                        clonedLegend.setLabelField(null);
1403
                        clonedLayer.setLegend(clonedLegend);
1404
                        clonedLayer.setLayerText(lyrText);
1405
                        clonedLegend.setLabelField(antLabelField);
1406
1407
                }
1408
                else
1409
                        clonedLayer.setLegend((VectorialLegend)getLegend());
1410
                return clonedLayer;
1411
        }
1412
1413 10048 fjp
        /**
1414
         * This method tells the driver that the layer is going to be removed
1415
         * from the view, if it is a VectorialDatabaseDriver, because those
1416
         * drivers can have problems if that event is not notified.
1417
         * This overwrites FLyrDefault.removingThisLayer().
1418
         */
1419
        public void removingThisLayer() {
1420
1421
                Driver drv = sds.getDriver();
1422
                if (drv != null) {
1423
                        if (drv instanceof VectorialDatabaseDriver) {
1424
                                ((VectorialDatabaseDriver) drv).remove();
1425
                        }
1426
                } else {
1427
                        logger.warn("Unable to send removing event to driver (driver is null)");
1428
                }
1429
1430
        }
1431
1432 214 fernando
}