Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / EditVertexCADTool.java @ 5878

History | View | Annotate | Download (25 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.cad.tools;
42

    
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.AffineTransform;
47
import java.awt.geom.PathIterator;
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.io.IOException;
51
import java.util.ArrayList;
52

    
53
import com.iver.andami.PluginServices;
54
import com.iver.cit.gvsig.CADExtension;
55
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
57
import com.iver.cit.gvsig.fmap.core.FPoint2D;
58
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
59
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
60
import com.iver.cit.gvsig.fmap.core.FShape;
61
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
62
import com.iver.cit.gvsig.fmap.core.Handler;
63
import com.iver.cit.gvsig.fmap.core.IFeature;
64
import com.iver.cit.gvsig.fmap.core.IGeometry;
65
import com.iver.cit.gvsig.fmap.core.IRow;
66
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
67
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
68
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
69
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
70
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
71
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
72
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
73
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
74
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
75
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext;
76
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext.EditVertexCADToolState;
77
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
78

    
79

    
80
/**
81
 * DOCUMENT ME!
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85
public class EditVertexCADTool extends DefaultCADTool {
86
    private EditVertexCADToolContext _fsm;
87
    private int numSelect=0;
88
        private int numHandlers;
89
        private boolean addVertex=false;
90
    /**
91
     * Crea un nuevo PolylineCADTool.
92
     */
93
    public EditVertexCADTool() {
94
    }
95

    
96
    /**
97
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
98
     * carga previa a la utilizaci?n de la herramienta.
99
     */
100
    public void init() {
101
        _fsm = new EditVertexCADToolContext(this);
102
    }
103

    
104
    /* (non-Javadoc)
105
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
106
     */
107
    public void transition(double x, double y, InputEvent event) {
108
        addPoint(x, y, event);
109
    }
110

    
111
    /* (non-Javadoc)
112
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
113
     */
114
    public void transition(double d) {
115
            _fsm.addValue(d);
116
    }
117

    
118
    /* (non-Javadoc)
119
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
120
     */
121
    public void transition(String s) throws CommandException {
122
            if (!super.changeCommand(s)){
123
                    _fsm.addOption(s);
124
            }
125
    }
126

    
127
    /**
128
     * DOCUMENT ME!
129
     */
130
    public void selection() {
131
            ArrayList selectedRow=getSelectedRows();
132
        if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
133
            CADExtension.setCADTool("_selection",false);
134
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
135
                "_editvertex");
136
        }
137
    }
138

    
139
    /**
140
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
141
     * editableFeatureSource que ya estar? creado.
142
     *
143
     * @param x par?metro x del punto que se pase en esta transici?n.
144
     * @param y par?metro y del punto que se pase en esta transici?n.
145
     */
146
    public void addPoint(double x, double y,InputEvent event) {
147
            selectHandler(x,y);
148
            addVertex=false;
149
    }
150

    
151
    private IGeometry getSelectedGeometry() {
152
        ArrayList selectedRows=getSelectedRows();
153
//            VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
154
//        FBitSet selection = vea.getSelection();
155
        IRowEdited row=null;
156
        IGeometry ig=null;
157
        if (selectedRows.size()==1){
158
                        row=(DefaultRowEdited) selectedRows.get(0);
159
                                //row = getCadToolAdapter().getVectorialAdapter().getRow(selection.nextSetBit(0));
160
                        ig=((IFeature)row.getLinkedRow()).getGeometry().cloneGeometry();
161
                return ig;
162
        }
163

    
164
                return null;
165
        }
166

    
167
        /**
168
     * M?todo para dibujar la lo necesario para el estado en el que nos
169
     * encontremos.
170
     *
171
     * @param g Graphics sobre el que dibujar.
172
     * @param x par?metro x del punto que se pase para dibujar.
173
     * @param y par?metro x del punto que se pase para dibujar.
174
     */
