Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_914 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / InternalPolygonCADTool.java @ 11873

History | View | Annotate | Download (10.3 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.PathIterator;
47
import java.awt.geom.Point2D;
48
import java.io.IOException;
49
import java.util.ArrayList;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.CADExtension;
53
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
55
import com.iver.cit.gvsig.fmap.core.FShape;
56
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
57
import com.iver.cit.gvsig.fmap.core.IFeature;
58
import com.iver.cit.gvsig.fmap.core.IGeometry;
59
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
60
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
63
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
64
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
65
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
66
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
67
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.InternalPolygonCADToolContext;
69
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
70

    
71

    
72
/**
73
 * DOCUMENT ME!
74
 *
75
 * @author Vicente Caballero Navarro
76
 */
77
public class InternalPolygonCADTool extends DefaultCADTool {
78
    private InternalPolygonCADToolContext _fsm;
79
   private ArrayList points=new ArrayList();
80
    /**
81
     * Crea un nuevo PolylineCADTool.
82
     */
83
    public InternalPolygonCADTool() {
84
    }
85

    
86
    /**
87
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
88
     * carga previa a la utilizaci?n de la herramienta.
89
     */
90
    public void init() {
91
        _fsm = new InternalPolygonCADToolContext(this);
92
        points.clear();
93
    }
94

    
95
    /* (non-Javadoc)
96
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
97
     */
98
    public void transition(double x, double y, InputEvent event) {
99
        _fsm.addPoint(x, y, event);
100
    }
101

    
102
    /* (non-Javadoc)
103
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
104
     */
105
    public void transition(double d) {
106
        _fsm.addValue(d);
107
    }
108

    
109
    /* (non-Javadoc)
110
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
111
     */
112
    public void transition(String s) throws CommandException {
113
            if (!super.changeCommand(s)){
114
                    _fsm.addOption(s);
115
            }
116
    }
117

    
118
    /**
119
     * DOCUMENT ME!
120
     */
121
    public void selection() {
122
            ArrayList selectedRow=getSelectedRows();
123
        if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
124
            CADExtension.setCADTool("_selection",false);
125
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
126
                "_internalpolygon");
127
        }
128
    }
129

    
130
    /**
131
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
132
     * editableFeatureSource que ya estar? creado.
133
     *
134
     * @param x par?metro x del punto que se pase en esta transici?n.
135
     * @param y par?metro y del punto que se pase en esta transici?n.
136
     */
137
    public void addPoint(double x, double y,InputEvent event) {
138
            VectorialLayerEdited vle=getVLE();
139
        ArrayList selectedRows=vle.getSelectedRow();
140
        if (selectedRows.size()==1){
141
                        points.add(new Point2D.Double(x,y));
142
        }
143
        }
144

    
145
        /**
146
     * M?todo para dibujar la lo necesario para el estado en el que nos
147
     * encontremos.
148
     *
149
     * @param g Graphics sobre el que dibujar.
150
     * @param x par?metro x del punto que se pase para dibujar.
151
     * @param y par?metro x del punto que se pase para dibujar.
152
     */
