Statistics
| Revision:

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

History | View | Annotate | Download (26.7 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
import java.util.Iterator;
52

    
53
import javax.swing.JOptionPane;
54

    
55
import org.gvsig.fmap.dal.exception.DataException;
56
import org.gvsig.fmap.dal.exception.ReadException;
57
import org.gvsig.fmap.dal.feature.EditableFeature;
58
import org.gvsig.fmap.dal.feature.Feature;
59
import org.gvsig.fmap.dal.feature.FeatureSelection;
60
import org.gvsig.fmap.dal.feature.FeatureSet;
61
import org.gvsig.fmap.dal.feature.FeatureStore;
62
import org.gvsig.fmap.geom.Geometry;
63
import org.gvsig.fmap.geom.GeometryFactory;
64
import org.gvsig.fmap.geom.GeometryLocator;
65
import org.gvsig.fmap.geom.aggregate.BaseMultiPrimitive;
66
import org.gvsig.fmap.geom.aggregate.BaseMultiPrimitive2D;
67
import org.gvsig.fmap.geom.handler.Handler;
68
import org.gvsig.fmap.geom.operation.DrawInts;
69
import org.gvsig.fmap.geom.operation.DrawOperationContext;
70
import org.gvsig.fmap.geom.operation.GeometryOperationException;
71
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
72
import org.gvsig.fmap.geom.primitive.Curve2D;
73
import org.gvsig.fmap.geom.primitive.GeneralPathX;
74
import org.gvsig.fmap.geom.primitive.Surface2D;
75
import org.gvsig.fmap.geom.util.Converter;
76
import org.gvsig.fmap.mapcontext.ViewPort;
77
import org.gvsig.fmap.mapcontext.rendering.symbols.FGraphicUtilities;
78
import org.gvsig.fmap.mapcontrol.MapControl;
79

    
80
import com.iver.andami.PluginServices;
81
import com.iver.andami.messages.NotificationManager;
82
import com.iver.cit.gvsig.CADExtension;
83
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
84
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
85
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext;
86
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext.EditVertexCADToolState;
87
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
88

    
89

    
90
/**
91
 * DOCUMENT ME!
92
 *
93
 * @author Vicente Caballero Navarro
94
 */
95
public class EditVertexCADTool extends DefaultCADTool {
96
    private EditVertexCADToolContext _fsm;
97
    private int numSelect=0;
98
        private int numHandlers;
99
        private boolean addVertex=false;
100
        private GeometryFactory geomFactory = GeometryLocator.getGeometryManager().getGeometryFactory();
101

    
102
    /**
103
     * Crea un nuevo PolylineCADTool.
104
     */
105
    public EditVertexCADTool() {
106
    }
107

    
108
    /**
109
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
110
     * carga previa a la utilizaci?n de la herramienta.
111
     */
112
    public void init() {
113
        _fsm = new EditVertexCADToolContext(this);
114
    }
115

    
116
    /* (non-Javadoc)
117
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
118
     */
119
    public void transition(double x, double y, InputEvent event) {
120
        _fsm.addPoint(x, y, event);
121
    }
122

    
123
    /* (non-Javadoc)
124
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
125
     */
126
    public void transition(double d) {
127
            _fsm.addValue(d);
128
    }
129

    
130
    /* (non-Javadoc)
131
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
132
     */
133
    public void transition(String s) throws CommandException {
134
            if (!super.changeCommand(s)){
135
                    _fsm.addOption(s);
136
            }
137
    }
138

    
139
    /**
140
     * DOCUMENT ME!
141
     */
142
    public void selection() {
143
            FeatureSet selection=null;
144
            try {
145
                    selection = (FeatureSet)getVLE().getFeatureStore().getSelection();
146

    
147
                    if (selection.getSize() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
148
                            CADExtension.setCADTool("_selection",false);
149
                            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
150
                            "_editvertex");
151
                    }
152
            } catch (ReadException e) {
153
                    // TODO Auto-generated catch block
154
                    e.printStackTrace();
155
            } catch (DataException e) {
156
                        // TODO Auto-generated catch block
157
                        e.printStackTrace();
158
                }
159
    }
160

    
161
    /**
162
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
163
     * editableFeatureSource que ya estar? creado.
164
     *
165
     * @param x par?metro x del punto que se pase en esta transici?n.
166
     * @param y par?metro y del punto que se pase en esta transici?n.
167
     */
168
    public void addPoint(double x, double y,InputEvent event) {
169
            selectHandler(x,y);
170
            addVertex=false;
171
    }
172

    
173
    private Geometry getSelectedGeometry() {
174
            FeatureSet selection=null;
175
            try {
176
                    selection = (FeatureSet)getVLE().getFeatureStore().getSelection();
177

    
178
                    Feature feature=null;
179
                    Geometry ig=null;
180
                    if (selection.getSize()==1){
181
                            feature=(Feature)selection.iterator().next();
182
                            ig=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
183
                            return ig;
184
                    }
185
            } catch (ReadException e) {
186
                    // TODO Auto-generated catch block
187
                    e.printStackTrace();
188
            } catch (DataException e) {
189
                        // TODO Auto-generated catch block
190
                        e.printStackTrace();
191
                }
192

    
193
            return null;
194
    }
195

    
196
        /**
197
     * M?todo para dibujar la lo necesario para el estado en el que nos
198
     * encontremos.
199
     *
200
     * @param g Graphics sobre el que dibujar.
201
     * @param x par?metro x del punto que se pase para dibujar.
202
     * @param y par?metro x del punto que se pase para dibujar.
203
     */
204
    public void drawOperation(Graphics g, double x, double y) {
205
        drawVertex(g,getCadToolAdapter().getMapControl().getViewPort());
206
    }
207

    
208
    /**
209
     * Add a diferent option.
210
     *
211
     * @param s Diferent option.
212
     */
213
    public void addOption(String s) {
214
            EditVertexCADToolState actualState = (EditVertexCADToolState) _fsm.getPreviousState();
215
        String status = actualState.getName();
216
        VectorialLayerEdited vle=getVLE();
217
        FeatureStore featureStore=null;
218
                try {
219
                        featureStore = vle.getFeatureStore();
220

    
221
        FeatureSet selection=(FeatureSet)featureStore.getSelection();
222
        Feature feature=null;
223
        Geometry ig=null;
224
        Handler[] handlers=null;
225
        if (selection.getSize()==1){
226
                                feature =  (Feature)selection.iterator().next();
227
                        ig=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
228
                handlers=ig.getHandlers(Geometry.SELECTHANDLER);
229
                numHandlers=handlers.length;
230
                if (numHandlers ==0){
231
                        try {
232
                                featureStore.delete(feature);
233
                                } catch (ReadException e) {
234
                                        NotificationManager.addError(e.getMessage(),e);
235
                                } catch (DataException e) {
236
                                        NotificationManager.addError(e.getMessage(),e);
237
                                }
238
                }
239
        }else{
240
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"hay_mas_de_una_geometria_seleccionada"));
241
        }
242

    
243
        int dif=1;//En el caso de ser pol?gono.
244
        if (ig instanceof BaseMultiPrimitive){
245
                dif=2;
246
        }
247

    
248
        if (status.equals("EditVertex.SelectVertexOrDelete")){
249
                if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.nextvertex")) || s.equals(PluginServices.getText(this,"next"))){
250
                        numSelect=numSelect-dif;
251
                        if (numSelect<0){
252
                                numSelect=numHandlers-1+(numSelect+1);
253
                        }
254
           }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.previousvertex")) || s.equals(PluginServices.getText(this,"previous"))){
255
                           numSelect=numSelect+dif;
256
                               if (numSelect>(numHandlers-1)){
257
                                       numSelect=numSelect-(numHandlers);
258
                               }
259

    
260
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.delvertex")) || s.equals(PluginServices.getText(this,"del"))){
261
                        if (handlers!=null){
262
                                Geometry newGeometry=null;
263
                                if (ig instanceof BaseMultiPrimitive) {
264
                                        newGeometry=removeVertexGC((BaseMultiPrimitive)ig,handlers[numSelect]);
265
                                }else {
266
                                        newGeometry=removeVertex(ig,handlers,numSelect);
267
                                }
268
                                try {
269
                                        EditableFeature eFeature=feature.getEditable();
270
                                        eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),ig);
271
                                        featureStore.update(eFeature);
272
                                        clearSelection();
273
                                } catch (ReadException e) {
274
                                        NotificationManager.addError(e.getMessage(),e);
275
                                }
