Statistics
| Revision:

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

History | View | Annotate | Download (42.3 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 408 fernando
import com.hardcode.driverManager.DriverLoadException;
59 2694 fjp
import com.hardcode.gdbms.engine.data.DataSourceFactory;
60 1828 fernando
import com.hardcode.gdbms.engine.data.NoSuchTableException;
61 470 fjp
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
62 652 fernando
import com.iver.cit.gvsig.fmap.DriverException;
63 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType;
64 214 fernando
import com.iver.cit.gvsig.fmap.ViewPort;
65 757 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
66 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
67 5152 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
68 2978 fjp
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
69 214 fernando
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
70 2778 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
71 3301 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
72 6534 jmvivo
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
73 5390 caballero
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
74 4256 caballero
import com.iver.cit.gvsig.fmap.edition.AnnotationEditableAdapter;
75 5184 caballero
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
76 4147 fjp
import com.iver.cit.gvsig.fmap.edition.EditionException;
77 6856 fjp
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
78
import com.iver.cit.gvsig.fmap.edition.IWriteable;
79
import com.iver.cit.gvsig.fmap.edition.IWriter;
80 3981 caballero
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
81 4159 fjp
import com.iver.cit.gvsig.fmap.edition.VectorialEditableDBAdapter;
82 562 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
83
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
84 5048 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
85 562 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
86
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
87
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
88
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
89 5057 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialXMLItem;
90 5201 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
91 214 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
92 231 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
93
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
94 926 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
95 9066 caballero
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
96 461 fernando
import com.iver.cit.gvsig.fmap.rendering.Legend;
97
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
98 214 fernando
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
99 3035 fjp
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
100 321 fernando
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
101 4977 azabala
import com.iver.cit.gvsig.fmap.spatialindex.IPersistentSpatialIndex;
102
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex;
103 5414 azabala
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeGt2;
104 4977 azabala
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
105
import com.iver.cit.gvsig.fmap.spatialindex.SpatialIndexException;
106
import com.iver.utiles.FileUtils;
107 3301 fjp
import com.iver.utiles.IPersistance;
108 2672 fjp
import com.iver.utiles.PostProcessSupport;
109 415 fernando
import com.iver.utiles.XMLEntity;
110 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
111
import com.iver.utiles.swing.threads.CancellableMonitorable;
112 214 fernando
113 562 fernando
/**
114 1034 vcaballero
 * Capa b?sica Vectorial.
115 5184 caballero
 *
116 562 fernando
 * @author Fernando Gonz?lez Cort?s
117
 */
118 885 fjp
119 5152 fjp
// TODO Cuando no sea para pruebas debe no ser public
120
public class FLyrVect extends FLyrDefault implements Labelable,
121
                ClassifiableVectorial, SingleLayer, VectorialData, RandomVectorialData,
122
                AlphanumericData, InfoByPoint {
123 526 vcaballero
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
124 562 fernando
125 526 vcaballero
        /** Leyenda de la capa vectorial */
126 562 fernando
        private VectorialLegend legend;
127 5152 fjp
128 562 fernando
        private int typeShape = -1;
129 3963 caballero
130 562 fernando
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
131 5152 fjp
132 3940 caballero
        private ReadableVectorial source;
133 5152 fjp
134 4455 fjp
        private SelectableDataSource sds;
135 5152 fjp
136 4455 fjp
        private SelectionSupport selectionSupport = new SelectionSupport();
137 5390 caballero
138 5813 fjp
        private SpatialCache spatialCache = new SpatialCache();
139 5574 fjp
        private boolean spatialCacheEnabled = false;
140 3940 caballero
141 5152 fjp
        // protected SpatialIndex spatialIndex = null;
142 4977 azabala
        /**
143
         * An implementation of gvSIG spatial index
144
         */
145 5152 fjp
        protected ISpatialIndex spatialIndex = null;
146 5115 caballero
147 5152 fjp
        private boolean bHasJoin = false;
148 6476 caballero
149 6400 jmvivo
        private XMLEntity orgXMLEntity = null;
150 9066 caballero
151 6761 jmvivo
        private XMLEntity loadSelection = null;
152
        private VectorialLegend loadLegend = null;
153 461 fernando
154 9066 caballero
155 562 fernando
        /**
156 1034 vcaballero
         * A?ade un SelectionListener a la lista de listeners.
157 5184 caballero
         *
158 5152 fjp
         * @param listener
159
         *            SelectionListener.
160 562 fernando
         */
161 5152 fjp
        /*
162
         * public void addSelectionListener(SelectionListener listener) { try {
163
         * getRecordset().addSelectionListener(listener); } catch (DriverException
164
         * e) { // TODO Auto-generated catch block e.printStackTrace(); } }
165
         */
166 562 fernando
        /**
167 1034 vcaballero
         * Borra un selectionListener de la lista de listeners.
168 5184 caballero
         *
169 5152 fjp
         * @param listener
170
         *            SelectionListener
171 562 fernando
         */
172 5152 fjp
        /*
173
         * public void removeSelectionListener(SelectionListener listener) { try {
174
         * getRecordset().removeSelectionListener(listener); } catch
175
         * (DriverException e) { // TODO Auto-generated catch block
176
         * e.printStackTrace(); } }
177
         */
178 562 fernando
        /**
179
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
180
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
181
         * eventos, se realiza la propagaci?n de manera manual al final de la
182
         * "r?faga" de eventos
183
         */
184 5152 fjp
        /*
185
         * public void fireSelectionEvents() { try {
186
         * getRecordset().fireSelectionEvents(); } catch (DriverException e) { //
187
         * TODO Auto-generated catch block e.printStackTrace(); } }
188
         */
189 562 fernando
        /**
190 1034 vcaballero
         * Devuelve el VectorialAdapater de la capa.
191 5184 caballero
         *
192 1034 vcaballero
         * @return VectorialAdapter.
193 562 fernando
         */
194 3940 caballero
        public ReadableVectorial getSource() {
195 6543 jmvivo
                if (!this.isAvailable()) return null;
196 562 fernando
                return source;
197
        }
198 5115 caballero
199 4977 azabala
        /**
200 5152 fjp
         * If we use a persistent spatial index associated with this layer, and the
201
         * index is not intrisic to the layer (for example spatial databases) this
202
         * method looks for existent spatial index, and loads it.
203 5184 caballero
         *
204 4977 azabala
         */
205 5152 fjp
        private void loadSpatialIndex() {
206 5414 azabala
                //FIXME: Al abrir el indice en fichero...
207
                //?C?mo lo liberamos? un metodo Layer.shutdown()
208 5484 caballero
209
210 4977 azabala
                ReadableVectorial source = getSource();
211 5414 azabala
                //REVISAR QUE PASA CON LOS DRIVERS DXF, DGN, etc.
212
                //PUES SON VECTORIALFILEADAPTER
213 5152 fjp
                if (!(source instanceof VectorialFileAdapter)) {
214
                        // we are not interested in db adapters
215 4977 azabala
                        return;
216
                }
217
                VectorialDriver driver = source.getDriver();
218 5152 fjp
                if (!(driver instanceof BoundedShapes)) {
219
                        // we dont spatially index layers that are not bounded
220 4977 azabala
                        return;
221
                }
222 5152 fjp
                File file = ((VectorialFileAdapter) source).getFile();
223 5414 azabala
                String fileName = file.getAbsolutePath();
224
                File sptFile = new File(fileName + ".qix");
225 5152 fjp
                if (!sptFile.exists() || (!(sptFile.length() > 0))) {
226
                        // before to exit, look for it in temp path
227 4977 azabala
                        String tempPath = System.getProperty("java.io.tmpdir");
228
                        fileName = tempPath + File.separator + sptFile.getName();
229
                        sptFile = new File(fileName);
230 5152 fjp
                        // it doesnt exists, must to create
231
                        if (!sptFile.exists() || (!(sptFile.length() > 0))) {
232 4977 azabala
                                return;
233 5152 fjp
                        }// if
234
                }// if
235 5484 caballero
236 4977 azabala
                try {
237 5414 azabala
                        source.start();
238
                        spatialIndex = new QuadtreeGt2(FileUtils.getFileWithoutExtension(sptFile),
239
                                        "NM", source.getFullExtent(), source.getShapeCount(), false);
240 4977 azabala
                } catch (SpatialIndexException e) {
241
                        spatialIndex = null;
242
                        e.printStackTrace();
243
                        return;
244 5414 azabala
                } catch (DriverIOException e) {
245
                        // TODO Auto-generated catch block
246
                        e.printStackTrace();
247 5484 caballero
                }
248
249 4977 azabala
        }
250 5115 caballero
251 4977 azabala
        /**
252
         * Checks if it has associated an external spatial index
253 5414 azabala
         * (an spatial index file).
254 5484 caballero
         *
255 5414 azabala
         * It looks for it in main file path, or in temp system path.
256
         * If main file is rivers.shp, it looks for a file called
257
         * rivers.shp.qix.
258 5484 caballero

259 4977 azabala
         * @return
260
         */
261 5152 fjp
        public boolean isExternallySpatiallyIndexed() {
262 5414 azabala
                /*
263
                 * FIXME (AZABALA): Independizar del tipo de fichero de ?ndice
264
                  * con el que se trabaje (ahora mismo considera la extension .qix,
265
             * pero esto depender? del tipo de ?ndice)
266
                 * */
267 4977 azabala
                ReadableVectorial source = getSource();
268 5152 fjp
                if (!(source instanceof VectorialFileAdapter)) {
269 5414 azabala
                        // we are not interested in db adapters.
270
                        // think in non spatial dbs, like HSQLDB
271 4977 azabala
                        return false;
272
                }
273 5152 fjp
                File file = ((VectorialFileAdapter) source).getFile();
274 5414 azabala
                String fileName = file.getAbsolutePath();
275
                File sptFile = new File(fileName + ".qix");
276 5152 fjp
                if (!sptFile.exists() || (!(sptFile.length() > 0))) {
277
                        // before to exit, look for it in temp path
278
                        // it doesnt exists, must to create
279 4977 azabala
                        String tempPath = System.getProperty("java.io.tmpdir");
280
                        fileName = tempPath + File.separator + sptFile.getName();
281
                        sptFile = new File(fileName);
282 5152 fjp
                        if (!sptFile.exists() || (!(sptFile.length() > 0))) {
283 4977 azabala
                                return false;
284 5152 fjp
                        }// if
285
                }// if
286 4977 azabala
                return true;
287
        }
288 472 fernando
289 562 fernando
        /**
290 1034 vcaballero
         * Inserta el VectorialAdapter a la capa.
291 5184 caballero
         *
292 5152 fjp
         * @param va
293
         *            VectorialAdapter.
294 562 fernando
         */
295 3940 caballero
        public void setSource(ReadableVectorial rv) {
296
                source = rv;
297 5152 fjp
                // azabala: we check if this layer could have a file spatial index
298 5414 azabala
                // and load it if it exists
299 4977 azabala
                loadSpatialIndex();
300 562 fernando
        }
301 214 fernando
302
        /**
303 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
304 214 fernando
         */
305 652 fernando
        public Rectangle2D getFullExtent() throws DriverException {
306 885 fjp
                try {
307 652 fernando
                        Rectangle2D rAux;
308 8765 jjdelcerro
//                        logger.debug(getName() + "_source.start()");
309 652 fernando
                        source.start();
310 6246 caballero
                        rAux = (Rectangle2D)source.getFullExtent().clone();
311 8765 jjdelcerro
//                        logger.debug(getName() + "_source.stop()");
312 652 fernando
                        source.stop();
313 885 fjp
314
                        // Si existe reproyecci?n, reproyectar el extent
315
                        ICoordTrans ct = getCoordTrans();
316
317
                        if (ct != null) {
318
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
319
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
320
                                pt1 = ct.convert(pt1, null);
321
                                pt2 = ct.convert(pt2, null);
322
                                rAux = new Rectangle2D.Double();
323
                                rAux.setFrameFromDiagonal(pt1, pt2);
324
                        }
325
326 6476 caballero
                        //Esto es para cuando se crea una capa nueva con el fullExtent de ancho y alto 0.
327
                        if (rAux.getWidth()==0 && rAux.getHeight()==0) {
328
                                rAux=new Rectangle2D.Double(0,0,100,100);
329
                        }
330
331 652 fernando
                        return rAux;
332 885 fjp
                } catch (DriverIOException e) {
333 652 fernando
                        throw new DriverException(e);
334
                }
335 214 fernando
        }
336
337
        /**
338 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
339 8765 jjdelcerro
         *      java.awt.Graphics2D, ISymbol)
340 214 fernando
         */
341 562 fernando
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
342 5152 fjp
                        Cancellable cancel, double scale) throws DriverException {
343 6246 caballero
344 5152 fjp
                if (isWithinScale(scale)) {
345
                        // Las que solo tienen etiquetado sin pintar el shape,
346
                        // no pasamos por ellas
347
                        boolean bDrawShapes = true;
348
                        if (legend instanceof SingleSymbolLegend) {
349
                                if (legend.getDefaultSymbol().isShapeVisible() == false)
350
                                        bDrawShapes = false;
351
                        }
352
                        if (bDrawShapes) {
353
                                Strategy strategy = StrategyManager.getStrategy(this);
354
                                try {
355 5813 fjp
                                        prepareDrawing(image, g, viewPort);
356 5152 fjp
                                        strategy.draw(image, g, viewPort, cancel);
357
                                } catch (DriverException e) {
358 3462 caballero
                                        this.setVisible(false);
359
                                        this.setActive(false);
360
                                        throw e;
361
                                }
362 5152 fjp
                        }
363
                        if (getVirtualLayers() != null) {
364
                                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
365
                        }
366 3940 caballero
367 5152 fjp
                        if (getLayerText() != null) {
368
                                getLayerText().draw(image, g, viewPort, cancel, scale);
369
                        }
370 885 fjp
                }
371 214 fernando
        }
372
373
        /**
374 5813 fjp
         * Se llama antes de empezar a pintar.
375
         * Es ?til para preparar la cache a emplear, las leyendas, etc.
376
         * @param image
377
         * @param g
378
         * @param viewPort
379
         */
380
        private void prepareDrawing(BufferedImage image, Graphics2D g, ViewPort viewPort) {
381 6246 caballero
382 5813 fjp
        }
383
384
        /**
385 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
386 5152 fjp
         *      com.iver.cit.gvsig.fmap.ViewPort,
387 5317 fjp
         *      com.iver.utiles.swing.threads.Cancellable)
388 562 fernando
         */
389 5152 fjp
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
390 9013 caballero
                        double scale, PrintRequestAttributeSet properties) throws DriverException {
391 5152 fjp
                if (isVisible() && isWithinScale(scale)) {
392
                        Strategy strategy = StrategyManager.getStrategy(this);
393 650 vcaballero
394 9013 caballero
                        strategy.print(g, viewPort, cancel, properties);
395 757 fjp
396 5152 fjp
                        if (getLayerText() != null) {
397
                                getLayerText().draw(null, g, viewPort, cancel, scale);
398
                        }
399 885 fjp
                }
400 562 fernando
        }
