Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableAdapter.java @ 5893

History | View | Annotate | Download (28.5 KB)

1
/*
2
 * Created on 12-ene-2006 by fjp
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: VectorialEditableAdapter.java 5880 2006-06-16 10:44:01Z fjp $
47
 * $Log$
48
 * Revision 1.56  2006-06-16 10:44:01  fjp
49
 * Por mandato de Vicente
50
 *
51
 * Revision 1.54  2006/06/01 16:15:16  fjp
52
 * Escritura que permite crear drivers de manera m?s sencilla.
53
 *
54
 * Revision 1.53  2006/05/30 13:03:41  fjp
55
 * setFlatness solo se debe aplicar a bases de datos espaciales.
56
 *
57
 * Revision 1.52  2006/05/24 09:29:30  caballero
58
 * a?adir flatness
59
 *
60
 * Revision 1.51  2006/05/16 16:07:19  fjp
61
 * snapping. Revisar
62
 *
63
 * Revision 1.50  2006/05/16 07:07:46  caballero
64
 * Modificar la geometr?a desde fuera
65
 *
66
 * Revision 1.49  2006/05/15 10:52:23  caballero
67
 * Saber si se realiza una operaci?n desde la vista o desde la tabla.
68
 *
69
 * Revision 1.48  2006/05/09 15:58:37  caballero
70
 * Para el IGN
71
 *
72
 * Revision 1.47  2006/05/09 10:28:28  caballero
73
 * faltaba controlar undo y redo
74
 *
75
 * Revision 1.46  2006/04/27 06:44:56  caballero
76
 * Soluci?n undo y redo de anotaciones
77
 *
78
 * Revision 1.45  2006/04/18 06:56:55  caballero
79
 * Cambiar VectorialAdapter por ReadableVectorial
80
 *
81
 * Revision 1.44  2006/04/12 17:13:39  fjp
82
 * *** empty log message ***
83
 *
84
 * Revision 1.43  2006/04/11 12:12:29  fjp
85
 * Con edici?n en PostGIS y guardando pseudo-arcos en los shapes.
86
 *
87
 * Revision 1.42  2006/04/11 06:53:20  fjp
88
 * Preparando el driver de escritura PostGIS
89
 *
90
 * Revision 1.41  2006/04/04 11:27:16  fjp
91
 * Consola escuchando bien y selecci?n en tabla sincronizada cuando hay edici?n
92
 *
93
 * Revision 1.40  2006/04/03 11:04:48  caballero
94
 * Posibilidad de a?adir una anotaci?n
95
 *
96
 * Revision 1.39  2006/03/29 06:26:37  caballero
97
 * acelerar con una imagen las herramientas
98
 *
99
 * Revision 1.38  2006/03/23 16:20:52  fjp
100
 * Un fallo un tanto inverosimil con el mapOverview
101
 *
102
 * Revision 1.37  2006/03/23 10:08:11  caballero
103
 * calculo del fullExtent recorriendo todas las geometr?as
104
 *
105
 * Revision 1.36  2006/03/22 11:46:29  caballero
106
 * Editar capa de anotaciones
107
 *
108
 * Revision 1.35  2006/02/28 18:15:22  fjp
109
 * Consola de CAD
110
 *
111
 * Revision 1.34  2006/02/24 11:30:32  fjp
112
 * FUNCIONA!!! (Creo)
113
 *
114
 * Revision 1.33  2006/02/24 07:57:58  fjp
115
 * FUNCIONA!!! (Creo)
116
 *
117
 * Revision 1.32  2006/02/23 17:55:45  fjp
118
 * Preparando para poder editar con el EditionManager
119
 *
120
 * Revision 1.31  2006/02/21 16:44:08  fjp
121
 * Preparando para poder editar con el EditionManager
122
 *
123
 * Revision 1.30  2006/02/20 18:14:59  fjp
124
 * Preparando para poder editar con el EditionManager
125
 *
126
 * Revision 1.29  2006/02/20 10:32:54  fjp
127
 * Preparando para poder editar con el EditionManager
128
 *
129
 * Revision 1.28  2006/02/17 13:40:03  fjp
130
 * Preparando para poder editar con el EditionManager
131
 *
132
 * Revision 1.27  2006/02/17 10:41:14  fjp
133
 * Evento de edici?n lanzado cuando una capa se pone en edici?n
134
 *
135
 * Revision 1.26  2006/02/17 08:21:19  fjp
136
 * *** empty log message ***
137
 *
138
 * Revision 1.25  2006/02/16 09:38:10  fjp
139
 * Preparando compatibilidad para bases de datos (y de paso, acelerando :-)
140
 *
141
 * Revision 1.24  2006/02/16 09:06:28  caballero
142
 * commandStack
143
 *
144
 * Revision 1.23  2006/02/15 18:16:02  fjp
145
 * POR TERMINAR
146
 *
147
 * Revision 1.22  2006/02/13 18:18:31  fjp
148
 * POR TERMINAR
149
 *
150
 * Revision 1.21  2006/02/10 13:28:23  caballero
151
 * poder cambiar la selecci?n
152
 *
153
 * Revision 1.20  2006/02/09 13:11:54  caballero
154
 * *** empty log message ***
155
 *
156
 * Revision 1.19  2006/02/08 16:45:29  caballero
157
 * elimnar c?dio no usado
158
 *
159
 * Revision 1.18  2006/02/08 15:18:45  caballero
160
 * control de las rows eliminadas
161
 *
162
 * Revision 1.17  2006/02/07 10:18:44  caballero
163
 * Con BoundedShape
164
 *
165
 * Revision 1.16  2006/02/06 12:01:41  caballero
166
 * driver del ova
167
 *
168
 * Revision 1.15  2006/02/03 14:09:32  fjp
169
 * Preparando edici?n
170
 *
171
 * Revision 1.14  2006/02/03 12:16:33  fjp
172
 * Preparando edici?n
173
 *
174
 * Revision 1.13  2006/02/03 11:54:12  caballero
175
 * tablas con vectorialEditableAdapter en edici?n
176
 *
177
 * Revision 1.11  2006/01/31 08:10:05  caballero
178
 * cambio de feature a row
179
 *
180
 * Revision 1.10  2006/01/30 08:18:14  caballero
181
 * m?todos para deshacer y rehacer
182
 *
183
 * Revision 1.9  2006/01/23 17:30:28  caballero
184
 * coger los datos del ova
185
 *
186
 * Revision 1.8  2006/01/23 16:16:16  caballero
187
 * getRowIndex
188
 *
189
 * Revision 1.7  2006/01/20 08:37:10  fjp
190
 * Preparando la edici?n
191
 *
192
 * Revision 1.6  2006/01/19 12:48:20  caballero
193
 * poder modificar su vectorial Adapter
194
 *
195
 * Revision 1.5  2006/01/19 09:28:11  fjp
196
 * Preparando la edici?n
197
 *
198
 * Revision 1.4  2006/01/17 10:24:02  fjp
199
 * Preparando edici?n
200
 *
201
 * Revision 1.3  2006/01/16 12:47:38  fjp
202
 * Preparando edici?n
203
 *
204
 * Revision 1.2  2006/01/16 11:23:00  fjp
205
 * Preparando edici?n
206
 *
207
 * Revision 1.1  2006/01/12 13:39:14  fjp
208
 * preaparar edicion
209
 *
210
 *
211
 */
