Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / EditVertexCADTool.java @ 23086

History | View | Annotate | Download (26 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.Component;
44
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46
import java.awt.event.InputEvent;
47
import java.awt.geom.PathIterator;
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.util.ArrayList;
51

    
52
import javax.swing.JOptionPane;
53

    
54
import org.gvsig.fmap.data.ReadException;
55
import org.gvsig.fmap.data.feature.Feature;
56
import org.gvsig.fmap.data.feature.FeatureCollection;
57
import org.gvsig.fmap.data.feature.FeatureStore;
58
import org.gvsig.fmap.geom.Geometry;
59
import org.gvsig.fmap.geom.GeometryFactory;
60
import org.gvsig.fmap.geom.GeometryManager;
61
import org.gvsig.fmap.geom.aggregate.BaseMultiPrimitive;
62
import org.gvsig.fmap.geom.handler.Handler;
63
import org.gvsig.fmap.geom.primitive.Curve2D;
64
import org.gvsig.fmap.geom.primitive.GeneralPathX;
65
import org.gvsig.fmap.geom.primitive.Surface2D;
66
import org.gvsig.fmap.geom.util.Converter;
67
import org.gvsig.fmap.mapcontext.ViewPort;
68
import org.gvsig.fmap.mapcontext.rendering.symbols.FGraphicUtilities;
69

    
70
import com.iver.andami.PluginServices;
71
import com.iver.andami.messages.NotificationManager;
72
import com.iver.cit.gvsig.CADExtension;
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
        private GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
91

    
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
            FeatureCollection selection=null;
134
                try {
135
                        selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
136
                } catch (ReadException e) {
137
                        // TODO Auto-generated catch block
138
                        e.printStackTrace();
139
                }
140
        if (selection.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
141
            CADExtension.setCADTool("_selection",false);
142
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
143
                "_editvertex");
144
        }
145
    }
146

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

    
159
    private Geometry getSelectedGeometry() {
160
            FeatureCollection selection=null;
161
            try {
162
                    selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
163

    
164
                    Feature feature=null;
165
                    Geometry ig=null;
166
                    if (selection.size()==1){
167
                            feature=selection.getFeature(0);
168
                            //row = getCadToolAdapter().getVectorialAdapter().getRow(selection.nextSetBit(0));
169
                            ig=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
170
                            return ig;
171
                    }
172
            } catch (ReadException e) {
173
                    // TODO Auto-generated catch block
174
                    e.printStackTrace();
175
            }
176

    
177
            return null;
178
    }
179

    
180
        /**
181
     * M?todo para dibujar la lo necesario para el estado en el que nos
182
     * encontremos.
183
     *
184
     * @param g Graphics sobre el que dibujar.
185
     * @param x par?metro x del punto que se pase para dibujar.
186
     * @param y par?metro x del punto que se pase para dibujar.
187
     */
188
    public void drawOperation(Graphics g, double x, double y) {
189
        drawVertex(g,getCadToolAdapter().getMapControl().getViewPort());
190
    }
191

    
192
    /**
193
     * Add a diferent option.
194
     *
195
     * @param s Diferent option.
196
     */
197
    public void addOption(String s) {
198
            EditVertexCADToolState actualState = (EditVertexCADToolState) _fsm.getPreviousState();
199
        String status = actualState.getName();
200
        VectorialLayerEdited vle=getVLE();
201
        FeatureStore featureStore=vle.getFeatureStore();
202
        FeatureCollection selection=(FeatureCollection)featureStore.getSelection();
203
        Feature feature=null;
204
        Geometry ig=null;
205
        Handler[] handlers=null;
206
        if (selection.size()==1){
207
                                feature =  selection.getFeature(0);
208
                        ig=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
209
                handlers=ig.getHandlers(Geometry.SELECTHANDLER);
210
                numHandlers=handlers.length;
211
                if (numHandlers ==0){
212
                        try {
213
                                featureStore.delete(feature);
214
//                                        vea.removeRow(row.getIndex(),getName(),EditionEvent.GRAPHIC);
215
                                } catch (ReadException e) {
216
                                        NotificationManager.addError(e.getMessage(),e);
217
                                }
218
                }
219
        }else{
220
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"hay_mas_de_una_geometria_seleccionada"));
221
        }
222
        int dif=1;//En el caso de ser pol?gono.
223
        if (ig instanceof BaseMultiPrimitive){
224
                dif=2;
225
        }
226
        if (status.equals("EditVertex.SelectVertexOrDelete")){
227
                if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.nextvertex")) || s.equals(PluginServices.getText(this,"next"))){
228
                        numSelect=numSelect-dif;
229
                        if (numSelect<0){
230
                                numSelect=numHandlers-1+(numSelect+1);
231
                        }
232
           }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.previousvertex")) || s.equals(PluginServices.getText(this,"previous"))){
233
                           numSelect=numSelect+dif;
