Statistics
| Revision:

root / branches / piloto3d / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableAdapter.java @ 9524

History | View | Annotate | Download (30.7 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 9524 2007-01-03 15:14:45Z sbayarri $
47
 * $Log$
48
 * Revision 1.62.2.1  2007-01-03 15:12:33  sbayarri
49
 * v10 synch & 3D
50
 *
51
 * Revision 1.62.4.3  2006/11/22 09:23:09  fjp
52
 * Fallo al editar una capa de postgis 8.0 o superior. Y de paso, para que al guardar los cambios no haga falta leer todos los registros.
53
 *
54
 * Revision 1.62.4.2  2006/11/15 00:08:24  jjdelcerro
55
 * *** empty log message ***
56
 *
57
 * Revision 1.64  2006/09/21 17:32:11  azabala
58
 * *** empty log message ***
59
 *
60
 * Revision 1.62  2006/08/10 08:20:31  caballero
61
 * flatness
62
 *
63
 * Revision 1.61  2006/07/20 13:03:07  fjp
64
 * nuevos campos
65
 *
66
 * Revision 1.60  2006/07/12 11:48:41  fjp
67
 * Draft to add, remove and delete fields
68
 *
69
 * Revision 1.59  2006/07/12 10:34:52  fjp
70
 * Draft to add, remove and delete fields
71
 *
72
 * Revision 1.58  2006/07/06 08:31:29  fjp
73
 * Draft to add, remove and delete fields
74
 *
75
 * Revision 1.57  2006/06/22 11:38:12  caballero
76
 * soluci?n error al borrar geometr?as
77
 *
78
 * Revision 1.56  2006/06/16 10:44:01  fjp
79
 * Por mandato de Vicente
80
 *
81
 * Revision 1.54  2006/06/01 16:15:16  fjp
82
 * Escritura que permite crear drivers de manera m?s sencilla.
83
 *
84
 * Revision 1.53  2006/05/30 13:03:41  fjp
85
 * setFlatness solo se debe aplicar a bases de datos espaciales.
86
 *
87
 * Revision 1.52  2006/05/24 09:29:30  caballero
88
 * a?adir flatness
89
 *
90
 * Revision 1.51  2006/05/16 16:07:19  fjp
91
 * snapping. Revisar
92
 *
93
 * Revision 1.50  2006/05/16 07:07:46  caballero
94
 * Modificar la geometr?a desde fuera
95
 *
96
 * Revision 1.49  2006/05/15 10:52:23  caballero
97
 * Saber si se realiza una operaci?n desde la vista o desde la tabla.
98
 *
99
 * Revision 1.48  2006/05/09 15:58:37  caballero
100
 * Para el IGN
101
 *
102
 * Revision 1.47  2006/05/09 10:28:28  caballero
103
 * faltaba controlar undo y redo
104
 *
105
 * Revision 1.46  2006/04/27 06:44:56  caballero
106
 * Soluci?n undo y redo de anotaciones
107
 *
108
 * Revision 1.45  2006/04/18 06:56:55  caballero
109
 * Cambiar VectorialAdapter por ReadableVectorial
110
 *
111
 * Revision 1.44  2006/04/12 17:13:39  fjp
112
 * *** empty log message ***
113
 *
114
 * Revision 1.43  2006/04/11 12:12:29  fjp
115
 * Con edici?n en PostGIS y guardando pseudo-arcos en los shapes.
116
 *
117
 * Revision 1.42  2006/04/11 06:53:20  fjp
118
 * Preparando el driver de escritura PostGIS
119
 *
120
 * Revision 1.41  2006/04/04 11:27:16  fjp
121
 * Consola escuchando bien y selecci?n en tabla sincronizada cuando hay edici?n
122
 *
123
 * Revision 1.40  2006/04/03 11:04:48  caballero
124
 * Posibilidad de a?adir una anotaci?n
125
 *
126
 * Revision 1.39  2006/03/29 06:26:37  caballero
127
 * acelerar con una imagen las herramientas
128
 *
129
 * Revision 1.38  2006/03/23 16:20:52  fjp
130
 * Un fallo un tanto inverosimil con el mapOverview
131
 *
132
 * Revision 1.37  2006/03/23 10:08:11  caballero
133
 * calculo del fullExtent recorriendo todas las geometr?as
134
 *
135
 * Revision 1.36  2006/03/22 11:46:29  caballero
136
 * Editar capa de anotaciones
137
 *
138
 * Revision 1.35  2006/02/28 18:15:22  fjp
139
 * Consola de CAD
140
 *
141
 * Revision 1.34  2006/02/24 11:30:32  fjp
142
 * FUNCIONA!!! (Creo)
143
 *
144
 * Revision 1.33  2006/02/24 07:57:58  fjp
145
 * FUNCIONA!!! (Creo)
146
 *
147
 * Revision 1.32  2006/02/23 17:55:45  fjp
148
 * Preparando para poder editar con el EditionManager
149
 *
150
 * Revision 1.31  2006/02/21 16:44:08  fjp
151
 * Preparando para poder editar con el EditionManager
152
 *
153
 * Revision 1.30  2006/02/20 18:14:59  fjp
154
 * Preparando para poder editar con el EditionManager
155
 *
156
 * Revision 1.29  2006/02/20 10:32:54  fjp
157
 * Preparando para poder editar con el EditionManager
158
 *
159
 * Revision 1.28  2006/02/17 13:40:03  fjp
160
 * Preparando para poder editar con el EditionManager
161
 *
162
 * Revision 1.27  2006/02/17 10:41:14  fjp
163
 * Evento de edici?n lanzado cuando una capa se pone en edici?n
164
 *
165
 * Revision 1.26  2006/02/17 08:21:19  fjp
166
 * *** empty log message ***
167
 *
168
 * Revision 1.25  2006/02/16 09:38:10  fjp
169
 * Preparando compatibilidad para bases de datos (y de paso, acelerando :-)
170
 *
171
 * Revision 1.24  2006/02/16 09:06:28  caballero
172
 * commandStack
173
 *
174
 * Revision 1.23  2006/02/15 18:16:02  fjp
175
 * POR TERMINAR
176
 *
177
 * Revision 1.22  2006/02/13 18:18:31  fjp
178
 * POR TERMINAR
179
 *
180
 * Revision 1.21  2006/02/10 13:28:23  caballero
181
 * poder cambiar la selecci?n
182
 *
183
 * Revision 1.20  2006/02/09 13:11:54  caballero
184
 * *** empty log message ***
185
 *
186
 * Revision 1.19  2006/02/08 16:45:29  caballero
187
 * elimnar c?dio no usado
188
 *
189
 * Revision 1.18  2006/02/08 15:18:45  caballero
190
 * control de las rows eliminadas
191
 *
192
 * Revision 1.17  2006/02/07 10:18:44  caballero
193
 * Con BoundedShape
194
 *
195
 * Revision 1.16  2006/02/06 12:01:41  caballero
196
 * driver del ova
197
 *
198
 * Revision 1.15  2006/02/03 14:09:32  fjp
199
 * Preparando edici?n
200
 *
201
 * Revision 1.14  2006/02/03 12:16:33  fjp
202
 * Preparando edici?n
203
 *
204
 * Revision 1.13  2006/02/03 11:54:12  caballero
205
 * tablas con vectorialEditableAdapter en edici?n
206
 *
207
 * Revision 1.11  2006/01/31 08:10:05  caballero
208
 * cambio de feature a row
209
 *
210
 * Revision 1.10  2006/01/30 08:18:14  caballero
211
 * m?todos para deshacer y rehacer
212
 *
213
 * Revision 1.9  2006/01/23 17:30:28  caballero
214
 * coger los datos del ova
215
 *
216
 * Revision 1.8  2006/01/23 16:16:16  caballero
217
 * getRowIndex
218
 *
219
 * Revision 1.7  2006/01/20 08:37:10  fjp
220
 * Preparando la edici?n
221
 *
222
 * Revision 1.6  2006/01/19 12:48:20  caballero
223
 * poder modificar su vectorial Adapter
224
 *
225
 * Revision 1.5  2006/01/19 09:28:11  fjp
226
 * Preparando la edici?n
227
 *
228
 * Revision 1.4  2006/01/17 10:24:02  fjp
229
 * Preparando edici?n
230
 *
231
 * Revision 1.3  2006/01/16 12:47:38  fjp
232
 * Preparando edici?n
233
 *
234
 * Revision 1.2  2006/01/16 11:23:00  fjp
235
 * Preparando edici?n
236
 *
237
 * Revision 1.1  2006/01/12 13:39:14  fjp
238
 * preaparar edicion
239
 *
240
 *
241
 */
242
package com.iver.cit.gvsig.fmap.edition;
243

    
244
import java.awt.Image;
245
import java.awt.geom.Rectangle2D;
246
import java.awt.image.BufferedImage;
247
import java.io.IOException;
248
import java.util.List;
249

    
250
import com.hardcode.driverManager.Driver;
251
import com.hardcode.driverManager.DriverLoadException;
252
import com.hardcode.gdbms.engine.data.DataSource;
253
import com.hardcode.gdbms.engine.values.Value;
254
import com.iver.cit.gvsig.fmap.DriverException;
255
import com.iver.cit.gvsig.fmap.DriverIOExceptionType;
256
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType;
257
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
258
import com.iver.cit.gvsig.fmap.core.FShape;
259
import com.iver.cit.gvsig.fmap.core.IFeature;
260
import com.iver.cit.gvsig.fmap.core.IGeometry;
261
import com.iver.cit.gvsig.fmap.core.IRow;
262
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
263
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
264
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
265
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
266
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
267
import com.iver.cit.gvsig.fmap.layers.FBitSet;
268
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
269
import com.vividsolutions.jts.geom.Envelope;
270
import com.vividsolutions.jts.index.SpatialIndex;
271
import com.vividsolutions.jts.index.quadtree.Quadtree;
272

    
273
/**
274
 * @author fjp
275
 *
276
 */
277
public class VectorialEditableAdapter extends EditableAdapter implements
278
                ReadableVectorial, BoundedShapes {
279
        protected ReadableVectorial ova;
280

    
281
        protected Quadtree index;
282

    
283
        protected Rectangle2D fullExtent;
284

    
285
        protected Image selectionImage;
286

    
287
        protected BufferedImage handlersImage;
288

    
289
        //private double flatness=0.8;
290
        /*
291
         * private class MyFeatureIterator implements IFeatureIterator { int numReg =
292
         * 0; Rectangle2D rect; String epsg; IFeatureIterator origFeatIt; boolean
293
         * bHasNext = true;
294
         *
295
         * public MyFeatureIterator(Rectangle2D r, String strEPSG) throws
296
         * DriverException { rect = r; epsg = strEPSG; origFeatIt =
297
         * ova.getFeatureIterator(r, epsg); } public boolean hasNext() throws
298
         * DriverException { return bHasNext; }
299
         *
300
         * public IFeature next() throws DriverException { IFeature aux =
301
         * origFeatIt.next(); return null; }
302
         *
303
         * public void closeIterator() throws DriverException {
304
         *  }
305
         *  }
306
         */
307

    
308
        public VectorialEditableAdapter() {
309
                super();
310
        }
311

    
312
        public void setOriginalVectorialAdapter(ReadableVectorial rv) {
313
                ova = rv;
314
                try {
315
                        setOriginalDataSource(rv.getRecordset());
316
                } catch (DriverLoadException e) {
317
                        e.printStackTrace();
318
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
319
                        // TODO Auto-generated catch block
320
                        e.printStackTrace();
321
                }
322
        }
323

    
324
        /*
325
         * (non-Javadoc)
326
         *
327
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#start()
328
         */
329
        public void start() throws DriverIOException {
330
                ova.start();
331
        }
332

    
333
        public IWriter getWriter() {
334
                if (ova.getDriver() instanceof IWriteable)
335
                {
336
                        IWriter writer = ((IWriteable) ova.getDriver()).getWriter();
337
                        if (writer instanceof ISpatialWriter)
338
                                return writer;
339
                }
340
                return null;
341
        }
342

    
343
        /*
344
         * (non-Javadoc)
345
         *
346
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#stop()
347
         */
348
        public void stop() throws DriverIOException {
349
                ova.stop();
350
        }
351

    
352
        /*
353
         * (non-Javadoc)
354
         *
355
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
356
         */
357
        public IGeometry getShape(int rowIndex) throws DriverIOException {
358
                // Si no est? en el fichero de expansi?n
359
                int calculatedIndex = getCalculatedIndex(rowIndex);
360
                Integer integer = new Integer(calculatedIndex);
361
                if (!relations.containsKey(integer)) {
362
                        // Si ha sido eliminada
363
                        /*
364
                         * if (delRows.get(integer.intValue())) { return null; } else {
365
                         */
366
                        return ova.getShape(calculatedIndex);
367
                        // }
368
                } else {
369
                        int num = ((Integer) relations.get(integer)).intValue();
370
                        DefaultRowEdited feat;
371
                        try {
372
                                feat = (DefaultRowEdited) expansionFile.getRow(num);
373
                                return ((IFeature) feat.getLinkedRow()).getGeometry()
374
                                                .cloneGeometry();// getGeometry();
375
                        } catch (IOException e) {
376
                                e.printStackTrace();
377
                                throw new DriverIOException(e);
378
                        }
379
                }
380

    
381
        }
382

    
383
        /*
384
         * (non-Javadoc)
385
         *
386
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
387
         */
388
        public int getShapeType() throws DriverIOException {
389
                return ova.getShapeType();
390
        }
391

    
392
        public ReadableVectorial getOriginalAdapter() {
393
                return ova;
394
        }
395

    
396
        public VectorialDriver getDriver() {
397
                return ova.getDriver();
398
        }
399

    
400
        public void setDriver(VectorialDriver driver) {
401
                this.ova.setDriver(driver);
402
        }
403

    
404
        public DriverAttributes getDriverAttributes() {
405
                return ova.getDriverAttributes();
406
        }
407

    
408
        /**
409
         * DOCUMENT ME!
410
         *
411
         * @throws EditionException
412
         *             DOCUMENT ME!
413
         */
414
        public void startEdition(int sourceType) throws EditionException {
415
                super.startEdition(sourceType);
416
                Driver drv = ova.getDriver();
417
                if (drv instanceof IWriteable)
418
                {
419
                        setWriter(((IWriteable) drv).getWriter());
420
                }
421

    
422

    
423
                try {
424
                        expansionFile.open();
425
                        if (index == null || fullExtent == null) {
426
                                // TODO: Si la capa dispone de un ?ndice espacial, hacer
427
                                // algo aqu? para que se use ese ?ndice espacial.
428
                                index = new Quadtree();
429

    
430
                                for (int i = 0; i < ova.getShapeCount(); i++) {
431
                                        IGeometry g = null;
432
                                        try {
433
                                                g = ((DefaultFeature) ova.getFeature(i)).getGeometry();
434
                                        } catch (DriverException e1) {
435
                                                // TODO Auto-generated catch block
436
                                                e1.printStackTrace();
437
                                        }
438

    
439
                                        if (g == null) {
440
                                                continue;
441
                                        }
442

    
443
                                        Rectangle2D r = g.getBounds2D();
444
                                        Envelope e = new Envelope(r.getX(),
445
                                                        r.getX() + r.getWidth(), r.getY(), r.getY()
446
                                                                        + r.getHeight());
447
                                        index.insert(e, new Integer(i));
448
                                        if (fullExtent == null) {
449
                                                fullExtent = r;
450
                                        } else {
451
                                                fullExtent = fullExtent.createUnion(r);
452
                                        }
453
                                }
454
                        }
455
                } catch (DriverIOException e) {
456
                        throw new EditionException(e);
457
                } catch (IOException e) {
458
                        throw new EditionException(e);
459
                }
460

    
461
                System.err.println("Se han metido en el ?ndice "
462
                                + index.queryAll().size() + " geometr?as");
463
        }
464

    
465
        /*
466
         * (non-Javadoc)
467
         *
468
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
469
         */
470
        public IRowEdited getRow(int index) throws DriverIOException, IOException {
471
                int calculatedIndex = getCalculatedIndex(index);
472
                Integer integer = new Integer(calculatedIndex);
473
                // Si no est? en el fichero de expansi?n
474
                DefaultRowEdited edRow = null;
475
                if (!relations.containsKey(integer)) {
476
                        try {
477
                                edRow = new DefaultRowEdited(ova.getFeature(calculatedIndex),
478
                                                DefaultRowEdited.STATUS_ORIGINAL, index);
479
                                return createExternalRow(edRow, 0);
480
                        } catch (DriverException e) {
481
                                e.printStackTrace();
482
                                throw new DriverIOException(e);
483
                        }
484

    
485
//                        return edRow;
486
                } else {
487
                        int num = ((Integer) relations.get(integer)).intValue();
488
                        IRowEdited aux = expansionFile.getRow(num);
489
                        edRow = new DefaultRowEdited(aux.getLinkedRow().cloneRow(), aux
490
                                        .getStatus(), index);
491

    
492
                        return edRow;
493
                }
494
        }
495

    
496
        /**
497
         * Elimina una geometria. Si es una geometr?a original de la capa en edici?n
498
         * se marca como eliminada (haya sido modificada o no). Si es una geometr?a
499
         * a?adida posteriormente se invalida en el fichero de expansi?n, para que
500
         * una futura compactaci?n termine con ella.
501
         *
502
         * @param index
503
         *            ?ndice de la geometr?a.
504
         *
505
         * @throws DriverIOException
506
         * @throws IOException
507
         */
508
        public IRow doRemoveRow(int index, int sourceType)
509
                        throws DriverIOException, IOException {
510
                boolean cancel = fireBeforeRemoveRow(index, sourceType);
511
                if (cancel)
512
                        return null;
513
                // Llega el calculatedIndex
514
                Integer integer = new Integer(index);
515

    
516
                IFeature feat = null;
517
                delRows.set(index, true);
518
                // Si la geometr?a no ha sido modificada
519
                if (!relations.containsKey(integer)) {
520

    
521
                        try {
522
                                feat = (DefaultFeature) (ova.getFeature(index));
523
                        } catch (DriverException e) {
524
                                // TODO Auto-generated catch block
525
                                e.printStackTrace();
526
                        }
527
                } else {
528
                        int num = ((Integer) relations.get(integer)).intValue();
529
                        feat = (IFeature) expansionFile.getRow(num).getLinkedRow();
530
                        // expansionFile.invalidateRow(num);
531
                }
532
                System.err.println("Elimina una Row en la posici?n: " + index);
533
                // Se actualiza el ?ndice
534
                if (feat != null) {
535
                        Rectangle2D r = feat.getGeometry().getBounds2D();
536
                        boolean borrado = this.index.remove(new Envelope(r.getX(), r.getX()
537
                                        + r.getWidth(), r.getY(), r.getY() + r.getHeight()),
538
                                        new Integer(index));
539
                        System.out.println("Est? borrado : " + borrado);
540
                        System.out.println("Index.lenght : " + this.index.size());
541
                        isFullExtentDirty = true;
542
                }
543
                setSelection(new FBitSet());
544
                fireAfterRemoveRow(index, sourceType);
545
                return feat;
546
        }
547

    
548
        /**
549
         * Si se intenta modificar una geometr?a original de la capa en edici?n se
550
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
551
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
552
         * fichero de expansi?n (por ser nueva o original pero modificada) se invoca
553
         * el m?todo modifyGeometry y se actualiza el ?ndice de la geometria en el
554
         * fichero.
555
         *
556
         * @param calculatedIndex
557
         *            DOCUMENT ME!
558
         * @param feat
559
         *            DOCUMENT ME!
560
         *
561
         * @return position inside ExpansionFile
562
         *
563
         * @throws IOException
564
         * @throws DriverIOException
565
         */
566
        public int doModifyRow(int calculatedIndex, IRow feat, int sourceType)
567
                        throws IOException, DriverIOException {
568
                boolean cancel = fireBeforeModifyRow(feat, calculatedIndex, sourceType);
569
                if (cancel)
570
                        return -1;
571
                int posAnteriorInExpansionFile = -1;
572
                Integer integer = new Integer(calculatedIndex);
573

    
574
                IFeature featAnt = null;
575
                System.err.println("Modifica una Row en la posici?n: "
576
                                + calculatedIndex);
577
                // Si la geometr?a no ha sido modificada
578
                if (!relations.containsKey(integer)) {
579
                        int newPosition = expansionFile.addRow(feat,
580
                                        IRowEdited.STATUS_MODIFIED, actualIndexFields);
581
                        relations.put(integer, new Integer(newPosition));
582

    
583
                        // Se actualiza el ?ndice espacial
584
                        try {
585
                                featAnt = (DefaultFeature) (ova.getFeature(calculatedIndex));
586
                        } catch (DriverException e) {
587
                                e.printStackTrace();
588
                        }
589
                        IGeometry g = featAnt.getGeometry();
590
                        Rectangle2D rAnt = g.getBounds2D();
591
                        Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
592
                        this.index.remove(new Envelope(rAnt.getX(), rAnt.getX()
593
                                        + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
594
                                        + rAnt.getHeight()), new Integer(calculatedIndex));
595
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
596
                                        .getY(), r.getY() + r.getHeight()), new Integer(
597
                                        calculatedIndex));
598
                } else {
599
                        // Obtenemos el ?ndice en el fichero de expansi?n
600
                        int num = ((Integer) relations.get(integer)).intValue();
601
                        posAnteriorInExpansionFile = num;
602

    
603
                        // Obtenemos la geometr?a para actualiza el ?ndice
604
                        // espacialposteriormente
605
                        featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow();
606

    
607
                        /*
608
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
609
                         * fichero de expansi?n en el que se encuentra la geometr?a
610
                         * modificada
611
                         */
612
                        num = expansionFile.modifyRow(num, feat, actualIndexFields);
613

    
614
                        /*
615
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
616
                         * fichero de expansi?n.
617
                         */
618
                        relations.put(integer, new Integer(num));
619

    
620
                        // Se modifica el ?ndice espacial
621
                        Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
622
                        Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
623
                        this.index.remove(new Envelope(rAnt.getX(), rAnt.getX()
624
                                        + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
625
                                        + rAnt.getHeight()), new Integer(calculatedIndex));
626
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
627
                                        .getY(), r.getY() + r.getHeight()), new Integer(
628
                                        calculatedIndex));