212
package com.iver.cit.gvsig.fmap.edition;
213

    
214
import java.awt.Image;
215
import java.awt.geom.Rectangle2D;
216
import java.awt.image.BufferedImage;
217
import java.io.IOException;
218
import java.util.List;
219

    
220
import com.hardcode.driverManager.Driver;
221
import com.hardcode.driverManager.DriverLoadException;
222
import com.hardcode.gdbms.engine.data.DataSource;
223
import com.hardcode.gdbms.engine.values.Value;
224
import com.iver.cit.gvsig.fmap.DriverException;
225
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
226
import com.iver.cit.gvsig.fmap.core.FShape;
227
import com.iver.cit.gvsig.fmap.core.IFeature;
228
import com.iver.cit.gvsig.fmap.core.IGeometry;
229
import com.iver.cit.gvsig.fmap.core.IRow;
230
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
231
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
232
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
233
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
234
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
235
import com.iver.cit.gvsig.fmap.layers.FBitSet;
236
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
237
import com.vividsolutions.jts.geom.Envelope;
238
import com.vividsolutions.jts.index.SpatialIndex;
239
import com.vividsolutions.jts.index.quadtree.Quadtree;
240

    
241
/**
242
 * @author fjp
243
 *
244
 */
245
public class VectorialEditableAdapter extends EditableAdapter implements
246
                ReadableVectorial, BoundedShapes {
247
        protected ReadableVectorial ova;
248

    
249
        protected Quadtree index;
250

    
251
        protected Rectangle2D fullExtent;
252

    
253
        protected Image selectionImage;
254

    
255
        protected BufferedImage handlersImage;
256

    
257
        private double flatness=0.8;
258
        /*
259
         * private class MyFeatureIterator implements IFeatureIterator { int numReg =
260
         * 0; Rectangle2D rect; String epsg; IFeatureIterator origFeatIt; boolean
261
         * bHasNext = true;
262
         *
263
         * public MyFeatureIterator(Rectangle2D r, String strEPSG) throws
264
         * DriverException { rect = r; epsg = strEPSG; origFeatIt =
265
         * ova.getFeatureIterator(r, epsg); } public boolean hasNext() throws
266
         * DriverException { return bHasNext; }
267
         *
268
         * public IFeature next() throws DriverException { IFeature aux =
269
         * origFeatIt.next(); return null; }
270
         *
271
         * public void closeIterator() throws DriverException {
272
         *  }
273
         *  }
274
         */
275

    
276
        public VectorialEditableAdapter() {
277
                super();
278
        }
279

    
280
        public void setOriginalVectorialAdapter(ReadableVectorial rv) {
281
                ova = rv;
282
                try {
283
                        setOriginalDataSource(rv.getRecordset());
284
                } catch (DriverLoadException e) {
285
                        e.printStackTrace();
286
                }
287
        }
288

    
289
        /*
290
         * (non-Javadoc)
291
         *
292
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#start()
293
         */
294
        public void start() throws DriverIOException {
295
                ova.start();
296
        }
297

    
298
        public IWriter getWriter() {
299
                if (ova.getDriver() instanceof IWriteable)
300
                {
301
                        IWriter writer = ((IWriteable) ova.getDriver()).getWriter();
302
                        if (writer instanceof ISpatialWriter)
303
                                return writer;
304
                }
305
                return null;
306
        }
307

    
308
        /*
309
         * (non-Javadoc)
310
         *
311
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#stop()
312
         */
313
        public void stop() throws DriverIOException {
314
                ova.stop();
315
        }
316

    
317
        /*
318
         * (non-Javadoc)
319
         *
320
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
321
         */
322
        public IGeometry getShape(int rowIndex) throws DriverIOException {
323
                // Si no est? en el fichero de expansi?n
324
                int calculatedIndex = getCalculatedIndex(rowIndex);
325
                Integer integer = new Integer(calculatedIndex);
326
                if (!relations.containsKey(integer)) {
327
                        // Si ha sido eliminada
328
                        /*
329
                         * if (delRows.get(integer.intValue())) { return null; } else {
330
                         */
331
                        return ova.getShape(calculatedIndex);
332
                        // }
333
                } else {
334
                        int num = ((Integer) relations.get(integer)).intValue();
335
                        DefaultRowEdited feat;
336
                        try {
337
                                feat = (DefaultRowEdited) expansionFile.getRow(num);
338
                                return ((IFeature) feat.getLinkedRow()).getGeometry()
339
                                                .cloneGeometry();// getGeometry();
340
                        } catch (IOException e) {
341
                                e.printStackTrace();
342
                                throw new DriverIOException(e);
343
                        }
344
                }
345

    
346
        }
347

    
348
        /*
349
         * (non-Javadoc)
350
         *
351
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
352
         */
353
        public int getShapeType() throws DriverIOException {
354
                return ova.getShapeType();
355
        }
356

    
357
        public ReadableVectorial getOriginalAdapter() {
358
                return ova;
359
        }
360

    
361
        public VectorialDriver getDriver() {
362
                return ova.getDriver();
363
        }
364

    
365
        public void setDriver(VectorialDriver driver) {
366
                this.ova.setDriver(driver);
367
        }
368

    
369
        public DriverAttributes getDriverAttributes() {
370
                return ova.getDriverAttributes();
371
        }
372

    
373
        /**
374
         * DOCUMENT ME!
375
         *
376
         * @throws EditionException
377
         *             DOCUMENT ME!
378
         */
379
        public void startEdition(int sourceType) throws EditionException {
380
                super.startEdition(sourceType);
381
                Driver drv = ova.getDriver();
382
                if (drv instanceof IWriteable)
383
                {
384
                        setWriter(((IWriteable) drv).getWriter());
385
                }
386

    
387

    
388
                try {
389
                        expansionFile.open();
390
                        if (index == null || fullExtent == null) {
391
                                // TODO: Si la capa dispone de un ?ndice espacial, hacer
392
                                // algo aqu? para que se use ese ?ndice espacial.
393
                                index = new Quadtree();
394

    
395
                                for (int i = 0; i < ova.getShapeCount(); i++) {
396
                                        IGeometry g = null;
397
                                        try {
398
                                                g = ((DefaultFeature) ova.getFeature(i)).getGeometry();
399
                                        } catch (DriverException e1) {
400
                                                // TODO Auto-generated catch block
401
                                                e1.printStackTrace();
402
                                        }
403

    
404
                                        if (g == null) {
405
                                                continue;
406
                                        }
407

    
408
                                        Rectangle2D r = g.getBounds2D();
409
                                        Envelope e = new Envelope(r.getX(),
410
                                                        r.getX() + r.getWidth(), r.getY(), r.getY()
411
                                                                        + r.getHeight());
412
                                        index.insert(e, new Integer(i));
413
                                        if (fullExtent == null) {
414
                                                fullExtent = r;
415
                                        } else {
416
                                                fullExtent = fullExtent.createUnion(r);
417
                                        }
418
                                }
419
                        }
420
                } catch (DriverIOException e) {
421
                        throw new EditionException(e);
422
                } catch (IOException e) {
423
                        throw new EditionException(e);
424
                }
425

    
426
                System.err.println("Se han metido en el ?ndice "
427
                                + index.queryAll().size() + " geometr?as");
428
        }
429

    
430
        /*
431
         * (non-Javadoc)
432
         *
433
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
434
         */
435
        public IRowEdited getRow(int index) throws DriverIOException, IOException {
436
                int calculatedIndex = getCalculatedIndex(index);
437
                Integer integer = new Integer(calculatedIndex);
438
                // Si no est? en el fichero de expansi?n
439
                DefaultRowEdited edRow = null;
440
                if (!relations.containsKey(integer)) {
441
                        try {
442
                                edRow = new DefaultRowEdited(ova.getFeature(calculatedIndex),
443
                                                DefaultRowEdited.STATUS_ORIGINAL, index);
444
                                // /System.out.println("Piden la feature con ID= " + index);
445
                                /*
446
                                 * Exception e = new Exception(); e.printStackTrace();
447
                                 */
448
                        } catch (DriverException e) {
449
                                // TODO Auto-generated catch block
450
                                e.printStackTrace();
451
                        }
452

    
453
                        return edRow;
454
                } else {
455
                        int num = ((Integer) relations.get(integer)).intValue();
456
                        IRowEdited aux = expansionFile.getRow(num);
457
                        edRow = new DefaultRowEdited(aux.getLinkedRow().cloneRow(), aux
458
                                        .getStatus(), index);
459
                        return edRow;
460
                }
461
        }
462

    
463
        /**
464
         * Elimina una geometria. Si es una geometr?a original de la capa en edici?n
465
         * se marca como eliminada (haya sido modificada o no). Si es una geometr?a
466
         * a?adida posteriormente se invalida en el fichero de expansi?n, para que
467
         * una futura compactaci?n termine con ella.
468
         *
469
         * @param index
470
         *            ?ndice de la geometr?a.
471
         *
472
         * @throws DriverIOException
473
         * @throws IOException
474
         */
475
        public IRow doRemoveRow(int index, int sourceType)
476
                        throws DriverIOException, IOException {
477
                boolean cancel = fireBeforeRemoveRow(index, sourceType);
478
                if (cancel)
479
                        return null;
480
                // Llega el calculatedIndex
481
                Integer integer = new Integer(index);
482

    
483
                IFeature feat = null;
484
                delRows.set(index, true);
485
                // Si la geometr?a no ha sido modificada
486
                if (!relations.containsKey(integer)) {
487

    
488
                        try {
489
                                feat = (DefaultFeature) (ova.getFeature(index));
490
                        } catch (DriverException e) {
491
                                // TODO Auto-generated catch block
492
                                e.printStackTrace();
493
                        }
494
                } else {
495
                        int num = ((Integer) relations.get(integer)).intValue();
496
                        feat = (IFeature) expansionFile.getRow(num).getLinkedRow();
497
                        // expansionFile.invalidateRow(num);
498
                }
499
                System.err.println("Elimina una Row en la posici?n: " + index);
500
                // Se actualiza el ?ndice
501
                if (feat != null) {
502
                        Rectangle2D r = feat.getGeometry().getBounds2D();
503
                        boolean borrado = this.index.remove(new Envelope(r.getX(), r.getX()
504
                                        + r.getWidth(), r.getY(), r.getY() + r.getHeight()),
505
                                        new Integer(index));
506
                        System.out.println("Est? borrado : " + borrado);
507
                        System.out.println("Index.lenght : " + this.index.size());
508

    
509
                }
510
                setSelection(new FBitSet());
511
                fireAfterRemoveRow(index, sourceType);
512
                return feat;
513
        }
514

    
515
        /**
516
         * Si se intenta modificar una geometr?a original de la capa en edici?n se
517
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
518
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
519
         * fichero de expansi?n (por ser nueva o original pero modificada) se invoca
520
         * el m?todo modifyGeometry y se actualiza el ?ndice de la geometria en el
521
         * fichero.
522
         *
523
         * @param calculatedIndex
524
         *            DOCUMENT ME!
525
         * @param feat
526
         *            DOCUMENT ME!
527
         *
528
         * @return position inside ExpansionFile
529
         *
530
         * @throws IOException
531
         * @throws DriverIOException
532
         */
533
        public int doModifyRow(int calculatedIndex, IRow feat, int sourceType)
534
                        throws IOException, DriverIOException {
535
                boolean cancel = fireBeforeModifyRow(feat, calculatedIndex, sourceType);
536
                if (cancel)
537
                        return -1;
538
                int posAnteriorInExpansionFile = -1;
539
                Integer integer = new Integer(calculatedIndex);
540

    
541
                IFeature featAnt = null;
542
                System.err.println("Modifica una Row en la posici?n: "
543
                                + calculatedIndex);
544
                // Si la geometr?a no ha sido modificada
545
                if (!relations.containsKey(integer)) {
546
                        int newPosition = expansionFile.addRow(feat,
547
                                        IRowEdited.STATUS_MODIFIED);
548
                        relations.put(integer, new Integer(newPosition));
549

    
550
                        // Se actualiza el ?ndice espacial
551
                        try {
552
                                featAnt = (DefaultFeature) (ova.getFeature(calculatedIndex));
553
                        } catch (DriverException e) {
554
                                e.printStackTrace();
555
                        }
556
                        IGeometry g = featAnt.getGeometry();
557
                        Rectangle2D rAnt = g.getBounds2D();
558
                        Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
559
                        this.index.remove(new Envelope(rAnt.getX(), rAnt.getX()
560
                                        + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
561
                                        + rAnt.getHeight()), new Integer(calculatedIndex));
562
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
563
                                        .getY(), r.getY() + r.getHeight()), new Integer(
564
                                        calculatedIndex));
565
                } else {
566
                        // Obtenemos el ?ndice en el fichero de expansi?n
567
                        int num = ((Integer) relations.get(integer)).intValue();
568
                        posAnteriorInExpansionFile = num;
569

    
570
                        // Obtenemos la geometr?a para actualiza el ?ndice
571
                        // espacialposteriormente
572
                        featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow();
573

    
574
                        /*
575
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
576
                         * fichero de expansi?n en el que se encuentra la geometr?a
577
                         * modificada
578
                         */
579
                        num = expansionFile.modifyRow(num, feat);
580

    
581
                        /*
582
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
583
                         * fichero de expansi?n.
584
                         */
585
                        relations.put(integer, new Integer(num));
586

    
587
                        // Se modifica el ?ndice espacial
588
                        Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
589
                        Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
590
                        this.index.remove(new Envelope(rAnt.getX(), rAnt.getX()
591
                                        + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
592
                                        + rAnt.getHeight()), new Integer(calculatedIndex));
593
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
594
                                        .getY(), r.getY() + r.getHeight()), new Integer(
595
                                        calculatedIndex));
