Statistics
| Revision:

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

History | View | Annotate | Download (11.5 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.util.ArrayList;
49

    
50
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.CADExtension;
53
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
54
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
55
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
56
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
57
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
58
import com.iver.cit.gvsig.fmap.core.FShape;
59
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
60
import com.iver.cit.gvsig.fmap.core.IFeature;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
63
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
64
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
65
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
66
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
67
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
68
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
69
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
70
import com.iver.cit.gvsig.gui.cad.tools.smc.InternalPolygonCADToolContext;
71
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
72

    
73

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

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

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

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

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

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

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

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

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

    
170
            }
171
            gpx.lineTo(x,y);
172
            gpx.closePath();
173
            gpx1.closePath();
174
            if (ps.length==1){
175
                    IGeometry geom=ShapeFactory.createPolyline2D(gpx);
176
                geom.drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.geometrySelectSymbol);
177
            }
178
            IGeometry geom=ShapeFactory.createPolygon2D(gpx);
179
            IGeometry geom1=ShapeFactory.createPolygon2D(gpx1);
180
            geom1.drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.selectionSymbol);
181
            geom.drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.geometrySelectSymbol);
182
            }
183
            DefaultRowEdited[] rows=(DefaultRowEdited[])getSelectedRows().toArray(new DefaultRowEdited[0]);
184
            for (int i=0;i<rows.length;i++){
185
                    ((IFeature)rows[i].getLinkedRow()).getGeometry().drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.selectionSymbol);
186
            }
187
    }
188

    
189
    /**
190
     * Add a diferent option.
191
     *
192
     * @param s Diferent option.
193
     */
194
    public void addOption(String s) {
195
            VectorialLayerEdited vle=getVLE();
196
            ArrayList selectedRows=vle.getSelectedRow();
197
            VectorialEditableAdapter vea = vle.getVEA();
198
            IRowEdited row=null;
199
            if (s.equals(PluginServices.getText(this,"end")) || s.equalsIgnoreCase(PluginServices.getText(this,"InternalPolygonCADTool.end"))){
200
                    if (points.size()>0){
201
                            row =  (DefaultRowEdited) selectedRows.get(0);
202
                            IFeature feat = (IFeature) row.getLinkedRow().cloneRow();
203

    
204
                            IGeometry geometry=feat.getGeometry();
205
                            if (geometry instanceof FGeometryCollection) {
206
                                    FGeometryCollection gc=(FGeometryCollection)geometry;
207
                                    geometry=createNewPolygonGC(gc,(Point2D[])points.toArray(new Point2D[0]));
208
                            }else {
209
                                    geometry=createNewPolygon(geometry,(Point2D[])points.toArray(new Point2D[0]));
210
                            }
211
                            DefaultFeature df=new DefaultFeature(geometry,feat.getAttributes(), feat.getID());
212
                            DefaultRowEdited dre=new DefaultRowEdited(df,DefaultRowEdited.STATUS_MODIFIED,row.getIndex());
213
                            try {
214
                                        vea.modifyRow(dre.getIndex(),dre.getLinkedRow(),getName(),EditionEvent.GRAPHIC);
215
                                } catch (ValidateRowException e) {
216
                                        // TODO Auto-generated catch block
217
                                        e.printStackTrace();
218
                                } catch (ExpansionFileWriteException e) {
219
                                        // TODO Auto-generated catch block
220
                                        e.printStackTrace();
221
                                } catch (ReadDriverException e) {
222
                                        // TODO Auto-generated catch block
223
                                        e.printStackTrace();
224
                                } catch (ExpansionFileReadException e) {
225
                                        // TODO Auto-generated catch block
226
                                        e.printStackTrace();
227
                                }
228
                                ArrayList rows=new ArrayList();
229
                                rows.add(dre);
230
                                vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, rows);
231
                        }
232
                    points.clear();
233

    
234

    
235
            }else if (s.equals(PluginServices.getText(this,"cancel"))){
236
                    points.clear();
237
            }
238
    }
239
    /* (non-Javadoc)
240
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
241
     */
242
    public void addValue(double d) {
243
    }
244
    private IGeometry createNewPolygon(IGeometry gp,Point2D[] ps) {
245
        GeneralPathX newGp = new GeneralPathX();
246
        double[] theData = new double[6];
247

    
248
        PathIterator theIterator;
249
        int theType;
250
        int numParts = 0;
251

    
252

    
253
        theIterator = gp.getPathIterator(null, FConverter.FLATNESS);
254
        while (!theIterator.isDone()) {
255
            theType = theIterator.currentSegment(theData);
256
            switch (theType) {
257

    
258
                case PathIterator.SEG_MOVETO:
259
                    numParts++;
260
                    newGp.moveTo(theData[0], theData[1]);
261
                    break;
262

    
263
                case PathIterator.SEG_LINETO:
264
                    newGp.lineTo(theData[0], theData[1]);
265
                    break;
266

    
267
                case PathIterator.SEG_QUADTO:
268
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
269
                    break;
270

    
271
                case PathIterator.SEG_CUBICTO:
272
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
273
                    break;
274

    
275
                case PathIterator.SEG_CLOSE:
276
                        newGp.closePath();
277
                    break;
278
            } //end switch
279

    
280
            theIterator.next();
281
        } //end while loop
282
     GeneralPathX gpxInternal=new GeneralPathX();
283
     gpxInternal.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
284
     for (int i=ps.length-1;i>=0;i--){
285
             gpxInternal.lineTo(ps[i].getX(),ps[i].getY());
286
     }
287
     gpxInternal.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
288
     if (!gpxInternal.isCCW()) {
289
             gpxInternal.flip();
290
     }
291
     newGp.append(gpxInternal,false);
292

    
293
     return ShapeFactory.createPolygon2D(newGp);
294
    }
295
    private IGeometry createNewPolygonGC(FGeometryCollection gp,Point2D[] ps) {
296
            ArrayList geoms=new ArrayList();
297
            IGeometry[] geometries=gp.getGeometries();
298
            for (int i = 0;i<geometries.length;i++) {
299
                    geoms.add(geometries[i]);
300
            }
301
            GeneralPathX gpx=new GeneralPathX();
302
                gpx.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
303
            for (int i=ps.length-2;i>=0;i--){
304
                    gpx.lineTo(ps[i].getX(),ps[i].getY());
305
                    geoms.add(ShapeFactory.createPolyline2D(gpx));
306
                    gpx=new GeneralPathX();
307
                    gpx.moveTo(ps[i].getX(),ps[i].getY());
308
            }
309
            gpx.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
310
            geoms.add(ShapeFactory.createPolyline2D(gpx));
311
            FGeometryCollection gc=new FGeometryCollection((IGeometry[])geoms.toArray(new IGeometry[0]));
312
//            gc.setGeometryType(FShape.POLYGON);
313
            return gc;
314
    }
315
    public String getName() {
316
                return PluginServices.getText(this,"internal_polygon_");
317
        }
318

    
319
        public String toString() {
320
                return "_internalpolygon";
321
        }
322

    
323
        public boolean isApplicable(int shapeType) {
324
                switch (shapeType) {
325
                case FShape.POINT:
326
                case FShape.LINE:
327
                case FShape.MULTIPOINT:
328
                        return false;
329
                }
330
                return true;
331
        }
332

    
333

    
334
}