Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_916 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableAdapter.java @ 12327

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

    
247
import java.awt.Image;
248
import java.awt.geom.Rectangle2D;
249
import java.awt.image.BufferedImage;
250
import java.io.IOException;
251
import java.util.List;
252

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

    
276
/**
277
 * @author fjp
278
 *
279
 */
280
public class VectorialEditableAdapter extends EditableAdapter implements
281
                ReadableVectorial, BoundedShapes {
282
        protected ReadableVectorial ova;
283

    
284
        protected Quadtree index;
285

    
286
        protected Rectangle2D fullExtent;
287

    
288
        protected Image selectionImage;
289

    
290
        protected BufferedImage handlersImage;
291

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

    
311
        public VectorialEditableAdapter() {
312
                super();
313
        }
314

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

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

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

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

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

    
384
        }
385

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

    
395
        public ReadableVectorial getOriginalAdapter() {
396
                return ova;
397
        }
398

    
399
        public VectorialDriver getDriver() {
400
                return ova.getDriver();
401
        }
402

    
403
        public void setDriver(VectorialDriver driver) {
404
                this.ova.setDriver(driver);
405
        }
406

    
407
        public DriverAttributes getDriverAttributes() {
408
                return ova.getDriverAttributes();
409
        }
410

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

    
425

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

    
433
                                for (int i = 0; i < ova.getShapeCount(); i++) {
434
                                        Rectangle2D r=null;
435
                                        if (ova.getDriver() instanceof BoundedShapes) {
436
                                                r = ((BoundedShapes) ova.getDriver()).getShapeBounds(i);
437
                                        } else {
438
                                                IGeometry g = null;
439
                                                try {
440
                                                        g = ((DefaultFeature) ova.getFeature(i))
441
                                                                        .getGeometry();
442
                                                } catch (DriverException e1) {
443
                                                        e1.printStackTrace();
444
                                                }
445

    
446
                                                if (g == null) {
447
                                                        continue;
448
                                                }
449

    
450
                                                r = g.getBounds2D();
451
                                        }
452
                                        Envelope e = new Envelope(r.getX(),
453
                                                        r.getX() + r.getWidth(), r.getY(), r.getY()
454
                                                                        + r.getHeight());
455
                                        index.insert(e, new Integer(i));
456
                                        if (fullExtent == null) {
457
                                                fullExtent = r;
458
                                        } else {
459
                                                fullExtent = fullExtent.createUnion(r);
460
                                        }
461
                                }
462
                        }
463
                } catch (DriverIOException e) {
464
                        throw new EditionException(e);
465
                } catch (IOException e) {
466
                        throw new EditionException(e);
467
                }
468

    
469
                System.err.println("Se han metido en el ?ndice "
470
                                + index.queryAll().size() + " geometr?as");
471
        }
472

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

    
493
//                        return edRow;
494
                } else {
495
                        int num = ((Integer) relations.get(integer)).intValue();
496
                        IRowEdited aux = expansionFile.getRow(num);
497
                        edRow = new DefaultRowEdited(aux.getLinkedRow().cloneRow(), aux
498
                                        .getStatus(), index);
499

    
500
                        return edRow;
501
                }
502
        }
503

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

    
524
                IFeature feat = null;
525
                delRows.set(index, true);
526
                // Si la geometr?a no ha sido modificada