401
402
        /**
403 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
404 214 fernando
         */
405 2629 fjp
        // public FLayer createLabelLayer(int fieldId) {
406 5152 fjp
        public FLayer createLabelLayer(SelectableDataSource ds) {
407 3247 fjp
                FLyrText layerText = null;
408 5152 fjp
                try {
409
                        layerText = new FLyrText();
410 763 fjp
                        layerText.setLegend((VectorialLegend) getLegend());
411 5152 fjp
                        layerText.createLabels(this);
412 763 fjp
                } catch (FieldNotFoundException e1) {
413
                        // TODO Auto-generated catch block
414
                        e1.printStackTrace();
415
                } catch (DriverException e1) {
416
                        // TODO Auto-generated catch block
417
                        e1.printStackTrace();
418 5152 fjp
                }
419 885 fjp
420 757 fjp
                setLayerText(layerText);
421 3438 caballero
                layerText.setCoordTrans(getCoordTrans());
422 757 fjp
                return layerText;
423 214 fernando
        }
424
425
        /**
426 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
427 214 fernando
         */
428 228 fernando
        public void removeLabels() {
429 757 fjp
                setLayerText(null);
430 214 fernando
        }
431
432 5152 fjp
        /*
433
         * (non-Javadoc)
434 5184 caballero
         *
435 5152 fjp
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#deleteSpatialIndex()
436
         */
