Statistics
| Revision:

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

History | View | Annotate | Download (24.5 KB)

1 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 1828 fernando
import java.awt.Graphics2D;
44
import java.awt.geom.Point2D;
45
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47 2978 fjp
import java.io.IOException;
48 1828 fernando
49
import org.apache.log4j.Logger;
50
import org.cresques.cts.ICoordTrans;
51
52 408 fernando
import com.hardcode.driverManager.DriverLoadException;
53 2694 fjp
import com.hardcode.gdbms.engine.data.DataSourceFactory;
54 1828 fernando
import com.hardcode.gdbms.engine.data.NoSuchTableException;
55 470 fjp
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
56 652 fernando
import com.iver.cit.gvsig.fmap.DriverException;
57 214 fernando
import com.iver.cit.gvsig.fmap.ViewPort;
58 4223 caballero
import com.iver.cit.gvsig.fmap.MapControl.CancelDraw;
59 757 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
60 2978 fjp
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
61 214 fernando
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62 2778 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
63 3301 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
64 3963 caballero
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
65 4147 fjp
import com.iver.cit.gvsig.fmap.edition.EditionException;
66 3981 caballero
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
67 4159 fjp
import com.iver.cit.gvsig.fmap.edition.VectorialEditableDBAdapter;
68 562 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
69
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.Labelable;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
74 244 fernando
import com.iver.cit.gvsig.fmap.operations.Cancellable;
75 214 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
76 231 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
77
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
78 926 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
79 461 fernando
import com.iver.cit.gvsig.fmap.rendering.Legend;
80
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
81 214 fernando
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
82 3035 fjp
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
83 321 fernando
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
84 3301 fjp
import com.iver.utiles.IPersistance;
85 2672 fjp
import com.iver.utiles.PostProcessSupport;
86 415 fernando
import com.iver.utiles.XMLEntity;
87 2978 fjp
import com.vividsolutions.jts.geom.Coordinate;
88
import com.vividsolutions.jts.geom.Envelope;
89 3560 fjp
import com.vividsolutions.jts.index.SpatialIndex;
90 2978 fjp
import com.vividsolutions.jts.index.quadtree.Quadtree;
91 214 fernando
92 562 fernando
93
/**
94 1034 vcaballero
 * Capa b?sica Vectorial.
95 562 fernando
 *
96
 * @author Fernando Gonz?lez Cort?s
97
 */
