Statistics
| Revision:

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

History | View | Annotate | Download (23.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 3836 2006-01-30 08:18:50Z caballero $
47
 * $Log$
48
 * Revision 1.10  2006-01-30 08:18:14  caballero
49
 * m?todos para deshacer y rehacer
50
 *
51
 * Revision 1.9  2006/01/23 17:30:28  caballero
52
 * coger los datos del ova
53
 *
54
 * Revision 1.8  2006/01/23 16:16:16  caballero
55
 * getRowIndex
56
 *
57
 * Revision 1.7  2006/01/20 08:37:10  fjp
58
 * Preparando la edici?n
59
 *
60
 * Revision 1.6  2006/01/19 12:48:20  caballero
61
 * poder modificar su vectorial Adapter
62
 *
63
 * Revision 1.5  2006/01/19 09:28:11  fjp
64
 * Preparando la edici?n
65
 *
66
 * Revision 1.4  2006/01/17 10:24:02  fjp
67
 * Preparando edici?n
68
 *
69
 * Revision 1.3  2006/01/16 12:47:38  fjp
70
 * Preparando edici?n
71
 *
72
 * Revision 1.2  2006/01/16 11:23:00  fjp
73
 * Preparando edici?n
74
 *
75
 * Revision 1.1  2006/01/12 13:39:14  fjp
76
 * preaparar edicion
77
 *
78
 *
79
 */
80
package com.iver.cit.gvsig.fmap.edition;
81

    
82
import java.awt.Image;
83
import java.awt.geom.Rectangle2D;
84
import java.io.IOException;
85
import java.util.HashMap;
86
import java.util.List;
87
import java.util.Vector;
88

    
89
import com.hardcode.driverManager.DriverLoadException;
90
import com.hardcode.gdbms.engine.data.DataSource;
91
import com.hardcode.gdbms.engine.data.DataSourceFactory;
92
import com.hardcode.gdbms.engine.data.NoSuchTableException;
93
import com.hardcode.gdbms.engine.data.driver.DriverException;
94
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
95
import com.hardcode.gdbms.engine.data.edition.DataWare;
96
import com.hardcode.gdbms.engine.values.Value;
97
import com.iver.cit.gvsig.fmap.core.IFeature;
98
import com.iver.cit.gvsig.fmap.core.IGeometry;
99
import com.iver.cit.gvsig.fmap.core.IRow;
100
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
101
import com.iver.cit.gvsig.fmap.edition.commands.AddRowCommand;
102
import com.iver.cit.gvsig.fmap.edition.commands.CommandCollection;
103
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord;
104
import com.iver.cit.gvsig.fmap.edition.commands.MemoryCommandRecord;
105
import com.iver.cit.gvsig.fmap.edition.commands.ModifyRowCommand;
106
import com.iver.cit.gvsig.fmap.edition.commands.RemoveRowCommand;
107
import com.iver.cit.gvsig.fmap.layers.FBitSet;
108
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
109
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
110
import com.vividsolutions.jts.geom.Envelope;
111
import com.vividsolutions.jts.index.quadtree.Quadtree;
112

    
113
/**
114
 * @author fjp
115
 *
116
 */
117
public class VectorialEditableAdapter extends VectorialAdapter implements
118
                IEditableSource {
119
        private VectorialAdapter ova; // Original Vectorial Adapter
120

    
121
        private boolean isEditing = false;
122

    
123
        private DataSource ds = null;
124

    
125
        private FBitSet delgeometries = new FBitSet();
126

    
127
        private CommandRecord cr;
128

    
129
        private FBitSet seleccion;
130
        private Image selectionImage;
131

    
132
        private Quadtree index;
133

    
134
        /**
135
         * Flag que indica que hay que tomar las siguientes operaciones como una
136
         * operaci?n at?mica *
137
         */
138
        private boolean complex = false;
139

    
140
        private CommandCollection commands = null;
141

    
142
        /*
143
         * Establece una relaci?n entre los ?ndices de las geometr?as en el
144
         * EditableFeatureSource y los ?ndices en el fichero de expansi?n
145
         */
146
        private HashMap relations = new HashMap();
147

    
148
        /*
149
         * Fichero en el que se guardan las nuevas geometr?as, producto de adiciones
150
         * o de modificaciones
151
         */
152
        private ExpansionFile expansionFile;
153

    
154
        private int numAdd = 0;
155

    
156
        private class myObjectDriver implements ObjectDriver {
157

    
158
                /*
159
                 * (non-Javadoc)
160
                 *
161
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
162
                 */
163
                public int[] getPrimaryKeys() throws DriverException {
164
                        // TODO Auto-generated method stub
165
                        return null;
166
                }
167

    
168
                /*
169
                 * (non-Javadoc)
170
                 *
171
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
172
                 */
173
                public void write(DataWare dataWare) throws DriverException {
174
                        // TODO Auto-generated method stub
175

    
176
                }
177

    
178
                /*
179
                 * (non-Javadoc)
180
                 *
181
                 * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
182
                 */
183
                public void setDataSourceFactory(DataSourceFactory dsf) {
184
                        // TODO Auto-generated method stub
185

    
186
                }
187

    
188
                /*
189
                 * (non-Javadoc)
190
                 *
191
                 * @see com.hardcode.driverManager.Driver#getName()
192
                 */
193
                public String getName() {
194
                        // TODO Auto-generated method stub
195
                        return null;
196
                }
197

    
198
                /*
199
                 * (non-Javadoc)
200
                 *
201
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
202
                 *      int)
203
                 */
204
                public Value getFieldValue(long rowIndex, int fieldId)
205
                                throws DriverException {
206
                        // Si no est? en el fichero de expansi?n
207
                        Integer integer = new Integer((int) rowIndex);
208
                        try {
209
                                if (!relations.containsKey(integer)) {
210
                                        // Si ha sido eliminada
211
                                        if (delgeometries.get(integer.intValue())) {
212
                                                return null;
213
                                        } else {
214
                                                return ova.getRecordset().getFieldValue(rowIndex,
215
                                                                fieldId);
216
                                        }
217
                                } else {
218
                                        int num = ((Integer) relations.get(integer)).intValue();
219
                                        IFeature feat = (IFeature) expansionFile.getRow(num).getLinkedRow();
220
                                        return feat.getAttribute(fieldId);
221
                                }
222
                        } catch (DriverException e) {
223
                                e.printStackTrace();
224
                                throw new DriverException(e);
225
                        } catch (DriverLoadException e) {
226
                                e.printStackTrace();
227
                                throw new DriverException(e);
228
                        } catch (IOException e) {
229
                                e.printStackTrace();
230
                                throw new DriverException(e);
231
                        }
232
                }
233

    
234
                /*
235
                 * (non-Javadoc)
236
                 *
237
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
238
                 */
239
                public int getFieldCount() throws DriverException {
240
                        // TODO Por ahora, no dejamos que se a?adan campos
241
                        try {
242
                                return ova.getRecordset().getFieldCount();
243
                        } catch (DriverLoadException e) {
244
                                e.printStackTrace();
245
                                throw new DriverException(e);
246
                        }
247
                }
248

    
249
                /*
250
                 * (non-Javadoc)
251
                 *
252
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
253
                 */
254
                public String getFieldName(int fieldId) throws DriverException {
255
                        try {
256
                                return ova.getRecordset().getFieldName(fieldId);
257
                        } catch (DriverLoadException e) {
258
                                e.printStackTrace();
259
                                throw new DriverException(e);
260
                        }
261
                }
262

    
263
                /*
264
                 * (non-Javadoc)
265
                 *
266
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
267
                 */
268
                public long getRowCount() throws DriverException {
269
                        try {
270
                                return ova.getShapeCount() + numAdd;
271
                        } catch (DriverIOException e) {
272
                                e.printStackTrace();
273
                                throw new DriverException(e);
274
                        }
275
                }
276

    
277
                /*
278
                 * (non-Javadoc)
279
                 *
280
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
281
                 */
282
                public int getFieldType(int i) throws DriverException {
283
                        try
284
                        {
285
                                return ova.getRecordset().getFieldType(i);
286
                        } catch (DriverLoadException e) {
287
                                e.printStackTrace();
288
                                throw new DriverException(e);
289
                        }
290

    
291
                }
292

    
293
        }
294

    
295
        private ObjectDriver editingDriver = new myObjectDriver();
296
        public VectorialEditableAdapter(VectorialAdapter ova){
297
                expansionFile=new MemoryExpansionFile();
298
                cr=new MemoryCommandRecord();
299
                this.ova = ova;
300
                this.seleccion=new FBitSet();
301
        }
302
        /*
303
         * (non-Javadoc)
304
         *
305
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
306
         */
307
        public DataSource getRecordset() throws DriverLoadException {
308
                if (isEditing) {
309
                        if (ds == null) {
310
                                String name = LayerFactory.getDataSourceFactory()
311
                                                .addDataSource((ObjectDriver) editingDriver);
312
                                try {
313
                                        ds = LayerFactory.getDataSourceFactory()
314
                                                        .createRandomDataSource(name,
315
                                                                        DataSourceFactory.AUTOMATIC_OPENING);
316
                                } catch (NoSuchTableException e) {
317
                                        throw new RuntimeException(e);
318
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
319
                                        throw new RuntimeException(e);
320
                                }
321
                        }
322
                        return ds;
323
                } else {
324
                        return ova.getRecordset();
325
                }
326
        }
327

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

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

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

    
373
        }
374

    
375
        /*
376
         * (non-Javadoc)
377
         *
378
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
379
         */
380
        public int getShapeCount() throws DriverIOException {
381
                return ova.getShapeCount() + numAdd;
382
        }
383

    
384
        /*
385
         * (non-Javadoc)
386
         *
387
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
388
         */
389
        public Rectangle2D getFullExtent() throws DriverIOException {
390
                // TODO: meter un fullExtent dentro de esta clase que se recalcule
391
                // con los a?adidos, modificados y borrados.
392
                return ova.getFullExtent();
393
        }
394

    
395
        /*
396
         * (non-Javadoc)
397
         *
398
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
399
         */
400
        public int getShapeType() throws DriverIOException {
401
                return ova.getShapeType();
402
        }
403

    
404
        /**
405
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
406
         * en la tabla relations.
407
         *
408
         * @param g
409
         *            geometr?a a guardar.
410
         *
411
         * @return DOCUMENT ME!
412
         *
413
         * @throws DriverIOException
414
         * @throws IOException
415
         */
416
        public int doAddFeature(IRow feat) throws DriverIOException, IOException {
417
                // A?ade la geometr?a
418
                int virtualIndex = ova.getShapeCount() + numAdd;
419
                int pos = expansionFile.addRow(feat);
420
                relations.put(new Integer(virtualIndex), new Integer(pos));
421
                numAdd++;
422

    
423
                // Actualiza el ?ndice espacial
424
                IGeometry g = ((IFeature) feat).getGeometry();
425
                Rectangle2D r = g.getBounds2D();
426
                index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(),
427
                                r.getY() + r.getHeight()), new Integer(virtualIndex));