437
        public void deleteSpatialIndex() {
438 5414 azabala
                //must we delete possible spatial indexes files?
439 5152 fjp
                spatialIndex = null;
440
        }
441 5484 caballero
442
443
444 5414 azabala
   /**
445
    * <p>
446
    * Creates an spatial index associated to this layer.
447
    * The spatial index will used
448
    * the native projection of the layer, so if the layer is reprojected, it will
449
    * be ignored.
450
    * </p>
451
    * @param cancelMonitor instance of CancellableMonitorable that allows
452
    * to monitor progress of spatial index creation, and cancel the process
453 5484 caballero
    */
454 5414 azabala
    public void createSpatialIndex(CancellableMonitorable cancelMonitor){
455
             // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
456
        // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
457
        // para que acepten recorrer sin geometria, solo con rectangulos.
458 5484 caballero
459 5414 azabala
            //If this vectorial layer is based in a spatial database, the spatial
460 5484 caballero
                //index is already implicit. We only will index file drivers
461 5414 azabala
            ReadableVectorial va = getSource();
462
            //We must think in non spatial databases, like HSQLDB
463
                if(!(va instanceof VectorialFileAdapter)){
464 4977 azabala
                        return;
465
                }
466 5152 fjp
                if (!(va.getDriver() instanceof BoundedShapes)) {
467 4977 azabala
                        return;
468
                }
469 5152 fjp
                File file = ((VectorialFileAdapter) va).getFile();
470 5414 azabala
                String fileName = file.getAbsolutePath();
471
                ISpatialIndex localCopy = null;
472 5152 fjp
                try {
473 5414 azabala
                        va.start();
474
                        localCopy = new QuadtreeGt2(fileName, "NM", va.getFullExtent(),
475
                                        va.getShapeCount(), true);
476 5484 caballero
477 4977 azabala
                } catch (SpatialIndexException e1) {
478 5152 fjp
                        // Probably we dont have writing permissions
479 4977 azabala
                        String directoryName = System.getProperty("java.io.tmpdir");
480 5484 caballero
                        File newFile = new File(directoryName +
481
                                        File.separator +
482 5414 azabala
                                        file.getName());
483 4977 azabala
                        String newFileName = newFile.getName();
484
                        try {
485 5414 azabala
                                localCopy = new QuadtreeGt2(newFileName, "NM", va.getFullExtent(),
486
                                                va.getShapeCount(), true);
487 4977 azabala
                        } catch (SpatialIndexException e) {
488 5414 azabala
                                // if we cant build a file based spatial index, we'll build
489
                                // a pure memory spatial index
490
                                localCopy = new QuadtreeJts();
491
                        } catch (DriverIOException e) {
492
                                // TODO Auto-generated catch block
493
                                e.printStackTrace();
494 4977 azabala
                        }
495 5115 caballero
496 5414 azabala
                }catch(Exception e){
497 5152 fjp
                        e.printStackTrace();
498 5414 azabala
                }//try
499
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
500
        try {
501
            for (int i=0; i < va.getShapeCount(); i++)
502
            {
503
                    if(cancelMonitor != null){
504
                            if(cancelMonitor.isCanceled())
505
                                    return;
506
                            cancelMonitor.reportStep();
507
                    }
508
                Rectangle2D r = shapeBounds.getShapeBounds(i);
509 5916 azabala
                if(r != null)
510
                        localCopy.insert(r, i);
511 5414 azabala
            } // for
512
            va.stop();
513
            if(localCopy instanceof IPersistentSpatialIndex)
514
                    ((IPersistentSpatialIndex) localCopy).flush();
515
            spatialIndex = localCopy;
516
        } catch (DriverIOException e) {
517
            // TODO Auto-generated catch block
518
            e.printStackTrace();
519
        } catch (IOException e) {
520
            // TODO Auto-generated catch block
521
            e.printStackTrace();
522
        }
523
    }