98 885 fjp
99 609 luisw
//TODO Cuando no sea para pruebas debe no ser public
100 3963 caballero
public class FLyrVect extends FLyrDefault implements Labelable, ClassifiableVectorial, SingleLayer, VectorialData,
101
        RandomVectorialData,AlphanumericData {
102 526 vcaballero
        private static Logger logger = Logger.getLogger(FLyrVect.class.getName());
103 562 fernando
104 526 vcaballero
        /** Leyenda de la capa vectorial */
105 562 fernando
        private VectorialLegend legend;
106
        private int typeShape = -1;
107 3963 caballero
108 562 fernando
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
109 3940 caballero
        private ReadableVectorial source;
110 3963 caballero
        //private SelectableDataSource sds;
111 3940 caballero
112 4213 caballero
    protected SpatialIndex spatialIndex = null;
113 3940 caballero
114 2672 fjp
    private boolean bHasJoin = false;
115 461 fernando
116 562 fernando
        /**
117 1034 vcaballero
         * A?ade un SelectionListener a la lista de listeners.
118 562 fernando
         *
119 1034 vcaballero
         * @param listener SelectionListener.
120 562 fernando
         */
121 3963 caballero
        /*public void addSelectionListener(SelectionListener listener) {
122
                try {
123
                        getRecordset().addSelectionListener(listener);
124
                } catch (DriverException e) {
125
                        // TODO Auto-generated catch block
126
                        e.printStackTrace();
127
                }
128 562 fernando
        }
129 3963 caballero
*/
130 562 fernando
        /**
131 1034 vcaballero
         * Borra un selectionListener de la lista de listeners.
132 562 fernando
         *
133 1034 vcaballero
         * @param listener SelectionListener
134 562 fernando
         */
135 3963 caballero
        /*public void removeSelectionListener(SelectionListener listener) {
136
                try {
137
                        getRecordset().removeSelectionListener(listener);
138
                } catch (DriverException e) {
139
                        // TODO Auto-generated catch block
140
                        e.printStackTrace();
141
                }
142 562 fernando
        }
143 3963 caballero
*/
144 562 fernando
        /**
145
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
146
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
147
         * eventos, se realiza la propagaci?n de manera manual al final de la
148
         * "r?faga" de eventos
149
         */
150 3963 caballero
        /*public void fireSelectionEvents() {
151
                try {
152
                        getRecordset().fireSelectionEvents();
153
                } catch (DriverException e) {
154
                        // TODO Auto-generated catch block
155
                        e.printStackTrace();
156
                }
157 562 fernando
        }
158 3963 caballero
*/
159 562 fernando
        /**
160 1034 vcaballero
         * Devuelve el VectorialAdapater de la capa.
161 562 fernando
         *
162 1034 vcaballero
         * @return VectorialAdapter.
163 562 fernando
         */
164 3940 caballero
        public ReadableVectorial getSource() {
165 562 fernando
                return source;
166
        }
167 472 fernando
168 562 fernando
        /**
169 1034 vcaballero
         * Inserta el VectorialAdapter a la capa.
170 562 fernando
         *
171 1034 vcaballero
         * @param va VectorialAdapter.
172 562 fernando
         */
173 3940 caballero
        public void setSource(ReadableVectorial rv) {
174
                source = rv;
175 562 fernando
        }
176 214 fernando
177
        /**
178 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
179 214 fernando
         */
180 652 fernando
        public Rectangle2D getFullExtent() throws DriverException {
181 885 fjp
                try {
182 652 fernando
                        Rectangle2D rAux;
183
                        logger.debug("source.start()");
184
                        source.start();
185
                        rAux = source.getFullExtent();
186
                        logger.debug("source.stop()");
187
                        source.stop();
188 885 fjp
189
                        // Si existe reproyecci?n, reproyectar el extent
190
                        ICoordTrans ct = getCoordTrans();
191
192
                        if (ct != null) {
193
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
194
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
195
                                pt1 = ct.convert(pt1, null);
196
                                pt2 = ct.convert(pt2, null);
197
                                rAux = new Rectangle2D.Double();
198
                                rAux.setFrameFromDiagonal(pt1, pt2);
199
                        }
200
201 652 fernando
                        return rAux;
202 885 fjp
                } catch (DriverIOException e) {
203 652 fernando
                        throw new DriverException(e);
204
                }
205 214 fernando
        }
206
207
        /**
208 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
209
         *                 java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
210 214 fernando
         */
211 562 fernando
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
212 2531 caballero
                Cancellable cancel,double scale) throws DriverException {
213 3940 caballero
214
                if (isWithinScale(scale)){
215 3035 fjp
            // Las que solo tienen etiquetado sin pintar el shape,
216
            // no pasamos por ellas
217
            boolean bDrawShapes = true;
218
            if (legend instanceof SingleSymbolLegend)
219
            {
220
                if (legend.getDefaultSymbol().isShapeVisible() == false)
221
                    bDrawShapes = false;
222
            }
223
            if (bDrawShapes)
224
            {
225 3940 caballero
                Strategy strategy = StrategyManager.getStrategy(this);
226 3462 caballero
                try{
227 3035 fjp
                strategy.draw(image, g, viewPort, cancel);
228 3462 caballero
                }catch (DriverException e) {
229
                                        this.setVisible(false);
230
                                        this.setActive(false);
231
                                        throw e;
232
                                }
233 3035 fjp
            }
234 3940 caballero
235 3035 fjp
                    if (getVirtualLayers() != null) {
236
                            getVirtualLayers().draw(image, g, viewPort, cancel,scale);
237
                    }
238 3940 caballero
239 3035 fjp
                    if (getLayerText() != null) {
240
                            getLayerText().draw(image, g, viewPort, cancel,scale);
241
                    }
242 885 fjp
                }
243 214 fernando
        }