629
                }
630
                isFullExtentDirty = true;
631
                fireAfterModifyRow(calculatedIndex, sourceType);
632
                return posAnteriorInExpansionFile;
633
        }
634

    
635
        /**
636
         * Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
637
         * geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
638
         * fuera una modificaci?n de una geometr?a que estuviese en el fichero de
639
         * expansi?n antes de ser modificada y se pone el puntero de escritura del
640
         * expansion file a justo despues de la penultima geometr?a
641
         *
642
         * @param calculatedIndex
643
         *            ?ndice de la geometr?a que se quiere deshacer su modificaci?n
644
         * @param previousExpansionFileIndex
645
         *            ?ndice que ten?a antes la geometr?a en el expansionFile. Si
646
         *            vale -1 quiere decir que es una modificaci?n de una geometr?a
647
         *            original y por tanto no hay que actualizar el mapa de indices
648
         *            sino eliminar su entrada.
649
         *
650
         * @throws IOException
651
         * @throws DriverIOException
652
         */
653
        public void undoModifyRow(int calculatedIndex,
654
                        int previousExpansionFileIndex, int sourceType) throws IOException,
655
                        DriverIOException {
656

    
657
                // Llega el CalculatedIndex
658
                /*
659
                 * Si la acci?n de modificar se realiz? sobre una geometr?a original
660
                 */
661
                if (previousExpansionFileIndex == -1) {
662

    
663
                        // Se obtiene la geometr?a para actualizar el ?ndice
664
                        // IGeometry g = ((DefaultFeature)
665
                        // getRow(calculatedIndex).getLinkedRow()).getGeometry();
666
                        int inverse = getInversedIndex(calculatedIndex);
667
                        DefaultFeature df = (DefaultFeature) getRow(inverse).getLinkedRow();
668
                        boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
669
                                        sourceType);
670
                        if (cancel)
671
                                return;
672
                        IGeometry g = df.getGeometry();
673
                        // IGeometry g = ova.getShape(calculatedIndex);
674
                        Rectangle2D r = g.getBounds2D();
675

    
676
                        // Se elimina de las relaciones y del fichero de expansi?n
677
                        relations.remove(new Integer(calculatedIndex));
678
                        expansionFile.deleteLastRow();
679

    
680
                        // Se actualizan los ?ndices
681
                        IGeometry gAnt = ova.getShape(calculatedIndex);
682
                        /*
683
                         * IGeometry gAnt = ((DefaultFeature) getRow(calculatedIndex)
684
                         * .getLinkedRow()).getGeometry();
685
                         */
686
                        Rectangle2D rAnt = gAnt.getBounds2D();
687
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
688
                                        .getY(), r.getY() + r.getHeight()), new Integer(
689
                                        calculatedIndex));