276
                                catch (DataException e) {
277
                                        // TODO Auto-generated catch block
278
                                        e.printStackTrace();
279
                                }
280
                        }
281
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.addvertex")) || s.equals(PluginServices.getText(this,"add"))){
282
                        addVertex=true;
283
                }
284
        }
285
                } catch (ReadException e1) {
286
                        // TODO Auto-generated catch block
287
                        e1.printStackTrace();
288
                } catch (DataException e) {
289
                        // TODO Auto-generated catch block
290
                        e.printStackTrace();
291
                }
292
    }
293
    private void drawVertex(Graphics g,ViewPort vp){
294
            VectorialLayerEdited vle=getVLE();
295
            Iterator iterator=null;
296
                try {
297
                        iterator = ((FeatureSelection)vle.getFeatureStore().getSelection()).iterator();
298
                } catch (ReadException e1) {
299
                        // TODO Auto-generated catch block
300
                        e1.printStackTrace();
301
                } catch (DataException e) {
302
                        // TODO Auto-generated catch block
303
                        e.printStackTrace();
304
                }
305
                while (iterator.hasNext()) {
306
                        Feature feature = (Feature) iterator.next();
307

    
308
                        Geometry ig = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
309
                        if (ig == null) continue;
310
                        DrawOperationContext doc=new DrawOperationContext();
311
                        doc.setGraphics((Graphics2D)g);
312
                        doc.setViewPort(vp);
313
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
314
                try {
315
                                ig.invokeOperation(DrawInts.CODE,doc);
316
                        } catch (GeometryOperationNotSupportedException e) {
317
                                e.printStackTrace();
318
                        } catch (GeometryOperationException e) {
319
                                e.printStackTrace();
320
                        }
321
                        Handler[] handlers=ig.getHandlers(Geometry.SELECTHANDLER);
322
                        if (numSelect>=handlers.length)
323
                                numSelect=0;
324
                        FGraphicUtilities.DrawVertex((Graphics2D)g,vp.getAffineTransform(),handlers[numSelect]);
325
                }
326
        }