428

    
429
                return virtualIndex;
430
        }
431

    
432
        public void startEdition() throws EditionException {
433
                isEditing = true;
434
                try {
435
                        expansionFile.open();
436
                        // TODO: Si la capa dispone de un ?ndice espacial, hacer
437
                        // algo aqu? para que se use ese ?ndice espacial.
438
                        index = new Quadtree();
439
                        for (int i = 0; i < ova.getShapeCount(); i++) {
440
                                IGeometry g = ova.getShape(i);
441

    
442
                                if (g == null) {
443
                                        continue;
444
                                }
445

    
446
                                Rectangle2D r = g.getBounds2D();
447
                                Envelope e = new Envelope(r.getX(), r.getX() + r.getWidth(),
448
                                                r.getY(), r.getY() + r.getHeight());
449
                                index.insert(e, new Integer(i));
450
                        }
451
                } catch (DriverIOException e) {
452
                        throw new EditionException(e);
453
                } catch (IOException e) {
454
                        throw new EditionException(e);
455
                }
456

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

    
460
        }
461

    
462
        public void stopEdition(IWriter writer) throws EditionException {
463
                writer.preProcess();
464
                try {
465
                        for (int i=0; i < getRowCount(); i++)
466
                        {
467
                                IRowEdited rowEdited = getRow(i);
468
                                writer.process(rowEdited);
469
                        }
470
                } catch (DriverIOException e) {
471
                        e.printStackTrace();
472
                        throw new EditionException(e);
473
                } catch (IOException e) {
474
                        e.printStackTrace();
475
                        throw new EditionException(e);
476
                }
477
                writer.postProcess();
478
                isEditing = false;
479
        }
