Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / EditVertexCADTool.java @ 23657

History | View | Annotate | Download (27.9 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.data.DataException;
56
import org.gvsig.fmap.data.ReadException;
57
import org.gvsig.fmap.data.feature.Feature;
58
import org.gvsig.fmap.data.feature.FeatureCollection;
59
import org.gvsig.fmap.data.feature.FeatureStore;
60
import org.gvsig.fmap.geom.Geometry;
61
import org.gvsig.fmap.geom.GeometryFactory;
62
import org.gvsig.fmap.geom.GeometryManager;
63
import org.gvsig.fmap.geom.aggregate.BaseMultiPrimitive;
64
import org.gvsig.fmap.geom.handler.Handler;
65
import org.gvsig.fmap.geom.operation.DrawInts;
66
import org.gvsig.fmap.geom.operation.DrawOperationContext;
67
import org.gvsig.fmap.geom.operation.GeometryOperationException;
68
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
69
import org.gvsig.fmap.geom.primitive.Curve2D;
70
import org.gvsig.fmap.geom.primitive.GeneralPathX;
71
import org.gvsig.fmap.geom.primitive.Surface2D;
72
import org.gvsig.fmap.geom.util.Converter;
73
import org.gvsig.fmap.mapcontext.ViewPort;
74
import org.gvsig.fmap.mapcontext.rendering.symbols.FGraphicUtilities;
75
import org.gvsig.fmap.mapcontrol.MapControl;
76

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

    
86

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

    
99
    /**
100
     * Crea un nuevo PolylineCADTool.
101
     */
102
    public EditVertexCADTool() {
103
    }
104

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

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

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

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

    
136
    /**
137
     * DOCUMENT ME!
138
     */
139
    public void selection() {
140
            FeatureCollection selection=null;
141
                try {
142
                        selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
143
                } catch (ReadException e) {
144
                        // TODO Auto-generated catch block
145
                        e.printStackTrace();
146
                }
147
        if (selection.size() == 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
    }
153

    
154
    /**
155
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
156
     * editableFeatureSource que ya estar? creado.
157
     *
158
     * @param x par?metro x del punto que se pase en esta transici?n.
159
     * @param y par?metro y del punto que se pase en esta transici?n.
160
     */
161
    public void addPoint(double x, double y,InputEvent event) {
162
            selectHandler(x,y);
163
            addVertex=false;
164
    }
165

    
166
    private Geometry getSelectedGeometry() {
167
            FeatureCollection selection=null;
168
            try {
169
                    selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
170

    
171
                    Feature feature=null;
172
                    Geometry ig=null;
173
                    if (selection.size()==1){
174
                            feature=selection.getFeature(0);
175
                            //row = getCadToolAdapter().getVectorialAdapter().getRow(selection.nextSetBit(0));
176
                            ig=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
177
                            return ig;
178
                    }
179
            } catch (ReadException e) {
180
                    // TODO Auto-generated catch block
181
                    e.printStackTrace();
182
            }
183

    
184
            return null;
185
    }
186

    
187
        /**
188
     * M?todo para dibujar la lo necesario para el estado en el que nos
189
     * encontremos.
190
     *
191
     * @param g Graphics sobre el que dibujar.
192
     * @param x par?metro x del punto que se pase para dibujar.
193
     * @param y par?metro x del punto que se pase para dibujar.
194
     */
195
    public void drawOperation(Graphics g, double x, double y) {
196
        drawVertex(g,getCadToolAdapter().getMapControl().getViewPort());
197
    }
198

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

    
253
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.delvertex")) || s.equals(PluginServices.getText(this,"del"))){
254
                        if (handlers!=null){
255
                                Geometry newGeometry=null;
256
                                if (ig instanceof BaseMultiPrimitive) {
257
                                        newGeometry=removeVertexGC((BaseMultiPrimitive)ig,handlers[numSelect]);
258
                                }else {
259
                                        newGeometry=removeVertex(ig,handlers,numSelect);
260
                                }
261
                                //numSelect=0;
262
                                try {
263
                                feature.editing();
264
                                feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(),ig);
265
//                                IRow newRow=new DefaultFeature(newGeometry,row.getAttributes(),row.getID());
266

    
267
                                                featureStore.update(feature);
268
//                                        vea.modifyRow(row.getIndex(),newRow,getName(),EditionEvent.GRAPHIC);
269
                                                clearSelection();
270
                                } catch (ReadException e) {
271
                                                NotificationManager.addError(e.getMessage(),e);
272
                                        }
273
//                                        vle.addSelectionCache(new DefaultRowEdited(newRow,
274
//                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
275
 catch (DataException e) {
276
                                                // TODO Auto-generated catch block
277
                                                e.printStackTrace();
278
                                        }
279

    
280
//                                        vle.refreshSelectionCache(new Point2D.Double(0,0),getCadToolAdapter());
281
//                                        refresh();
282

    
283
                        }
284
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.addvertex")) || s.equals(PluginServices.getText(this,"add"))){
285
                            addVertex=true;
286
                    }
287
        }
288
    }