527
                if (!relations.containsKey(integer)) {
528

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

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

    
582
                IFeature featAnt = null;
583
//                System.err.println("Modifica una Row en la posici?n: "
584
//                                + calculatedIndex);
585
                // Si la geometr?a no ha sido modificada
586
                if (!relations.containsKey(integer)) {
587
                        int newPosition = expansionFile.addRow(feat,
588
                                        IRowEdited.STATUS_MODIFIED, actualIndexFields);
589
                        relations.put(integer, new Integer(newPosition));
590

    
591
                        if (sourceType == EditionEvent.GRAPHIC) {
592
                                // Se actualiza el ?ndice espacial
593
                                try {
594
                                        featAnt = (DefaultFeature) (ova.getFeature(calculatedIndex));
595
                                } catch (DriverException e) {
596
                                        e.printStackTrace();
597
                                }
598
                                IGeometry g = featAnt.getGeometry();
599
                                Rectangle2D rAnt = g.getBounds2D();
600
                                Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
601
                                this.index.remove(new Envelope(rAnt.getX(), rAnt.getX()
602
                                                + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
603
                                                + rAnt.getHeight()), new Integer(calculatedIndex));
604
                                this.index.insert(new Envelope(r.getX(), r.getX()
605
                                                + r.getWidth(), r.getY(), r.getY() + r.getHeight()),
606
                                                new Integer(calculatedIndex));
607
                        }
608
                } else {
609
                        // Obtenemos el ?ndice en el fichero de expansi?n
610
                        int num = ((Integer) relations.get(integer)).intValue();
611
                        posAnteriorInExpansionFile = num;
612

    
613
                        // Obtenemos la geometr?a para actualiza el ?ndice
614
                        // espacialposteriormente
615
                        featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow();
616

    
617
                        /*
618
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
619
                         * fichero de expansi?n en el que se encuentra la geometr?a
620
                         * modificada
621
                         */
622
                        num = expansionFile.modifyRow(num, feat, actualIndexFields);
623

    
624
                        /*
625
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
626
                         * fichero de expansi?n.
627
                         */
628
                        relations.put(integer, new Integer(num));
629
                        if (sourceType == EditionEvent.GRAPHIC) {
630
                                // Se modifica el ?ndice espacial
631
                                Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
632
                                Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
633
                                this.index.remove(new Envelope(rAnt.getX(), rAnt.getX()
634
                                                + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
635
                                                + rAnt.getHeight()), new Integer(calculatedIndex));
636
                                this.index.insert(new Envelope(r.getX(), r.getX()
637
                                                + r.getWidth(), r.getY(), r.getY() + r.getHeight()),
638
                                                new Integer(calculatedIndex));
639
                        }
640
                }
641
                isFullExtentDirty = true;
642
                fireAfterModifyRow(calculatedIndex, sourceType);
643
                return posAnteriorInExpansionFile;
644
        }
645

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

    
668
                // Llega el CalculatedIndex
669
                /*
670
                 * Si la acci?n de modificar se realiz? sobre una geometr?a original
671
                 */
672
                if (previousExpansionFileIndex == -1) {
673

    
674
                        // Se obtiene la geometr?a para actualizar el ?ndice
675
                        // IGeometry g = ((DefaultFeature)
676
                        // getRow(calculatedIndex).getLinkedRow()).getGeometry();
677
                        int inverse = getInversedIndex(calculatedIndex);
678
                        DefaultFeature df = (DefaultFeature) getRow(inverse).getLinkedRow();
679
                        boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
680
                                        sourceType);
681
                        if (cancel)
682
                                return;
683
                        IGeometry g = df.getGeometry();
684
                        // IGeometry g = ova.getShape(calculatedIndex);
685
                        Rectangle2D r = g.getBounds2D();
686

    
687
                        // Se elimina de las relaciones y del fichero de expansi?n
688
                        relations.remove(new Integer(calculatedIndex));
689
                        expansionFile.deleteLastRow();
690

    
691
                        // Se actualizan los ?ndices
692
                        IGeometry gAnt = ova.getShape(calculatedIndex);
693
                        /*
694
                         * IGeometry gAnt = ((DefaultFeature) getRow(calculatedIndex)
695
                         * .getLinkedRow()).getGeometry();
696
                         */
697
                        Rectangle2D rAnt = gAnt.getBounds2D();
698
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
699
                                        .getY(), r.getY() + r.getHeight()), new Integer(
700
                                        calculatedIndex));
701
                        this.index.insert(new Envelope(rAnt.getX(), rAnt.getX()
702
                                        + rAnt.getWidth(), rAnt.getY(), rAnt.getY()
703
                                        + rAnt.getHeight()), new Integer(calculatedIndex));
704
                } else {
705
                        // Se obtiene la geometr?a para actualizar el ?ndice
706
                        IGeometry g = null;
707
                        int inverse = getInversedIndex(calculatedIndex);
708
                        DefaultFeature df = (DefaultFeature) getRow(inverse).getLinkedRow();
709
                        boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
710
                                        sourceType);
711
                        if (cancel)
712
                                return;
713
                        g = df.getGeometry();
714
                        System.out.println("Actual: " + g.toString());
715

    
716
                        Rectangle2D r = g.getBounds2D();
717
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
718
                                        .getY(), r.getY() + r.getHeight()), new Integer(
719
                                        calculatedIndex));