480

    
481
        public void cancelEdition() throws IOException {
482
                isEditing = false;
483
                expansionFile.close();
484

    
485
        }
486

    
487
        /* (non-Javadoc)
488
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
489
         */
490
        public IRowEdited getRow(int index)
491
                        throws DriverIOException, IOException {
492
                Integer integer = new Integer(index);
493

    
494
                //Si no est? en el fichero de expansi?n
495
                if (!relations.containsKey(integer)) {
496
                        //Si ha sido eliminada
497
                        if (delgeometries.get(index)) {
498
                                return null;
499
                        } else {
500
                                try {
501
                                        DefaultRowEdited edRow = new DefaultRowEdited(ova.getFeature(index),
502
                                                        DefaultRowEdited.STATUS_ORIGINAL);
503
                                        return edRow;
504
                                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
505
                                        e.printStackTrace();
506
                                        throw new DriverIOException(e);
507
                                }
508
                        }
509
                } else {
510
                        int num = ((Integer) relations.get(integer)).intValue();
511

    
512
                        return expansionFile.getRow(num);
513
                }
514
        }
515

    
516
        public int getRowCount() throws DriverIOException,
517
                        IOException {
518
                return ova.getShapeCount() + numAdd;
519
        }
520

    
521
        public void addRow(IRow row) throws DriverIOException, IOException {
522
                int virtualIndex = doAddFeature(row);
523

    
524
                if (complex) {
525
                        commands.add(new AddRowCommand(this, row, virtualIndex));
526
                } else {
527
                        cr.pushCommand(new AddRowCommand(this, row, virtualIndex));
528
                }
529

    
530
        }