690
                        this.index.insert(new Envelope(rAnt.getX(), rAnt.getX()
691
                                        + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
692
                                        + rAnt.getHeight()), new Integer(calculatedIndex));
693
                } else {
694
                        // Se obtiene la geometr?a para actualizar el ?ndice
695
                        IGeometry g = null;
696
                        int inverse = getInversedIndex(calculatedIndex);
697
                        DefaultFeature df = (DefaultFeature) getRow(inverse).getLinkedRow();
698
                        boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
699
                                        sourceType);
700
                        if (cancel)
701
                                return;
702
                        g = df.getGeometry();
703
                        System.out.println("Actual: " + g.toString());
704

    
705
                        Rectangle2D r = g.getBounds2D();
706
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
707
                                        .getY(), r.getY() + r.getHeight()), new Integer(
708
                                        calculatedIndex));
709

    
710
                        // Se actualiza la relaci?n de ?ndices
711
                        // Integer integer = new Integer(geometryIndex);
712
                        relations.put(new Integer(calculatedIndex), new Integer(
713
                                        previousExpansionFileIndex));
714

    
715
                        // Se recupera la geometr?a
716
                        // expansionFile.validateRow(previousExpansionFileIndex);
717

    
718
                        // Se actualizan los ?ndices
719
                        // g = ((IFeature)