244
245
        /**
246 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
247
         *                 com.iver.cit.gvsig.fmap.ViewPort,
248
         *                 com.iver.cit.gvsig.fmap.operations.Cancellable)
249
         */
250 2531 caballero
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale)
251 652 fernando
                throws DriverException {
252 3940 caballero
                if (isVisible() && isWithinScale(scale)){
253 650 vcaballero
                Strategy strategy = StrategyManager.getStrategy(this);
254
255
                strategy.print(g, viewPort, cancel);
256 757 fjp
257 885 fjp
                if (getLayerText() != null) {
258 2531 caballero
                        getLayerText().draw(null, g, viewPort, cancel,scale);
259 885 fjp
                }
260 2531 caballero
                }
261 562 fernando
        }
262
263
        /**
264 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#createLabelLayer(int)
265 214 fernando
         */
266 2629 fjp
        // public FLayer createLabelLayer(int fieldId) {
267
    public FLayer createLabelLayer(SelectableDataSource ds) {
268 3247 fjp
                FLyrText layerText = null;
269
        try {
270
            layerText = new FLyrText();
271 763 fjp
                        layerText.setLegend((VectorialLegend) getLegend());
272 3247 fjp
            layerText.createLabels(this);
273 763 fjp
                } catch (FieldNotFoundException e1) {
274
                        // TODO Auto-generated catch block
275
                        e1.printStackTrace();
276
                } catch (DriverException e1) {
277
                        // TODO Auto-generated catch block
278
                        e1.printStackTrace();
279 3247 fjp
        }
280 885 fjp
281 3247 fjp
282 757 fjp
                setLayerText(layerText);
283 3438 caballero
                layerText.setCoordTrans(getCoordTrans());
284 757 fjp
                return layerText;
285 214 fernando
        }
286
287
        /**
288 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#removeLabels()
289 214 fernando
         */
290 228 fernando
        public void removeLabels() {
291 757 fjp
                setLayerText(null);
292 214 fernando
        }
293
294 3560 fjp
    /* (non-Javadoc)
295
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#deleteSpatialIndex()
296
     */
297
    public void deleteSpatialIndex()
298
    {
299
        spatialIndex = null;
300
    }
301 2978 fjp
        /* (non-Javadoc)
302
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
303 214 fernando
         */
304 2978 fjp
        public void createSpatialIndex() {
305
        // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
306
        // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
307
        // para que acepten recorrer sin geometria, solo con rectangulos.
308
        spatialIndex = new Quadtree();
309 3940 caballero
        ReadableVectorial va = getSource();
310 2978 fjp
        ICoordTrans ct = getCoordTrans();
311
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
312
        try {
313
            va.start();
314
315
            for (int i=0; i < va.getShapeCount(); i++)
316 3940 caballero
            {
317 2978 fjp
                Rectangle2D r = shapeBounds.getShapeBounds(i);
318
                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
319
                if (ct != null) {
320
                    r = ct.convert(r);
321
                }
322
                Coordinate c1 = new Coordinate(r.getMinX(), r.getMinY());
323
                Coordinate c2 = new Coordinate(r.getMaxX(), r.getMaxY());
324
                Envelope env = new Envelope(c1, c2);
325
                spatialIndex.insert(env, new Integer(i));
326
            } // for
327
            va.stop();
328
        } catch (DriverIOException e) {
329
            // TODO Auto-generated catch block
330
            e.printStackTrace();
331
        } catch (IOException e) {
332
            // TODO Auto-generated catch block
333
            e.printStackTrace();
334
        }
335 3940 caballero
336 214 fernando
        }
337
338
        /**
339 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialOperations#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
340 2969 fjp
         *                 FBitSet)
341 214 fernando
         */
