Revision 3996 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/edition/EditableAdapter.java

View differences:

EditableAdapter.java
1 1
package com.iver.cit.gvsig.fmap.edition;
2 2

  
3
import java.awt.Image;
4
import java.io.IOException;
5
import java.util.HashMap;
3
import com.hardcode.driverManager.DriverLoadException;
6 4

  
7
import com.hardcode.driverManager.DriverLoadException;
8 5
import com.hardcode.gdbms.engine.data.DataSource;
9 6
import com.hardcode.gdbms.engine.data.DataSourceFactory;
10 7
import com.hardcode.gdbms.engine.data.NoSuchTableException;
......
12 9
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
13 10
import com.hardcode.gdbms.engine.data.edition.DataWare;
14 11
import com.hardcode.gdbms.engine.values.Value;
12

  
15 13
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
16 14
import com.iver.cit.gvsig.fmap.core.IFeature;
17 15
import com.iver.cit.gvsig.fmap.core.IRow;
......
26 24
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
27 25
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
28 26

  
27
import java.awt.Image;
29 28

  
29
import java.io.IOException;
30

  
31
import java.util.HashMap;
32

  
33

  
30 34
/**
31 35
 * DOCUMENT ME!
32 36
 *
......
37 41
    private SelectableDataSource ds = null;
38 42
    protected FBitSet delRows = new FBitSet();
39 43
    private CommandRecord cr;
44

  
40 45
    //private FBitSet seleccion;
41 46
    private Image selectionImage;
42 47

  
43

  
44 48
    /**
45 49
     * Flag que indica que hay que tomar las siguientes operaciones como una
46 50
     * operaci?n at?mica
......
69 73
    public EditableAdapter() {
70 74
        expansionFile = new MemoryExpansionFile();
71 75
        cr = new MemoryCommandRecord();
76

  
72 77
        //this.seleccion = new FBitSet();
73 78
    }
74 79

  
......
88 93
     */
89 94
    public void startEdition() throws EditionException {
90 95
        isEditing = true;
91

  
92 96
    }
93 97

  
94 98
    /**
......
104 108
        try {
105 109
            for (int i = 0; i < getRowCount(); i++) {
106 110
                IRowEdited rowEdited = getRow(i);
107
        		if (rowEdited != null)
108
        			writer.process(rowEdited);
111

  
112
                if (rowEdited != null) {
113
                    writer.process(rowEdited);
114
                }
109 115
            }
110 116
        } catch (DriverIOException e) {
111 117
            e.printStackTrace();
......
133 139
     * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
134 140
     */