596
                }
597
                fireAfterModifyRow(calculatedIndex, sourceType);
598
                return posAnteriorInExpansionFile;
599
        }
600

    
601
        /**
602
         * Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
603
         * geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
604
         * fuera una modificaci?n de una geometr?a que estuviese en el fichero de
605
         * expansi?n antes de ser modificada y se pone el puntero de escritura del
606
         * expansion file a justo despues de la penultima geometr?a
607
         *
608
         * @param calculatedIndex
609
         *            ?ndice de la geometr?a que se quiere deshacer su modificaci?n
610
         * @param previousExpansionFileIndex
611
         *            ?ndice que ten?a antes la geometr?a en el expansionFile. Si
612
         *            vale -1 quiere decir que es una modificaci?n de una geometr?a
613
         *            original y por tanto no hay que actualizar el mapa de indices
614
         *            sino eliminar su entrada.
615
         *
616
         * @throws IOException
617
         * @throws DriverIOException
618
         */
619
        public void undoModifyRow(int calculatedIndex,
620
                        int previousExpansionFileIndex, int sourceType) throws IOException,
621
                        DriverIOException {
622

    
623
                // Llega el CalculatedIndex
624
                /*
625
                 * Si la acci?n de modificar se realiz? sobre una geometr?a original
626
                 */
627
                if (previousExpansionFileIndex == -1) {
628

    
629
                        // Se obtiene la geometr?a para actualizar el ?ndice
630
                        // IGeometry g = ((DefaultFeature)
631
                        // getRow(calculatedIndex).getLinkedRow()).getGeometry();
632
                        int inverse = getInversedIndex(calculatedIndex);
633
                        DefaultFeature df = (DefaultFeature) getRow(inverse).getLinkedRow();
634
                        boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
635
                                        sourceType);
636
                        if (cancel)
637
                                return;
638
                        IGeometry g = df.getGeometry();
639
                        // IGeometry g = ova.getShape(calculatedIndex);
640
                        Rectangle2D r = g.getBounds2D();
641

    
642
                        // Se elimina de las relaciones y del fichero de expansi?n
643
                        relations.remove(new Integer(calculatedIndex));
644
                        expansionFile.deleteLastRow();
645

    
646
                        // Se actualizan los ?ndices
647
                        IGeometry gAnt = ova.getShape(calculatedIndex);
648
                        /*
649
                         * IGeometry gAnt = ((DefaultFeature) getRow(calculatedIndex)
650
                         * .getLinkedRow()).getGeometry();
651
                         */
652
                        Rectangle2D rAnt = gAnt.getBounds2D();
653
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
654
                                        .getY(), r.getY() + r.getHeight()), new Integer(
655
                                        calculatedIndex));