234
                               if (numSelect>(numHandlers-1)){
235
                                       numSelect=numSelect-(numHandlers);
236
                               }
237

    
238
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.delvertex")) || s.equals(PluginServices.getText(this,"del"))){
239
                        if (handlers!=null){
240
                                Geometry newGeometry=null;
241
                                if (ig instanceof BaseMultiPrimitive) {
242
                                        newGeometry=removeVertexGC((BaseMultiPrimitive)ig,handlers[numSelect]);
243
                                }else {
244
                                        newGeometry=removeVertex(ig,handlers,numSelect);
245
                                }
246
                                //numSelect=0;
247
                                feature.editing();
248
                                feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(),ig);
249
//                                IRow newRow=new DefaultFeature(newGeometry,row.getAttributes(),row.getID());
250
                                try {
251
                                                featureStore.update(feature);
252
//                                        vea.modifyRow(row.getIndex(),newRow,getName(),EditionEvent.GRAPHIC);
253
                                                clearSelection();
254
                                } catch (ReadException e) {
255
                                                NotificationManager.addError(e.getMessage(),e);
256
                                        }
257
//                                        vle.addSelectionCache(new DefaultRowEdited(newRow,
258
//                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
259

    
260
//                                        vle.refreshSelectionCache(new Point2D.Double(0,0),getCadToolAdapter());
261
//                                        refresh();
262

    
263
                        }
264
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.addvertex")) || s.equals(PluginServices.getText(this,"add"))){
265
                            addVertex=true;
266
                    }
267
        }
268
    }
269
    private void drawVertex(Graphics g,ViewPort vp){
270
                ArrayList selectedRows=getSelectedRows();
271
            for (int i = 0; i<selectedRows.size();
272
                                 i++) {
273
                    DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRows
274
                                        .get(i)).getLinkedRow();
275
                        Geometry ig = fea.getGeometry().cloneGeometry();
276
                        if (ig == null) continue;
277
                        ig.drawInts((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
278
                        Handler[] handlers=ig.getHandlers(Geometry.SELECTHANDLER);
279
                        if (numSelect>=handlers.length)
280
                                numSelect=0;
281
                        FGraphicUtilities.DrawVertex((Graphics2D)g,vp.getAffineTransform(),handlers[numSelect]);
282
                }
283
        }
284

    
285
    /* (non-Javadoc)
286
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
287
     */
288
    public void addValue(double d) {
289
    }
290
    private Geometry removeVertex(Geometry gp,Handler[] handlers,int numHandler) {
291
        GeneralPathX newGp = new GeneralPathX();
292
        double[] theData = new double[6];
293

    
294
        PathIterator theIterator;
295
        int theType;
296
        int numParts = 0;
297

    
298
        Point2D ptSrc = new Point2D.Double();
299
        boolean bFirst = false;
300

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

    
314
                case PathIterator.SEG_MOVETO:
315
                    numParts++;
316
                    ptSrc.setLocation(theData[0], theData[1]);
317
                    if (ptSrc.equals(handlers[numHandler].getPoint())){
318
                            numParts--;
319
                            bFirst=true;
320
                            break;
321
                    }
322
                    newGp.moveTo(ptSrc.getX(), ptSrc.getY());
323
                    numSegmentsAdded++;
324
                    bFirst = false;
325
                    break;
326

    
327
                case PathIterator.SEG_LINETO:
328
                    ptSrc.setLocation(theData[0], theData[1]);
329
                    if (ptSrc.equals(handlers[numHandler].getPoint())){
330
                            break;
331
                    }
332
                    newGp.lineTo(ptSrc.getX(), ptSrc.getY());
333
                    bFirst = false;
334
                    numSegmentsAdded++;
335
                    break;
336

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

    
342
                case PathIterator.SEG_CUBICTO:
343
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
344
                    numSegmentsAdded++;
345
                    break;
346

    
347
                case PathIterator.SEG_CLOSE:
348
                    if (numSegmentsAdded < 3)
349
                        newGp.lineTo(theData[0], theData[1]);
350
                    newGp.closePath();
351

    
352
                    break;
353
            } //end switch
354

    
355
            theIterator.next();
356
        } //end while loop
357
        Geometry shp = null;
358
        switch (gp.getType())
359
        {
360
            case Geometry.TYPES.POINT: //Tipo punto
361
            case Geometry.TYPES.POINT + Geometry.TYPES.Z:
362
                shp = new org.gvsig.fmap.geom.primitive.Point2D(ptSrc.getX(), ptSrc.getY());
363
                break;
364

    
365
            case Geometry.TYPES.CURVE:
366
            case Geometry.TYPES.CURVE + Geometry.TYPES.Z:
367
                shp = new Curve2D(newGp);
368
                break;
369
            case Geometry.TYPES.SURFACE:
370
            case Geometry.TYPES.SURFACE + Geometry.TYPES.Z:
371
                shp = new Surface2D(newGp);
372
                break;
373
        }