175
    public void drawOperation(Graphics g, double x, double y) {
176
        //EditVertexCADToolState actualState = ((EditVertexCADToolContext) _fsm).getState();
177
        //String status = actualState.getName();
178
        //VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
179
        //FBitSet selection = vea.getSelection();
180

    
181
        try {
182
            drawVertex(g,getCadToolAdapter().getMapControl().getViewPort()
183
                    .getAffineTransform());
184
        } catch (DriverIOException e) {
185
            e.printStackTrace();
186
        }
187

    
188
    }
189

    
190
    /**
191
     * Add a diferent option.
192
     *
193
     * @param s Diferent option.
194
     */
195
    public void addOption(String s) {
196
            EditVertexCADToolState actualState = (EditVertexCADToolState) _fsm.getPreviousState();
197
        String status = actualState.getName();
198
        VectorialLayerEdited vle=getVLE();
199
        VectorialEditableAdapter vea = vle.getVEA();
200
        ArrayList selectedRows=vle.getSelectedRow();
201
        IRowEdited row=null;
202
        IGeometry ig=null;
203
        Handler[] handlers=null;
204
        if (selectedRows.size()==1){
205
                                row =  (DefaultRowEdited) selectedRows.get(0);
206
                        ig=((IFeature)row.getLinkedRow()).getGeometry().cloneGeometry();
207
                handlers=ig.getHandlers(IGeometry.SELECTHANDLER);
208
                numHandlers=handlers.length;
209
                if (numHandlers ==0){
210
                        try {
211
                                        vea.removeRow(row.getIndex(),getName(),EditionEvent.GRAPHIC);
212
                                } catch (IOException e) {
213
                                        e.printStackTrace();
214
                                } catch (DriverIOException e) {
215
                                        e.printStackTrace();
216
                                }
217
                }
218
        }
219
        int dif=1;//En el caso de ser pol?gono.
220
        if (ig instanceof FGeometryCollection){
221
                dif=2;
222
        }
223
        if (status.equals("EditVertex.SelectVertexOrDelete")){
224
                if(s.equals("s") || s.equals("S") || s.equals(PluginServices.getText(this,"next"))){
225
                        numSelect=numSelect-dif;
226
                        if (numSelect<0){
227
                                numSelect=numHandlers-1+(numSelect+1);
228
                        }
229
           }else if(s.equals("a") || s.equals("A") || s.equals(PluginServices.getText(this,"previous"))){
230
                           numSelect=numSelect+dif;
231
                               if (numSelect>(numHandlers-1)){
232
                                       numSelect=numSelect-(numHandlers);
233
                               }
234

    
235
                }else if(s.equals("e") || s.equals("E") || s.equals(PluginServices.getText(this,"del"))){
236
                        if (handlers!=null){
237
                                IGeometry newGeometry=null;
238
                                if (ig instanceof FGeometryCollection) {
239
                                        newGeometry=removeVertexGC((FGeometryCollection)ig,handlers[numSelect]);
240
                                }else {
241
                                        newGeometry=removeVertex(ig,handlers[numSelect]);
242
                                }
243
                                numSelect=0;
244

    
245
                                IRow newRow=new DefaultFeature(newGeometry,row.getAttributes(),row.getID());
246
                                try {
247
                                                vea.modifyRow(row.getIndex(),newRow,getName(),EditionEvent.GRAPHIC);
248
                                        } catch (IOException e) {
249
                                                e.printStackTrace();
250
                                        } catch (DriverIOException e) {
251
                                                e.printStackTrace();
252
                                        }
253

    
254
                                        vle.refreshSelectionCache(new Point2D.Double(0,0),getCadToolAdapter());
255
                                        getCadToolAdapter().getMapControl().drawMap(false);
256

    
257
                        }
258
                }else if(s.equals("i") || s.equals("I") || s.equals(PluginServices.getText(this,"add"))){
259
                        addVertex=true;
260
                }
261
        }
262
    }