153
    public void drawOperation(Graphics g, double x, double y) {
154
            Point2D[] ps=(Point2D[])points.toArray(new Point2D[0]);
155
            GeneralPathX gpx=new GeneralPathX();
156
            GeneralPathX gpx1=new GeneralPathX();
157

    
158
            if (ps.length>0){
159
            for (int i=0;i<ps.length;i++){
160
                    if (i==0){
161
                            gpx.moveTo(ps[i].getX(),ps[i].getY());
162
                            gpx1.moveTo(ps[i].getX(),ps[i].getY());
163
                    }else{
164
                            gpx.lineTo(ps[i].getX(),ps[i].getY());
165
                            gpx1.lineTo(ps[i].getX(),ps[i].getY());
166
                    }
167

    
168
            }
169
            gpx.lineTo(x,y);
170
            gpx.closePath();
171
            gpx1.closePath();
172
            IGeometry geom=ShapeFactory.createPolygon2D(gpx);
173
            IGeometry geom1=ShapeFactory.createPolygon2D(gpx1);
174
            geom1.draw((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.selectionSymbol);
175
            geom.draw((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.geometrySelectSymbol);
176
            }
177
    }
178

    
179
    /**
180
     * Add a diferent option.
181
     *
182
     * @param s Diferent option.
183
     */
184
    public void addOption(String s) {
185
            VectorialLayerEdited vle=getVLE();
186
            ArrayList selectedRows=vle.getSelectedRow();
187
            VectorialEditableAdapter vea = vle.getVEA();
188
            IRowEdited row=null;
189
            if (s.equals(PluginServices.getText(this,"end")) || s.equals("e")|| s.equals("E")){
190
                    if (points.size()>0){
191
                            row =  (DefaultRowEdited) selectedRows.get(0);
192
                            IFeature feat = (IFeature) row.getLinkedRow().cloneRow();
193

    
194
                            IGeometry geometry=feat.getGeometry();
195
                            if (geometry instanceof FGeometryCollection) {
196
                                    FGeometryCollection gc=(FGeometryCollection)geometry;
197
                                    geometry=createNewPolygonGC(gc,(Point2D[])points.toArray(new Point2D[0]));
198
                            }else {
199
                                    geometry=createNewPolygon(geometry,(Point2D[])points.toArray(new Point2D[0]));
200
                            }
201
                            DefaultFeature df=new DefaultFeature(geometry,feat.getAttributes(), feat.getID());
202
                            DefaultRowEdited dre=new DefaultRowEdited(df,DefaultRowEdited.STATUS_MODIFIED,row.getIndex());
203
                            try {
204
                                        vea.modifyRow(dre.getIndex(),dre.getLinkedRow(),getName(),EditionEvent.GRAPHIC);
205
                                } catch (IOException e) {
206
                                        e.printStackTrace();
207
                                } catch (DriverIOException e) {
208
                                        e.printStackTrace();
209
                                }
210
                                selectedRows.clear();
211
                            selectedRows.add(dre);
212
                    }
213
                    points.clear();
214

    
215

    
216
            }else if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){
217
                    points.clear();
218
            }
219
    }
220
    /* (non-Javadoc)
221
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
222
     */
223
    public void addValue(double d) {
224
    }
225
    private IGeometry createNewPolygon(IGeometry gp,Point2D[] ps) {
226
        GeneralPathX newGp = new GeneralPathX();
227
        double[] theData = new double[6];
228

    
229
        PathIterator theIterator;
230
        int theType;
231
        int numParts = 0;
232

    
233

    
234
        theIterator = gp.getPathIterator(null, FConverter.FLATNESS);
235
        while (!theIterator.isDone()) {
236
            theType = theIterator.currentSegment(theData);
237
            switch (theType) {
238

    
239
                case PathIterator.SEG_MOVETO:
240
                    numParts++;
241
                    newGp.moveTo(theData[0], theData[1]);
242
                    break;
243

    
244
                case PathIterator.SEG_LINETO:
245
                    newGp.lineTo(theData[0], theData[1]);
246
                    break;
247

    
248
                case PathIterator.SEG_QUADTO:
249
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
250
                    break;
251

    
252
                case PathIterator.SEG_CUBICTO:
253
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
254
                    break;
255

    
256
                case PathIterator.SEG_CLOSE:
257
                        newGp.closePath();
258
                    break;
259
            } //end switch
260

    
261
            theIterator.next();
262
        } //end while loop
263
     GeneralPathX gpxInternal=new GeneralPathX();
264
     gpxInternal.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
265
     for (int i=ps.length-1;i>=0;i--){
266
             gpxInternal.lineTo(ps[i].getX(),ps[i].getY());
267
     }
268
     gpxInternal.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
269
     if (!gpxInternal.isCCW()) {
270
             gpxInternal.flip();
271
     }
272
     newGp.append(gpxInternal,false);
273

    
274
     return ShapeFactory.createPolygon2D(newGp);
275
    }
276
    private IGeometry createNewPolygonGC(FGeometryCollection gp,Point2D[] ps) {
277
            ArrayList geoms=new ArrayList();
278
            IGeometry[] geometries=gp.getGeometries();
279
            for (int i = 0;i<geometries.length;i++) {
280
                    geoms.add(geometries[i]);
281
            }
282
            GeneralPathX gpx=new GeneralPathX();
283
                gpx.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
284
            for (int i=ps.length-2;i>=0;i--){
285
                    gpx.lineTo(ps[i].getX(),ps[i].getY());
286
                    geoms.add(ShapeFactory.createPolyline2D(gpx));
287
                    gpx=new GeneralPathX();
288
                    gpx.moveTo(ps[i].getX(),ps[i].getY());
289
            }
290
            gpx.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
291
            geoms.add(ShapeFactory.createPolyline2D(gpx));
292
            FGeometryCollection gc=new FGeometryCollection((IGeometry[])geoms.toArray(new IGeometry[0]));
293
//            gc.setGeometryType(FShape.POLYGON);
294
            return gc;
295
    }
296
    public String getName() {
297
                return PluginServices.getText(this,"internal_polygon_");
298
        }
299

    
300
        public String toString() {
301
                return "_internalpolygon";
302
        }
303

    
304
        public boolean isApplicable(int shapeType) {
305
                switch (shapeType) {
306
                case FShape.POINT:
307
                case FShape.LINE:
308
                case FShape.MULTIPOINT:
309
                        return false;
310
                }
311
                return true;
312
        }
313

    
314

    
315
}