720

    
721
                        // Se actualiza la relaci?n de ?ndices
722
                        // Integer integer = new Integer(geometryIndex);
723
                        relations.put(new Integer(calculatedIndex), new Integer(
724
                                        previousExpansionFileIndex));
725

    
726
                        // Se recupera la geometr?a
727
                        // expansionFile.validateRow(previousExpansionFileIndex);
728

    
729
                        // Se actualizan los ?ndices
730
                        // g = ((IFeature)
731
                        // (expansionFile.getRow(previousExpansionFileIndex).getLinkedRow())).getGeometry();
732
                        // System.out.println("Anterior a la que volvemos : " +
733
                        // g.toString());
734
                        g = ((DefaultFeature) getRow(inverse).getLinkedRow()).getGeometry();
735
                        r = g.getBounds2D();
736
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
737
                                        .getY(), r.getY() + r.getHeight()), new Integer(
738
                                        calculatedIndex));
739

    
740
                }
741
                fireAfterModifyRow(calculatedIndex, sourceType);
742
        }
743

    
744
        /**
745
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
746
         * en la tabla relations.
747
         *
748
         * @param feat
749
         *            geometr?a a guardar.
750
         *
751
         * @return calculatedIndex
752
         *
753
         * @throws DriverIOException
754
         * @throws IOException
755
         */
756
        public int doAddRow(IRow feat, int sourceType) throws DriverIOException,
757
                        IOException {
758
                int calculatedIndex = super.doAddRow(feat, sourceType);
759
                // Actualiza el ?ndice espacial
760
                IGeometry g = ((IFeature) feat).getGeometry();
761
                Rectangle2D r = g.getBounds2D();
762
                index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(),
763
                                r.getY() + r.getHeight()), new Integer(calculatedIndex));
764
                isFullExtentDirty = true;
765
                return calculatedIndex;
766
        }
767

    
768
        /**
769
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
770
         * en el fichero original
771
         *
772
         * @param index
773
         *            DOCUMENT ME!
774
         *
775
         * @throws IOException
776
         * @throws DriverIOException
777
         */
778
        public void undoRemoveRow(int index, int sourceType) throws IOException,
779
                        DriverIOException {
780
                super.undoRemoveRow(index, sourceType);
781

    
782
                IGeometry g = null;
783
                g = ((IFeature) getRow(getInversedIndex(index)).getLinkedRow()).getGeometry();
784

    
785
                Rectangle2D r = g.getBounds2D();
786
                this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r
787
                                .getY(), r.getY() + r.getHeight()), new Integer(index));
788
        }
789

    
790
        /**
791
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
792
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
793
         * relaci?n del mapa de relaciones
794
         *
795
         * @param index
796
         *            ?ndice de la geometr?a que se a?adi?
797
         *
798
         * @throws DriverIOException
799
         * @throws IOException
800
         */
801
        public void undoAddRow(int calculatedIndex, int sourceType)
802
                        throws DriverIOException, IOException {
803
                int inverse = getInversedIndex(calculatedIndex);
804
                IGeometry g = ((IFeature) getRow(inverse).getLinkedRow()).getGeometry();
805
                Rectangle2D r = g.getBounds2D();
806
                this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(), r
807
                                .getY(), r.getY() + r.getHeight()),
808
                                new Integer(calculatedIndex));
809

    
810
                super.undoAddRow(calculatedIndex, sourceType);
811
                setSelection(new FBitSet());
812
        }
813

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

    
847
        /**
848
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
849
         */
850
        public Rectangle2D getFullExtent() throws DriverIOException {
851
                if (fullExtent == null) {
852
                        fullExtent = ova.getFullExtent();
853
                }
854

    
855
                if (isFullExtentDirty) {
856
                        fullExtent = reCalculateFullExtent();
857
                        isFullExtentDirty = false;
858
                }
859
                return fullExtent;
860
        }
861

    
862
        /**
863
         * Use it BEFORE writing to a file.
864
         *
865
         * @return real full extent.
866
         * @throws DriverIOException
867
         */
868
        public Rectangle2D reCalculateFullExtent() throws DriverIOException {
869
                if (getShapeCount() > 0) {
870
                        fullExtent = getShape(0).getBounds2D();
871
                        for (int i = 1; i < getShapeCount(); i++) {
872
                                fullExtent.add(getShape(i).getBounds2D());
873
                        }
874
                } else {
875
                        fullExtent = ova.getFullExtent();
876
                }
877
                return fullExtent;
878
        }