524 5484 caballero
525 5414 azabala
        /**
526 4977 azabala
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
527
         */
528
        public void createSpatialIndex() {
529
                createSpatialIndex(null);
530 214 fernando
        }
531
532
        /**
533 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
534 5152 fjp
         *      FBitSet)
535 214 fernando
         */
536 2969 fjp
        public void process(FeatureVisitor visitor, FBitSet subset)
537 5152 fjp
                        throws DriverException, VisitException {
538 562 fernando
                Strategy s = StrategyManager.getStrategy(this);
539
                s.process(visitor, subset);
540 228 fernando
        }
541
542
        /**
543 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
544
         */
545 5152 fjp
        public void process(FeatureVisitor visitor) throws DriverException,
546
                        VisitException {
547 562 fernando
                Strategy s = StrategyManager.getStrategy(this);
548
                s.process(visitor);
549
        }
550 4223 caballero
551 4200 azabala
        /**
552 5152 fjp
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
553
         *      Rectangle2D)
554 4200 azabala
         */
555
        public void process(FeatureVisitor visitor, Rectangle2D rect)
556 5152 fjp
                        throws DriverException, VisitException {
557 4200 azabala
                Strategy s = StrategyManager.getStrategy(this);
558
                s.process(visitor, rect);
559
        }
