Statistics
| Revision:

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

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.ViewPort;
56
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
57
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
58
import com.iver.cit.gvsig.fmap.core.FPoint2D;
59
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
60
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
61
import com.iver.cit.gvsig.fmap.core.FShape;
62
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
63
import com.iver.cit.gvsig.fmap.core.Handler;
64
import com.iver.cit.gvsig.fmap.core.IFeature;
65
import com.iver.cit.gvsig.fmap.core.IGeometry;
66
import com.iver.cit.gvsig.fmap.core.IRow;
67
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
68
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
69
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
70
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
71
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
72
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
73
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
74
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
75
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
76
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
77
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext;
78
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext.EditVertexCADToolState;
79
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
80

    
81

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

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

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

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

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

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

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

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

    
166
                return null;
167
        }
168

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

    
183
        try {
184
            drawVertex(g,getCadToolAdapter().getMapControl().getViewPort());
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,ViewPort vp) 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
                        ig.drawInts((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
274
                        if (ig == null) continue;
275
                                Handler[] handlers=ig.getHandlers(IGeometry.SELECTHANDLER);
276
                                if (numSelect>=handlers.length)
277
                                        numSelect=0;
278
                                FGraphicUtilities.DrawVertex((Graphics2D)g,vp.getAffineTransform(),handlers[numSelect]);
279
                }
280
        }
281

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

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

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

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

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

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

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

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

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

    
348
                    break;
349
            } //end switch
350

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

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

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

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

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

    
425

    
426

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

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

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

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

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

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

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

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

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

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

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

    
538

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

    
542
            //}
543

    
544

    
545

    
546

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

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

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

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

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

598
                                        }
599
                                    }
600
                            }
601

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

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

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

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

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

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

    
722
                                }
723
                        }
724
                }
725

    
726
        }
727

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

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

    
741

    
742
}