656
                        this.index.insert(new Envelope(rAnt.getX(), rAnt.getX()
657
                                        + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
658
                                        + rAnt.getHeight()), new Integer(calculatedIndex));
659
                } else {
660
                        // Se obtiene la geometr?a para actualizar el ?ndice
661
                        IGeometry g = null;
662
                        int inverse = getInversedIndex(calculatedIndex);
663
                        DefaultFeature df = (DefaultFeature) getRow(inverse).getLinkedRow();
664
                        boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
665
                                        sourceType);
666
                        if (cancel)
667
                                return;
668
                        g = df.getGeometry();
669
                        System.out.println("Actual: " + g.toString());
670

    
671
                        Rectangle2D r = g.getBounds2D();
672
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
673
                                        .getY(), r.getY() + r.getHeight()), new Integer(
674
                                        calculatedIndex));
675

    
676
                        // Se actualiza la relaci?n de ?ndices
677
                        // Integer integer = new Integer(geometryIndex);
678
                        relations.put(new Integer(calculatedIndex), new Integer(
679
                                        previousExpansionFileIndex));
680

    
681
                        // Se recupera la geometr?a
682
                        // expansionFile.validateRow(previousExpansionFileIndex);
683

    
684
                        // Se actualizan los ?ndices
685
                        // g = ((IFeature)
