Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / InternalPolygonCADTool.java @ 6160

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.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
62
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
63
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
64
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.InternalPolygonCADToolContext;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69

    
70

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

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

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

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

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

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

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

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

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

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

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

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

    
214

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

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

    
232

    
233
        theIterator = gp.getPathIterator(null); //, flatness);
234
        while (!theIterator.isDone()) {
235
            theType = theIterator.currentSegment(theData);
236
            switch (theType) {
237

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

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

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

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

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

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

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

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

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

    
313

    
314
}