342 2969 fjp
        public void process(FeatureVisitor visitor, FBitSet subset)
343 926 fernando
                throws DriverException, VisitException {
344 562 fernando
                Strategy s = StrategyManager.getStrategy(this);
345
                s.process(visitor, subset);
346 228 fernando
        }
347
348
        /**
349 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
350
         */
351 1034 vcaballero
        public void process(FeatureVisitor visitor)
352
                throws DriverException, VisitException {
353 562 fernando
                Strategy s = StrategyManager.getStrategy(this);
354
                s.process(visitor);
355
        }
356 4223 caballero
357 4200 azabala
        /**
358
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor, Rectangle2D)
359
         */
360
        public void process(FeatureVisitor visitor, Rectangle2D rect)
361
                throws DriverException, VisitException {
362
                Strategy s = StrategyManager.getStrategy(this);
363
                s.process(visitor, rect);
364
        }
365 562 fernando
366 4223 caballero
367
368 562 fernando
        /**
369 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setSelection(com.iver.cit.gvsig.fmap.operations.selection.VectorialSubSet)
370
         */
371 3963 caballero
        /*public void setSelection(FBitSet selection) {
372
                try {
373
                        getRecordset().setSelection(selection);
374
                } catch (DriverException e) {
375
                        // TODO Auto-generated catch block
376
                        e.printStackTrace();
377
                }
378 562 fernando
                fireSelectionEvents();
379 228 fernando
        }
380 3963 caballero
*/
381 228 fernando
        /**
382
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#isSelected(int)
383
         */
384 3963 caballero
        /*public boolean isSelected(int index) {
385
                try {
386
                        return getRecordset().isSelected(index);
387
                } catch (DriverException e) {
388
                        // TODO Auto-generated catch block
389
                        e.printStackTrace();
390
                }
391
                return false;
392 228 fernando
        }
393 3963 caballero
*/
394 228 fernando
        /**
395 456 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getSelection()
396
         */
397 3963 caballero
        /*public FBitSet getSelection() {
398
                try {
399
                        return getRecordset().getSelection();
400
                } catch (DriverException e) {
401
                        // TODO Auto-generated catch block
402
                        e.printStackTrace();
403
                }
404
                return new FBitSet();
405 456 fernando
        }
406 3963 caballero
*/
407 456 fernando
        /**
408 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#clearSelection()
409
         */
410 3963 caballero
        /*public void clearSelection() {
411
                try {
412
                        getRecordset().clearSelection();
413
                } catch (DriverException e) {
414
                        // TODO Auto-generated catch block
415
                        e.printStackTrace();
416
                }
417
        }*/
418 228 fernando
419 2978 fjp
        /* (non-Javadoc)
420
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
421 214 fernando
         */
422 2978 fjp
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
423 488 vcaballero
                Strategy s = StrategyManager.getStrategy(this);
424 562 fernando
425 488 vcaballero
                return s.queryByRect(rect);
426 214 fernando
        }
427
428 2978 fjp
    public FBitSet queryByPoint(Point2D p, double tolerance) throws DriverException
429
    {
430
        Strategy s = StrategyManager.getStrategy(this);
431 3940 caballero
        return s.queryByPoint(p, tolerance);
432 2978 fjp
    }
433 3940 caballero
434 2978 fjp
    public FBitSet queryByShape(IGeometry g, int relationship) throws DriverException, VisitException
435
    {
436
        Strategy s = StrategyManager.getStrategy(this);
437 3940 caballero
        return s.queryByShape(g, relationship);
438 2978 fjp
    }
439 214 fernando
        /**
440 1828 fernando
         * @throws DriverException
441 228 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#getRecordset()
442
         */