531

    
532
        public void undo() throws DriverIOException, IOException {
533
                seleccion.clear();
534

    
535
                if (moreUndoCommands()) {
536
                        cr.undoCommand();
537
                }
538

    
539
        }
540

    
541
        public void redo() throws DriverIOException, IOException {
542
                seleccion.clear();
543

    
544
                if (moreRedoCommands()) {
545
                        cr.redoCommand();
546
                }
547

    
548
        }
549

    
550
        public boolean moreUndoCommands() {
551
                return cr.moreUndoCommands();
552
        }
553

    
554
        public boolean moreRedoCommands() {
555
                return cr.moreRedoCommands();
556
        }
557

    
558
        /* (non-Javadoc)
559
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#removeRow(int)
560
         */
561
        public void removeRow(int index)
562
                        throws IOException, DriverIOException {
563
                if (complex) {
564
                        commands.add(new RemoveRowCommand(this, index));
565
                } else {
566
                        cr.pushCommand(new RemoveRowCommand(this, index));
567
                }
568

    
569
                doRemoveFeature(index);
570

    
571
        }
572

    
573
        /* (non-Javadoc)
574
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#modifyRow(int, com.iver.cit.gvsig.fmap.core.IRow)
575
         */
576
        public void modifyRow(int index, IRow row) throws IOException,
577
                        DriverIOException {
578
                IFeature feat = (IFeature) row;
579
                int pos = doModifyFeature(index, feat);
580

    
581
                if (complex) {
582
                        commands.add(new ModifyRowCommand(this, index, pos, feat));
583
                } else {
584
                        cr.pushCommand(new ModifyRowCommand(this, index, pos, feat));
585
                }
586

    
587
        }
588

    
589
        public void compact() {
590
                expansionFile.compact(relations);
591
        }
592

    
593
        public void setImage(Image i) {
594
                selectionImage = i;
595
        }
596

    
597
        public Image getImage() {
598
                // TODO Auto-generated method stub
599
                return null;
600
        }
601

    
602
        public void startComplexRow() {
603
                complex = true;
604
                commands = new CommandCollection();
605
        }
606

    
607
        public void endComplexRow() throws IOException, DriverIOException {
608
                cr.pushCommand(commands);
609
                complex = false;
610

    
611
        }
612

    
613

    
614
        /**
615
         * Elimina una geometria. Si es una geometr?a original de la capa en
616
         * edici?n se marca como eliminada (haya sido modificada o no). Si es una
617
         * geometr?a a?adida posteriormente se invalida en el fichero de
618
         * expansi?n, para que una futura compactaci?n termine con ella.
619
         *
620
         * @param index ?ndice de la geometr?a.
621
         *
622
         * @throws DriverIOException
623
         * @throws IOException
624
         * @throws com.iver.cit.gvsig.fmap.DriverException
625
         */
626
        public void doRemoveFeature(int index)
627
                throws DriverIOException, IOException {
628
                Integer integer = new Integer(index);
629

    
630
                IFeature feat = null;
631

    
632
                //Si la geometr?a no ha sido modificada
633
                if (!relations.containsKey(integer)) {
634
                        delgeometries.set(index, true);
635
                        try {
636
                                feat = ova.getFeature(index);
637
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
638
                                e.printStackTrace();
639
                                throw new DriverIOException(e);
640
                        }
641
                } else {
642
                        int num = ((Integer) relations.get(integer)).intValue();
643
                        feat = (IFeature) expansionFile.getRow(num);
644
                        expansionFile.invalidateRow(num);
645
                }
646

    
647
                //Se actualiza el ?ndice
648
                if (feat != null) {
649
                        Rectangle2D r = feat.getGeometry().getBounds2D();
650
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(),
651
                                        r.getY(), r.getY() + r.getHeight()), new Integer(index));