327

    
328
    /* (non-Javadoc)
329
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
330
     */
331
    public void addValue(double d) {
332
    }
333
    private Geometry removeVertex(Geometry gp,Handler[] handlers,int numHandler) {
334
        GeneralPathX newGp = new GeneralPathX();
335
        double[] theData = new double[6];
336

    
337
        PathIterator theIterator;
338
        int theType;
339
        int numParts = 0;
340

    
341
        Point2D ptSrc = new Point2D.Double();
342
        boolean bFirst = false;
343

    
344
        theIterator = gp.getPathIterator(null, Converter.FLATNESS);
345
        int numSegmentsAdded = 0;
346
        while (!theIterator.isDone()) {
347
            theType = theIterator.currentSegment(theData);
348
            if (bFirst){
349
                        newGp.moveTo(theData[0], theData[1]);
350
                        numSegmentsAdded++;
351
                        bFirst=false;
352
                        theIterator.next();
353
                        continue;
354
                }
355
            switch (theType) {
356

    
357
                case PathIterator.SEG_MOVETO:
358
                    numParts++;
359
                    ptSrc.setLocation(theData[0], theData[1]);
360
                    if (ptSrc.equals(handlers[numHandler].getPoint())){
361
                            numParts--;
362
                            bFirst=true;
363
                            break;
364
                    }
365
                    newGp.moveTo(ptSrc.getX(), ptSrc.getY());
366
                    numSegmentsAdded++;
367
                    bFirst = false;
368
                    break;
369

    
370
                case PathIterator.SEG_LINETO:
371
                    ptSrc.setLocation(theData[0], theData[1]);
372
                    if (ptSrc.equals(handlers[numHandler].getPoint())){
373
                            break;
374
                    }
375
                    newGp.lineTo(ptSrc.getX(), ptSrc.getY());
376
                    bFirst = false;
377
                    numSegmentsAdded++;
378
                    break;
379

    
380
                case PathIterator.SEG_QUADTO:
381
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
382
                    numSegmentsAdded++;
383
                    break;
384

    
385
                case PathIterator.SEG_CUBICTO:
386
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
387
                    numSegmentsAdded++;
388
                    break;
389

    
390
                case PathIterator.SEG_CLOSE:
391
                    if (numSegmentsAdded < 3)
392
                        newGp.lineTo(theData[0], theData[1]);
393
                    newGp.closePath();
394

    
395
                    break;
396
            } //end switch
397

    
398
            theIterator.next();
399
        } //end while loop
400
        Geometry shp = null;
401
        switch (gp.getType())
402
        {
403
            case Geometry.TYPES.POINT: //Tipo punto
404
            case Geometry.TYPES.POINT + Geometry.TYPES.Z:
405
                shp = new org.gvsig.fmap.geom.primitive.Point2D(ptSrc.getX(), ptSrc.getY());
406
                break;
407

    
408
            case Geometry.TYPES.CURVE:
409
            case Geometry.TYPES.CURVE + Geometry.TYPES.Z:
410
                shp = new Curve2D(newGp);
411
                break;
412
            case Geometry.TYPES.SURFACE:
413
            case Geometry.TYPES.SURFACE + Geometry.TYPES.Z:
414
                shp = new Surface2D(newGp);
415
                break;
416
        }
417
        Geometry ig=shp;
418
        int dif=1;//En el caso de ser pol?gono.
419
               numSelect=numSelect-dif;
420
                if (numSelect<0){
421
                        numSelect=numHandlers-1+(numSelect+1);
422
                }
423
        return ig;
424
    }
