Statistics
| Revision:

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

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

    
241
import java.awt.Image;
242
import java.awt.geom.Rectangle2D;
243
import java.awt.image.BufferedImage;
244
import java.io.IOException;
245
import java.util.List;
246

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

    
270
/**
271
 * @author fjp
272
 *
273
 */
274
public class VectorialEditableAdapter extends EditableAdapter implements
275
                ReadableVectorial, BoundedShapes {
276
        protected ReadableVectorial ova;
277

    
278
        protected Quadtree index;
279

    
280
        protected Rectangle2D fullExtent;
281

    
282
        protected Image selectionImage;
283

    
284
        protected BufferedImage handlersImage;
285

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

    
305
        public VectorialEditableAdapter() {
306
                super();
307
        }
308

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

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

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

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

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

    
378
        }
379

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

    
389
        public ReadableVectorial getOriginalAdapter() {
390
                return ova;
391
        }
392

    
393
        public VectorialDriver getDriver() {
394
                return ova.getDriver();
395
        }
396

    
397
        public void setDriver(VectorialDriver driver) {
398
                this.ova.setDriver(driver);
399
        }
400

    
401
        public DriverAttributes getDriverAttributes() {
402
                return ova.getDriverAttributes();
403
        }
404

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

    
419

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

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

    
436
                                        if (g == null) {
437
                                                continue;
438
                                        }
439

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

    
458
                System.err.println("Se han metido en el ?ndice "
459
                                + index.queryAll().size() + " geometr?as");
460
        }
461

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

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

    
489
                        return edRow;
490
                }
491
        }
492

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

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

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

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

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

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

    
600
                        // Obtenemos la geometr?a para actualiza el ?ndice
601
                        // espacialposteriormente
602
                        featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow();
603

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

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

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

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

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

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

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

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

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

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

    
712
                        // Se recupera la geometr?a
713
                        // expansionFile.validateRow(previousExpansionFileIndex);
714

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

    
726
                }
727
                fireAfterModifyRow(calculatedIndex, sourceType);
728
        }
729

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

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

    
768
                IGeometry g = null;
769
                g = ((IFeature) getRow(getInversedIndex(index)).getLinkedRow()).getGeometry();
770

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

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

    
796
                super.undoAddRow(calculatedIndex, sourceType);
797
                setSelection(new FBitSet());
798
        }
799

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

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

    
841
                if (isFullExtentDirty) {
842
                        fullExtent = reCalculateFullExtent();
843
                        isFullExtentDirty = false;
844
                }
845
                return fullExtent;
846
        }
847

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

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

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

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

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

    
944
        }
945

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

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

    
1001
        public void setSpatialIndex(SpatialIndex spatialIndex) {
1002
                index = (Quadtree) spatialIndex;
1003
        }
1004

    
1005
        public void setFullExtent(Rectangle2D fullExtent2) {
1006
                fullExtent = fullExtent2;
1007
        }
1008

    
1009
        /**
1010
         * DOCUMENT ME!
1011
         *
1012
         * @return DOCUMENT ME!
1013
         */
1014
        public Image getSelectionImage() {
1015
                return selectionImage;
1016
        }
1017

    
1018
        public Image getHandlersImage() {
1019
                return handlersImage;
1020
        }
1021

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

    
1032
        public void setHandlersImage(BufferedImage handlersImage) {
1033
                this.handlersImage = handlersImage;
1034
        }
1035

    
1036
//        public double getFlatness() {
1037
//                return flatness;
1038
//        }
1039
//
1040
//        public void setFlatness(double d) {
1041
//                flatness=d;
1042
//        }
1043

    
1044
}