879

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

    
906
                } catch (DriverLoadException e) {
907
                        DriverNotLoadedExceptionType type =
908
                                new DriverNotLoadedExceptionType();
909
                        type.setDriverName(getDriver().getName());
910
                        throw new DriverException(e, type);
911

    
912

    
913
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
914

    
915
                        throw new DriverException(e);
916
                }
917
                return feat;
918
        }
919

    
920
        public void stopEdition(IWriter writer, int sourceType)
921
                        throws EditionException {
922
                ISpatialWriter spatialWriter = (ISpatialWriter) writer;
923
//                spatialWriter.setFlatness(FConverter.flatness);
924
                super.stopEdition(writer, sourceType);
925
                try {
926
                        ova.getDriver().reload();
927
                } catch (IOException e) {
928
                        e.printStackTrace();
929
                        throw new EditionException(e);
930
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
931
                        e.printStackTrace();
932
                        throw new EditionException(e);
933
                }
934
        }
935

    
936
        public Rectangle2D getShapeBounds(int index) throws IOException {
937
                // Solo se utiliza cuando el driver es BoundedShapes
938
                // Si no est? en el fichero de expansi?n
939
                Integer integer = new Integer((int) index);
940
                if (!relations.containsKey(integer)) {
941
                        if (ova.getDriver() instanceof BoundedShapes) {
942
                                BoundedShapes bs = (BoundedShapes) ova.getDriver();
943
                                return bs.getShapeBounds(index);
944
                        } else {
945
                                return ova.getDriver().getShape(index).getBounds2D();
946
                        }
947

    
948
                } else {
949
                        int num = ((Integer) relations.get(integer)).intValue();
950
                        DefaultRowEdited feat;
951
                        feat = (DefaultRowEdited) expansionFile.getRow(num);
952
                        if (feat.getStatus() == IRowEdited.STATUS_DELETED)
953
                                return null;
954
                        IGeometry geom = ((IFeature) feat.getLinkedRow()).getGeometry();
955
                        return geom.getBounds2D();// getGeometry();
956
                }
957

    
958
        }
959

    
960
        public int getShapeType(int index) {
961
                try {
962
                        return ova.getShapeType();
963
                } catch (DriverIOException e) {
964
                        // TODO Auto-generated catch block
965
                        e.printStackTrace();
966
                }
967
                return FShape.MULTI;
968
        }
969

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

    
1001
                } catch (DriverLoadException ex) {
1002
                        DriverNotLoadedExceptionType type =
1003
                                new DriverNotLoadedExceptionType();
1004
                        type.setDriverName(getDriver().getName());
1005
                        throw new DriverException(ex, type);
1006
                } catch (DriverIOException ex) {
1007
                        DriverIOExceptionType type =
1008
                                new DriverIOExceptionType();
1009
                        type.setDriverName(this.getDriver().getName());
1010
                        throw new DriverException(ex, type);
1011
                }
1012
                return feats;
1013
        }
1014

    
1015
        public void setSpatialIndex(SpatialIndex spatialIndex) {
1016
                index = (Quadtree) spatialIndex;
1017
        }
1018

    
1019
        public void setFullExtent(Rectangle2D fullExtent2) {
1020
                fullExtent = fullExtent2;
1021
        }
1022

    
1023
        /**
1024
         * DOCUMENT ME!
1025
         *
1026
         * @return DOCUMENT ME!
1027
         */
1028
        public Image getSelectionImage() {
1029
                return selectionImage;
1030
        }
1031

    
1032
        public Image getHandlersImage() {
1033
                return handlersImage;
1034
        }
1035

    
1036
        /**
1037
         * DOCUMENT ME!
1038
         *
1039
         * @param i
1040
         *            DOCUMENT ME!
1041
         */
1042
        public void setSelectionImage(Image i) {
1043
                selectionImage = i;
1044
        }
1045

    
1046
        public void setHandlersImage(BufferedImage handlersImage) {
1047
                this.handlersImage = handlersImage;
1048
        }
1049

    
1050
//        public double getFlatness() {
1051
//                return flatness;
1052
//        }
1053
//
1054
//        public void setFlatness(double d) {
1055
//                flatness=d;
1056
//        }
1057

    
1058
}