263
    private void drawVertex(Graphics g,AffineTransform at) throws DriverIOException{
264
                ArrayList selectedRows=getSelectedRows();
265
            for (int i = 0; i<selectedRows.size();
266
                                 i++) {
267
                    DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRows
268
                                        .get(i)).getLinkedRow();
269
                        IGeometry ig = fea.getGeometry().cloneGeometry();
270
                        if (ig == null) continue;
271
                                Handler[] handlers=ig.getHandlers(IGeometry.SELECTHANDLER);
272
                                if (numSelect>handlers.length)
273
                                        numSelect=0;
274
                                FGraphicUtilities.DrawVertex((Graphics2D)g,at,handlers[numSelect]);
275
                }
276
        }
277

    
278
    /* (non-Javadoc)
279
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
280
     */
281
    public void addValue(double d) {
282
    }
283
    private IGeometry removeVertex(IGeometry gp,Handler handler) {
284
        GeneralPathX newGp = new GeneralPathX();
285
        double[] theData = new double[6];
286

    
287
        PathIterator theIterator;
288
        int theType;
289
        int numParts = 0;
290

    
291
        Point2D ptSrc = new Point2D.Double();
292
        boolean bFirst = false;
293

    
294
        theIterator = gp.getPathIterator(null); //, flatness);
295
        int numSegmentsAdded = 0;
296
        while (!theIterator.isDone()) {
297
            theType = theIterator.currentSegment(theData);
298
            if (bFirst){
299
                        newGp.moveTo(theData[0], theData[1]);
300
                        numSegmentsAdded++;
301
                        bFirst=false;
302
                        continue;
303
                }
304
            switch (theType) {
305

    
306
                case PathIterator.SEG_MOVETO:
307
                    numParts++;
308
                    ptSrc.setLocation(theData[0], theData[1]);
309
                    if (ptSrc.equals(handler.getPoint())){
310
                            numParts--;
311
                            bFirst=true;
312
                            break;
313
                    }
314
                    newGp.moveTo(ptSrc.getX(), ptSrc.getY());
315
                    numSegmentsAdded++;
316
                    bFirst = false;
317
                    break;
318

    
319
                case PathIterator.SEG_LINETO:
320
                    ptSrc.setLocation(theData[0], theData[1]);
321
                    if (ptSrc.equals(handler.getPoint())){
322
                            break;
323
                    }
324
                    newGp.lineTo(ptSrc.getX(), ptSrc.getY());
325
                    bFirst = false;
326
                    numSegmentsAdded++;
327
                    break;
328

    
329
                case PathIterator.SEG_QUADTO:
330
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
331
                    numSegmentsAdded++;
332
                    break;
333

    
334
                case PathIterator.SEG_CUBICTO:
335
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
336
                    numSegmentsAdded++;
337
                    break;
338

    
339
                case PathIterator.SEG_CLOSE:
340
                    if (numSegmentsAdded < 3)
341
                        newGp.lineTo(theData[0], theData[1]);
342
                    newGp.closePath();
343

    
344
                    break;
345
            } //end switch
346

    
347
            theIterator.next();
348
        } //end while loop
349
        FShape shp = null;
350
        switch (gp.getGeometryType())
351
        {
352
            case FShape.POINT: //Tipo punto
353
            case FShape.POINT + FShape.Z:
354
                shp = new FPoint2D(ptSrc.getX(), ptSrc.getY());
355
                break;
356

    
357
            case FShape.LINE:
358
            case FShape.LINE + FShape.Z:
359
                shp = new FPolyline2D(newGp);
360
                break;
361
            case FShape.POLYGON:
362
            case FShape.POLYGON + FShape.Z:
363
                shp = new FPolygon2D(newGp);
364
                break;
365
        }
366
        return ShapeFactory.createGeometry(shp);
367
    }