374
        Geometry ig=shp;
375
        int dif=1;//En el caso de ser pol?gono.
376
               numSelect=numSelect-dif;
377
                if (numSelect<0){
378
                        numSelect=numHandlers-1+(numSelect+1);
379
                }
380
        return ig;
381
    }
382

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

    
416
            }
417
        ArrayList newGeoms=new ArrayList();
418
        for (int i=0;i<pos;i++) {
419
                newGeoms.add(geoms[i]);
420
        }
421
        newGeoms.add(geomFactory.createPolyline2D(gpx));
422
        for (int i=pos+numGeomsAux;i<geoms.length;i++) {
423
                newGeoms.add(geoms[i]);
424
        }
425

    
426
            return new GeometryCollection((Geometry[])newGeoms.toArray(new Geometry[0]));
427
    }
428

    
429

    
430

    
431
    private Geometry addVertex(Geometry geome,Point2D p,Rectangle2D rect) {
432
            Geometry geometryCloned=geome.cloneGeometry();
433
            Geometry geom1=null;
434
            GeneralPathX gpxAux;
435
            boolean finish=false;
436
            //FGeometry geom2=null;
437

    
438
            //if (geometryCloned.getGeometryType() == FShape.POLYGON){
439
                    /////////////////
440

    
441
                    GeneralPathX newGp = new GeneralPathX();
442
            double[] theData = new double[6];
443

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

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

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

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

    
497
                    case PathIterator.SEG_CUBICTO:
498
                        newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
499
                        numSegmentsAdded++;
500
                        break;
501

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

    
519
                theIterator.next();
520
            } //end while loop
521
            Geometry shp = null;
522
            switch (geometryCloned.getType())
523
            {
524
                case Geometry.TYPES.POINT: //Tipo punto
525
                case Geometry.TYPES.POINT + Geometry.TYPES.Z:
526
                    shp = new org.gvsig.fmap.geom.primitive.Point2D(pLast.getX(), pLast.getY());
527
                    break;
528

    
529
                case Geometry.TYPES.CURVE:
530
                case Geometry.TYPES.CURVE + Geometry.TYPES.Z:
531
                    shp = new Curve2D(newGp);
532
                    break;
533
                case Geometry.TYPES.SURFACE:
534
                case Geometry.TYPES.SURFACE + Geometry.TYPES.Z:
535
                case Geometry.TYPES.CIRCLE:
536
                case Geometry.TYPES.ELLIPSE:
537
                    shp = new Surface2D(newGp);
538
                    break;
539
            }
540
            return shp;
541

    
542

    
543
                    /////////////////////
544
            //}else if (geometryCloned.getGeometryType() == FShape.LINE){
545

    
546
            //}
547

    
548

    
549

    
550

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

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

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

577
                                    ArrayList geomsAux=new ArrayList();
578
                                    geometries[i]=geom1;
579
                                    for (int j=i;j<geometries.length;j++){
580
                                            geomsAux.add(geometries[j]);
581
                                    }
582

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

602
                                        }
603
                                    }
604
                            }
605

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

    
669
                                if (!isSelectedHandler) {
670
                                        boolean isSelectedGeometry = false;
671
                                        try {
672

    
673
                                                //VectorialEditableAdapter vea = getCadToolAdapter()
674
                                                //                .getVectorialAdapter();
675
                                                //String strEPSG = getCadToolAdapter().getMapControl()
676
                                                //                .getViewPort().getProjection().getAbrev()
677
                                                //                .substring(5);
678
                                                //IRowEdited[] feats = vea.getFeatures(rect, strEPSG);
679

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

    
705
                                                        Handler[] newHandlers = geom
706
                                                                        .getHandlers(Geometry.SELECTHANDLER);
707
                                                        for (int h = 0; h < newHandlers.length; h++) {
708
                                                                if (newHandlers[h].getPoint().distance(
709
                                                                                posVertex) < tam) {
710
                                                                        numSelect = h;
711
                                                                        isSelectedHandler = true;
712
                                                                }
713
                                                        }
714

    
715
                                                        clearSelection();
716
                                                        vle.addSelectionCache(new DefaultRowEdited(df,
717
                                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
718
                                                        }
719
                                                }
720
                                        } catch (ValidateRowException e) {
721
                                                NotificationManager.addError(e.getMessage(),e);
722
                                        } catch (ReadException e) {
723
                                                NotificationManager.addError(e.getMessage(),e);
724
                                        }
725
                                }
726
                        }
727
                }
728

    
729
        }
730

    
731
        public String toString() {
732
                return "_editvertex";
733
        }
734

    
735
        public boolean isApplicable(int shapeType) {
736
                switch (shapeType) {
737
                case Geometry.TYPES.POINT:
738
                case Geometry.TYPES.MULTIPOINT:
739
                        return false;
740
                }
741
                return true;
742
        }
743

    
744

    
745
}