686
                        // (expansionFile.getRow(previousExpansionFileIndex).getLinkedRow())).getGeometry();
687
                        // System.out.println("Anterior a la que volvemos : " +
688
                        // g.toString());
689
                        g = ((DefaultFeature) getRow(inverse).getLinkedRow()).getGeometry();
690
                        r = g.getBounds2D();
691
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
692
                                        .getY(), r.getY() + r.getHeight()), new Integer(
693
                                        calculatedIndex));
694

    
695
                }
696
                fireAfterModifyRow(calculatedIndex, sourceType);
697
        }
698

    
699
        /**
700
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
701
         * en la tabla relations.
702
         *
703
         * @param feat
704
         *            geometr?a a guardar.
705
         *
706
         * @return calculatedIndex
707
         *
708
         * @throws DriverIOException
709
         * @throws IOException
710
         */
711
        public int doAddRow(IRow feat, int sourceType) throws DriverIOException,
712
                        IOException {
713
                int calculatedIndex = super.doAddRow(feat, sourceType);
714
                // Actualiza el ?ndice espacial
715
                IGeometry g = ((IFeature) feat).getGeometry();
716
                Rectangle2D r = g.getBounds2D();
717
                index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(),
718
                                r.getY() + r.getHeight()), new Integer(calculatedIndex));