652
                }
653
        }
654

    
655
        /**
656
         * Si se intenta modificar una geometr?a original de la capa en edici?n se
657
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
658
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
659
         * fichero de expansi?n (por ser nueva o original pero modificada) se
660
         * invoca el m?todo modifyGeometry y se actualiza el ?ndice de la
661
         * geometria en el fichero.
662
         *
663
         * @param index DOCUMENT ME!
664
         * @param g DOCUMENT ME!
665
         *
666
         * @return DOCUMENT ME!
667
         *
668
         * @throws IOException
669
         * @throws DriverIOException
670
         * @throws com.iver.cit.gvsig.fmap.DriverException
671
         */
672
        public int doModifyFeature(int index, IRow feat)
673
                throws IOException, DriverIOException {
674
                int pos = -1;
675
                Integer integer = new Integer(index);
676

    
677
                IFeature featAnt = null;
678

    
679
                //Si la geometr?a no ha sido modificada
680
                if (!relations.containsKey(integer)) {
681
                        int expansionIndex = expansionFile.addRow(feat);
682
                        relations.put(integer, new Integer(expansionIndex));
683

    
684
                        //Se actualiza el ?ndice espacial
685
                        try {
686
                                featAnt = ova.getFeature(index);
687

    
688
                                Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
689
                                Rectangle2D r = ((IFeature)feat).getGeometry().getBounds2D();
690
                                this.index.remove(new Envelope(rAnt.getX(),
691
                                                rAnt.getX() + rAnt.getWidth(), rAnt.getY(),
692
                                                rAnt.getY() + rAnt.getHeight()), new Integer(index));
693
                                this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(),
694
                                                r.getY(), r.getY() + r.getHeight()), new Integer(index));
695
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
696
                                e.printStackTrace();
697
                                throw new DriverIOException(e);
698
                        }
699

    
700
                } else {
701
                        //Obtenemos el ?ndice en el fichero de expansi?n
702
                        int num = ((Integer) relations.get(integer)).intValue();
703
                        pos = num;
704
                        //Obtenemos la geometr?a para actualiza el ?ndice espacialposteriormente
705
                        featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow();
706
                        /*
707
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del fichero
708
                         * de expansi?n en el que se encuentra la geometr?a modificada
709
                         */
710
                        num = expansionFile.modifyRow(num, feat);
711
                        /*
712
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el fichero
713
                         * de expansi?n.
714
                         */
715
                        relations.put(integer, new Integer(num));
716

    
717
                        //Se modifica el ?ndice espacial
718
                        Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
719
                        Rectangle2D r = ((IFeature)feat).getGeometry().getBounds2D();
720
                        this.index.remove(new Envelope(rAnt.getX(),
721
                                        rAnt.getX() + rAnt.getWidth(), rAnt.getY(),
722
                                        rAnt.getY() + rAnt.getHeight()), new Integer(index));
723
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(),
724
                                        r.getY(), r.getY() + r.getHeight()), new Integer(index));
725
                }
726

    
727
                return pos;
728
        }
729

    
730
        public VectorialAdapter getOriginalAdapter(){
731
                return ova;
732
        }
733
        public int[] getRowsIndexes(Rectangle2D r) {
734
                Envelope e = new Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(),
735
                                r.getY() + r.getHeight());
736
                List l = index.query(e);
737
                int[] indexes = new int[l.size()];
738

    
739
                for (int index = 0; index < l.size(); index++) {
740
                        Integer i = (Integer) l.get(index);
741
                        indexes[index] = i.intValue();
742
                }
743

    
744
                return indexes;
745
        }
746
        /**
747
         * @return
748
         */
749
        public FBitSet getSelection() {
750
                return seleccion;
751
        }
