Revision 10627 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/edition/AnnotationEditableAdapter.java

View differences:

AnnotationEditableAdapter.java
4 4
import java.awt.Font;
5 5
import java.awt.geom.Point2D;
6 6
import java.awt.geom.Rectangle2D;
7
import java.io.IOException;
8 7
import java.util.ArrayList;
9 8
import java.util.List;
10 9

  
10
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
11 11
import com.hardcode.gdbms.engine.values.DoubleValue;
12 12
import com.hardcode.gdbms.engine.values.IntValue;
13 13
import com.hardcode.gdbms.engine.values.NumericValue;
14 14
import com.hardcode.gdbms.engine.values.StringValue;
15 15
import com.hardcode.gdbms.engine.values.Value;
16 16
import com.hardcode.gdbms.engine.values.ValueFactory;
17
import com.iver.cit.gvsig.fmap.DriverException;
17
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException;
18
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
19
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
20
import com.iver.cit.gvsig.exceptions.expansionfile.OpenExpansionFileException;
21
import com.iver.cit.gvsig.exceptions.layers.StartEditionLayerException;
22
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
18 23
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
19
import com.iver.cit.gvsig.fmap.core.FShape;
20 24
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
21 25
import com.iver.cit.gvsig.fmap.core.IFeature;
22 26
import com.iver.cit.gvsig.fmap.core.IGeometry;
......
27 31
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
28 32
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
29 33
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
30
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
31 34
import com.iver.cit.gvsig.fmap.layers.FBitSet;
32 35
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
33 36
import com.iver.cit.gvsig.fmap.layers.MappingAnnotation;
......
151 154

  
152 155

  
153 156

  
154
	public int doAddRow(IRow feat, int sourceType) throws DriverIOException, IOException {
157
	public int doAddRow(IRow feat, int sourceType) throws ReadDriverException, ExpansionFileWriteException {
155 158
		boolean cancel = fireBeforeRowAdded(sourceType);
156 159
		if (cancel)
157 160
			return -1;
......
170 173
		///lyrAnnotation.deleteSpatialIndex();
171 174

  
172 175
		int calculatedIndex = -1;
173
		calculatedIndex = (int) getRowCount()+delRows.cardinality() ;
176
		calculatedIndex = getRowCount()+delRows.cardinality() ;
174 177

  
175 178
		int pos = expansionFile.addRow(feat, IRowEdited.STATUS_ADDED, actualIndexFields);
176 179
		labelExpansion.addLabel(label);
......
184 187
        return calculatedIndex;
185 188
	}
186 189

  
187
	public int doModifyRow(int calculatedIndex, IRow feat,int sourceType) throws IOException, DriverIOException {
190
	public int doModifyRow(int calculatedIndex, IRow feat,int sourceType) throws ExpansionFileWriteException {
188 191
		boolean cancel = fireBeforeModifyRow(feat,calculatedIndex, sourceType);
189 192
		if (cancel)
190 193
			return -1;
......
215 218
			g.getPathIterator(null).currentSegment(d);
216 219
			Point2D p=new Point2D.Double(d[0],d[1]);
217 220
			label.setBoundBox(new Rectangle2D.Double(p.getX(),p.getY(),rLabelAnt.getWidth(),rLabelAnt.getHeight()));
218
			Rectangle2D rLabel=(Rectangle2D)label.getBoundBox();
221
			Rectangle2D rLabel=label.getBoundBox();
219 222
			label.setOrig(p);
220 223
			index.remove(new Envelope(rLabelAnt.getX(), rLabelAnt.getX()
221 224
					+ rLabelAnt.getWidth(), rLabelAnt.getY(), rLabelAnt.getY()
......
257 260
			Point2D p=new Point2D.Double(r.getX(),r.getY());
258 261

  
259 262
			label.setBoundBox(new Rectangle2D.Double(p.getX(),p.getY()+rLabelAnt.getHeight(),rLabelAnt.getWidth(),rLabelAnt.getHeight()));
260
			Rectangle2D rLabel=(Rectangle2D)label.getBoundBox();
263
			Rectangle2D rLabel=label.getBoundBox();
261 264
			label.setOrig(p);
262 265
			index.remove(new Envelope(rLabelAnt.getX(), rLabelAnt.getX()
263 266
					+ rLabelAnt.getWidth(), rLabelAnt.getY(), rLabelAnt.getY()
......
271 274
		return posAnteriorInExpansionFile;
272 275
	}
273 276

  
274
	public IRow doRemoveRow(int index,int sourceType) throws DriverIOException, IOException {
277
	public IRow doRemoveRow(int index,int sourceType) throws ReadDriverException, ExpansionFileReadException {
275 278
		boolean cancel = fireBeforeRemoveRow(index, sourceType);
276 279
		if (cancel)
277 280
			return null;
......
284 287
        delRows.set(index, true);
285 288
        //Si la geometr?a no ha sido modificada
286 289
        if (!relations.containsKey(integer)) {
287

  
288
            try {
289
				feat = (DefaultFeature) (ova.getFeature(index));
290
			} catch (DriverException e) {
291
				throw new DriverIOException(e);
292
			}
290
           	feat = (DefaultFeature) (ova.getFeature(index));
293 291
       } else {
294 292
			int num = ((Integer) relations.get(integer)).intValue();
295 293
			feat = (IFeature) expansionFile.getRow(num).getLinkedRow();
......
306 304
        return feat;
307 305
	}
308 306

  
309
	public void undoAddRow(int calculatedIndex, int sourceType) throws DriverIOException, IOException {
307
	public void undoAddRow(int calculatedIndex, int sourceType) throws EditionCommandException {
310 308
		boolean cancel = fireBeforeRemoveRow(calculatedIndex, sourceType);
311 309
		if (cancel)
312 310
			return;
......
316 314
        expansionFile.deleteLastRow();
317 315
		relations.remove(new Integer(calculatedIndex));
318 316
		numAdd--;
319
        setSelection(new FBitSet());
317
        try {
318
			setSelection(new FBitSet());
319
		} catch (ReadDriverException e) {
320
			throw new EditionCommandException(writer.getName(),e);
321
		}
320 322
		labelExpansion.deleteLastLabel();
321 323
		fireAfterRemoveRow(calculatedIndex, sourceType);
322 324
	}
323 325

  
324
	public void undoModifyRow(int calculatedIndex, int previousExpansionFileIndex, int sourceType) throws IOException, DriverIOException {
325

  
326
	public void undoModifyRow(int calculatedIndex, int previousExpansionFileIndex, int sourceType) throws EditionCommandException {
327
		try {
326 328
		if (previousExpansionFileIndex == -1) {
327 329
	            //Se obtiene la geometr?a para actualizar el ?ndice
328 330
	        	int inverse = getInversedIndex(calculatedIndex);
329
	        	DefaultFeature df=(DefaultFeature)getRow(inverse).getLinkedRow();
330

  
331

  
332
	        	IGeometry g = df.getGeometry();
331
	        	DefaultFeature df;
332
				df = (DefaultFeature)getRow(inverse).getLinkedRow();
333
				IGeometry g = df.getGeometry();
333 334
	            Rectangle2D r = g.getBounds2D();
334 335
	            //Se elimina de las relaciones y del fichero de expansi?n
335 336
	            relations.remove(new Integer(calculatedIndex));
336 337
	            expansionFile.deleteLastRow();
337 338
	            labelExpansion.deleteLastLabel();
338 339
	            //Se actualizan los ?ndices
339
	            DefaultFeature dfAnt=(DefaultFeature)getRow(inverse).getLinkedRow();
340
	            IGeometry gAnt = dfAnt.getGeometry();
340
	            DefaultFeature dfAnt;
341
				dfAnt = (DefaultFeature)getRow(inverse).getLinkedRow();
342
				IGeometry gAnt = dfAnt.getGeometry();
341 343
	            boolean cancel = fireBeforeModifyRow(dfAnt,calculatedIndex, sourceType);
342 344
	    		if (cancel)
343 345
	    			return;
......
382 384
	    			label.setRotation(((NumericValue)value).doubleValue());
383 385
	    		}
384 386
	        }
387
		}  catch (ReadDriverException e) {
388
			throw new EditionCommandException(writer.getName(),e);
389
		} catch (ExpansionFileReadException e) {
390
			throw new EditionCommandException(writer.getName(),e);
391
		}
385 392
		fireAfterModifyRow(calculatedIndex, sourceType);
386 393
	}
387 394

  
388
	public void undoRemoveRow(int index, int sourceType) throws IOException, DriverIOException {
389
		boolean cancel = fireBeforeRowAdded(sourceType);
395
	public void undoRemoveRow(int index, int sourceType) throws EditionCommandException {
396
		boolean cancel;
397
		try {
398
			cancel = fireBeforeRowAdded(sourceType);
399
		} catch (ReadDriverException e) {
400
			throw new EditionCommandException(writer.getName(),e);
401
		}
390 402
		if (cancel)
391 403
			return;
392 404
		delRows.set(index, false);
......
404 416
		this.isInPixels=lyrAnnotation.isInPixels();
405 417
		this.lyrAnnotation=lyrAnnotation;
406 418
	}
407
	public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) throws DriverException {
419
	public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) throws ReadDriverException, ExpansionFileReadException {
408 420
		// En esta clase suponemos random access.
409 421
		// Luego tendremos otra clase que sea VectorialEditableDBAdapter
410 422
		// que reescribir? este m?todo.
411 423
        Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
412 424
        List l = index.query(e);
413 425
        IRowEdited[] feats = new IRowEdited[l.size()];
414
        try {
415
	        for (int index = 0; index < l.size(); index++) {
416
	            Integer i = (Integer) l.get(index);
417
	            int inverse = getInversedIndex(i.intValue());
418
				feats[index] = (IRowEdited) getRow(inverse);
419
	        }
420
		} catch (DriverIOException e1) {
421
			throw new DriverException(e1);
422
		} catch (IOException e1) {
423
			throw new DriverException(e1);
424
		}
425

  
426
        for (int index = 0; index < l.size(); index++) {
427
	        Integer i = (Integer) l.get(index);
428
	        int inverse = getInversedIndex(i.intValue());
429
	        feats[index] = getRow(inverse);
430
	    }
426 431
		return feats;
427 432
	}
428 433
	 /* (non-Javadoc)
429 434
     * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
430 435
     */
431
    public IRowEdited getRow(int index) throws DriverIOException, IOException {
436
    public IRowEdited getRow(int index) throws ReadDriverException, ExpansionFileReadException {
432 437
    	int calculatedIndex=getCalculatedIndex(index);
433 438
    	Integer integer = new Integer(calculatedIndex);
434 439
        //Si no est? en el fichero de expansi?n
435 440
    	DefaultRowEdited edRow=null;
436 441

  
437 442
    	if (!relations.containsKey(integer)) {
438
			try {
439
				IFeature f=ova.getFeature(calculatedIndex);
440
				String s=f.getID();
441
				FLabel label=getLabel(index,false);
442
				if (label==null)
443
					return null;
444
				IGeometry geom=getGeometry(label.getBoundBox());
445
				f=new DefaultFeature(geom,f.getAttributes(),s);
446
				//f.setID(s);
447
				edRow = new DefaultRowEdited(f,
448
				        DefaultRowEdited.STATUS_ORIGINAL, index);
449
			} catch (DriverException e) {
450
				throw new DriverIOException(e);
451
			}
452

  
453
            return edRow;
454
        } else {
455
            int num = ((Integer) relations.get(integer)).intValue();
456
            IRowEdited aux = expansionFile.getRow(num);
457
            IFeature f=(IFeature)aux.getLinkedRow().cloneRow();
458
            IGeometry geom=getGeometry(labelExpansion.getLabel(num).getBoundBox());
459
            String s=f.getID();
460
            f=new DefaultFeature(geom,f.getAttributes(),s);
461
            //f.setID(s);
462
            edRow = new DefaultRowEdited(f, aux.getStatus(), index);
463
            return edRow;
443
			IFeature f=ova.getFeature(calculatedIndex);
444
			String s=f.getID();
445
			FLabel label=getLabel(index,false);
446
			if (label==null)
447
				return null;
448
			IGeometry geom=getGeometry(label.getBoundBox());
449
			f=new DefaultFeature(geom,f.getAttributes(),s);
450
			//f.setID(s);
451
			edRow = new DefaultRowEdited(f,
452
			        IRowEdited.STATUS_ORIGINAL, index);
453
			return edRow;
464 454
        }
455
		int num = ((Integer) relations.get(integer)).intValue();
456
		IRowEdited aux = expansionFile.getRow(num);
457
		IFeature f=(IFeature)aux.getLinkedRow().cloneRow();
458
		IGeometry geom=getGeometry(labelExpansion.getLabel(num).getBoundBox());
459
		String s=f.getID();
460
		f=new DefaultFeature(geom,f.getAttributes(),s);
461
		//f.setID(s);
462
		edRow = new DefaultRowEdited(f, aux.getStatus(), index);
463
		return edRow;
465 464
    }
466 465
    private IGeometry getGeometry(Rectangle2D r){
467 466
    	GeneralPathX resul = new GeneralPathX();
......
481 480
		resul.closePath();
482 481
		return ShapeFactory.createPolygon2D(resul);
483 482
    }
484
    public void startEdition(int sourceType) throws EditionException {
483
    public void startEdition(int sourceType) throws StartWriterVisitorException {
485 484
    	isEditing = true;
486 485
		try {
487 486
			expansionFile.open();
488
				index = new Quadtree();
489

  
490
				for (int i = 0; i < labels.size(); i++) {
491
					Rectangle2D r=((FLabel)labels.get(i)).getBoundBox();
492
					Envelope e = new Envelope(r.getX(),
493
							r.getX() + r.getWidth(), r.getY(), r.getY()
494
									+ r.getHeight());
495
					index.insert(e, new Integer(i));
496
					if (fullExtent == null) {
497
						fullExtent = r;
498
					} else {
499
						fullExtent.add(r);
500
					}
501
				}
502
		} catch (IOException e) {
503
			throw new EditionException(e);
487
		} catch (OpenExpansionFileException e1) {
488
			throw new StartWriterVisitorException(lyrAnnotation.getName(),e1);
504 489
		}
490
		index = new Quadtree();
491
		for (int i = 0; i < labels.size(); i++) {
492
			Rectangle2D r=((FLabel)labels.get(i)).getBoundBox();
493
			Envelope e = new Envelope(r.getX(),
494
					r.getX() + r.getWidth(), r.getY(), r.getY()
495
							+ r.getHeight());
496
			index.insert(e, new Integer(i));
497
			if (fullExtent == null) {
498
				fullExtent = r;
499
			} else {
500
				fullExtent.add(r);
501
			}
502
		}
505 503

  
506 504
		System.err.println("Se han metido en el ?ndice "
507 505
				+ index.queryAll().size() + " labels");
508 506
	}
509 507

  
510
    public Rectangle2D getShapeBounds(int index) throws IOException {
508
    public Rectangle2D getShapeBounds(int index) throws ReadDriverException, ExpansionFileReadException {
511 509
		//Solo se utiliza cuando el driver es BoundedShapes
512 510
//		 Si no est? en el fichero de expansi?n
513
		Integer integer = new Integer((int) index);
511
		Integer integer = new Integer(index);
514 512
		if (!relations.containsKey(integer)) {
515 513
			if (ova.getDriver() instanceof BoundedShapes){
516 514
				BoundedShapes bs = (BoundedShapes) ova.getDriver();
517 515
				return bs.getShapeBounds(index);
518
			}else{
519
				return ova.getDriver().getShape(index).getBounds2D();
520 516
			}
517
			return ova.getDriver().getShape(index).getBounds2D();
521 518

  
522
		} else {
523
			int num = ((Integer) relations.get(integer)).intValue();
524
			DefaultRowEdited feat;
525
			feat = (DefaultRowEdited) expansionFile.getRow(num);
526
			if (feat.getStatus() == IRowEdited.STATUS_DELETED)
527
				return null;
528
			IGeometry geom = ((IFeature)feat.getLinkedRow()).getGeometry();
529
			return geom.getBounds2D();
530 519
		}
520
		int num = ((Integer) relations.get(integer)).intValue();
521
		DefaultRowEdited feat;
522
		feat = (DefaultRowEdited) expansionFile.getRow(num);
523
		if (feat.getStatus() == IRowEdited.STATUS_DELETED)
524
			return null;
525
		IGeometry geom = ((IFeature)feat.getLinkedRow()).getGeometry();
526
		return geom.getBounds2D();
531 527

  
532 528
	}
533 529
    public FLabel getLabel(int index,boolean calculated) {
......
545 541
    		}
546 542
    		label=(FLabel)labels.get(calculatedIndex);
547 543
		    return label;
548
        } else {
549
            int num = ((Integer) relations.get(integer)).intValue();
550
            label=labelExpansion.getLabel(num);
551
            return (FLabel)label;
552 544
        }
545
		int num = ((Integer) relations.get(integer)).intValue();
546
		label=labelExpansion.getLabel(num);
547
		return label;
553 548
    }
554 549

  
555 550

  

Also available in: Unified diff