719

    
720
                return calculatedIndex;
721
        }
722

    
723
        /**
724
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
725
         * en el fichero original
726
         *
727
         * @param index
728
         *            DOCUMENT ME!
729
         *
730
         * @throws IOException
731
         * @throws DriverIOException
732
         */
733
        public void undoRemoveRow(int index, int sourceType) throws IOException,
734
                        DriverIOException {
735
                super.undoRemoveRow(index, sourceType);
736

    
737
                IGeometry g = null;
738
                g = ((IFeature) getRow(index).getLinkedRow()).getGeometry();
739

    
740
                Rectangle2D r = g.getBounds2D();
741
                this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
742
                                .getY(), r.getY() + r.getHeight()), new Integer(index));
743
        }
744

    
745
        /**
746
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
747
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
748
         * relaci?n del mapa de relaciones
749
         *
750
         * @param index
751
         *            ?ndice de la geometr?a que se a?adi?
752
         *
753
         * @throws DriverIOException
754
         * @throws IOException
755
         */
756
        public void undoAddRow(int calculatedIndex, int sourceType)
757
                        throws DriverIOException, IOException {
758
                int inverse = getInversedIndex(calculatedIndex);
759
                IGeometry g = ((IFeature) getRow(inverse).getLinkedRow()).getGeometry();
760
                Rectangle2D r = g.getBounds2D();
761
                this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
762
                                .getY(), r.getY() + r.getHeight()),