443 3963 caballero
        /*public SelectableDataSource getRecordset() throws DriverException {
444 1828 fernando
                if (sds == null){
445 911 fernando
                        try {
446 3612 fjp
                                DataSource ds = getSource().getRecordset();
447 885 fjp

448 652 fernando
                                if (ds == null) {
449
                                        return null;
450
                                }
451 885 fjp

452 1064 fernando
                                sds = new SelectableDataSource(ds);
453 3963 caballero
                                //sds.setSelectionSupport(selectionSupport);
454 3940 caballero

455 1828 fernando
                                return sds;
456 885 fjp
                        } catch (DriverLoadException e) {
457 652 fernando
                                throw new DriverException(e);
458 2565 fernando
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
459
                                throw new DriverException(e);
460
            }
461 544 fernando
                }
462 461 fernando
                return sds;
463 217 fernando
        }
464 3963 caballero
*/
465 2629 fjp
    /**
466
     * Para cuando haces una uni?n, sustituyes el recorset por el nuevo.
467
     * De esta forma, podr?s poner leyendas basadas en el nuevo
468
     * recordset
469
     * @param newSds
470 3940 caballero
     * @throws DriverException
471
     * @throws FieldNotFoundException
472
     * @throws FieldNotFoundException
473 2629 fjp
     */
474 3963 caballero
 /*   public void setRecordset(SelectableDataSource newSds) throws DriverException, FieldNotFoundException
475 2629 fjp
    {
476
        sds = newSds;
477 3963 caballero
        //sds.setSelectionSupport(selectionSupport);
478 2759 fjp
        legend.setDataSource(sds);
479 2700 fjp
        logger.debug("Recordset cambiado a " + sds.getName());
480 2629 fjp
    }
481 3963 caballero
*/
482 214 fernando
        /**
483 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.CommonOperations#setLegend(int,
484
         *                 com.iver.cit.gvsig.fmap.rendering.Legend)
485 214 fernando
         */
486 562 fernando
        public void setLegend(VectorialLegend r)
487 652 fernando
                throws DriverException, FieldNotFoundException {
488 486 fjp
                VectorialLegend oldLegend = legend;
489 264 fernando
                legend = r;
490 885 fjp
491
                try {
492 652 fernando
                        legend.setDataSource(getRecordset());
493 885 fjp
494
                        if (legend.getLabelField() != null) {
495 2629 fjp
                            // sds.start();
496
                                // int idLabelField = getRecordset().getFieldIndexByName(legend.getLabelField());
497 3963 caballero
                                createLabelLayer(getSource().getRecordset());
498 2629 fjp
                                // sds.stop();
499 757 fjp
                        }
500 1283 fjp
                        else
501
                            removeLabels();
502 885 fjp
                } catch (DriverException e) {
503 652 fernando
                        throw new DriverException(e);
504 757 fjp
                } catch (FieldNotFoundException e) {
505
                        // TODO Auto-generated catch block
506
                        e.printStackTrace();
507 3963 caballero
        } catch (DriverLoadException e) {
508
                        // TODO Auto-generated catch block
509
                        e.printStackTrace();
510
                }
511 562 fernando
512 1082 fernando
                LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, legend);
513 486 fjp
                callLegendChanged(e);
514 214 fernando
        }
515 303 fernando
516 321 fernando
        /**
517 1034 vcaballero
         * Devuelve la Leyenda de la capa.
518 562 fernando
         *
519 1034 vcaballero
         * @return Leyenda.
520 321 fernando
         */
521 562 fernando
        public Legend getLegend() {
522
                return legend;
523 321 fernando
        }
524 373 fernando
525
        /**
526 1034 vcaballero
         * Devuelve el tipo de shape que contiene la capa.
527 562 fernando
         *
528 1034 vcaballero
         * @return tipo de shape.
529 562 fernando
         *
530 1034 vcaballero
         * @throws DriverException
531 373 fernando
         */