720
                        // (expansionFile.getRow(previousExpansionFileIndex).getLinkedRow())).getGeometry();
721
                        // System.out.println("Anterior a la que volvemos : " +
722
                        // g.toString());
723
                        g = ((DefaultFeature) getRow(inverse).getLinkedRow()).getGeometry();
724
                        r = g.getBounds2D();
725
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
726
                                        .getY(), r.getY() + r.getHeight()), new Integer(
727
                                        calculatedIndex));
728

    
729
                }
730
                fireAfterModifyRow(calculatedIndex, sourceType);
731
        }
732

    
733
        /**
734
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
735
         * en la tabla relations.
736
         *
737
         * @param feat
738
         *            geometr?a a guardar.
739
         *
740
         * @return calculatedIndex
741
         *
742
         * @throws DriverIOException
743
         * @throws IOException
744
         */
745
        public int doAddRow(IRow feat, int sourceType) throws DriverIOException,
746
                        IOException {
747
                int calculatedIndex = super.doAddRow(feat, sourceType);
748
                // Actualiza el ?ndice espacial
749
                IGeometry g = ((IFeature) feat).getGeometry();
750
                Rectangle2D r = g.getBounds2D();
751
                index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(),
752
                                r.getY() + r.getHeight()), new Integer(calculatedIndex));