763
                                new Integer(calculatedIndex));
764

    
765
                super.undoAddRow(calculatedIndex, sourceType);
766
                setSelection(new FBitSet());
767
        }
768

    
769
        /**
770
         * Obtiene las geometr?as que se encuentran en el rect?ngulo que se pasa
771
         * como par?metro haciendo uso del ?ndice espacial
772
         *
773
         * @param r
774
         *            Rect?ngulo indicando la porci?n del espacio para el cual se
775
         *            quiere saber los ?ndices de las geometr?as que se encuentran
776
         *            dentro de ?l
777
         *
778
         * @return Array de ?ndices para su uso con getGeometry, removeGeometry, ...
779
         */
780
        /*
781
         * public int[] getRowsIndexes_OL(Rectangle2D r) { Envelope e = new
782
         * Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(), r.getY() +
783
         * r.getHeight()); List l = index.query(e); int[] indexes = new
784
         * int[l.size()];
785
         *
786
         * for (int index = 0; index < l.size(); index++) { Integer i = (Integer)
787
         * l.get(index); indexes[index] = getInversedIndex(i.intValue()); }
788
         *
789
         * return indexes; }
790
         */
791
        /**
792
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
793
         */
794
        public int getShapeCount() throws DriverIOException {
795
                try {
796
                        return getRowCount();
797
                } catch (IOException e) {
798
                        throw new DriverIOException(e);
799
                }
800
        }
801

    
802
        /**
803
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
804
         */
805
        public Rectangle2D getFullExtent() throws DriverIOException {
806
                if (fullExtent == null) {
807
                        fullExtent = ova.getFullExtent();
808
                }
809
                return fullExtent;
810
        }
811

    
812
        /**
813
         * Use it BEFORE writing to a file.
814
         *
815
         * @return real full extent.
816
         * @throws DriverIOException
817
         */
818
        public Rectangle2D reCalculateFullExtent() throws DriverIOException {
819
                if (getShapeCount() > 0) {
820
                        fullExtent = getShape(0).getBounds2D();
821
                        for (int i = 1; i < getShapeCount(); i++) {
822
                                fullExtent.add(getShape(i).getBounds2D());
823
                        }
824
                } else {
825
                        fullExtent = ova.getFullExtent();
826
                }
827
                return fullExtent;
828
        }
829

    
830
        /**
831
         * En la implementaci?n por defecto podemos hacer que cada feature tenga ID =
832
         * numero de registro. En el DBAdapter podr?amos "overrride" este m?todo y
833
         * poner como ID de la Feature el campo ?nico escogido en la base de datos.
834
         *
835
         * @param numReg
836
         * @return
837
         * @throws DriverException
838
         */
839
        public IFeature getFeature(int numReg) throws DriverException {
840
                IGeometry geom;
841
                IFeature feat = null;
842
                try {
843
                        geom = getShape(numReg);
844
                        DataSource rs = getRecordset();
845
                        Value[] regAtt = new Value[rs.getFieldCount()];
846
                        for (int fieldId = 0; fieldId < rs.getFieldCount(); fieldId++) {
847
                                regAtt[fieldId] = rs.getFieldValue(numReg, fieldId);
848
                        }
849

    
850
                        feat = new DefaultFeature(geom, regAtt, numReg + "");
851
                } catch (DriverIOException e) {
852
                        throw new DriverException(e);
853
                } catch (DriverLoadException e) {
854
                        throw new DriverException(e);
855
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
856
                        throw new DriverException(e);
857
                }
858
                return feat;
859
        }
