Statistics
| Revision:

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

History | View | Annotate | Download (11 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.ViewPort;
54
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
55
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
56
import com.iver.cit.gvsig.fmap.core.FShape;
57
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
58
import com.iver.cit.gvsig.fmap.core.IFeature;
59
import com.iver.cit.gvsig.fmap.core.IGeometry;
60
import com.iver.cit.gvsig.fmap.core.IRow;
61
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
62
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
63
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
64
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
65
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
66
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
67
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
68
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
69
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
70
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
71
import com.iver.cit.gvsig.gui.cad.tools.smc.InternalPolygonCADToolContext;
72
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
73

    
74

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

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

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

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

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

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

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

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

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

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

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

    
205
                            IGeometry geometry=feat.getGeometry();
206
                            if (geometry instanceof FGeometryCollection) {
207
                                    FGeometryCollection gc=(FGeometryCollection)geometry;
208
                                    geometry=createNewPolygonGC(gc,(Point2D[])points.toArray(new Point2D[0]));
209
                            }else {
210
                                    geometry=createNewPolygon(geometry,(Point2D[])points.toArray(new Point2D[0]));
211
                            }
212
                            DefaultFeature df=new DefaultFeature(geometry,feat.getAttributes(), feat.getID());
213
                            DefaultRowEdited dre=new DefaultRowEdited(df,DefaultRowEdited.STATUS_MODIFIED,row.getIndex());
214
                            try {
215
                                        vea.modifyRow(dre.getIndex(),dre.getLinkedRow(),getName(),EditionEvent.GRAPHIC);
216
                                } catch (IOException e) {
217
                                        e.printStackTrace();
218
                                } catch (DriverIOException e) {
219
                                        e.printStackTrace();
220
                                }
221
                                selectedRows.clear();
222
                            selectedRows.add(dre);
223
                    }
224
                    points.clear();
225

    
226

    
227
            }else if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){
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
}