560 562 fernando
561
        /**
562 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
563
         */
564 5152 fjp
        /*
565
         * public void setSelection(FBitSet selection) { try {
566
         * getRecordset().setSelection(selection); } catch (DriverException e) { //
567
         * TODO Auto-generated catch block e.printStackTrace(); }
568
         * fireSelectionEvents(); }
569
         */
570 228 fernando
        /**
571
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
572
         */
573 5152 fjp
        /*
574
         * public boolean isSelected(int index) { try { return
575
         * getRecordset().isSelected(index); } catch (DriverException e) { // TODO
576
         * Auto-generated catch block e.printStackTrace(); } return false; }
577
         */
578 228 fernando
        /**
579 456 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
580
         */
581 5152 fjp
        /*
582
         * public FBitSet getSelection() { try { return
583
         * getRecordset().getSelection(); } catch (DriverException e) { // TODO
584
         * Auto-generated catch block e.printStackTrace(); } return new FBitSet(); }
585
         */
586 456 fernando
        /**
587 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
588
         */
589 5152 fjp
        /*
590
         * public void clearSelection() { try { getRecordset().clearSelection(); }
591
         * catch (DriverException e) { // TODO Auto-generated catch block
592
         * e.printStackTrace(); } }
593
         */
594 228 fernando
595 5152 fjp
        /*
596
         * (non-Javadoc)
597 5184 caballero
         *
598 2978 fjp
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
599 214 fernando
         */
600 2978 fjp
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
601 488 vcaballero
                Strategy s = StrategyManager.getStrategy(this);
602 562 fernando
603 488 vcaballero
                return s.queryByRect(rect);
604 214 fernando
        }
605
606 5152 fjp
        public FBitSet queryByPoint(Point2D p, double tolerance)
607
                        throws DriverException {
608
                Strategy s = StrategyManager.getStrategy(this);
609
                return s.queryByPoint(p, tolerance);
610
        }
611 3940 caballero
612 5152 fjp
        public FBitSet queryByShape(IGeometry g, int relationship)
613
                        throws DriverException, VisitException {
614
                Strategy s = StrategyManager.getStrategy(this);
615
                return s.queryByShape(g, relationship);
616
        }
