Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / EditVertexCADTool.java @ 4313

History | View | Annotate | Download (12.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.cad.tools;
42

    
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.AffineTransform;
47
import java.awt.geom.PathIterator;
48
import java.awt.geom.Point2D;
49
import java.io.IOException;
50

    
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
53
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
54
import com.iver.cit.gvsig.fmap.core.FPoint2D;
55
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
56
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
57
import com.iver.cit.gvsig.fmap.core.FShape;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.GeneralPathXIterator;
60
import com.iver.cit.gvsig.fmap.core.Handler;
61
import com.iver.cit.gvsig.fmap.core.IFeature;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63
import com.iver.cit.gvsig.fmap.core.IRow;
64
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
65
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
66
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
67
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
68
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
69
import com.iver.cit.gvsig.fmap.layers.FBitSet;
70
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
71
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext;
72
import com.iver.cit.gvsig.gui.cad.tools.smc.EditVertexCADToolContext.EditVertexCADToolState;
73
import com.vividsolutions.jts.geom.Geometry;
74

    
75

    
76
/**
77
 * DOCUMENT ME!
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class EditVertexCADTool extends DefaultCADTool {
82
    private EditVertexCADToolContext _fsm;
83
    private int numSelect=0;
84
        private int numHandlers;
85

    
86
    /**
87
     * Crea un nuevo PolylineCADTool.
88
     */
89
    public EditVertexCADTool() {
90
    }
91

    
92
    /**
93
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
94
     * carga previa a la utilizaci?n de la herramienta.
95
     */
96
    public void init() {
97
        _fsm = new EditVertexCADToolContext(this);
98
    }
99

    
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
102
     */
103
    public void transition(double x, double y, InputEvent event) {
104
        addPoint(x, y);
105
    }
106

    
107
    /* (non-Javadoc)
108
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
109
     */
110
    public void transition(double d) {
111
        //_fsm.addValue(sel,d);
112
    }
113

    
114
    /* (non-Javadoc)
115
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
116
     */
117
    public void transition(String s) {
118
        _fsm.addOption(s);
119
    }
120

    
121
    /**
122
     * DOCUMENT ME!
123
     */
124
    public void selection() {
125
        FBitSet selection = CADExtension.getCADToolAdapter()
126
                                        .getVectorialAdapter().getSelection();
127

    
128
        if (selection.cardinality() == 0) {
129
            CADExtension.setCADTool("selection");
130
            ((SelectionCADTool) CADExtension.getCADToolAdapter().getCadTool()).setNextTool(
131
                "editvertex");
132
        }
133
    }
134

    
135
    /**
136
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
137
     * editableFeatureSource que ya estar? creado.
138
     *
139
     * @param x par?metro x del punto que se pase en esta transici?n.
140
     * @param y par?metro y del punto que se pase en esta transici?n.
141
     */
142
    public void addPoint(double x, double y) {
143
            IGeometry geom = getSelectedGeometry();
144
            Geometry jtsGeom = geom.toJTSGeometry();
145
            
146
                /* IRow newRow=new DefaultFeature(newGeometry,row.getAttributes());
147
                try {
148
                        vea.modifyRow(selection.nextSetBit(0),newRow,getName());
149
                } catch (IOException e) {
150
                        // TODO Auto-generated catch block
151
                        e.printStackTrace();
152
                } catch (DriverIOException e) {
153
                        // TODO Auto-generated catch block
154
                        e.printStackTrace();
155
                } */
156
                getCadToolAdapter().getMapControl().drawMap(false);            
157
            
158
            
159
    }
160

    
161
    private IGeometry getSelectedGeometry() {
162
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
163
        FBitSet selection = vea.getSelection();
164
        IRowEdited row=null;
165
        IGeometry ig=null;
166
        if (selection.cardinality()==1){
167

    
168
                        try {
169
                                row = getCadToolAdapter().getVectorialAdapter().getRow(selection.nextSetBit(0));
170
                        } catch (DriverIOException e) {
171
                                // TODO Auto-generated catch block
172
                                e.printStackTrace();
173
                        } catch (IOException e) {
174
                                // TODO Auto-generated catch block
175
                                e.printStackTrace();
176
                        }
177
                ig=((IFeature)row.getLinkedRow()).getGeometry().cloneGeometry();
178
                return ig;
179
        }
180

    
181
                return null;
182
        }
183

    
184
        /**
185
     * M?todo para dibujar la lo necesario para el estado en el que nos
186
     * encontremos.
187
     *
188
     * @param g Graphics sobre el que dibujar.
189
     * @param x par?metro x del punto que se pase para dibujar.
190
     * @param y par?metro x del punto que se pase para dibujar.
191
     */
192
    public void drawOperation(Graphics g, double x, double y) {
193
        EditVertexCADToolState actualState = ((EditVertexCADToolContext) _fsm).getState();
194
        String status = actualState.getName();
195
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
196
        FBitSet selection = vea.getSelection();
197

    
198
        try {
199
            drawVertex(g, selection,
200
                getCadToolAdapter().getMapControl().getViewPort()
201
                    .getAffineTransform());
202
        } catch (DriverIOException e) {
203
            // TODO Auto-generated catch block
204
            e.printStackTrace();
205
        }
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
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
217
        FBitSet selection = vea.getSelection();
218
        IRowEdited row=null;
219
        IGeometry ig=null;
220
        Handler[] handlers=null;
221
        if (selection.cardinality()==1){
222

    
223
                        try {
224
                                row = getCadToolAdapter().getVectorialAdapter().getRow(selection.nextSetBit(0));
225
                        } catch (DriverIOException e) {
226
                                // TODO Auto-generated catch block
227
                                e.printStackTrace();
228
                        } catch (IOException e) {
229
                                // TODO Auto-generated catch block
230
                                e.printStackTrace();
231
                        }
232
                ig=((IFeature)row.getLinkedRow()).getGeometry().cloneGeometry();
233
                handlers=ig.getHandlers(IGeometry.SELECTHANDLER);
234
                numHandlers=handlers.length;
235
                if (numHandlers ==0){
236
                        try {
237
                                        vea.removeRow(selection.nextSetBit(0),getName());
238
                                } catch (IOException e) {
239
                                        // TODO Auto-generated catch block
240
                                        e.printStackTrace();
241
                                } catch (DriverIOException e) {
242
                                        // TODO Auto-generated catch block
243
                                        e.printStackTrace();
244
                                }
245
                }
246
        }
247
        int dif=1;//En el caso de ser pol?gono.
248
        if (ig instanceof FGeometryCollection){
249
                dif=2;
250
        }
251
        if (status.equals("EditVertex.SelectVertexOrDelete")){
252
                if(s.equals("s") || s.equals("S") || s.equals("Siguiente")){
253
                        numSelect=numSelect-dif;
254
                        if (numSelect<0){
255
                                numSelect=numHandlers-1+(numSelect+1);
256
                        }
257
           }else if(s.equals("a") || s.equals("a") || s.equals("Anterior")){
258
                           numSelect=numSelect+dif;
259
                               if (numSelect>(numHandlers-1)){
260
                                       numSelect=numSelect-(numHandlers);
261
                               }
262

    
263
                }else if(s.equals("e") || s.equals("E") || s.equals("Eliminar")){
264
                        if (handlers!=null){
265
                                IGeometry newGeometry=removeVertex(ig,handlers[numSelect]);
266
                                numSelect=0;
267

    
268
                                IRow newRow=new DefaultFeature(newGeometry,row.getAttributes());
269
                                try {
270
                                                vea.modifyRow(selection.nextSetBit(0),newRow,getName());
271
                                        } catch (IOException e) {
272
                                                // TODO Auto-generated catch block
273
                                                e.printStackTrace();
274
                                        } catch (DriverIOException e) {
275
                                                // TODO Auto-generated catch block
276
                                                e.printStackTrace();
277
                                        }
278
                                        getCadToolAdapter().getMapControl().drawMap(false);
279
                        }
280
                }
281
        }
282
    }
283
    private void drawVertex(Graphics g,FBitSet sel,AffineTransform at) throws DriverIOException{
284
                 for (int i = sel.nextSetBit(0); i >= 0;
285
                                 i = sel.nextSetBit(i + 1)) {
286
                        IGeometry ig = getCadToolAdapter().getVectorialAdapter().getShape(i).cloneGeometry();
287
                        if (ig == null) continue;
288
                                Handler[] handlers=ig.getHandlers(IGeometry.SELECTHANDLER);
289
                                FGraphicUtilities.DrawVertex((Graphics2D)g,at,handlers[numSelect]);
290
                }
291
        }
292
    /* (non-Javadoc)
293
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
294
     */
295
    public void addValue(double d) {
296
    }
297
    public IGeometry removeVertex(IGeometry gp,Handler handler) {
298
        GeneralPathX newGp = new GeneralPathX();
299
        double[] theData = new double[6];
300

    
301
        GeneralPathXIterator theIterator;
302
        int theType;
303
        int numParts = 0;
304

    
305
        Point2D ptSrc = new Point2D.Double();
306
        boolean bFirst = false;
307

    
308
        theIterator = gp.getGeneralPathXIterator(); //, flatness);
309
        int numSegmentsAdded = 0;
310
        while (!theIterator.isDone()) {
311
            theType = theIterator.currentSegment(theData);
312
            if (bFirst){
313
                        newGp.moveTo(theData[0], theData[1]);
314
                        numSegmentsAdded++;
315
                        bFirst=false;
316
                        continue;
317
                }
318
            switch (theType) {
319

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

    
333
                case PathIterator.SEG_LINETO:
334
                    ptSrc.setLocation(theData[0], theData[1]);
335
                    if (ptSrc.equals(handler.getPoint())){
336
                            break;
337
                    }
338
                    newGp.lineTo(ptSrc.getX(), ptSrc.getY());
339
                    bFirst = false;
340
                    numSegmentsAdded++;
341
                    break;
342

    
343
                case PathIterator.SEG_QUADTO:
344
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
345
                    numSegmentsAdded++;
346
                    break;
347

    
348
                case PathIterator.SEG_CUBICTO:
349
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
350
                    numSegmentsAdded++;
351
                    break;
352

    
353
                case PathIterator.SEG_CLOSE:
354
                    if (numSegmentsAdded < 3)
355
                        newGp.lineTo(theData[0], theData[1]);
356
                    newGp.closePath();
357

    
358
                    break;
359
            } //end switch
360

    
361
            theIterator.next();
362
        } //end while loop
363
        FShape shp = null;
364
        switch (gp.getGeometryType())
365
        {
366
            case FShape.POINT: //Tipo punto
367
            case FShape.POINT + FShape.Z:
368
                shp = new FPoint2D(ptSrc.getX(), ptSrc.getY());
369
                break;
370

    
371
            case FShape.LINE:
372
            case FShape.LINE + FShape.Z:
373
                shp = new FPolyline2D(newGp);
374
                break;
375
            case FShape.POLYGON:
376
            case FShape.POLYGON + FShape.Z:
377
                shp = new FPolygon2D(newGp);
378
                break;
379
        }
380
        return ShapeFactory.createGeometry(shp);
381
    }
382

    
383
        public String getName() {
384
                return "EDITAR VERTICE";
385
        }
386
}