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 4118 caballero
/* 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 15668 vcaballero
import java.awt.Component;
44 4118 caballero
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46 4313 fjp
import java.awt.event.InputEvent;
47 4118 caballero
import java.awt.geom.PathIterator;
48
import java.awt.geom.Point2D;
49 4365 caballero
import java.awt.geom.Rectangle2D;
50 4584 caballero
import java.util.ArrayList;
51 4118 caballero
52 15668 vcaballero
import javax.swing.JOptionPane;
53
54 21911 jmvivo
import org.gvsig.fmap.data.ReadException;
55 23086 vcaballero
import org.gvsig.fmap.data.feature.Feature;
56
import org.gvsig.fmap.data.feature.FeatureCollection;
57
import org.gvsig.fmap.data.feature.FeatureStore;
58 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
59
import org.gvsig.fmap.geom.GeometryFactory;
60
import org.gvsig.fmap.geom.GeometryManager;
61 23086 vcaballero
import org.gvsig.fmap.geom.aggregate.BaseMultiPrimitive;
62 21668 vcaballero
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 4584 caballero
import com.iver.andami.PluginServices;
71 12739 caballero
import com.iver.andami.messages.NotificationManager;
72 4118 caballero
import com.iver.cit.gvsig.CADExtension;
73
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
74 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
75 4118 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext;
76
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext.EditVertexCADToolState;
77 4365 caballero
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
78 4118 caballero
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 4365 caballero
        private boolean addVertex=false;
90 21668 vcaballero
        private GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
91
92 4118 caballero
    /**
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 4313 fjp
    public void transition(double x, double y, InputEvent event) {
110 6159 caballero
        _fsm.addPoint(x, y, event);
111 4118 caballero
    }
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 5735 caballero
            _fsm.addValue(d);
118 4118 caballero
    }
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 5735 caballero
    public void transition(String s) throws CommandException {
124 4892 caballero
            if (!super.changeCommand(s)){
125
                    _fsm.addOption(s);
126
            }
127 4118 caballero
    }
128
129
    /**
130
     * DOCUMENT ME!
131
     */
132
    public void selection() {
133 23086 vcaballero
            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 5071 caballero
            CADExtension.setCADTool("_selection",false);
142 4584 caballero
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
143 5071 caballero
                "_editvertex");
144 4118 caballero
        }
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 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
155
            selectHandler(x,y);
156
            addVertex=false;
157 4118 caballero
    }