617 5048 ldiaz
618 8765 jjdelcerro
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws DriverException {
619 6881 cesar
                Point2D pReal = this.getMapContext().getViewPort().toMapPoint(p);
620 5152 fjp
                FBitSet bs = queryByPoint(pReal, tolerance);
621
                VectorialXMLItem[] item = new VectorialXMLItem[1];
622
                item[0] = new VectorialXMLItem(bs, this);
623 5115 caballero
624 5152 fjp
                return item;
625
        }
626
627 214 fernando
        /**
628 1828 fernando
         * @throws DriverException
629 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
630
         */
631 5152 fjp
        /*
632
         * public SelectableDataSource getRecordset() throws DriverException { if
633
         * (sds == null){ try { DataSource ds = getSource().getRecordset();
634 5184 caballero
         *
635 5152 fjp
         * if (ds == null) { return null; }
636 5184 caballero
         *
637 5152 fjp
         * sds = new SelectableDataSource(ds);
638
         * //sds.setSelectionSupport(selectionSupport);
639 5184 caballero
         *
640 5152 fjp
         * return sds; } catch (DriverLoadException e) { throw new
641
         * DriverException(e); } catch
642
         * (com.hardcode.gdbms.engine.data.driver.DriverException e) { throw new
643
         * DriverException(e); } } return sds; }
644
         */
645 214 fernando
        /**
646 5152 fjp
         * Para cuando haces una uni?n, sustituyes el recorset por el nuevo. De esta
647
         * forma, podr?s poner leyendas basadas en el nuevo recordset
648 5184 caballero
         *
649 5152 fjp
         * @param newSds
650
         * @throws DriverException
651
         * @throws FieldNotFoundException
652
         * @throws FieldNotFoundException
653
         */
654
        /*
655
         * public void setRecordset(SelectableDataSource newSds) throws
656
         * DriverException, FieldNotFoundException { sds = newSds;
657
         * //sds.setSelectionSupport(selectionSupport); legend.setDataSource(sds);
658
         * logger.debug("Recordset cambiado a " + sds.getName()); }
659
         */
660
        /**
661 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
662 5152 fjp
         *      com.iver.cit.gvsig.fmap.rendering.Legend)
663 214 fernando
         */
664 5152 fjp
        public void setLegend(VectorialLegend r) throws DriverException,
665
                        FieldNotFoundException {
666 486 fjp
                VectorialLegend oldLegend = legend;
667 264 fernando
                legend = r;
668 885 fjp
669
                try {
670 652 fernando
                        legend.setDataSource(getRecordset());
671 885 fjp
672
                        if (legend.getLabelField() != null) {
673 5152 fjp
                                // sds.start();
674
                                // int idLabelField =
675
                                // getRecordset().getFieldIndexByName(legend.getLabelField());
676 3963 caballero
                                createLabelLayer(getSource().getRecordset());
677 2629 fjp
                                // sds.stop();
678 5152 fjp
                        } else
679
                                removeLabels();
680 885 fjp
                } catch (DriverException e) {
681 652 fernando
                        throw new DriverException(e);
682 757 fjp
                } catch (FieldNotFoundException e) {
683
                        // TODO Auto-generated catch block
684
                        e.printStackTrace();
685 5152 fjp
                } catch (DriverLoadException e) {
686 3963 caballero
                        // TODO Auto-generated catch block
687
                        e.printStackTrace();
688
                }
689 562 fernando
690 5152 fjp
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(
691
                                oldLegend, legend);
692 486 fjp
                callLegendChanged(e);
693 214 fernando
        }
694 303 fernando
695 321 fernando
        /**
696 1034 vcaballero
         * Devuelve la Leyenda de la capa.
697 5184 caballero
         *
698 1034 vcaballero
         * @return Leyenda.
699 321 fernando
         */
700 562 fernando
        public Legend getLegend() {
701
                return legend;
702 321 fernando
        }
703 373 fernando
704
        /**
705 1034 vcaballero
         * Devuelve el tipo de shape que contiene la capa.
706 5184 caballero
         *
707 1034 vcaballero
         * @return tipo de shape.
708 5184 caballero
         *
709 1034 vcaballero
         * @throws DriverException
710 373 fernando
         */
711 652 fernando
        public int getShapeType() throws DriverException {
712 562 fernando
                if (typeShape == -1) {
713 885 fjp
                        try {
714 8765 jjdelcerro
//                                logger.debug("source.start()");
715 4213 caballero
                                getSource().start();
716
                                typeShape = getSource().getShapeType();
717 8765 jjdelcerro
//                                logger.debug("source.stop()");
718 4213 caballero
                                getSource().stop();
719 885 fjp
                        } catch (DriverIOException e) {
720 652 fernando
                                throw new DriverException(e);
721
                        }
722 526 vcaballero
                }
723 562 fernando
724 526 vcaballero
                return typeShape;
725 373 fernando
        }