289
    private void drawVertex(Graphics g,ViewPort vp){
290
            VectorialLayerEdited vle=getVLE();
291
            Iterator iterator=null;
292
                try {
293
                        iterator = vle.getFeatureStore().getSelection().iterator();
294
                } catch (ReadException e1) {
295
                        // TODO Auto-generated catch block
296
                        e1.printStackTrace();
297
                }
298
                while (iterator.hasNext()) {
299
                        Feature feature = (Feature) iterator.next();
300

    
301
//                }
302
//            ArrayList selectedRows=getSelectedRows();
303
//            for (int i = 0; i<selectedRows.size();
304
//                                 i++) {
305
//                    DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRows
306
//                                        .get(i)).getLinkedRow();
307
                        Geometry ig = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
308
                        if (ig == null) continue;
309
                        DrawOperationContext doc=new DrawOperationContext();
310
                        doc.setGraphics((Graphics2D)g);
311
                        doc.setViewPort(vp);
312
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
313
                try {
314
                                ig.invokeOperation(DrawInts.CODE,doc);
315
                        } catch (GeometryOperationNotSupportedException e) {
316
                                e.printStackTrace();
317
                        } catch (GeometryOperationException e) {
318
                                e.printStackTrace();
319
                        }
320
//                        ig.drawInts((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
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 BaseMultiPrimitive((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 BaseMultiPrimitive((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
                } catch (ReadException e1) {
697
                        // TODO Auto-generated catch block
698
                        e1.printStackTrace();
699
                }
700
                Iterator iterator=featureStore.getSelection().iterator();
701
//                ArrayList selectedRows = vle.getSelectedRow();
702

    
703
                double tam = getCadToolAdapter().getMapControl().getViewPort()
704
                                .toMapDistance(MapControl.tolerance);
705
                Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam,
706
                                firstPoint.getY() - tam, tam * 2, tam * 2);
707
                while (iterator.hasNext()) {
708
                        Feature feature = (Feature) iterator.next();
709

    
710
//                }
711
//                if (selectedRows.size() > 0) {
712
                        boolean isSelectedHandler = false;
713
                        Geometry geometry = getSelectedGeometry();
714
                        if (geometry != null) {
715
                                Handler[] handlers = geometry
716
                                                .getHandlers(Geometry.SELECTHANDLER);
717
                                for (int h = 0; h < handlers.length; h++) {
718
                                        if (handlers[h].getPoint().distance(firstPoint) < tam) {
719
                                                numSelect = h;
720
                                                isSelectedHandler = true;
721
                                        }
722
                                }
723

    
724
                                if (!isSelectedHandler) {
725
                                        boolean isSelectedGeometry = false;
726
                                        try {
727

    
728
                                                //VectorialEditableAdapter vea = getCadToolAdapter()
729
                                                //                .getVectorialAdapter();
730
                                                //String strEPSG = getCadToolAdapter().getMapControl()
731
                                                //                .getViewPort().getProjection().getAbrev()
732
                                                //                .substring(5);
733
                                                //IRowEdited[] feats = vea.getFeatures(rect, strEPSG);
734

    
735
                                                //for (int i = 0; i < feats.length; i++) {
736
                                                        if (geometry.intersects(rect)) { // , 0.1)){
737
                                                                isSelectedGeometry = true;
738
                                                        }
739
                                                //}
740
                                                if (isSelectedGeometry && addVertex) {
741
                                                        Feature feat = (Feature)featureStore.getSelection().iterator().next();
742
//                                                        DefaultFeature fea = null;
743
//                                                        DefaultRowEdited row = null;
744
//                                                        row = (DefaultRowEdited) selectedRows.get(0);
745
//                                                        fea = (DefaultFeature) row.getLinkedRow();
746
                                                        Point2D posVertex = new Point2D.Double(x, y);
747
                                                        Geometry geom1=((Geometry)feat.getDefaultGeometry()).cloneGeometry();
748
                                                        Geometry geom=null;
749
                                                        if (geom1 instanceof BaseMultiPrimitive) {
750
                                                                geom = addVertexGC((BaseMultiPrimitive)geom1, posVertex, rect);
751
                                                        }else {
752
                                                                geom = addVertex(geom1, posVertex, rect);
753
                                                        }
754
                                                        if (geom!=null) {
755
//                                                        DefaultFeature df = new DefaultFeature(geom, fea
756
//                                                                        .getAttributes(),row.getID());
757
                                                        feat.editing();
758
                                                        feat.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(), geom);
759
                                                        featureStore.update(feat);
760
//                                                        vea.modifyRow(row.getIndex(), df,
761
//                                                                        PluginServices.getText(this,"add_vertex"),EditionEvent.GRAPHIC);
762

    
763
                                                        Handler[] newHandlers = geom
764
                                                                        .getHandlers(Geometry.SELECTHANDLER);
765
                                                        for (int h = 0; h < newHandlers.length; h++) {
766
                                                                if (newHandlers[h].getPoint().distance(
767
                                                                                posVertex) < tam) {
768
                                                                        numSelect = h;
769
                                                                        isSelectedHandler = true;
770
                                                                }
771
                                                        }
772

    
773
                                                        clearSelection();
774
//                                                        vle.addSelectionCache(new DefaultRowEdited(df,
775
//                                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
776
                                                        }
777
                                                }
778
                                        } catch (ReadException e) {
779
                                                NotificationManager.addError(e.getMessage(),e);
780
                                        } catch (DataException e) {
781
                                                // TODO Auto-generated catch block
782
                                                e.printStackTrace();
783
                                        }
784
                                }
785
                        }
786
                }
787

    
788
        }
789

    
790
        public String toString() {
791
                return "_editvertex";
792
        }
793

    
794
        public boolean isApplicable(int shapeType) {
795
                switch (shapeType) {
796
                case Geometry.TYPES.POINT:
797
                case Geometry.TYPES.MULTIPOINT:
798
                        return false;
799
                }
800
                return true;
801
        }
802

    
803

    
804
}