425

    
426
    private Geometry removeVertexGC(BaseMultiPrimitive gc,Handler handler) {
427
        Geometry[] geoms=gc.getGeometries();
428
            ArrayList geomsAux=new ArrayList();
429
        int pos=-1;
430
            for (int i=0;i<geoms.length;i++) {
431
                    Handler[] handlers=geoms[i].getHandlers(Geometry.SELECTHANDLER);
432
                    for (int j=0;j<handlers.length;j++) {
433
                            if (handlers[j].equalsPoint(handler)) {
434
                                    geomsAux.add(geoms[i]);
435
                                    if (pos==-1)
436
                                            pos=i;
437
                            }
438
                    }
439
            }
440
            int numGeomsAux=geomsAux.size();
441
            GeneralPathX gpx=new GeneralPathX();
442
        for (int i=0;i<numGeomsAux;i++) {
443
                    Handler[] handlers=((Geometry)geomsAux.get(i)).getHandlers(Geometry.SELECTHANDLER);
444
                    if (numGeomsAux == 2) {
445
                                for (int j = 0; j < handlers.length; j++) {
446
                                        if (handlers[j].equalsPoint(handler)) {
447
                                                if (j == (handlers.length - 1)) {
448
                                                        Point2D ph = handlers[0].getPoint();
449
                                                        gpx.moveTo(ph.getX(), ph.getY());
450
                                                } else {
451
                                                        Point2D ph = handlers[handlers.length - 1]
452
                                                                        .getPoint();
453
                                                        gpx.lineTo(ph.getX(), ph.getY());
454
                                                }
455
                                        }
456
                                }
457
                        }
458

    
459
            }
460
        ArrayList newGeoms=new ArrayList();
461
        for (int i=0;i<pos;i++) {
462
                newGeoms.add(geoms[i]);
463
        }
464
        newGeoms.add(geomFactory.createPolyline2D(gpx));
465
        for (int i=pos+numGeomsAux;i<geoms.length;i++) {
466
                newGeoms.add(geoms[i]);
467
        }
468

    
469
            return new BaseMultiPrimitive2D((Geometry[])newGeoms.toArray(new Geometry[0]));
470
    }