753
                isFullExtentDirty = true;
754
                return calculatedIndex;
755
        }
756

    
757
        /**
758
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
759
         * en el fichero original
760
         *
761
         * @param index
762
         *            DOCUMENT ME!
763
         *
764
         * @throws IOException
765
         * @throws DriverIOException
766
         */
767
        public void undoRemoveRow(int index, int sourceType) throws IOException,
768
                        DriverIOException {
769
                super.undoRemoveRow(index, sourceType);
770

    
771
                IGeometry g = null;
772
                g = ((IFeature) getRow(getInversedIndex(index)).getLinkedRow()).getGeometry();
773

    
774
                Rectangle2D r = g.getBounds2D();
775
                this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
776
                                .getY(), r.getY() + r.getHeight()), new Integer(index));
777
        }
778

    
779
        /**
780
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
781
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
782
         * relaci?n del mapa de relaciones
783
         *
784
         * @param index
785
         *            ?ndice de la geometr?a que se a?adi?
786
         *
787
         * @throws DriverIOException
788
         * @throws IOException
789
         */
790
        public void undoAddRow(int calculatedIndex, int sourceType)
791
                        throws DriverIOException, IOException {
792
                int inverse = getInversedIndex(calculatedIndex);
793
                IGeometry g = ((IFeature) getRow(inverse).getLinkedRow()).getGeometry();
794
                Rectangle2D r = g.getBounds2D();
795
                this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
796
                                .getY(), r.getY() + r.getHeight()),