860

    
861
        public void stopEdition(IWriter writer, int sourceType)
862
                        throws EditionException {
863
                ISpatialWriter spatialWriter = (ISpatialWriter) writer;
864
                spatialWriter.setFlatness(getFlatness());
865
                super.stopEdition(writer, sourceType);
866
                try {
867
                        ova.getDriver().reLoad();
868
                } catch (IOException e) {
869
                        e.printStackTrace();
870
                        throw new EditionException(e);
871
                }
872
        }
873

    
874
        public Rectangle2D getShapeBounds(int index) throws IOException {
875
                // Solo se utiliza cuando el driver es BoundedShapes
876
                // Si no est? en el fichero de expansi?n
877
                Integer integer = new Integer((int) index);
878
                if (!relations.containsKey(integer)) {
879
                        if (ova.getDriver() instanceof BoundedShapes) {
880
                                BoundedShapes bs = (BoundedShapes) ova.getDriver();
881
                                return bs.getShapeBounds(index);
882
                        } else {
883
                                return ova.getDriver().getShape(index).getBounds2D();
884
                        }
885

    
886
                } else {
887
                        int num = ((Integer) relations.get(integer)).intValue();
888
                        DefaultRowEdited feat;
889
                        feat = (DefaultRowEdited) expansionFile.getRow(num);
890
                        if (feat.getStatus() == IRowEdited.STATUS_DELETED)
891
                                return null;
892
                        IGeometry geom = ((IFeature) feat.getLinkedRow()).getGeometry();
893
                        return geom.getBounds2D();// getGeometry();
894
                }
895

    
896
        }
897

    
898
        public int getShapeType(int index) {
899
                try {
900
                        return ova.getShapeType();
901
                } catch (DriverIOException e) {
902
                        // TODO Auto-generated catch block
903
                        e.printStackTrace();
904
                }
905
                return FShape.MULTI;
906
        }
907

    
908
        /**
909
         * Usar solo cuando est?s seguro de que puedes gastar memoria. Nosotros lo
910
         * usamos para las b?squedas por ?ndice espacial con el handle. La idea es
911
         * usarlo una vez, guardar las geometr?as que necesitas en ese extent y
912
         * trabajar con ellas hasta el siguiente cambio de extent.
913
         *
914
         * @param r
915
         * @param strEPSG
916
         * @return
917
         * @throws DriverException
918
         */
919
        public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG)
920
                        throws DriverException {
921
                // En esta clase suponemos random access.
922
                // Luego tendremos otra clase que sea VectorialEditableDBAdapter
923
                // que reescribir? este m?todo.
924
                Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
925
                List l = index.query(e);
926
                IRowEdited[] feats = new IRowEdited[l.size()];
927
                try {
928
                        for (int index = 0; index < l.size(); index++) {
929
                                Integer i = (Integer) l.get(index);
930
                                int inverse = getInversedIndex(i.intValue());
931
                                feats[index] = (IRowEdited) getRow(inverse);
932
                        }
933
                } catch (DriverIOException e1) {
934
                        throw new DriverException(e1);
935
                } catch (IOException e1) {
936
                        throw new DriverException(e1);
937
                }
938

    
939
                return feats;
940
        }
941

    
942
        public void setSpatialIndex(SpatialIndex spatialIndex) {
943
                index = (Quadtree) spatialIndex;
944
        }
945

    
946
        public void setFullExtent(Rectangle2D fullExtent2) {
947
                fullExtent = fullExtent2;
948
        }
949

    
950
        /**
951
         * DOCUMENT ME!
952
         *
953
         * @return DOCUMENT ME!
954
         */
955
        public Image getSelectionImage() {
956
                return selectionImage;
957
        }
958

    
959
        public Image getHandlersImage() {
960
                return handlersImage;
961
        }
962

    
963
        /**
964
         * DOCUMENT ME!
965
         *
966
         * @param i
967
         *            DOCUMENT ME!
968
         */
969
        public void setSelectionImage(Image i) {
970
                selectionImage = i;
971
        }
972

    
973
        public void setHandlersImage(BufferedImage handlersImage) {
974
                this.handlersImage = handlersImage;
975
        }
976

    
977
        public double getFlatness() {
978
                return flatness;
979
        }
980

    
981
        public void setFlatness(double d) {
982
                flatness=d;
983
        }
984
}