532 652 fernando
        public int getShapeType() throws DriverException {
533 562 fernando
                if (typeShape == -1) {
534 885 fjp
                        try {
535 652 fernando
                                logger.debug("source.start()");
536 4213 caballero
                                getSource().start();
537
                                typeShape = getSource().getShapeType();
538 652 fernando
                                logger.debug("source.stop()");
539 4213 caballero
                                getSource().stop();
540 885 fjp
                        } catch (DriverIOException e) {
541 652 fernando
                                throw new DriverException(e);
542
                        }
543 526 vcaballero
                }
544 562 fernando
545 526 vcaballero
                return typeShape;
546 373 fernando
        }
547 435 vcaballero
548
        /**
549 1828 fernando
         * @throws XMLException
550 435 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
551
         */
552 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
553 562 fernando
                XMLEntity xml = super.getXMLEntity();
554 4213 caballero
       xml.addChild(getLegend().getXMLEntity());
555 3963 caballero
        //try {
556
                        xml.addChild(getRecordset().getSelectionSupport().getXMLEntity());
557 562 fernando
558 3963 caballero
                //} catch (DriverException e1) {
559
                        // TODO Auto-generated catch block
560
                //        e1.printStackTrace();
561
                //}
562 4213 caballero
                if (getSource() instanceof VectorialEditableAdapter){
563
                        setSource(((VectorialEditableAdapter)source).getOriginalAdapter());
564 3981 caballero
                }
565 4213 caballero
                if (getSource() instanceof VectorialFileAdapter) {
566 1828 fernando
                        xml.putProperty("type", "vectorial");
567 4213 caballero
                        xml.putProperty("file", ((VectorialFileAdapter) getSource()).getFile());
568 3963 caballero
                        //try {
569
                                try {
570
                                        xml.putProperty("recordset-name", getSource().getRecordset().getName());
571
                                } catch (DriverLoadException e) {
572
                                        // TODO Auto-generated catch block
573
                                        e.printStackTrace();
574
                                }
575
                        //} catch (DriverException e) {
576
                        //        throw new XMLException(e);
577
                        //}
578 562 fernando
                } else if (source instanceof VectorialDBAdapter) {
579 2748 fjp
            xml.putProperty("type", "vectorial");
580 3940 caballero
581 4213 caballero
            VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) getSource().getDriver();
582 3940 caballero
583 2778 fjp
            // Guardamos el nombre del driver para poder recuperarlo
584
            // con el DriverManager de Fernando.
585 3940 caballero
            xml.putProperty("db", dbDriver.getName());
586 3963 caballero
            //try {
587
                try {
588
                                        xml.putProperty("recordset-name", getSource().getRecordset().getName());
589
                                } catch (DriverLoadException e) {
590
                                        // TODO Auto-generated catch block
591
                                        e.printStackTrace();
592
                                }
593
            //} catch (DriverException e) {
594
            //    throw new XMLException(e);
595
            //}
596 2778 fjp
            xml.addChild(dbDriver.getXMLEntity()); // Tercer child. Antes hemos metido la leyenda y el selection support
597 3940 caballero
        } else if (source instanceof VectorialAdapter) {
598 3301 fjp
            // Se supone que hemos hecho algo gen?rico.
599
            xml.putProperty("type", "vectorial");
600 3940 caballero
601 4213 caballero
            VectorialDriver driver = (VectorialDriver) getSource().getDriver();
602 3940 caballero
603 3301 fjp
            // Guardamos el nombre del driver para poder recuperarlo
604
            // con el DriverManager de Fernando.
605 3940 caballero
            xml.putProperty("other", driver.getName());
606 3963 caballero
            //try {
607
                try {
608
                                        xml.putProperty("recordset-name", getSource().getRecordset().getName());
609
                                } catch (DriverLoadException e) {
610
                                        // TODO Auto-generated catch block
611
                                        e.printStackTrace();
612
                                }
613
            //} catch (DriverException e) {
614
            //    throw new XMLException(e);
615
            //}
616 3301 fjp
            if (driver instanceof IPersistance)
617
            {
618 3304 jmorell
                // xml.putProperty("className", driver.getClass().getName());
619 3301 fjp
                IPersistance persist = (IPersistance) driver;
620
                xml.addChild(persist.getXMLEntity()); // Tercer child. Antes hemos metido la leyenda y el selection support
621
            }
622 3940 caballero
                }