471

    
472

    
473

    
474
    private Geometry addVertex(Geometry geome,Point2D p,Rectangle2D rect) {
475
            Geometry geometryCloned=geome.cloneGeometry();
476
            Geometry geom1=null;
477
            GeneralPathX gpxAux;
478
            boolean finish=false;
479
            //FGeometry geom2=null;
480

    
481
            //if (geometryCloned.getGeometryType() == FShape.POLYGON){
482
                    /////////////////
483

    
484
                    GeneralPathX newGp = new GeneralPathX();
485
            double[] theData = new double[6];
486

    
487
            PathIterator theIterator;
488
            int theType;
489
            int numParts = 0;
490
            Point2D pLast=new Point2D.Double();
491
            Point2D pAnt = new Point2D.Double();
492
            Point2D firstPoint=null;
493
            theIterator = geome.getPathIterator(null,Converter.FLATNESS); //, flatness);
494
            int numSegmentsAdded = 0;
495
            while (!theIterator.isDone()) {
496
                theType = theIterator.currentSegment(theData);
497
                switch (theType) {
498
                    case PathIterator.SEG_MOVETO:
499
                            pLast.setLocation(theData[0], theData[1]);
500
                            if (numParts==0)
501
                                    firstPoint=(Point2D)pLast.clone();
502
                            numParts++;
503

    
504
                            gpxAux=new GeneralPathX();
505
                            gpxAux.moveTo(pAnt.getX(),pAnt.getY());
506
                            gpxAux.lineTo(pLast.getX(),pLast.getY());
507
                            geom1=geomFactory.createPolyline2D(gpxAux);
508
                            if (geom1.intersects(rect)){
509
                                    finish=true;
510
                                    newGp.moveTo(pLast.getX(), pLast.getY());
511
                                    //newGp.lineTo(pLast.getX(),pLast.getY());
512
                            }else{
513
                                    newGp.moveTo(pLast.getX(), pLast.getY());
514
                            }
515
                        pAnt.setLocation(pLast.getX(), pLast.getY());
516
                        numSegmentsAdded++;
517
                        break;
518

    
519
                    case PathIterator.SEG_LINETO:
520
                            pLast.setLocation(theData[0], theData[1]);
521
                            gpxAux=new GeneralPathX();
522
                            gpxAux.moveTo(pAnt.getX(),pAnt.getY());
523
                            gpxAux.lineTo(pLast.getX(),pLast.getY());
524
                            geom1=geomFactory.createPolyline2D(gpxAux);
525
                            if (geom1.intersects(rect)){
526
                                    newGp.lineTo(p.getX(), p.getY());
527
                                    newGp.lineTo(pLast.getX(),pLast.getY());
528
                            }else{
529
                                    newGp.lineTo(pLast.getX(), pLast.getY());
530
                            }
531
                            pAnt.setLocation(pLast.getX(), pLast.getY());
532
                        numSegmentsAdded++;
533
                        break;
534

    
535
                    case PathIterator.SEG_QUADTO:
536
                        newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
537
                        numSegmentsAdded++;
538
                        break;
539

    
540
                    case PathIterator.SEG_CUBICTO:
541
                        newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
542
                        numSegmentsAdded++;
543
                        break;
544

    
545
                    case PathIterator.SEG_CLOSE:
546
                        //if (numSegmentsAdded < 3){
547
                                gpxAux=new GeneralPathX();
548
                                gpxAux.moveTo(pAnt.getX(),pAnt.getY());
549
                                gpxAux.lineTo(firstPoint.getX(),firstPoint.getY());
550
                                geom1=geomFactory.createPolyline2D(gpxAux);
551
                                if (geom1.intersects(rect)|| finish){
552
                                        newGp.lineTo(p.getX(), p.getY());
553
                                        newGp.lineTo(pLast.getX(),pLast.getY());
554
                                }else{
555
                                        newGp.lineTo(pLast.getX(), pLast.getY());
556
                                }
557
                        //}
558
                        newGp.closePath();
559
                        break;
560
                } //end switch
561

    
562
                theIterator.next();
563
            } //end while loop
564
            Geometry shp = null;
565
            switch (geometryCloned.getType())
566
            {
567
                case Geometry.TYPES.POINT: //Tipo punto
568
                case Geometry.TYPES.POINT + Geometry.TYPES.Z:
569
                    shp = new org.gvsig.fmap.geom.primitive.Point2D(pLast.getX(), pLast.getY());
570
                    break;
571

    
572
                case Geometry.TYPES.CURVE:
573
                case Geometry.TYPES.CURVE + Geometry.TYPES.Z:
574
                    shp = new Curve2D(newGp);
575
                    break;
576
                case Geometry.TYPES.SURFACE:
577
                case Geometry.TYPES.SURFACE + Geometry.TYPES.Z:
578
                case Geometry.TYPES.CIRCLE:
579
                case Geometry.TYPES.ELLIPSE:
580
                    shp = new Surface2D(newGp);
581
                    break;
582
            }
583
            return shp;
584

    
585

    
586
                    /////////////////////
587
            //}else if (geometryCloned.getGeometryType() == FShape.LINE){
588

    
589
            //}
590

    
591

    
592

    
593

    
594
    /*        if (geometryCloned instanceof FGeometryCollection){
595
                    IGeometry[] geometries=((FGeometryCollection)geometryCloned).getGeometries();
596
                    boolean isSelected=false;
597
                    for (int i=0;i<geometries.length;i++){
598
                            if (geometries[i].intersects(rect) && !isSelected){
599
                                    isSelected=true;
600
                                    Handler[] handlers=geometries[i].getHandlers(IGeometry.SELECTHANDLER);
601

602
                                    GeneralPathX gp1=new GeneralPathX();
603
                                    Point2D pinit1=(Point2D)handlers[0].getPoint().clone();
604
                                    gp1.moveTo(pinit1.getX(),pinit1.getY());
605
                                    System.out.println("Handler inicial = "+pinit1);
606
                                    gp1.lineTo(p.getX(),p.getY());
607
                                    System.out.println("Handler medio = "+p);
608
                                    FPolyline2D poly1=new FPolyline2D(gp1);
609
                                    geom1=ShapeFactory.createGeometry(poly1);
610

611
                                    GeneralPathX gp2=new GeneralPathX();
612
                                    gp2.moveTo(p.getX(),p.getY());
613
                                    System.out.println("Handler medio = "+p);
614
                                    Point2D pEnd=(Point2D)handlers[1].getPoint().clone();
615
                                    gp2.lineTo(pEnd.getX(),pEnd.getY());
616
                                    System.out.println("Handler final = "+pEnd);
617
                                    FPolyline2D poly2=new FPolyline2D(gp2);
618
                                    geom2=ShapeFactory.createGeometry(poly2);
619

620
                                    ArrayList geomsAux=new ArrayList();
621
                                    geometries[i]=geom1;
622
                                    for (int j=i;j<geometries.length;j++){
623
                                            geomsAux.add(geometries[j]);
624
                                    }
625

626
                                    if (i<geometries.length-1){
627
                                            geometries[i+1]=geom2;
628
                                            Handler[] hands=((IGeometry)geom1).getHandlers(IGeometry.SELECTHANDLER);
629
                                            for (int h=0;h<hands.length;h++)
630
                                            System.out.println("Handlers New Geometry = "+hands[h].getPoint());
631
                                            Handler[] hands2=((IGeometry)geom2).getHandlers(IGeometry.SELECTHANDLER);
632
                                            for (int h=0;h<hands2.length;h++)
633
                                            System.out.println("Handlers New Geometry = "+hands2[h].getPoint());
634
                                    }else{
635
                                            geometryCloned=new FGeometryCollection(geometries);
636
                                            ((FGeometryCollection)geometryCloned).addGeometry(geom2);
637
                                    }
638
                                    for (int j=i+1;j<geometries.length;j++){
639
                                            if ((j-i)<geomsAux.size()-1){
640
                                                geometries[j+1]=(IGeometry)geomsAux.get(j-i);
641
                                        }else{
642
                                                geometryCloned=new FGeometryCollection(geometries);
643
                                                ((FGeometryCollection)geometryCloned).addGeometry((IGeometry)geomsAux.get(j-i));
644

645
                                        }
646
                                    }
647
                            }
648

649
                    }
650
            }
651
            return geometryCloned;
652
*/
653
    }
