Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_903 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / EditVertexCADTool.java @ 10704

History | View | Annotate | Download (25.5 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.FConverter;
68
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
69
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
70
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
71
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
72
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
73
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
74
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
75
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
76
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext;
77
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext.EditVertexCADToolState;
78
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
79

    
80

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

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

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

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

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

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

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

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

    
165
                return null;
166
        }
167

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

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

    
189
    }
190

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

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

    
246
                                IRow newRow=new DefaultFeature(newGeometry,row.getAttributes(),row.getID());
247
                                try {
248
                                                vea.modifyRow(row.getIndex(),newRow,getName(),EditionEvent.GRAPHIC);
249
                                        } catch (IOException e) {
250
                                                e.printStackTrace();
251
                                        } catch (DriverIOException e) {
252
                                                e.printStackTrace();
253
                                        }
254
                                        clearSelection();
255
                                        selectedRows.add(new DefaultRowEdited(newRow,
256
                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
257
//                                        vle.refreshSelectionCache(new Point2D.Double(0,0),getCadToolAdapter());
258
//                                        refresh();
259

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

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

    
290
        PathIterator theIterator;
291
        int theType;
292
        int numParts = 0;
293

    
294
        Point2D ptSrc = new Point2D.Double();
295
        boolean bFirst = false;
296

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

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

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

    
332
                case PathIterator.SEG_QUADTO:
333
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
334
                    numSegmentsAdded++;
335
                    break;
336

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

    
342
                case PathIterator.SEG_CLOSE:
343
                    if (numSegmentsAdded < 3)
344
                        newGp.lineTo(theData[0], theData[1]);
345
                    newGp.closePath();
346

    
347
                    break;
348
            } //end switch
349

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

    
360
            case FShape.LINE:
361
            case FShape.LINE + FShape.Z:
362
                shp = new FPolyline2D(newGp);
363
                break;
364
            case FShape.POLYGON:
365
            case FShape.POLYGON + FShape.Z:
366
                shp = new FPolygon2D(newGp);
367
                break;
368
        }
369
        IGeometry ig=ShapeFactory.createGeometry(shp);
370
        int dif=1;//En el caso de ser pol?gono.
371
               numSelect=numSelect-dif;
372
                if (numSelect<0){
373
                        numSelect=numHandlers-1+(numSelect+1);
374
                }
375
        return ig;
376
    }
377

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

    
411
            }
412
        ArrayList newGeoms=new ArrayList();
413
        for (int i=0;i<pos;i++) {
414
                newGeoms.add(geoms[i]);
415
        }
416
        newGeoms.add(ShapeFactory.createPolyline2D(gpx));
417
        for (int i=pos+numGeomsAux;i<geoms.length;i++) {
418
                newGeoms.add(geoms[i]);
419
        }
420

    
421
            return new FGeometryCollection((IGeometry[])newGeoms.toArray(new IGeometry[0]));
422
    }