368

    
369
    private IGeometry removeVertexGC(FGeometryCollection gc,Handler handler) {
370
        IGeometry[] geoms=gc.getGeometries();
371
            ArrayList geomsAux=new ArrayList();
372
        int pos=-1;
373
            for (int i=0;i<geoms.length;i++) {
374
                    Handler[] handlers=geoms[i].getHandlers(IGeometry.SELECTHANDLER);
375
                    for (int j=0;j<handlers.length;j++) {
376
                            if (handlers[j].equalsPoint(handler)) {
377
                                    geomsAux.add(geoms[i]);
378
                                    if (pos==-1)
379
                                            pos=i;
380
                            }
381
                    }
382
            }
383
            int numGeomsAux=geomsAux.size();
384
            GeneralPathX gpx=new GeneralPathX();
385
        for (int i=0;i<numGeomsAux;i++) {
386
                    Handler[] handlers=((IGeometry)geomsAux.get(i)).getHandlers(IGeometry.SELECTHANDLER);
387
                    if (numGeomsAux == 2) {
388
                                for (int j = 0; j < handlers.length; j++) {
389
                                        if (handlers[j].equalsPoint(handler)) {
390
                                                if (j == (handlers.length - 1)) {
391
                                                        Point2D ph = handlers[0].getPoint();
392
                                                        gpx.moveTo(ph.getX(), ph.getY());
393
                                                } else {
394
                                                        Point2D ph = handlers[handlers.length - 1]
395
                                                                        .getPoint();
396
                                                        gpx.lineTo(ph.getX(), ph.getY());
397
                                                }
398
                                        }
399
                                }
400
                        }
401

    
402
            }
403
        ArrayList newGeoms=new ArrayList();
404
        for (int i=0;i<pos;i++) {
405
                newGeoms.add(geoms[i]);
406
        }
407
        newGeoms.add(ShapeFactory.createPolyline2D(gpx));
408
        for (int i=pos+numGeomsAux;i<geoms.length;i++) {
409
                newGeoms.add(geoms[i]);
410
        }
411

    
412
            return new FGeometryCollection((IGeometry[])newGeoms.toArray(new IGeometry[0]));
413
    }