654
    private Geometry addVertexGC(BaseMultiPrimitive gc,Point2D p,Rectangle2D rect) {
655
            Geometry[] geoms=gc.getGeometries();
656
            int pos=-1;
657
            for (int i=0;i<geoms.length;i++) {
658
                    if (geoms[i].intersects(rect)) {
659
                            pos=i;
660
                    }
661
            }
662
            ArrayList newGeoms=new ArrayList();
663
            for (int i=0;i<pos;i++) {
664
                    newGeoms.add(geoms[i]);
665
            }
666
            if (pos!=-1) {
667
            GeneralPathX gpx1=new GeneralPathX();
668
            GeneralPathX gpx2=new GeneralPathX();
669
            Handler[] handlers=geoms[pos].getHandlers(Geometry.SELECTHANDLER);
670
            Point2D p1=handlers[0].getPoint();
671
            Point2D p2=p;
672
            Point2D p3=handlers[handlers.length-1].getPoint();
673
            gpx1.moveTo(p1.getX(),p1.getY());
674
            gpx1.lineTo(p2.getX(),p2.getY());
675
            gpx2.moveTo(p2.getX(),p2.getY());
676
            gpx2.lineTo(p3.getX(),p3.getY());
677
            newGeoms.add(geomFactory.createPolyline2D(gpx1));
678
            newGeoms.add(geomFactory.createPolyline2D(gpx2));
679
            for (int i=pos+1;i<geoms.length;i++) {
680
                    newGeoms.add(geoms[i]);
681
            }
682
            return new BaseMultiPrimitive2D((Geometry[])newGeoms.toArray(new Geometry[0]));
683
            }else {
684
                    return null;
685
            }
686
    }