423

    
424

    
425

    
426
    private IGeometry addVertex(IGeometry geome,Point2D p,Rectangle2D rect) {
427
            IGeometry geometryCloned=geome.cloneGeometry();
428
            IGeometry geom1=null;
429
            GeneralPathX gpxAux;
430
            boolean finish=false;
431
            //FGeometry geom2=null;
432

    
433
            //if (geometryCloned.getGeometryType() == FShape.POLYGON){
434
                    /////////////////
435

    
436
                    GeneralPathX newGp = new GeneralPathX();
437
            double[] theData = new double[6];
438

    
439
            PathIterator theIterator;
440
            int theType;
441
            int numParts = 0;
442
            Point2D pLast=new Point2D.Double();
443
            Point2D pAnt = new Point2D.Double();
444
            Point2D firstPoint=null;
445
            theIterator = geome.getPathIterator(null,FConverter.FLATNESS); //, flatness);
446
            int numSegmentsAdded = 0;
447
            while (!theIterator.isDone()) {
448
                theType = theIterator.currentSegment(theData);
449
                switch (theType) {
450
                    case PathIterator.SEG_MOVETO:
451
                            pLast.setLocation(theData[0], theData[1]);
452
                            if (numParts==0)
453
                                    firstPoint=(Point2D)pLast.clone();
454
                            numParts++;
455

    
456
                            gpxAux=new GeneralPathX();
457
                            gpxAux.moveTo(pAnt.getX(),pAnt.getY());
458
                            gpxAux.lineTo(pLast.getX(),pLast.getY());
459
                            geom1=ShapeFactory.createPolyline2D(gpxAux);
460
                            if (geom1.intersects(rect)){
461
                                    finish=true;
462
                                    newGp.moveTo(pLast.getX(), pLast.getY());
463
                                    //newGp.lineTo(pLast.getX(),pLast.getY());
464
                            }else{
465
                                    newGp.moveTo(pLast.getX(), pLast.getY());
466
                            }
467
                        pAnt.setLocation(pLast.getX(), pLast.getY());
468
                        numSegmentsAdded++;
469
                        break;
470

    
471
                    case PathIterator.SEG_LINETO:
472
                            pLast.setLocation(theData[0], theData[1]);
473
                            gpxAux=new GeneralPathX();
474
                            gpxAux.moveTo(pAnt.getX(),pAnt.getY());
475
                            gpxAux.lineTo(pLast.getX(),pLast.getY());
476
                            geom1=ShapeFactory.createPolyline2D(gpxAux);
477
                            if (geom1.intersects(rect)){
478
                                    newGp.lineTo(p.getX(), p.getY());
479
                                    newGp.lineTo(pLast.getX(),pLast.getY());
480
                            }else{
481
                                    newGp.lineTo(pLast.getX(), pLast.getY());
482
                            }
483
                            pAnt.setLocation(pLast.getX(), pLast.getY());
484
                        numSegmentsAdded++;
485
                        break;
486

    
487
                    case PathIterator.SEG_QUADTO:
488
                        newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
489
                        numSegmentsAdded++;
490
                        break;
491

    
492
                    case PathIterator.SEG_CUBICTO:
493
                        newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
494
                        numSegmentsAdded++;
495
                        break;
496

    
497
                    case PathIterator.SEG_CLOSE:
498
                        //if (numSegmentsAdded < 3){
499
                                gpxAux=new GeneralPathX();
500
                                gpxAux.moveTo(pAnt.getX(),pAnt.getY());
501
                                gpxAux.lineTo(firstPoint.getX(),firstPoint.getY());
502
                                geom1=ShapeFactory.createPolyline2D(gpxAux);
503
                                if (geom1.intersects(rect)|| finish){
504
                                        newGp.lineTo(p.getX(), p.getY());
505
                                        newGp.lineTo(pLast.getX(),pLast.getY());
506
                                }else{
507
                                        newGp.lineTo(pLast.getX(), pLast.getY());
508
                                }
509
                        //}
510
                        newGp.closePath();
511
                        break;
512
                } //end switch
513

    
514
                theIterator.next();
515
            } //end while loop
516
            FShape shp = null;
517
            switch (geometryCloned.getGeometryType())
518
            {
519
                case FShape.POINT: //Tipo punto
520
                case FShape.POINT + FShape.Z:
521
                    shp = new FPoint2D(pLast.getX(), pLast.getY());
522
                    break;
523

    
524
                case FShape.LINE:
525
                case FShape.LINE + FShape.Z:
526
                    shp = new FPolyline2D(newGp);
527
                    break;
528
                case FShape.POLYGON:
529
                case FShape.POLYGON + FShape.Z:
530
                case FShape.CIRCLE:
531
                case FShape.ELLIPSE:
532
                    shp = new FPolygon2D(newGp);
533
                    break;
534
            }
535
            return ShapeFactory.createGeometry(shp);
536

    
537

    
538
                    /////////////////////
539
            //}else if (geometryCloned.getGeometryType() == FShape.LINE){
540

    
541
            //}
542

    
543

    
544

    
545

    
546
    /*        if (geometryCloned instanceof FGeometryCollection){
547
                    IGeometry[] geometries=((FGeometryCollection)geometryCloned).getGeometries();
548
                    boolean isSelected=false;
549
                    for (int i=0;i<geometries.length;i++){
550
                            if (geometries[i].intersects(rect) && !isSelected){
551
                                    isSelected=true;
552
                                    Handler[] handlers=geometries[i].getHandlers(IGeometry.SELECTHANDLER);
553

554
                                    GeneralPathX gp1=new GeneralPathX();
555
                                    Point2D pinit1=(Point2D)handlers[0].getPoint().clone();
556
                                    gp1.moveTo(pinit1.getX(),pinit1.getY());
557
                                    System.out.println("Handler inicial = "+pinit1);
558
                                    gp1.lineTo(p.getX(),p.getY());
559
                                    System.out.println("Handler medio = "+p);
560
                                    FPolyline2D poly1=new FPolyline2D(gp1);
561
                                    geom1=ShapeFactory.createGeometry(poly1);
562

563
                                    GeneralPathX gp2=new GeneralPathX();
564
                                    gp2.moveTo(p.getX(),p.getY());
565
                                    System.out.println("Handler medio = "+p);
566
                                    Point2D pEnd=(Point2D)handlers[1].getPoint().clone();
567
                                    gp2.lineTo(pEnd.getX(),pEnd.getY());
568
                                    System.out.println("Handler final = "+pEnd);
569
                                    FPolyline2D poly2=new FPolyline2D(gp2);
570
                                    geom2=ShapeFactory.createGeometry(poly2);
571

572
                                    ArrayList geomsAux=new ArrayList();
573
                                    geometries[i]=geom1;
574
                                    for (int j=i;j<geometries.length;j++){
575
                                            geomsAux.add(geometries[j]);
576
                                    }
577

578
                                    if (i<geometries.length-1){
579
                                            geometries[i+1]=geom2;
580
                                            Handler[] hands=((IGeometry)geom1).getHandlers(IGeometry.SELECTHANDLER);
581
                                            for (int h=0;h<hands.length;h++)
582
                                            System.out.println("Handlers New Geometry = "+hands[h].getPoint());
583
                                            Handler[] hands2=((IGeometry)geom2).getHandlers(IGeometry.SELECTHANDLER);
584
                                            for (int h=0;h<hands2.length;h++)
585
                                            System.out.println("Handlers New Geometry = "+hands2[h].getPoint());
586
                                    }else{
587
                                            geometryCloned=new FGeometryCollection(geometries);
588
                                            ((FGeometryCollection)geometryCloned).addGeometry(geom2);
589
                                    }
590
                                    for (int j=i+1;j<geometries.length;j++){
591
                                            if ((j-i)<geomsAux.size()-1){
592
                                                geometries[j+1]=(IGeometry)geomsAux.get(j-i);
593
                                        }else{
594
                                                geometryCloned=new FGeometryCollection(geometries);
595
                                                ((FGeometryCollection)geometryCloned).addGeometry((IGeometry)geomsAux.get(j-i));
596

597
                                        }
598
                                    }
599
                            }
600

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

    
664
                                if (!isSelectedHandler) {
665
                                        boolean isSelectedGeometry = false;
666
                                        try {
667

    
668
                                                //VectorialEditableAdapter vea = getCadToolAdapter()
669
                                                //                .getVectorialAdapter();
670
                                                //String strEPSG = getCadToolAdapter().getMapControl()
671
                                                //                .getViewPort().getProjection().getAbrev()
672
                                                //                .substring(5);
673
                                                //IRowEdited[] feats = vea.getFeatures(rect, strEPSG);
674

    
675
                                                //for (int i = 0; i < feats.length; i++) {
676
                                                        if (geometry.intersects(rect)) { // , 0.1)){
677
                                                                isSelectedGeometry = true;
678
                                                        }
679
                                                //}
680
                                                if (isSelectedGeometry && addVertex) {
681
                                                        selectedRows = getSelectedRows();
682
                                                        DefaultFeature fea = null;
683
                                                        DefaultRowEdited row = null;
684
                                                        row = (DefaultRowEdited) selectedRows.get(0);
685
                                                        fea = (DefaultFeature) row.getLinkedRow();
686
                                                        Point2D posVertex = new Point2D.Double(x, y);
687
                                                        IGeometry geom1=fea.getGeometry().cloneGeometry();
688
                                                        IGeometry geom=null;
689
                                                        if (geom1 instanceof FGeometryCollection) {
690
                                                                geom = addVertexGC((FGeometryCollection)geom1, posVertex, rect);
691
                                                        }else {
692
                                                                geom = addVertex(geom1, posVertex, rect);
693
                                                        }
694
                                                        if (geom!=null) {
695
                                                        DefaultFeature df = new DefaultFeature(geom, fea
696
                                                                        .getAttributes(),row.getID());
697
                                                        vea.modifyRow(row.getIndex(), df,
698
                                                                        PluginServices.getText(this,"add_vertex"),EditionEvent.GRAPHIC);
699

    
700
                                                        Handler[] newHandlers = geom
701
                                                                        .getHandlers(IGeometry.SELECTHANDLER);
702
                                                        for (int h = 0; h < newHandlers.length; h++) {
703
                                                                if (newHandlers[h].getPoint().distance(
704
                                                                                posVertex) < tam) {
705
                                                                        numSelect = h;
706
                                                                        isSelectedHandler = true;
707
                                                                }
708
                                                        }
709

    
710
                                                        clearSelection();
711
                                                        selectedRows.add(new DefaultRowEdited(df,
712
                                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
713
                                                        }
714
                                                }
715
                                        } catch (IOException e) {
716
                                                e.printStackTrace();
717
                                        } catch (DriverIOException e) {
718
                                                e.printStackTrace();
719
                                        }
720

    
721
                                }
722
                        }
723
                }
724

    
725
        }
726

    
727
        public String toString() {
728
                return "_editvertex";
729
        }
730

    
731
        public boolean isApplicable(int shapeType) {
732
                switch (shapeType) {
733
                case FShape.POINT:
734
                case FShape.MULTIPOINT:
735
                        return false;
736
                }
737
                return true;
738
        }
739

    
740

    
741
}