797
                                new Integer(calculatedIndex));
798

    
799
                super.undoAddRow(calculatedIndex, sourceType);
800
                setSelection(new FBitSet());
801
        }
802

    
803
        /**
804
         * Obtiene las geometr?as que se encuentran en el rect?ngulo que se pasa
805
         * como par?metro haciendo uso del ?ndice espacial
806
         *
807
         * @param r
808
         *            Rect?ngulo indicando la porci?n del espacio para el cual se
809
         *            quiere saber los ?ndices de las geometr?as que se encuentran
810
         *            dentro de ?l
811
         *
812
         * @return Array de ?ndices para su uso con getGeometry, removeGeometry, ...
813
         */
814
        /*
815
         * public int[] getRowsIndexes_OL(Rectangle2D r) { Envelope e = new
816
         * Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(), r.getY() +
817
         * r.getHeight()); List l = index.query(e); int[] indexes = new
818
         * int[l.size()];
819
         *
820
         * for (int index = 0; index < l.size(); index++) { Integer i = (Integer)
821
         * l.get(index); indexes[index] = getInversedIndex(i.intValue()); }
822
         *
823
         * return indexes; }
824
         */
825
        /**
826
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
827
         */
828
        public int getShapeCount() throws DriverIOException {
829
                try {
830
                        return getRowCount();
831
                } catch (IOException e) {
832
                        throw new DriverIOException(e);
833
                }
834
        }