687
        public String getName() {
688
                return PluginServices.getText(this,"edit_vertex_");
689
        }
690
        private void selectHandler(double x, double y) {
691
                Point2D firstPoint = new Point2D.Double(x, y);
692
                VectorialLayerEdited vle = getVLE();
693
                FeatureStore featureStore=null;
694
                try {
695
                        featureStore = vle.getFeatureStore();
696

    
697
                Iterator iterator=((FeatureSelection)featureStore.getSelection()).iterator();
698
                double tam = getCadToolAdapter().getMapControl().getViewPort()
699
                                .toMapDistance(MapControl.tolerance);
700
                Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam,
701
                                firstPoint.getY() - tam, tam * 2, tam * 2);
702
                while (iterator.hasNext()) {
703
                        Feature feature = (Feature) iterator.next();
704

    
705
                        boolean isSelectedHandler = false;
706
                        Geometry geometry = getSelectedGeometry();
707
                        if (geometry != null) {
708
                                Handler[] handlers = geometry
709
                                                .getHandlers(Geometry.SELECTHANDLER);
710
                                for (int h = 0; h < handlers.length; h++) {
711
                                        if (handlers[h].getPoint().distance(firstPoint) < tam) {
712
                                                numSelect = h;
713
                                                isSelectedHandler = true;
714
                                        }
715
                                }
716

    
717
                                if (!isSelectedHandler) {
718
                                        boolean isSelectedGeometry = false;
719
                                        if (geometry.intersects(rect)) { // , 0.1)){
720
                                                isSelectedGeometry = true;
721
                                        }
722
                                        if (isSelectedGeometry && addVertex) {
723
                                                Feature feat = (Feature)((FeatureSelection)featureStore.getSelection()).iterator().next();
724
                                                Point2D posVertex = new Point2D.Double(x, y);
725
                                                Geometry geom1=((Geometry)feat.getDefaultGeometry()).cloneGeometry();
726
                                                Geometry geom=null;
727
                                                if (geom1 instanceof BaseMultiPrimitive) {
728
                                                        geom = addVertexGC((BaseMultiPrimitive)geom1, posVertex, rect);
729
                                                }else {
730
                                                        geom = addVertex(geom1, posVertex, rect);
731
                                                }
732
                                                if (geom!=null) {
733
                                                        EditableFeature eFeature=feature.getEditable();
734
                                                        eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(), geom);
735
                                                        featureStore.update(eFeature);
736
                                                        Handler[] newHandlers = geom
737
                                                        .getHandlers(Geometry.SELECTHANDLER);
738
                                                        for (int h = 0; h < newHandlers.length; h++) {
739
                                                                if (newHandlers[h].getPoint().distance(
740
                                                                                posVertex) < tam) {
741
                                                                        numSelect = h;
742
                                                                        isSelectedHandler = true;
743
                                                                }
744
                                                        }
745

    
746
                                                        clearSelection();
747
                                                }
748
                                        }
749
                                }
750
                        }
751
                }
752
                } catch (ReadException e1) {
753
                        // TODO Auto-generated catch block
754
                        e1.printStackTrace();
755
                } catch (DataException e) {
756
                        // TODO Auto-generated catch block
757
                        e.printStackTrace();
758
                }
759

    
760
        }
761

    
762
        public String toString() {
763
                return "_editvertex";
764
        }
765

    
766
        public boolean isApplicable(int shapeType) {
767
                switch (shapeType) {
768
                case Geometry.TYPES.POINT:
769
                case Geometry.TYPES.MULTIPOINT:
770
                        return false;
771
                }
772
                return true;
773
        }
774

    
775

    
776
}