414

    
415

    
416

    
417
    private IGeometry addVertex(IGeometry geome,Point2D p,Rectangle2D rect) {
418
            IGeometry geometryCloned=geome.cloneGeometry();
419
            IGeometry geom1=null;
420
            GeneralPathX gpxAux;
421
            boolean finish=false;
422
            //FGeometry geom2=null;
423

    
424
            //if (geometryCloned.getGeometryType() == FShape.POLYGON){
425
                    /////////////////
426

    
427
                    GeneralPathX newGp = new GeneralPathX();
428
            double[] theData = new double[6];
429

    
430
            PathIterator theIterator;
431
            int theType;
432
            int numParts = 0;
433
            Point2D pLast=new Point2D.Double();
434
            Point2D pAnt = new Point2D.Double();
435
            Point2D firstPoint=null;
436
            theIterator = geome.getPathIterator(null); //, flatness);
437
            int numSegmentsAdded = 0;
438
            while (!theIterator.isDone()) {
439
                theType = theIterator.currentSegment(theData);
440
                switch (theType) {
441
                    case PathIterator.SEG_MOVETO:
442
                            pLast.setLocation(theData[0], theData[1]);
443
                            if (numParts==0)
444
                                    firstPoint=(Point2D)pLast.clone();
445
                            numParts++;
446

    
447
                            gpxAux=new GeneralPathX();
448
                            gpxAux.moveTo(pAnt.getX(),pAnt.getY());
449
                            gpxAux.lineTo(pLast.getX(),pLast.getY());
450
                            geom1=ShapeFactory.createPolyline2D(gpxAux);
451
                            if (geom1.intersects(rect)){
452
                                    finish=true;
453
                                    newGp.moveTo(pLast.getX(), pLast.getY());
454
                                    //newGp.lineTo(pLast.getX(),pLast.getY());
455
                            }else{
456
                                    newGp.moveTo(pLast.getX(), pLast.getY());
457
                            }
458
                        pAnt.setLocation(pLast.getX(), pLast.getY());
459
                        numSegmentsAdded++;
460
                        break;
461

    
462
                    case PathIterator.SEG_LINETO:
463
                            pLast.setLocation(theData[0], theData[1]);
464
                            gpxAux=new GeneralPathX();
465
                            gpxAux.moveTo(pAnt.getX(),pAnt.getY());
466
                            gpxAux.lineTo(pLast.getX(),pLast.getY());
467
                            geom1=ShapeFactory.createPolyline2D(gpxAux);
468
                            if (geom1.intersects(rect)){
469
                                    newGp.lineTo(p.getX(), p.getY());
470
                                    newGp.lineTo(pLast.getX(),pLast.getY());
471
                            }else{
472
                                    newGp.lineTo(pLast.getX(), pLast.getY());
473
                            }
474
                            pAnt.setLocation(pLast.getX(), pLast.getY());
475
                        numSegmentsAdded++;
476
                        break;
477

    
478
                    case PathIterator.SEG_QUADTO:
479
                        newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
480
                        numSegmentsAdded++;
481
                        break;
482

    
483
                    case PathIterator.SEG_CUBICTO:
484
                        newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
485
                        numSegmentsAdded++;
486
                        break;
487

    
488
                    case PathIterator.SEG_CLOSE:
489
                        //if (numSegmentsAdded < 3){
490
                                gpxAux=new GeneralPathX();
491
                                gpxAux.moveTo(pAnt.getX(),pAnt.getY());
492
                                gpxAux.lineTo(firstPoint.getX(),firstPoint.getY());
493
                                geom1=ShapeFactory.createPolyline2D(gpxAux);
494
                                if (geom1.intersects(rect)|| finish){
495
                                        newGp.lineTo(p.getX(), p.getY());
496
                                        newGp.lineTo(pLast.getX(),pLast.getY());
497
                                }else{
498
                                        newGp.lineTo(pLast.getX(), pLast.getY());
499
                                }
500
                        //}
501
                        newGp.closePath();
502
                        break;
503
                } //end switch
504

    
505
                theIterator.next();
506
            } //end while loop
507
            FShape shp = null;
508
            switch (geometryCloned.getGeometryType())
509
            {
510
                case FShape.POINT: //Tipo punto
511
                case FShape.POINT + FShape.Z:
512
                    shp = new FPoint2D(pLast.getX(), pLast.getY());
513
                    break;
514

    
515
                case FShape.LINE:
516
                case FShape.LINE + FShape.Z:
517
                    shp = new FPolyline2D(newGp);
518
                    break;
519
                case FShape.POLYGON:
520
                case FShape.POLYGON + FShape.Z:
521
                    shp = new FPolygon2D(newGp);
522
                    break;
523
            }
524
            return ShapeFactory.createGeometry(shp);
525

    
526

    
527
                    /////////////////////
528
            //}else if (geometryCloned.getGeometryType() == FShape.LINE){
529

    
530
            //}
531

    
532

    
533

    
534

    
535
    /*        if (geometryCloned instanceof FGeometryCollection){
536
                    IGeometry[] geometries=((FGeometryCollection)geometryCloned).getGeometries();
537
                    boolean isSelected=false;
538
                    for (int i=0;i<geometries.length;i++){
539
                            if (geometries[i].intersects(rect) && !isSelected){
540
                                    isSelected=true;
541
                                    Handler[] handlers=geometries[i].getHandlers(IGeometry.SELECTHANDLER);
542

543
                                    GeneralPathX gp1=new GeneralPathX();
544
                                    Point2D pinit1=(Point2D)handlers[0].getPoint().clone();
545
                                    gp1.moveTo(pinit1.getX(),pinit1.getY());
546
                                    System.out.println("Handler inicial = "+pinit1);
547
                                    gp1.lineTo(p.getX(),p.getY());
548
                                    System.out.println("Handler medio = "+p);
549
                                    FPolyline2D poly1=new FPolyline2D(gp1);
550
                                    geom1=ShapeFactory.createGeometry(poly1);
551

552
                                    GeneralPathX gp2=new GeneralPathX();
553
                                    gp2.moveTo(p.getX(),p.getY());
554
                                    System.out.println("Handler medio = "+p);
555
                                    Point2D pEnd=(Point2D)handlers[1].getPoint().clone();
556
                                    gp2.lineTo(pEnd.getX(),pEnd.getY());
557
                                    System.out.println("Handler final = "+pEnd);
558
                                    FPolyline2D poly2=new FPolyline2D(gp2);
559
                                    geom2=ShapeFactory.createGeometry(poly2);
560

561
                                    ArrayList geomsAux=new ArrayList();
562
                                    geometries[i]=geom1;
563
                                    for (int j=i;j<geometries.length;j++){
564
                                            geomsAux.add(geometries[j]);
565
                                    }
566

567
                                    if (i<geometries.length-1){
568
                                            geometries[i+1]=geom2;
569
                                            Handler[] hands=((IGeometry)geom1).getHandlers(IGeometry.SELECTHANDLER);
570
                                            for (int h=0;h<hands.length;h++)
571
                                            System.out.println("Handlers New Geometry = "+hands[h].getPoint());
572
                                            Handler[] hands2=((IGeometry)geom2).getHandlers(IGeometry.SELECTHANDLER);
573
                                            for (int h=0;h<hands2.length;h++)
574
                                            System.out.println("Handlers New Geometry = "+hands2[h].getPoint());
575
                                    }else{
576
                                            geometryCloned=new FGeometryCollection(geometries);
577
                                            ((FGeometryCollection)geometryCloned).addGeometry(geom2);
578
                                    }
579
                                    for (int j=i+1;j<geometries.length;j++){
580
                                            if ((j-i)<geomsAux.size()-1){
581
                                                geometries[j+1]=(IGeometry)geomsAux.get(j-i);
582
                                        }else{
583
                                                geometryCloned=new FGeometryCollection(geometries);
584
                                                ((FGeometryCollection)geometryCloned).addGeometry((IGeometry)geomsAux.get(j-i));
585

586
                                        }
587
                                    }
588
                            }
589

590
                    }
591
            }
592
            return geometryCloned;
593
*/
594
    }