135 141
    public IRowEdited getRow(int index) throws DriverIOException, IOException {
136
        Integer integer = new Integer(index);
142
    	int calculatedIndex=getCalculatedIndex(index);
143
    	Integer integer = new Integer(calculatedIndex);
144

  
137 145
        //Si no est? en el fichero de expansi?n
138 146
        if (!relations.containsKey(integer)) {
139 147
            //Si ha sido eliminada
140
            if (delRows.get(index)) {
148
           /* if (delRows.get(index)) {
141 149
                return null;
142
            } else {
143
                DefaultRowEdited edRow=null;
144
				try {
145
					edRow = new DefaultRowEdited(new DefaultFeature(null,ods.getRow(index)),
146
					        DefaultRowEdited.STATUS_ORIGINAL);
147
				} catch (DriverException e) {
148
					// TODO Auto-generated catch block
149
					e.printStackTrace();
150
				}
150
            } else {*/
151
                DefaultRowEdited edRow = null;
151 152

  
153
                try {
154
                    edRow = new DefaultRowEdited(new DefaultFeature(null,
155
                                ods.getRow(calculatedIndex)),
156
                            DefaultRowEdited.STATUS_ORIGINAL);
157
                } catch (DriverException e) {
158
                    // TODO Auto-generated catch block
159
                    e.printStackTrace();
160
                }
161

  
152 162
                return edRow;
153
            }
163
            // }
154 164
        } else {
155 165
            int num = ((Integer) relations.get(integer)).intValue();
156 166

  
......
168 178
     */
169 179
    public int getRowCount() throws DriverIOException, IOException {
170 180
        try {
171
			return (int) (ods.getRowCount() + numAdd);
172
		} catch (DriverException e) {
173
			// TODO Auto-generated catch block
174
			e.printStackTrace();
175
		}
176
		return 0;
181
            return (int) (ods.getRowCount() + numAdd) - delRows.cardinality();// - expansionFile.getInvalidRows().cardinality();
182
        } catch (DriverException e) {
183
            // TODO Auto-generated catch block
184
            e.printStackTrace();
185
        }
186

  
187
        return 0;
177 188
    }
178 189

  
179 190
    /**
......
202 213
     */
203 214
    public void undo() throws DriverIOException, IOException {
204 215
        //seleccion.clear();
205

  
206 216
        if (moreUndoCommands()) {
207 217
            cr.undoCommand();
208 218
        }
......
216 226
     */
217 227
    public void redo() throws DriverIOException, IOException {
218 228
        //seleccion.clear();
219

  
220 229
        if (moreRedoCommands()) {
221 230
            cr.redoCommand();
222 231
        }
......
244 253
     * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#removeRow(int)
245 254
     */
246 255
    public void removeRow(int index) throws IOException, DriverIOException {
247
        if (complex) {
248
            commands.add(new RemoveRowCommand(this, index));
256
    	int calculatedIndex = getCalculatedIndex(index);
257
    	if (complex) {
258
            commands.add(new RemoveRowCommand(this, calculatedIndex));
249 259
        } else {
250
            cr.pushCommand(new RemoveRowCommand(this, index));
260
            cr.pushCommand(new RemoveRowCommand(this, calculatedIndex));
251 261
        }
252 262

  
253
        doRemoveRow(index);
263
        doRemoveRow(calculatedIndex);
254 264
    }
255 265

  
256 266
    /* (non-Javadoc)
......
258 268
     */
259 269
    public void modifyRow(int index, IRow row)
260 270
        throws IOException, DriverIOException {
271
    	int calculatedIndex = getCalculatedIndex(index);
261 272
        IFeature feat = (IFeature) row;
262
        int pos = doModifyRow(index, feat);
273
        int pos = doModifyRow(calculatedIndex, feat);
263 274

  
264 275
        if (complex) {
265
            commands.add(new ModifyRowCommand(this, index, pos, feat));
276
            commands.add(new ModifyRowCommand(this, calculatedIndex, pos, feat));
266 277
        } else {
267
            cr.pushCommand(new ModifyRowCommand(this, index, pos, feat));
278
            cr.pushCommand(new ModifyRowCommand(this, calculatedIndex, pos, feat));
268 279
        }
269 280
    }
270 281

  
271

  
272

  
273 282
    /**
274 283
     * DOCUMENT ME!
275 284
     */
......
342 351
            //Se actualiza la relaci?n de ?ndices
343 352
            relations.put(new Integer(geometryIndex),
344 353
                new Integer(previousExpansionFileIndex));
345
            expansionFile.validateRow(previousExpansionFileIndex);
354
            //expansionFile.validateRow(previousExpansionFileIndex);
346 355
        }
347 356
    }
348 357

  
......
358 367
     * @throws IOException
359 368
     */
360 369
    public void doRemoveRow(int index) throws DriverIOException, IOException {
361
        Integer integer = new Integer(index);
370
//        Integer integer = new Integer(index);
371
//Llega un calculatedIndex
362 372
        //Si la geometr?a no ha sido modificada
363
        if (!relations.containsKey(integer)) {
373
        //if (!relations.containsKey(integer)) {
364 374
            delRows.set(index, true);
365
        } else {
366
            int num = ((Integer) relations.get(integer)).intValue();
367
            expansionFile.invalidateRow(num);
368
        }
375
            System.err.println("Elimina una Row en la posici?n: " + index);
376
//        } else {
377
//            int num = ((Integer) relations.get(integer)).intValue();
378
//            expansionFile.invalidateRow(num);
379
//        }
369 380
    }
370 381

  
371 382
    /**
......
387 398
    public int doModifyRow(int index, IRow feat)
388 399
        throws IOException, DriverIOException {
389 400
        int pos = -1;
401
        //int calculatedIndex = getCalculatedIndex(index);
390 402
        Integer integer = new Integer(index);
403
        System.err.println("Modifica una Row en la posici?n: " + index);
391 404
        //Si la geometr?a no ha sido modificada
392 405
        if (!relations.containsKey(integer)) {
393 406
            int expansionIndex = expansionFile.addRow(feat);
......
396 409
            //Obtenemos el ?ndice en el fichero de expansi?n
397 410
            int num = ((Integer) relations.get(integer)).intValue();
398 411
            pos = num;
412

  
399 413
            /*
400 414
             * Se modifica la geometr?a y nos guardamos el ?ndice dentro del fichero
401 415
             * de expansi?n en el que se encuentra la geometr?a modificada
......
407 421
             * de expansi?n.
408 422
             */
409 423
            relations.put(integer, new Integer(num));
410

  
411 424
        }
412 425

  
413 426
        return pos;
......
426 439
     */
427 440
    public int doAddRow(IRow feat) throws DriverIOException, IOException {
428 441
        // A?ade la geometr?a
429
        int virtualIndex=0;
430
		try {
431
			virtualIndex = (int) ods.getRowCount() + numAdd;
432
		} catch (DriverException e) {
433
			// TODO Auto-generated catch block
434
			e.printStackTrace();
435
		}
442
        int virtualIndex = 0;
443

  
444
        try {
445
            virtualIndex = (int) ods.getRowCount() + numAdd;
446
        } catch (DriverException e) {
447
            // TODO Auto-generated catch block
448
            e.printStackTrace();
449
        }
450

  
436 451
        int pos = expansionFile.addRow(feat);
437 452
        relations.put(new Integer(virtualIndex), new Integer(pos));
438 453
        numAdd++;
439

  
454
        System.err.println("A?ade una Row en la posici?n: " + virtualIndex);
440 455
        return virtualIndex;
441 456
    }
442 457

  
......
451 466
     */
452 467
    public void undoRemoveRow(int index) throws IOException, DriverIOException {
453 468
        // Si la relaci?n
454
        if (relations.containsKey(new Integer(index))) {
469
       /* if (relations.containsKey(new Integer(index))) {
455 470
            expansionFile.validateRow(((Integer) relations.get(
456 471
                    new Integer(index))).intValue());
457
        } else {
472
        } else {*/
458 473
            delRows.set(index, false);
459
        }
474
//        }
460 475
    }
461 476

  
462 477
    /**
......
487 502

  
488 503
                try {
489 504
                    ds = new SelectableDataSource(LayerFactory.getDataSourceFactory()
490
                                     .createRandomDataSource(name,
491
                            DataSourceFactory.AUTOMATIC_OPENING));
505
                                                              .createRandomDataSource(name,
506
                                DataSourceFactory.AUTOMATIC_OPENING));
492 507
                } catch (NoSuchTableException e) {
493 508
                    throw new RuntimeException(e);
494 509
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
......
501 516
            return ods;
502 517
        }
503 518
    }
504
    public void setRecordSet(SelectableDataSource sds){
505
    	this.ods=sds;
519

  
520
    /**
521
     * DOCUMENT ME!
522
     *
523
     * @param sds DOCUMENT ME!
524
     */
525
    public void setRecordSet(SelectableDataSource sds) {
526
        this.ods = sds;
506 527
    }
507 528

  
508 529
    /**
......
517 538
    /**
518 539
     * DOCUMENT ME!
519 540
     *
541
     * @return DOCUMENT ME!
542
     */
543
    public boolean isEditing() {
544
        return isEditing;
545
    }
546
    protected int getInversedIndex(long rowIndex) {
547

  
548
    	int intervalNotDeleted=0;
549
        int antDeleted=-1;
550
        int calculatedIndex;
551
        int idPedido = (int) rowIndex;
552
        int numNotDeleted=0;
553
        int numBorradosAnt = 0;
554

  
555
    	for (int i = delRows.nextSetBit(0); i>=0; i = delRows.nextSetBit(i+1)) {
556
    		intervalNotDeleted = i-antDeleted-1;
557
    		numNotDeleted += intervalNotDeleted;
558
    		if (i > idPedido){
559
    			numNotDeleted = numNotDeleted + (i - idPedido);
560
    			break;
561
    		}
562
    		numBorradosAnt++;
563
    		antDeleted = i;
564
        }
565
    	// if (numNotDeleted < idPedido){
566
			numNotDeleted =  idPedido-numBorradosAnt;
567
		/* }
568
    	 if (numBorradosAnt==0)
569
    		 numNotDeleted=idPedido; */
570
    	System.out.println("Piden Viejo : "+ rowIndex + " y devuelvo como nuevo " + (numNotDeleted));
571
        return numNotDeleted;
572
    }
573

  
574
    /**
575
     * DOCUMENT ME!
576
     *
577
     * @param rowIndex DOCUMENT ME!
578
     *
579
     * @return DOCUMENT ME!
580
     */
581
    protected int getCalculatedIndex(long rowIndex) {
582
        int numNotDeleted=0;
583
    	int intervalNotDeleted=0;
584
        int antDeleted=-1;
585
        int calculatedIndex;
586
        int idPedido = (int) rowIndex;
587
        int numBorradosAnt = 0;
588

  
589
    	for (int i = delRows.nextSetBit(0); i>=0; i = delRows.nextSetBit(i+1)) {
590
    		intervalNotDeleted = i-antDeleted -1 ;
591
    		numNotDeleted += intervalNotDeleted;
592
    		if (numNotDeleted > idPedido)
593
    		{
594
    			break;
595
    		}
596
    		numBorradosAnt++;
597
    		antDeleted = i;
598
        }
599
    	 calculatedIndex = numBorradosAnt + idPedido;
600
/*
601

  
602

  
603

  
604
    	//int deleted = 0;
605
        int calculated=(int)rowIndex;
606
        int numSaltos=0;
607

  
608
        // int delModified = -1;
609

  
610
        //if (delRows.cardinality() > 0) {
611
        	//for (int j = bitset.nextSetBit(0);j >= 0;j = bitset.nextSetBit(j + 1))
612
            for (int i = delRows.nextSetBit(0); i>=0; i = delRows.nextSetBit(i+1)) {
613
                if (i<=calculated){
614
                	numSaltos++;
615
                	calculated++;
616
                }
617
            }
618
        //}
619

  
620
        /*if (expansionFile.getInvalidRows().cardinality() > 0) {
621
            for (int i = 0; i <= rowIndex;
622
                    i = expansionFile.getInvalidRows().nextSetBit(i)) {
623
                delModified++;
624
            }
625
        }
626
*/
627
    	System.out.println("Piden Registro : "+ rowIndex + " y devuelvo el " + (calculatedIndex));
628
        return calculatedIndex;
629
    }
630

  
631
    /**
632
     * DOCUMENT ME!
633
     *
520 634
     * @author Vicente Caballero Navarro
521 635
     */
522 636
    private class myObjectDriver implements ObjectDriver {
......
567 681
        public Value getFieldValue(long rowIndex, int fieldId)
568 682
            throws DriverException {
569 683
            // Si no est? en el fichero de expansi?n
570
            Integer integer = new Integer((int) rowIndex);
684
            Integer integer = new Integer(getCalculatedIndex(rowIndex));
571 685

  
572 686
            try {
573 687
                if (!relations.containsKey(integer)) {
574
                    // Si ha sido eliminada
575
                    if (delRows.get(integer.intValue())) {
576
                        return null;
577
                    } else {
578
                        return ods.getFieldValue(rowIndex,
579
                            fieldId);
580
                    }
688
                    return ods.getFieldValue(rowIndex, fieldId);
581 689
                } else {
582 690
                    int num = ((Integer) relations.get(integer)).intValue();
583
                    IFeature feat = (IFeature) expansionFile.getRow(num)
584
                                                            .getLinkedRow();
691
                    DefaultRowEdited feat = (DefaultRowEdited) expansionFile.getRow(num);
585 692

  
693
                    if (feat == null) {
694
                        return null;
695
                    }
696

  
586 697
                    return feat.getAttribute(fieldId);
587 698
                }
588 699
            } catch (DriverException e) {
......
592 703
                e.printStackTrace();
593 704
                throw new DriverException(e);
594 705
            }
706

  
707
            /**
708
             * try { if (!relations.containsKey(integer)) { // Si ha sido
709
             * eliminada if (delRows.get(integer.intValue())) { return null; }
710
             * else { return ods.getFieldValue(rowIndex, fieldId); }} else {
711
             * int num = ((Integer) relations.get(integer)).intValue();
712
             * DefaultRowEdited feat = (DefaultRowEdited)
713
             * expansionFile.getRow(num); if (feat==null)return null; return
714
             * feat.getAttribute(fieldId); }} catch (DriverException e) {
715
             * e.printStackTrace(); throw new DriverException(e); } catch
716
             * (IOException e) { e.printStackTrace(); throw new
717
             * DriverException(e); }
718
             */
595 719
        }
596 720

  
597 721
        /*
......
620 744
         */
621 745
        public long getRowCount() {
622 746
            try {
623
				return ods.getRowCount() + numAdd;
624
			} catch (DriverException e) {
625
				// TODO Auto-generated catch block
626
				e.printStackTrace();
627
			}
628
			return 0;
747
                return ods.getRowCount() + numAdd;
748
            } catch (DriverException e) {
749
                // TODO Auto-generated catch block
750
                e.printStackTrace();
751
            }
752

  
753
            return 0;
629 754
        }
630 755

  
631 756
        /*
......
634 759
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
635 760
         */
636 761
        public int getFieldType(int i) throws DriverException {
637
                return ods.getFieldType(i);
762
            return ods.getFieldType(i);
638 763
        }
639 764
    }
640 765
}

Also available in: Unified diff