158
159 21668 vcaballero
    private Geometry getSelectedGeometry() {
160 23086 vcaballero
            FeatureCollection selection=null;
161
            try {
162
                    selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
163 4313 fjp
164 23086 vcaballero
                    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 4313 fjp
177 23086 vcaballero
            return null;
178
    }
179
180 4313 fjp
        /**
181 4118 caballero
     * 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 12148 caballero
        drawVertex(g,getCadToolAdapter().getMapControl().getViewPort());
190 4118 caballero
    }
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 4724 caballero
        VectorialLayerEdited vle=getVLE();
201 23086 vcaballero
        FeatureStore featureStore=vle.getFeatureStore();
202
        FeatureCollection selection=(FeatureCollection)featureStore.getSelection();
203
        Feature feature=null;
204
        Geometry ig=null;
205 4118 caballero
        Handler[] handlers=null;
206 23086 vcaballero
        if (selection.size()==1){
207
                                feature =  selection.getFeature(0);
208
                        ig=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
209
                handlers=ig.getHandlers(Geometry.SELECTHANDLER);
210 4118 caballero
                numHandlers=handlers.length;
211
                if (numHandlers ==0){
212
                        try {
213 23086 vcaballero
                                featureStore.delete(feature);
214
//                                        vea.removeRow(row.getIndex(),getName(),EditionEvent.GRAPHIC);
215 21668 vcaballero
                                } catch (ReadException e) {
216 12739 caballero
                                        NotificationManager.addError(e.getMessage(),e);
217 15668 vcaballero
                                }
218 4118 caballero
                }
219 15668 vcaballero
        }else{
220
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"hay_mas_de_una_geometria_seleccionada"));
221 4118 caballero
        }
222
        int dif=1;//En el caso de ser pol?gono.
223 23086 vcaballero
        if (ig instanceof BaseMultiPrimitive){
224 4118 caballero
                dif=2;
225
        }
226
        if (status.equals("EditVertex.SelectVertexOrDelete")){
227 9121 caballero
                if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.nextvertex")) || s.equals(PluginServices.getText(this,"next"))){
228 4118 caballero
                        numSelect=numSelect-dif;
229
                        if (numSelect<0){
230
                                numSelect=numHandlers-1+(numSelect+1);
231
                        }
232 9121 caballero
           }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.previousvertex")) || s.equals(PluginServices.getText(this,"previous"))){
233 4118 caballero
                           numSelect=numSelect+dif;
234
                               if (numSelect>(numHandlers-1)){
235
                                       numSelect=numSelect-(numHandlers);
236
                               }
237
238 9121 caballero
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.delvertex")) || s.equals(PluginServices.getText(this,"del"))){
239 4118 caballero
                        if (handlers!=null){
240 21668 vcaballero
                                Geometry newGeometry=null;
241 23086 vcaballero
                                if (ig instanceof BaseMultiPrimitive) {
242
                                        newGeometry=removeVertexGC((BaseMultiPrimitive)ig,handlers[numSelect]);
243 5884 caballero
                                }else {
244 13770 caballero
                                        newGeometry=removeVertex(ig,handlers,numSelect);
245 5884 caballero
                                }
246 9259 caballero
                                //numSelect=0;
247 23086 vcaballero
                                feature.editing();
248
                                feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(),ig);
249
//                                IRow newRow=new DefaultFeature(newGeometry,row.getAttributes(),row.getID());
250 4118 caballero
                                try {
251 23086 vcaballero
                                                featureStore.update(feature);
252
//                                        vea.modifyRow(row.getIndex(),newRow,getName(),EditionEvent.GRAPHIC);
253 10626 caballero
                                                clearSelection();
254 23086 vcaballero
                                } catch (ReadException e) {
255 12739 caballero
                                                NotificationManager.addError(e.getMessage(),e);
256 15668 vcaballero
                                        }
257 23086 vcaballero
//                                        vle.addSelectionCache(new DefaultRowEdited(newRow,
258
//                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
259 4724 caballero
260 9121 caballero
//                                        vle.refreshSelectionCache(new Point2D.Double(0,0),getCadToolAdapter());
261
//                                        refresh();
262 4724 caballero
263 4118 caballero
                        }
264 9121 caballero
                }else if(s.equalsIgnoreCase(PluginServices.getText(this,"EditVertexCADTool.addvertex")) || s.equals(PluginServices.getText(this,"add"))){
265 6159 caballero
                            addVertex=true;
266
                    }
267 4118 caballero
        }
268
    }
269 12148 caballero
    private void drawVertex(Graphics g,ViewPort vp){
270 4584 caballero
                ArrayList selectedRows=getSelectedRows();
271
            for (int i = 0; i<selectedRows.size();
272
                                 i++) {
273
                    DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRows
274
                                        .get(i)).getLinkedRow();
275 21668 vcaballero
                        Geometry ig = fea.getGeometry().cloneGeometry();
276 4118 caballero
                        if (ig == null) continue;
277 12148 caballero
                        ig.drawInts((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
278 21668 vcaballero
                        Handler[] handlers=ig.getHandlers(Geometry.SELECTHANDLER);
279 12148 caballero
                        if (numSelect>=handlers.length)
280
                                numSelect=0;
281
                        FGraphicUtilities.DrawVertex((Graphics2D)g,vp.getAffineTransform(),handlers[numSelect]);
282 4118 caballero
                }
283
        }
284 4365 caballero
285 4118 caballero
    /* (non-Javadoc)
286
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
287
     */
288
    public void addValue(double d) {
289
    }
290 21668 vcaballero
    private Geometry removeVertex(Geometry gp,Handler[] handlers,int numHandler) {
291 4118 caballero
        GeneralPathX newGp = new GeneralPathX();
292
        double[] theData = new double[6];
293
294 4880 fjp
        PathIterator theIterator;
295 4118 caballero
        int theType;
296
        int numParts = 0;
297
298
        Point2D ptSrc = new Point2D.Double();
299
        boolean bFirst = false;
300
301 21668 vcaballero
        theIterator = gp.getPathIterator(null, Converter.FLATNESS);
302 4118 caballero
        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 10427 caballero
                        theIterator.next();
310 4118 caballero
                        continue;
311
                }
312
            switch (theType) {
313
314
                case PathIterator.SEG_MOVETO:
315
                    numParts++;
316
                    ptSrc.setLocation(theData[0], theData[1]);
317 13770 caballero
                    if (ptSrc.equals(handlers[numHandler].getPoint())){
318 4118 caballero
                            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 13770 caballero
                    if (ptSrc.equals(handlers[numHandler].getPoint())){
330 4118 caballero
                            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 21668 vcaballero
        Geometry shp = null;
358
        switch (gp.getType())
359 4118 caballero
        {
360 21668 vcaballero
            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 4118 caballero
                break;
364
365 21668 vcaballero
            case Geometry.TYPES.CURVE:
366
            case Geometry.TYPES.CURVE + Geometry.TYPES.Z:
367
                shp = new Curve2D(newGp);
368 4118 caballero
                break;
369 21668 vcaballero
            case Geometry.TYPES.SURFACE:
370
            case Geometry.TYPES.SURFACE + Geometry.TYPES.Z:
371
                shp = new Surface2D(newGp);
372 4118 caballero
                break;
373
        }
374 21668 vcaballero
        Geometry ig=shp;
375 9121 caballero
        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 4118 caballero
    }
382 5884 caballero
383 21668 vcaballero
    private Geometry removeVertexGC(GeometryCollection gc,Handler handler) {
384
        Geometry[] geoms=gc.getGeometries();
385 5884 caballero
            ArrayList geomsAux=new ArrayList();
386
        int pos=-1;
387
            for (int i=0;i<geoms.length;i++) {
388 21668 vcaballero
                    Handler[] handlers=geoms[i].getHandlers(Geometry.SELECTHANDLER);
389 5884 caballero
                    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 21668 vcaballero
                    Handler[] handlers=((Geometry)geomsAux.get(i)).getHandlers(Geometry.SELECTHANDLER);
401 5884 caballero
                    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 21668 vcaballero
        newGeoms.add(geomFactory.createPolyline2D(gpx));
422 5884 caballero
        for (int i=pos+numGeomsAux;i<geoms.length;i++) {
423
                newGeoms.add(geoms[i]);
424
        }
425
426 21668 vcaballero
            return new GeometryCollection((Geometry[])newGeoms.toArray(new Geometry[0]));
427 5884 caballero
    }
428
429
430
431 21668 vcaballero
    private Geometry addVertex(Geometry geome,Point2D p,Rectangle2D rect) {
432
            Geometry geometryCloned=geome.cloneGeometry();
433
            Geometry geom1=null;
434 4522 caballero
            GeneralPathX gpxAux;
435 5216 caballero
            boolean finish=false;
436 4522 caballero
            //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 4880 fjp
            PathIterator theIterator;
445 4522 caballero
            int theType;
446
            int numParts = 0;
447
            Point2D pLast=new Point2D.Double();
448
            Point2D pAnt = new Point2D.Double();
449 4846 caballero
            Point2D firstPoint=null;
450 21668 vcaballero
            theIterator = geome.getPathIterator(null,Converter.FLATNESS); //, flatness);
451 4522 caballero
            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 4846 caballero
                            if (numParts==0)
458
                                    firstPoint=(Point2D)pLast.clone();
459
                            numParts++;
460 4522 caballero
461
                            gpxAux=new GeneralPathX();
462
                            gpxAux.moveTo(pAnt.getX(),pAnt.getY());
463
                            gpxAux.lineTo(pLast.getX(),pLast.getY());
464 21668 vcaballero
                            geom1=geomFactory.createPolyline2D(gpxAux);
465 4522 caballero
                            if (geom1.intersects(rect)){
466 5216 caballero
                                    finish=true;
467
                                    newGp.moveTo(pLast.getX(), pLast.getY());
468
                                    //newGp.lineTo(pLast.getX(),pLast.getY());
469 4522 caballero
                            }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 21668 vcaballero
                            geom1=geomFactory.createPolyline2D(gpxAux);
482 4522 caballero
                            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 4846 caballero
                        //if (numSegmentsAdded < 3){
504
                                gpxAux=new GeneralPathX();
505
                                gpxAux.moveTo(pAnt.getX(),pAnt.getY());
506
                                gpxAux.lineTo(firstPoint.getX(),firstPoint.getY());
507 21668 vcaballero
                                geom1=geomFactory.createPolyline2D(gpxAux);
508 5216 caballero
                                if (geom1.intersects(rect)|| finish){
509 4846 caballero
                                        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 4522 caballero
                        newGp.closePath();
516
                        break;
517
                } //end switch
518
519
                theIterator.next();
520
            } //end while loop
521 21668 vcaballero
            Geometry shp = null;
522
            switch (geometryCloned.getType())
523 4522 caballero
            {
524 21668 vcaballero
                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 4522 caballero
                    break;
528
529 21668 vcaballero
                case Geometry.TYPES.CURVE:
530
                case Geometry.TYPES.CURVE + Geometry.TYPES.Z:
531
                    shp = new Curve2D(newGp);
532 4522 caballero
                    break;
533 21668 vcaballero
                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 4522 caballero
                    break;
539
            }
540 21668 vcaballero
            return shp;
541 4522 caballero
542
543
                    /////////////////////
544
            //}else if (geometryCloned.getGeometryType() == FShape.LINE){
545
546
            //}
547
548
549
550
551
    /*        if (geometryCloned instanceof FGeometryCollection){
552 4365 caballero
                    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 4118 caballero

559 4365 caballero
                                    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 4522 caballero
*/
610 4365 caballero
    }
611 21668 vcaballero
    private Geometry addVertexGC(GeometryCollection gc,Point2D p,Rectangle2D rect) {
612
            Geometry[] geoms=gc.getGeometries();
613 5884 caballero
            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 21668 vcaballero
            Handler[] handlers=geoms[pos].getHandlers(Geometry.SELECTHANDLER);
627 5884 caballero
            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 21668 vcaballero
            newGeoms.add(geomFactory.createPolyline2D(gpx1));
635
            newGeoms.add(geomFactory.createPolyline2D(gpx2));
636 5884 caballero
            for (int i=pos+1;i<geoms.length;i++) {
637
                    newGeoms.add(geoms[i]);
638
            }
639 21668 vcaballero
            return new GeometryCollection((Geometry[])newGeoms.toArray(new Geometry[0]));
640 5884 caballero
            }else {
641
                    return null;
642
            }
643
    }
644 4118 caballero
        public String getName() {
645 4584 caballero
                return PluginServices.getText(this,"edit_vertex_");
646 4118 caballero
        }
647 4365 caballero
        private void selectHandler(double x, double y) {
648
                Point2D firstPoint = new Point2D.Double(x, y);
649 4846 caballero
                VectorialLayerEdited vle = getVLE();
650 5985 caballero
                VectorialEditableAdapter vea=vle.getVEA();
651 4846 caballero
                ArrayList selectedRows = vle.getSelectedRow();
652 4365 caballero
                double tam = getCadToolAdapter().getMapControl().getViewPort()
653
                                .toMapDistance(SelectionCADTool.tolerance);
654 4846 caballero
                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 21668 vcaballero
                        Geometry geometry = getSelectedGeometry();
659 4846 caballero
                        if (geometry != null) {
660
                                Handler[] handlers = geometry
661 21668 vcaballero
                                                .getHandlers(Geometry.SELECTHANDLER);
662 4846 caballero
                                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 4365 caballero
669 4846 caballero
                                if (!isSelectedHandler) {
670
                                        boolean isSelectedGeometry = false;
671
                                        try {
672 4365 caballero
673 4971 caballero
                                                //VectorialEditableAdapter vea = getCadToolAdapter()
674
                                                //                .getVectorialAdapter();
675
                                                //String strEPSG = getCadToolAdapter().getMapControl()
676
                                                //                .getViewPort().getProjection().getAbrev()
677
                                                //                .substring(5);
678 4846 caballero
                                                //IRowEdited[] feats = vea.getFeatures(rect, strEPSG);
679 4365 caballero
680 4846 caballero
                                                //for (int i = 0; i < feats.length; i++) {
681
                                                        if (geometry.intersects(rect)) { // , 0.1)){
682
                                                                isSelectedGeometry = true;
683 4365 caballero
                                                        }
684 4846 caballero
                                                //}
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 21668 vcaballero
                                                        Geometry geom1=fea.getGeometry().cloneGeometry();
693
                                                        Geometry geom=null;
694
                                                        if (geom1 instanceof GeometryCollection) {
695
                                                                geom = addVertexGC((GeometryCollection)geom1, posVertex, rect);
696 5884 caballero
                                                        }else {
697
                                                                geom = addVertex(geom1, posVertex, rect);
698
                                                        }
699
                                                        if (geom!=null) {
700 4846 caballero
                                                        DefaultFeature df = new DefaultFeature(geom, fea
701 5884 caballero
                                                                        .getAttributes(),row.getID());
702 5985 caballero
                                                        vea.modifyRow(row.getIndex(), df,
703 5184 caballero
                                                                        PluginServices.getText(this,"add_vertex"),EditionEvent.GRAPHIC);
704 4365 caballero
705 4846 caballero
                                                        Handler[] newHandlers = geom
706 21668 vcaballero
                                                                        .getHandlers(Geometry.SELECTHANDLER);
707 4846 caballero
                                                        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 4365 caballero
                                                        }
714 4846 caballero
715 5884 caballero
                                                        clearSelection();
716 8938 caballero
                                                        vle.addSelectionCache(new DefaultRowEdited(df,
717 5985 caballero
                                                                        IRowEdited.STATUS_MODIFIED, row.getIndex()));
718 5884 caballero
                                                        }
719 4365 caballero
                                                }
720 10626 caballero
                                        } catch (ValidateRowException e) {
721 12739 caballero
                                                NotificationManager.addError(e.getMessage(),e);
722 21668 vcaballero
                                        } catch (ReadException e) {
723 12739 caballero
                                                NotificationManager.addError(e.getMessage(),e);
724 15668 vcaballero
                                        }
725 4846 caballero
                                }
726 4724 caballero
                        }
727 4365 caballero
                }
728
729
        }
730
731 4892 caballero
        public String toString() {
732
                return "_editvertex";
733
        }
734 4365 caballero
735 5170 caballero
        public boolean isApplicable(int shapeType) {
736
                switch (shapeType) {
737 21668 vcaballero
                case Geometry.TYPES.POINT:
738
                case Geometry.TYPES.MULTIPOINT:
739 5170 caballero
                        return false;
740
                }
741
                return true;
742
        }
743 4892 caballero
744 5170 caballero
745 4118 caballero
}