752
        /**
753
         * Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
754
         * geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
755
         * fuera una modificaci?n de una geometr?a que estuviese en el fichero de
756
         * expansi?n antes de ser modificada y se pone el puntero de escritura del
757
         * expansion file a justo despues de la penultima geometr?a
758
         *
759
         * @param geometryIndex ?ndice de la geometr?a que se quiere deshacer su
760
         *                   modificaci?n
761
         * @param previousExpansionFileIndex ?ndice que ten?a antes la geometr?a en
762
         *                   el expansionFile. Si vale -1 quiere decir que es una
763
         *                   modificaci?n de una geometr?a original y por tanto no hay que
764
         *                   actualizar el mapa de indices sino eliminar su entrada.
765
         *
766
         * @throws IOException
767
         * @throws DriverIOException
768
         */
769
        public void undoModifyRow(int geometryIndex,
770
                int previousExpansionFileIndex) throws IOException, DriverIOException {
771
                /*
772
                 * Si la acci?n de modificar se realiz? sobre una geometr?a original
773
                 */
774
                if (previousExpansionFileIndex == -1) {
775
                        //Se obtiene la geometr?a para actualizar el ?ndice
776
                        IGeometry g = getShape(geometryIndex);
777
                        Rectangle2D r = g.getBounds2D();
778
                        //Se elimina de las relaciones y del fichero de expansi?n
779
                        relations.remove(new Integer(geometryIndex));
780
                        expansionFile.deleteLastRow();
781

    
782
                        //Se actualizan los ?ndices
783
                        IGeometry gAnt = getShape(geometryIndex);
784
                        Rectangle2D rAnt = gAnt.getBounds2D();
785
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(),
786
                                        r.getY(), r.getY() + r.getHeight()),
787
                                new Integer(geometryIndex));
788
                        this.index.insert(new Envelope(rAnt.getX(),
789
                                        rAnt.getX() + rAnt.getWidth(), rAnt.getY(),
790
                                        rAnt.getY() + rAnt.getHeight()), new Integer(geometryIndex));
791
                } else {
792

    
793
                        //Se obtiene la geometr?a para actualizar el ?ndice
794
                        IGeometry g = null;
795
                        g = getShape(geometryIndex);
796

    
797
                        Rectangle2D r = g.getBounds2D();
798
                        this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(),
799
                                        r.getY(), r.getY() + r.getHeight()),
800
                                new Integer(geometryIndex));
801

    
802
                        //Se actualiza la relaci?n de ?ndices
803
                        Integer integer = new Integer(geometryIndex);
804
                        relations.put(new Integer(geometryIndex),
805
                                new Integer(previousExpansionFileIndex));
806

    
807
                        //Se recupera la geometr?a
808
                        expansionFile.validateRow(previousExpansionFileIndex);
809

    
810
                        //Se actualizan los ?ndices
811
                        g = getShape(geometryIndex);
812
                        r = g.getBounds2D();
813
                        this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(),
814
                                        r.getY(), r.getY() + r.getHeight()),
815
                                new Integer(geometryIndex));
816
                }
817
        }
818
        /**
819
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
820
         * en el fichero original
821
         *
822
         * @param index DOCUMENT ME!
823
         *
824
         * @throws IOException
825
         * @throws DriverIOException
826
         */
827
        public void undoRemoveRow(int index)
828
                throws IOException, DriverIOException {
829
                // Si la relaci?n
830
                if (relations.containsKey(new Integer(index))) {
831
                        expansionFile.validateRow(((Integer) relations.get(
832
                                        new Integer(index))).intValue());
833
                } else {
834
                        delgeometries.set(index, false);
835
                }
836

    
837
                IGeometry g = null;
838
                g = ((IFeature)getRow(index).getLinkedRow()).getGeometry();
839

    
840
                Rectangle2D r = g.getBounds2D();
841
                this.index.insert(new Envelope(r.getX(), r.getX() + r.getWidth(),
842
                                r.getY(), r.getY() + r.getHeight()), new Integer(index));
843
        }
844
        /**
845
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
846
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar
847
         * la relaci?n del mapa de relaciones
848
         *
849
         * @param index ?ndice de la geometr?a que se a?adi?
850
         *
851
         * @throws DriverIOException
852
         * @throws IOException
853
         */
854
        public void undoAddRow(int index)
855
                throws DriverIOException, IOException {
856
                //doRemoveGeometry(index);
857
                IGeometry g = ((IFeature)getRow(index).getLinkedRow()).getGeometry();
858
                Rectangle2D r = g.getBounds2D();
859
                this.index.remove(new Envelope(r.getX(), r.getX() + r.getWidth(),
860
                                r.getY(), r.getY() + r.getHeight()), new Integer(index));
861

    
862
                expansionFile.deleteLastRow();
863
                relations.remove(new Integer(index));
864
                numAdd--;
865
        }
866

    
867
}