623 562 fernando
                xml.putProperty("driverName", getSource().getDriver().getName());
624 2672 fjp
        if (bHasJoin)
625
            xml.putProperty("hasJoin", "true");
626 3940 caballero
627
628 435 vcaballero
                return xml;
629
        }
630
631
        /**
632
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
633
         */
634 2183 fernando
        public void setXMLEntity03(XMLEntity xml)
635
                throws XMLException {
636 3940 caballero
637 2183 fernando
                super.setXMLEntity(xml);
638
                legend = LegendFactory.createFromXML03(xml.getChild(0));
639
640
                try {
641
                        // legend.setDataSource(getRecordset());
642
                        setLegend(legend);
643
                } catch (FieldNotFoundException e) {
644
                        throw new XMLException(e);
645
                } catch (DriverException e) {
646
                        throw new XMLException(e);
647 3940 caballero
                }
648 2183 fernando
649 3963 caballero
                //try {
650
                                getRecordset().getSelectionSupport().setXMLEntity03(xml.getChild(1));
651
                //} catch (DriverException e) {
652
                        // TODO Auto-generated catch block
653
                //        e.printStackTrace();
654
                //}
655 2183 fernando
        }
656
657
        /**
658
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
659
         */
660 885 fjp
        public void setXMLEntity(XMLEntity xml)
661 1056 vcaballero
                throws XMLException {
662 2659 fjp
        super.setXMLEntity(xml);
663 3940 caballero
664 4213 caballero
                VectorialLegend leg = LegendFactory.createFromXML(xml.getChild(0));
665
                getRecordset().getSelectionSupport().setXMLEntity(xml.getChild(1));
666 885 fjp
667 3963 caballero
668 1828 fernando
                String recordsetName = xml.getStringProperty("recordset-name");
669
                try {
670
                        LayerFactory.getDataSourceFactory().changeDataSourceName(
671 3963 caballero
                   getSource().getRecordset().getName(), recordsetName
672 2542 fernando
                                        );
673 2694 fjp
            // Hacemos que el recordset que hemos creado antes (los
674
            // recordset se crean antes que todo) sea el recordset
675
            // de esta capa. Y del que se crea automaticamente
676
            // en el setLegend del createLayer, nos olvidamos
677 3959 caballero
                        SelectableDataSource sds=new SelectableDataSource(LayerFactory.getDataSourceFactory()
678
                    .createRandomDataSource(recordsetName, DataSourceFactory.AUTOMATIC_OPENING));
679 3963 caballero
                        //sds.setSelectionSupport(selectionSupport);
680 3981 caballero
            //((EditableAdapter)getSource()).setRecordSet(sds);
681 1828 fernando
                } catch (NoSuchTableException e1) {
682
                        throw new XMLException(e1);
683 3963 caballero
                }  catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
684 2759 fjp
            throw new XMLException(e1);
685
        } catch (DriverLoadException e1) {
686
            throw new XMLException(e1);
687 2672 fjp
        }
688 2694 fjp
        // Si tiene una uni?n, lo marcamos para que no se cree la leyenda hasta el final
689
        // de la lectura del proyecto
690
        if (xml.contains("hasJoin"))
691
        {
692
            setIsJoined(true);
693 4213 caballero
            PostProcessSupport.addToPostProcess(this,"setLegend", leg, 1);
694 2694 fjp
        }
695
        else
696
        {
697
            try {
698
                // legend.setDataSource(getRecordset());
699 4213 caballero
                setLegend(leg);
700 2694 fjp
            } catch (FieldNotFoundException e) {
701
                throw new XMLException(e);
702
            } catch (DriverException e) {
703
                throw new XMLException(e);
704 3940 caballero
            }
705
        }
706
707 435 vcaballero
        }