726 435 vcaballero
727
        /**
728 1828 fernando
         * @throws XMLException
729 435 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
730
         */
731 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
732 6761 jmvivo
                if (!this.isAvailable() && this.orgXMLEntity != null) {
733 6400 jmvivo
                        return this.orgXMLEntity;
734
                }
735 562 fernando
                XMLEntity xml = super.getXMLEntity();
736 5152 fjp
                xml.addChild(getLegend().getXMLEntity());
737
                try {
738 3963 caballero
                        xml.addChild(getRecordset().getSelectionSupport().getXMLEntity());
739 562 fernando
740 4455 fjp
                } catch (DriverException e1) {
741
                        e1.printStackTrace();
742
                        throw new XMLException(e1);
743
                }
744 5484 caballero
                //Repongo el mismo ReadableVectorial m?s abajo para cuando se guarda el proyecto.
745
                ReadableVectorial rv=getSource();
746 5152 fjp
                if (getSource() instanceof VectorialEditableAdapter) {
747
                        setSource(((VectorialEditableAdapter) source).getOriginalAdapter());
748 3981 caballero
                }
749 4213 caballero
                if (getSource() instanceof VectorialFileAdapter) {
750 1828 fernando
                        xml.putProperty("type", "vectorial");
751 5152 fjp
                        xml.putProperty("file", ((VectorialFileAdapter) getSource())
752
                                        .getFile());
753
                        // try {
754
                        try {
755
                                xml.putProperty("recordset-name", getSource().getRecordset()
756
                                                .getName());
757
                        } catch (DriverLoadException e) {
758
                                // TODO Auto-generated catch block
759
                                e.printStackTrace();
760
                        }
761
                        // } catch (DriverException e) {
762
                        // throw new XMLException(e);
763
                        // }
764 562 fernando
                } else if (source instanceof VectorialDBAdapter) {
765 5152 fjp
                        xml.putProperty("type", "vectorial");
766 3940 caballero
767 5152 fjp
                        VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) getSource()
768
                                        .getDriver();
769 3940 caballero
770 5152 fjp
                        // Guardamos el nombre del driver para poder recuperarlo
771
                        // con el DriverManager de Fernando.
772
                        xml.putProperty("db", dbDriver.getName());
773
                        // try {
774
                        try {
775
                                xml.putProperty("recordset-name", getSource().getRecordset()
776
                                                .getName());
777
                        } catch (DriverLoadException e) {
778
                                // TODO Auto-generated catch block
779
                                e.printStackTrace();
780
                        }
781
                        // } catch (DriverException e) {
782
                        // throw new XMLException(e);
783
                        // }
784
                        xml.addChild(dbDriver.getXMLEntity()); // Tercer child. Antes hemos
785
                                                                                                        // metido la leyenda y el
786
                                                                                                        // selection support
787
                } else if (source instanceof VectorialAdapter) {
788
                        // Se supone que hemos hecho algo gen?rico.
789
                        xml.putProperty("type", "vectorial");
790 3940 caballero
791 5152 fjp
                        VectorialDriver driver = (VectorialDriver) getSource().getDriver();
792 3940 caballero
793 5152 fjp
                        // Guardamos el nombre del driver para poder recuperarlo
794
                        // con el DriverManager de Fernando.
795
                        xml.putProperty("other", driver.getName());
796
                        // try {
797
                        try {
798
                                xml.putProperty("recordset-name", getSource().getRecordset()
799
                                                .getName());
800
                        } catch (DriverLoadException e) {
801
                                // TODO Auto-generated catch block
802
                                e.printStackTrace();
803
                        }
804
                        // } catch (DriverException e) {
805
                        // throw new XMLException(e);
806
                        // }
807
                        if (driver instanceof IPersistance) {
808
                                // xml.putProperty("className", driver.getClass().getName());
809
                                IPersistance persist = (IPersistance) driver;
810
                                xml.addChild(persist.getXMLEntity()); // Tercer child. Antes
811
                                                                                                                // hemos metido la
812
                                                                                                                // leyenda y el
813
                                                                                                                // selection support
814
                        }
815 3940 caballero
                }
816 5484 caballero
                setSource(rv);
817 562 fernando
                xml.putProperty("driverName", getSource().getDriver().getName());
818 5152 fjp
                if (bHasJoin)
819
                        xml.putProperty("hasJoin", "true");
820 3940 caballero
821 435 vcaballero
                return xml;
822
        }
823
824
        /**
825
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
826
         */