835

    
836
        /**
837
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
838
         */
839
        public Rectangle2D getFullExtent() throws DriverIOException {
840
                if (fullExtent == null) {
841
                        fullExtent = ova.getFullExtent();
842
                }
843

    
844
                if (isFullExtentDirty) {
845
                        fullExtent = reCalculateFullExtent();
846
                        isFullExtentDirty = false;
847
                }
848
                return fullExtent;
849
        }
850

    
851
        /**
852
         * Use it BEFORE writing to a file.
853
         *
854
         * @return real full extent.
855
         * @throws DriverIOException
856
         */
857
        public Rectangle2D reCalculateFullExtent() throws DriverIOException {
858
                if (getShapeCount() > 0) {
859
                        fullExtent = getShape(0).getBounds2D();
860
                        for (int i = 1; i < getShapeCount(); i++) {
861
                                fullExtent.add(getShape(i).getBounds2D());
862
                        }
863
                } else {
864
                        fullExtent = ova.getFullExtent();
865
                }
866
                return fullExtent;
867
        }
868

    
869
        /**
870
         * En la implementaci?n por defecto podemos hacer que cada feature tenga ID =
871
         * numero de registro. En el DBAdapter podr?amos "overrride" este m?todo y
872
         * poner como ID de la Feature el campo ?nico escogido en la base de datos.
873
         *
874
         * @param numReg
875
         * @return
876
         * @throws DriverException
877
         */
878
        public IFeature getFeature(int numReg) throws DriverException {
879
                IGeometry geom;
880
                IFeature feat = null;
881
                try {
882
                        geom = getShape(numReg);
883
                        DataSource rs = getRecordset();
884
                        Value[] regAtt = new Value[rs.getFieldCount()];
885
                        for (int fieldId = 0; fieldId < rs.getFieldCount(); fieldId++) {
886
                                regAtt[fieldId] = rs.getFieldValue(numReg, fieldId);
887
                        }
888
                        feat = new DefaultFeature(geom, regAtt, numReg + "");
889
                } catch (DriverIOException e) {
890
                        DriverIOExceptionType type =
891
                                new DriverIOExceptionType();
892
                        type.setDriverName(this.getDriver().getName());
893
                        throw new DriverException(e, type);
894
                        
895
                } catch (DriverLoadException e) {
896
                        DriverNotLoadedExceptionType type =
897
                                new DriverNotLoadedExceptionType();
898
                        type.setDriverName(getDriver().getName());
899
                        throw new DriverException(e, type);
900
                        
901
                        
902
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
903
                        
904
                        throw new DriverException(e);
905
                }
906
                return feat;
907
        }
908

    
909
        public void stopEdition(IWriter writer, int sourceType)