708 562 fernando
709
        /**
710 1034 vcaballero
         * A?ade un LegendListener a la lista de Listeners.
711 562 fernando
         *
712 1034 vcaballero
         * @param listener LegendListener.
713 562 fernando
         */
714
        public void addLegendListener(LegendListener listener) {
715 472 fernando
                layerChangeSupport.addLayerListener(listener);
716
        }
717 562 fernando
718
        /**
719 1034 vcaballero
         * Llamada al m?todo callLegendChanged de los listener.
720 562 fernando
         *
721 1034 vcaballero
         * @param e Evento.
722 562 fernando
         */
723 486 fjp
        private void callLegendChanged(LegendChangedEvent e) {
724 472 fernando
                layerChangeSupport.callLegendChanged(e);
725
        }
726 562 fernando
727
        /**
728 1034 vcaballero
         * Borra un LegendListener de la lista de Listeners
729 562 fernando
         *
730 1034 vcaballero
         * @param listener LegendListener.
731 562 fernando
         */
732
        public void removeLegendListener(LegendListener listener) {
733 472 fernando
                layerChangeSupport.removeLayerListener(listener);
734
        }
735 544 fernando
736
        /**
737 1034 vcaballero
         * Sobreimplementaci?n del m?todo toString para que las bases de datos
738
         * identifiquen la capa.
739 911 fernando
         *
740
         * @return DOCUMENT ME!
741
         */
742
        public String toString() {
743
                /*
744
                 * Se usa internamente para que la parte de datos
745
                 * identifique de forma un?voca las tablas
746
                 */
747
                String ret = super.toString();
748
749
                return "layer" + ret.substring(ret.indexOf('@') + 1);
750
        }
751 2672 fjp
752
    public boolean isJoined() {
753
        return bHasJoin;
754
    }
755
756
    public void setIsJoined(boolean hasJoin) {
757
        bHasJoin = hasJoin;
758
    }
759 2978 fjp
760
    /**
761
     * @return Returns the spatialIndex.
762
     */
763 3560 fjp
    public SpatialIndex getSpatialIndex() {
764 2978 fjp
        return spatialIndex;
765
    }
766 3366 caballero
767 3963 caballero
        public SelectableDataSource getRecordset() {
768
                try {
769
                        return getSource().getRecordset();
770
                } catch (DriverLoadException e) {
771
                        // TODO Auto-generated catch block
772
                        e.printStackTrace();
773
                }
774
                return null;
775
        }
776
777 4147 fjp
        /* (non-Javadoc)
778
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setEditing(boolean)
779
         */
780
        public void setEditing(boolean b) {
781
                super.setEditing(b);
782
                if (b)
783
                {
784 4159 fjp
                        VectorialEditableAdapter vea = null;
785
                        // TODO: Qu? pasa si hay m?s tipos de adapters?
786
                        if (getSource() instanceof VectorialDBAdapter)
787
                        {
788 4226 caballero
                                vea = new VectorialEditableDBAdapter();
789 4159 fjp
                        }
790
                        else
791
                        {
792 4226 caballero
                                vea = new VectorialEditableAdapter();
793 4159 fjp
                        }
794 4147 fjp
            vea.setOriginalVectorialAdapter(getSource());
795
            try{
796 4223 caballero
                    vea.startEdition();
797 4147 fjp
            }catch (EditionException e) {
798
                                e.printStackTrace();
799
                        }
800
            setSource(vea);
801 4223 caballero
            getRecordset().setSelectionSupport(vea.getOriginalAdapter().getRecordset().getSelectionSupport());
802
803 4147 fjp
                }
804
                else
805
                {
806
                        VectorialEditableAdapter vea = (VectorialEditableAdapter) getSource();
807
                        setSource(vea.getOriginalAdapter());
808
                }
809
                callEditionChanged(LayerEvent.createEditionChangedEvent(this, "edition"));
810 4223 caballero
811 4147 fjp
        }
812
813 3963 caballero
        /*public SelectionSupport getSelectionSupport() {
814 3366 caballero
                return selectionSupport;
815
        }
816

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