Statistics
| Revision:

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

History | View | Annotate | Download (10.9 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
            IGeometry geom=ShapeFactory.createPolygon2D(gpx);
175
            IGeometry geom1=ShapeFactory.createPolygon2D(gpx1);
176
            geom1.draw((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.drawingSymbol);
177
            geom.draw((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.modifySymbol);
178
            }
179
    }
180

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

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

    
226

    
227
            }else if (s.equals(PluginServices.getText(this,"cancel"))){
228
                    points.clear();
229
            }
230
    }
231
    /* (non-Javadoc)
232
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
233
     */
234
    public void addValue(double d) {
235
    }
236
    private IGeometry createNewPolygon(IGeometry gp,Point2D[] ps) {
237
        GeneralPathX newGp = new GeneralPathX();
238
        double[] theData = new double[6];
239

    
240
        PathIterator theIterator;
241
        int theType;
242
        int numParts = 0;
243

    
244

    
245
        theIterator = gp.getPathIterator(null, FConverter.FLATNESS);
246
        while (!theIterator.isDone()) {
247
            theType = theIterator.currentSegment(theData);
248
            switch (theType) {
249

    
250
                case PathIterator.SEG_MOVETO:
251
                    numParts++;
252
                    newGp.moveTo(theData[0], theData[1]);
253
                    break;
254

    
255
                case PathIterator.SEG_LINETO:
256
                    newGp.lineTo(theData[0], theData[1]);
257
                    break;
258

    
259
                case PathIterator.SEG_QUADTO:
260
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
261
                    break;
262

    
263
                case PathIterator.SEG_CUBICTO:
264
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
265
                    break;
266

    
267
                case PathIterator.SEG_CLOSE:
268
                        newGp.closePath();
269
                    break;
270
            } //end switch
271

    
272
            theIterator.next();
273
        } //end while loop
274
     GeneralPathX gpxInternal=new GeneralPathX();
275
     gpxInternal.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
276
     for (int i=ps.length-1;i>=0;i--){
277
             gpxInternal.lineTo(ps[i].getX(),ps[i].getY());
278
     }
279
     gpxInternal.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
280
     if (!gpxInternal.isCCW()) {
281
             gpxInternal.flip();
282
     }
283
     newGp.append(gpxInternal,false);
284

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

    
311
        public String toString() {
312
                return "_internalpolygon";
313
        }
314

    
315
        public boolean isApplicable(int shapeType) {
316
                switch (shapeType) {
317
                case FShape.POINT:
318
                case FShape.LINE:
319
                case FShape.MULTIPOINT:
320
                        return false;
321
                }
322
                return true;
323
        }
324

    
325

    
326
}