595
    private IGeometry addVertexGC(FGeometryCollection gc,Point2D p,Rectangle2D rect) {
596
            IGeometry[] geoms=gc.getGeometries();
597
            int pos=-1;
598
            for (int i=0;i<geoms.length;i++) {
599
                    if (geoms[i].intersects(rect)) {
600
                            pos=i;
601
                    }
602
            }
603
            ArrayList newGeoms=new ArrayList();
604
            for (int i=0;i<pos;i++) {
605
                    newGeoms.add(geoms[i]);
606
            }
607
            if (pos!=-1) {
608
            GeneralPathX gpx1=new GeneralPathX();
609
            GeneralPathX gpx2=new GeneralPathX();
610
            Handler[] handlers=geoms[pos].getHandlers(IGeometry.SELECTHANDLER);
611
            Point2D p1=handlers[0].getPoint();
612
            Point2D p2=p;
613
            Point2D p3=handlers[handlers.length-1].getPoint();
614
            gpx1.moveTo(p1.getX(),p1.getY());
615
            gpx1.lineTo(p2.getX(),p2.getY());
616
            gpx2.moveTo(p2.getX(),p2.getY());
617
            gpx2.lineTo(p3.getX(),p3.getY());
618
            newGeoms.add(ShapeFactory.createPolyline2D(gpx1));
619
            newGeoms.add(ShapeFactory.createPolyline2D(gpx2));
620
            for (int i=pos+1;i<geoms.length;i++) {
621
                    newGeoms.add(geoms[i]);
622
            }
623
            return new FGeometryCollection((IGeometry[])newGeoms.toArray(new IGeometry[0]));
624
            }else {
625
                    return null;
626
            }
627
    }
628
        public String getName() {
629
                return PluginServices.getText(this,"edit_vertex_");
630
        }