910
                        throws EditionException {
911
                ISpatialWriter spatialWriter = (ISpatialWriter) writer;
912
//                spatialWriter.setFlatness(FConverter.flatness);
913
                super.stopEdition(writer, sourceType);
914
                try {
915
                        ova.getDriver().reload();
916
                } catch (IOException e) {
917
                        e.printStackTrace();
918
                        throw new EditionException(e);
919
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
920
                        e.printStackTrace();
921
                        throw new EditionException(e);
922
                }
923
        }
924

    
925
        public Rectangle2D getShapeBounds(int index) throws IOException {
926
                // Solo se utiliza cuando el driver es BoundedShapes
927
                // Si no est? en el fichero de expansi?n
928
                Integer integer = new Integer((int) index);
929
                if (!relations.containsKey(integer)) {
930
                        if (ova.getDriver() instanceof BoundedShapes) {
931
                                BoundedShapes bs = (BoundedShapes) ova.getDriver();
932
                                return bs.getShapeBounds(index);
933
                        } else {
934
                                return ova.getDriver().getShape(index).getBounds2D();
935
                        }
936

    
937
                } else {
938
                        int num = ((Integer) relations.get(integer)).intValue();
939
                        DefaultRowEdited feat;
940
                        feat = (DefaultRowEdited) expansionFile.getRow(num);
941
                        if (feat.getStatus() == IRowEdited.STATUS_DELETED)
942
                                return null;
943
                        IGeometry geom = ((IFeature) feat.getLinkedRow()).getGeometry();
944
                        return geom.getBounds2D();// getGeometry();
945
                }
946

    
947
        }
948

    
949
        public int getShapeType(int index) {
950
                try {
951
                        return ova.getShapeType();
952
                } catch (DriverIOException e) {
953
                        // TODO Auto-generated catch block
954
                        e.printStackTrace();
955
                }
956
                return FShape.MULTI;
957
        }
958

    
959
        /**
960
         * Usar solo cuando est?s seguro de que puedes gastar memoria. Nosotros lo
961
         * usamos para las b?squedas por ?ndice espacial con el handle. La idea es
962
         * usarlo una vez, guardar las geometr?as que necesitas en ese extent y
963
         * trabajar con ellas hasta el siguiente cambio de extent.
964
         *
965
         * @param r
966
         * @param strEPSG
967
         * @return
968
         * @throws DriverException
969
         */
970
        public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG)
971
                        throws DriverException {
972
                // En esta clase suponemos random access.
973
                // Luego tendremos otra clase que sea VectorialEditableDBAdapter
974
                // que reescribir? este m?todo.
975
                Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
976
                List l = index.query(e);
977
                IRowEdited[] feats = new IRowEdited[l.size()];
978
                try {
979
                        for (int index = 0; index < l.size(); index++) {
980
                                Integer i = (Integer) l.get(index);
981
                                int inverse = getInversedIndex(i.intValue());
982
                                feats[index] = (IRowEdited) getRow(inverse);
983
                        }
984
                } catch (IOException ex) {
985
                        DriverIOExceptionType type =
986
                                new DriverIOExceptionType();
987
                        type.setDriverName(this.getDriver().getName());
988
                        throw new DriverException(ex, type);
989
                        
990
                } catch (DriverLoadException ex) {
991
                        DriverNotLoadedExceptionType type =
992
                                new DriverNotLoadedExceptionType();
993
                        type.setDriverName(getDriver().getName());
994
                        throw new DriverException(ex, type);
995
                } catch (DriverIOException ex) {
996
                        DriverIOExceptionType type =
997
                                new DriverIOExceptionType();
998
                        type.setDriverName(this.getDriver().getName());
999
                        throw new DriverException(ex, type);
1000
                } 
1001
                return feats;
1002
        }
1003

    
1004
        public void setSpatialIndex(SpatialIndex spatialIndex) {
1005
                index = (Quadtree) spatialIndex;
1006
        }
1007

    
1008
        public void setFullExtent(Rectangle2D fullExtent2) {
1009
                fullExtent = fullExtent2;
1010
        }
1011

    
1012
        /**
1013
         * DOCUMENT ME!
1014
         *
1015
         * @return DOCUMENT ME!
1016
         */
1017
        public Image getSelectionImage() {
1018
                return selectionImage;
1019
        }
1020

    
1021
        public Image getHandlersImage() {
1022
                return handlersImage;
1023
        }
1024

    
1025
        /**
1026
         * DOCUMENT ME!
1027
         *
1028
         * @param i
1029
         *            DOCUMENT ME!
1030
         */
1031
        public void setSelectionImage(Image i) {
1032
                selectionImage = i;
1033
        }
1034

    
1035
        public void setHandlersImage(BufferedImage handlersImage) {
1036
                this.handlersImage = handlersImage;
1037
        }
1038

    
1039
//        public double getFlatness() {
1040
//                return flatness;
1041
//        }
1042
//
1043
//        public void setFlatness(double d) {
1044
//                flatness=d;
1045
//        }
1046

    
1047
}