827 5152 fjp
        public void setXMLEntity03(XMLEntity xml) throws XMLException {
828 3940 caballero
829 2183 fernando
                super.setXMLEntity(xml);
830
                legend = LegendFactory.createFromXML03(xml.getChild(0));
831
832
                try {
833
                        // legend.setDataSource(getRecordset());
834
                        setLegend(legend);
835
                } catch (FieldNotFoundException e) {
836
                        throw new XMLException(e);
837
                } catch (DriverException e) {
838
                        throw new XMLException(e);
839 3940 caballero
                }
840 2183 fernando
841 4455 fjp
                try {
842 5152 fjp
                        getRecordset().getSelectionSupport()
843
                                        .setXMLEntity03(xml.getChild(1));
844 4455 fjp
                } catch (DriverException e) {
845
                        e.printStackTrace();
846
                }
847 2183 fernando
        }
848
849
        /**
850
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
851
         */
852 5152 fjp
        public void setXMLEntity(XMLEntity xml) throws XMLException {
853 4455 fjp
                try {
854 6400 jmvivo
                        super.setXMLEntity(xml);
855 6476 caballero
856 6400 jmvivo
                        VectorialLegend leg = LegendFactory.createFromXML(xml.getChild(0));
857 5152 fjp
                        try {
858 6400 jmvivo
                                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
859 6761 jmvivo
                                /* JMVIVO
860
                                this.setLoadSelection(xml.getChild(1));
861
                                */
862
                                // JMVIVO: Esto sirve para algo????
863 6400 jmvivo
                                String recordsetName = xml.getStringProperty("recordset-name");
864 6476 caballero
865 6400 jmvivo
                                LayerFactory.getDataSourceFactory().changeDataSourceName(
866
                                                getSource().getRecordset().getName(), recordsetName);
867
                                // Hacemos que el recordset que hemos creado antes (los
868
                                // recordset se crean antes que todo) sea el recordset
869
                                // de esta capa. Y del que se crea automaticamente
870
                                // en el setLegend del createLayer, nos olvidamos
871
                                SelectableDataSource sds = new SelectableDataSource(LayerFactory
872
                                                .getDataSourceFactory().createRandomDataSource(
873
                                                                recordsetName, DataSourceFactory.AUTOMATIC_OPENING));
874
                                // sds.setSelectionSupport(selectionSupport);
875
                                // ((EditableAdapter)getSource()).setRecordSet(sds);
876
                        } catch (NoSuchTableException e1) {
877 6534 jmvivo
                                this.setAvailable(false);
878 6400 jmvivo
                                throw new XMLException(e1);
879
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
880 6534 jmvivo
                                this.setAvailable(false);
881 6400 jmvivo
                                throw new XMLException(e1);
882
                        } catch (DriverLoadException e1) {
883 6534 jmvivo
                                this.setAvailable(false);
884 6400 jmvivo
                                throw new XMLException(e1);
885
                        } catch (DriverException e1) {
886 6534 jmvivo
                                this.setAvailable(false);
887 6400 jmvivo
                                throw new XMLException(e1);
888 5152 fjp
                        }
889 6400 jmvivo
                        // Si tiene una uni?n, lo marcamos para que no se cree la leyenda hasta
890
                        // el final
891
                        // de la lectura del proyecto
892
                        if (xml.contains("hasJoin")) {
893
                                setIsJoined(true);
894
                                PostProcessSupport.addToPostProcess(this, "setLegend", leg, 1);
895
                        } else {
896 6761 jmvivo
                                /*
897 9066 caballero
                                JMVIVO
898 6761 jmvivo
                                this.setLoadLegend(leg);
899
                                */
900 6400 jmvivo
                                try {
901
                                        // legend.setDataSource(getRecordset());
902
                                        setLegend(leg);
903
                                } catch (FieldNotFoundException e) {
904
                                        throw new XMLException(e);
905
                                } catch (DriverException e) {
906
                                        throw new XMLException(e);
907
                                }
908
                        }
909 6476 caballero
910 6400 jmvivo
                } catch (XMLException e) {
911 6534 jmvivo
                        this.setAvailable(false);
912 6400 jmvivo
                        this.orgXMLEntity = xml;
913
                        //throw e;
914
                } catch (Exception e) {
915 6534 jmvivo
                        this.setAvailable(false);
916 6400 jmvivo
                        this.orgXMLEntity = xml;
917
                        //throw new XMLException(e);
918 5152 fjp
                }
919 3940 caballero
920 6476 caballero
921 435 vcaballero
        }
922 562 fernando
923 6768 jmvivo
        public void setXMLEntityNew(XMLEntity xml) throws XMLException {
924
                try {
925
                        super.setXMLEntity(xml);
926
927
                        VectorialLegend leg = LegendFactory.createFromXML(xml.getChild(0));
928
                        try {
929
                                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
930 9066 caballero
931 6768 jmvivo
                                this.setLoadSelection(xml.getChild(1));
932
                                /*
933
                                // JMVIVO: Esto sirve para algo????
934
                                String recordsetName = xml.getStringProperty("recordset-name");
935

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

948 9066 caballero

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