631
        private void selectHandler(double x, double y) {
632
                Point2D firstPoint = new Point2D.Double(x, y);
633
                VectorialLayerEdited vle = getVLE();
634
                ArrayList selectedRows = vle.getSelectedRow();
635
                double tam = getCadToolAdapter().getMapControl().getViewPort()
636
                                .toMapDistance(SelectionCADTool.tolerance);
637
                Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam,
638
                                firstPoint.getY() - tam, tam * 2, tam * 2);
639
                if (selectedRows.size() > 0) {
640
                        boolean isSelectedHandler = false;
641
                        IGeometry geometry = getSelectedGeometry();
642
                        if (geometry != null) {
643
                                Handler[] handlers = geometry
644
                                                .getHandlers(IGeometry.SELECTHANDLER);
645
                                for (int h = 0; h < handlers.length; h++) {
646
                                        if (handlers[h].getPoint().distance(firstPoint) < tam) {
647
                                                numSelect = h;
648
                                                isSelectedHandler = true;
649
                                        }
650
                                }
651

    
652
                                if (!isSelectedHandler) {
653
                                        boolean isSelectedGeometry = false;
654
                                        try {
655

    
656
                                                //VectorialEditableAdapter vea = getCadToolAdapter()
657
                                                //                .getVectorialAdapter();
658
                                                //String strEPSG = getCadToolAdapter().getMapControl()
659
                                                //                .getViewPort().getProjection().getAbrev()
660
                                                //                .substring(5);
661
                                                //IRowEdited[] feats = vea.getFeatures(rect, strEPSG);
662

    
663
                                                //for (int i = 0; i < feats.length; i++) {
664
                                                        if (geometry.intersects(rect)) { // , 0.1)){
665
                                                                isSelectedGeometry = true;
666
                                                        }
667
                                                //}
668
                                                if (isSelectedGeometry && addVertex) {
669
                                                        selectedRows = getSelectedRows();
670
                                                        DefaultFeature fea = null;
671
                                                        DefaultRowEdited row = null;
672
                                                        row = (DefaultRowEdited) selectedRows.get(0);
673
                                                        fea = (DefaultFeature) row.getLinkedRow();
674
                                                        Point2D posVertex = new Point2D.Double(x, y);
675
                                                        IGeometry geom1=fea.getGeometry().cloneGeometry();
676
                                                        IGeometry geom=null;
677
                                                        if (geom1 instanceof FGeometryCollection) {
678
                                                                geom = addVertexGC((FGeometryCollection)geom1, posVertex, rect);
679
                                                        }else {
680
                                                                geom = addVertex(geom1, posVertex, rect);
681
                                                        }
682
                                                        if (geom!=null) {
683
                                                        DefaultFeature df = new DefaultFeature(geom, fea
684
                                                                        .getAttributes(),row.getID());
685
                                                        int index=vle.getVEA().modifyRow(row.getIndex(), df,
686
                                                                        PluginServices.getText(this,"add_vertex"),EditionEvent.GRAPHIC);
687

    
688
                                                        Handler[] newHandlers = geom
689
                                                                        .getHandlers(IGeometry.SELECTHANDLER);
690
                                                        for (int h = 0; h < newHandlers.length; h++) {
691
                                                                if (newHandlers[h].getPoint().distance(
692
                                                                                posVertex) < tam) {
693
                                                                        numSelect = h;
694
                                                                        isSelectedHandler = true;
695
                                                                }
696
                                                        }
697

    
698
                                                        clearSelection();
699
                                                        selectedRows.add(new DefaultRowEdited(df,
700
                                                                        IRowEdited.STATUS_MODIFIED, index));
701
                                                        }
702
                                                }
703
                                        } catch (IOException e) {
704
                                                e.printStackTrace();
705
                                        } catch (DriverIOException e) {
706
                                                e.printStackTrace();
707
                                        }
708

    
709
                                }
710
                        }
711
                }
712

    
713
        }
714

    
715
        public String toString() {
716
                return "_editvertex";
717
        }
718

    
719
        public boolean isApplicable(int shapeType) {
720
                switch (shapeType) {
721
                case FShape.POINT:
722
                        return false;
723
                }
724
                return true;
725
        }
726

    
727

    
728
}