Statistics
| Revision:

root / tags / v2_0_0_Build_2030 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / InternalPolygonCADTool.java @ 35878

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 org.gvsig.editing.gui.cad.tools;
42

    
43
import java.awt.Component;
44
import java.awt.event.InputEvent;
45
import java.awt.event.MouseEvent;
46
import java.awt.geom.PathIterator;
47
import java.awt.geom.Point2D;
48
import java.util.ArrayList;
49
import java.util.List;
50

    
51
import javax.swing.JOptionPane;
52

    
53
import com.vividsolutions.jts.geom.GeometryCollection;
54

    
55
import org.gvsig.andami.PluginServices;
56
import org.gvsig.andami.messages.NotificationManager;
57
import org.gvsig.editing.CADExtension;
58
import org.gvsig.editing.gui.cad.DefaultCADTool;
59
import org.gvsig.editing.gui.cad.exception.CommandException;
60
import org.gvsig.editing.gui.cad.tools.smc.InternalPolygonCADToolContext;
61
import org.gvsig.editing.layers.VectorialLayerEdited;
62
import org.gvsig.fmap.dal.exception.DataException;
63
import org.gvsig.fmap.dal.exception.ReadException;
64
import org.gvsig.fmap.dal.feature.EditableFeature;
65
import org.gvsig.fmap.dal.feature.Feature;
66
import org.gvsig.fmap.dal.feature.FeatureSelection;
67
import org.gvsig.fmap.dal.feature.FeatureSet;
68
import org.gvsig.fmap.dal.feature.FeatureStore;
69
import org.gvsig.fmap.geom.Geometry;
70
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
71
import org.gvsig.fmap.geom.aggregate.MultiSurface;
72
import org.gvsig.fmap.geom.aggregate.impl.BaseMultiPrimitive;
73
import org.gvsig.fmap.geom.primitive.GeneralPathX;
74
import org.gvsig.fmap.geom.primitive.Point;
75
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
76
import org.gvsig.tools.dispose.DisposableIterator;
77
import org.gvsig.tools.dispose.DisposeUtils;
78

    
79

    
80
/**
81
 * DOCUMENT ME!
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85
public class InternalPolygonCADTool extends DefaultCADTool {
86
        protected InternalPolygonCADToolContext _fsm;
87
        protected List<Point> points = new ArrayList<Point>();
88
        protected Geometry geometry = null;
89

    
90
        /**
91
         * Crea un nuevo PolylineCADTool.
92
         */
93
        public InternalPolygonCADTool() {
94
        }
95

    
96
        /**
97
         * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
98
         * carga previa a la utilizaci?n de la herramienta.
99
         */
100
        public void init() {
101
                _fsm = new InternalPolygonCADToolContext(this);
102
                points.clear();
103
        }
104

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

    
112
        /* (non-Javadoc)
113
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
114
         */
115
        public void transition(double d) {
116
                _fsm.addValue(d);
117
        }
118

    
119
        /* (non-Javadoc)
120
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
121
         */
122
        public void transition(String s) throws CommandException {
123
                if (!super.changeCommand(s)){
124
                        _fsm.addOption(s);
125
                }
126
        }
127

    
128
        /**
129
         * DOCUMENT ME!
130
         */
131
        public void selection() {
132
                FeatureSet selection=null;
133
                try {
134
                        selection = (FeatureSet)getVLE().getFeatureStore().getSelection();
135

    
136
                        if (selection.getSize() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
137
                                CADExtension.setCADTool("_selection",false);
138
                                ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
139
                                "_internalpolygon");
140
                        }
141
                } catch (ReadException e) {
142
                        // TODO Auto-generated catch block
143
                        e.printStackTrace();
144
                } catch (DataException e) {
145
                        // TODO Auto-generated catch block
146
                        e.printStackTrace();
147
                }
148
        }
149

    
150
        /**
151
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
152
         * editableFeatureSource que ya estar? creado.
153
         *
154
         * @param x par?metro x del punto que se pase en esta transici?n.
155
         * @param y par?metro y del punto que se pase en esta transici?n.
156
         */
157
        public void addPoint(double x, double y,InputEvent event) {
158
                if (((MouseEvent)event).getClickCount()==2){
159
                        addOption(PluginServices.getText(this,"InternalPolygonCADTool.end"));
160
                        return;
161
                }
162
                VectorialLayerEdited vle=getVLE();
163
                FeatureSet featureCollection=null;
164
                DisposableIterator iterator = null;
165
                try {
166
                        featureCollection = (FeatureSet)vle.getFeatureStore().getSelection();
167
                        if (featureCollection.getSize()==1){
168
                                iterator = featureCollection.iterator();
169
                                Feature feature = (Feature) iterator.next();
170
                                geometry=(feature.getDefaultGeometry()).cloneGeometry();
171
                                if (geometry.contains(x,y)){
172
                                        points.add(createPoint(x, y));
173
                                }else{
174
                                        JOptionPane.showMessageDialog(((Component)PluginServices.getMainFrame()),PluginServices.getText(this,"debe_insertar_el_punto_dentro_de_los_limites_de_la_geometria"));
175
                                }
176
                        }
177
                } catch (ReadException e) {
178
                        NotificationManager.addError(e.getMessage(),e);
179
                } catch (DataException e) {
180
                        NotificationManager.addError(e.getMessage(),e);
181
                } finally {
182
                        DisposeUtils.dispose(iterator);                        
183
                }
184
        }
185

    
186
        /**
187
         * M?todo para dibujar la lo necesario para el estado en el que nos
188
         * encontremos.
189
         *
190
         * @param g Graphics sobre el que dibujar.
191
         * @param x par?metro x del punto que se pase para dibujar.
192
         * @param y par?metro x del punto que se pase para dibujar.
193
         */
194
        public void drawOperation(MapControlDrawer renderer, double x, double y) {
195
                GeneralPathX gpx = new GeneralPathX();
196
                GeneralPathX gpx1 = new GeneralPathX();
197

    
198
                if (points.size()>0){
199
                        for (int i=0 ; i<points.size() ; i++){
200
                                if (i==0){
201
                                        gpx.moveTo(points.get(i));
202
                                        gpx1.moveTo(points.get(i));
203
                                }else{
204
                                        gpx.lineTo(points.get(i));
205
                                        gpx1.lineTo(points.get(i));
206
                                }
207

    
208
                        }
209
                        gpx.lineTo(createPoint(x,y));
210
                        gpx.closePath();
211
                        gpx1.closePath();
212

    
213
                        Geometry geom = createSurface(gpx);
214
                        Geometry geom1 = createSurface(gpx1);
215

    
216
                        renderer.draw(geom1, mapControlManager.getSelectionSymbol());
217
                        renderer.draw(geom,mapControlManager.getGeometrySelectionSymbol());
218
                }
219
        }
220

    
221
        /**
222
         * Add a diferent option.
223
         *
224
         * @param s Diferent option.
225
         */
226
        public void addOption(String s) {
227
                VectorialLayerEdited vle=getVLE();
228
                FeatureStore featureStore=null;
229
                try {
230
                        featureStore = vle.getFeatureStore();
231
                } catch (ReadException e1) {
232
                        // TODO Auto-generated catch block
233
                        e1.printStackTrace();
234
                }
235
                DisposableIterator iterator = null;
236
                try {
237
                        iterator = ((FeatureSelection) featureStore.getSelection())
238
                        .iterator();
239
                        if (s.equals(PluginServices.getText(this, "end"))
240
                                        || s.equalsIgnoreCase(PluginServices.getText(this,
241
                                        "InternalPolygonCADTool.end"))) {
242
                                if (points.size() > 0) {
243
                                        Feature feature = (Feature) iterator.next();
244
                                        geometry = (feature.getDefaultGeometry()).cloneGeometry();
245
                                        if (geometry instanceof GeometryCollection) {
246
                                                MultiPrimitive gc = (MultiPrimitive) geometry;
247
                                                geometry = createNewPolygonGC(gc, points);                                                                
248
                                        } else {
249
                                                geometry = createNewPolygon(geometry, points);
250
                                        }
251
                                        try {
252
                                                EditableFeature eFeature = feature.getEditable();
253
                                                eFeature.setGeometry(featureStore
254
                                                                .getDefaultFeatureType()
255
                                                                .getDefaultGeometryAttributeName(), geometry);
256
                                                featureStore.update(eFeature);
257
                                        } catch (ReadException e) {
258
                                                NotificationManager.addError(e.getMessage(), e);
259
                                        } catch (DataException e) {
260
                                                NotificationManager.addError(e.getMessage(), e);
261
                                        }
262
                                        ArrayList rows = new ArrayList();
263
                                        rows.add(feature);
264
                                        // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
265
                                        // rows);
266
                                }
267
                                points.clear();
268
                                refresh();
269

    
270
                        } else if (s.equals(PluginServices.getText(this, "cancel"))) {
271
                                points.clear();
272
                        }
273
                } catch (DataException e1) {
274
                        e1.printStackTrace();
275
                } finally {
276
                        if (iterator != null) {
277
                                iterator.dispose();
278
                        }
279
                }
280
        }
281
        /* (non-Javadoc)
282
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
283
         */
284
        public void addValue(double d) {
285
        }
286
        private Geometry createNewPolygon(Geometry geometry, List<Point> points) {
287
                GeneralPathX newGp = new GeneralPathX();
288
                double[] theData = new double[6];
289

    
290
                PathIterator theIterator;
291
                int theType;
292
                int numParts = 0;
293

    
294
                theIterator = geometry.getPathIterator(null, geomManager.getFlatness());
295
                while (!theIterator.isDone()) {
296
                        theType = theIterator.currentSegment(theData);
297
                        switch (theType) {
298

    
299
                        case PathIterator.SEG_MOVETO:
300
                                numParts++;
301
                                newGp.moveTo(theData[0], theData[1]);
302
                                break;
303

    
304
                        case PathIterator.SEG_LINETO:
305
                                newGp.lineTo(theData[0], theData[1]);
306
                                break;
307

    
308
                        case PathIterator.SEG_QUADTO:
309
                                newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
310
                                break;
311

    
312
                        case PathIterator.SEG_CUBICTO:
313
                                newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
314
                                break;
315

    
316
                        case PathIterator.SEG_CLOSE:
317
                                newGp.closePath();
318
                                break;
319
                        } //end switch
320

    
321
                        theIterator.next();
322
                } //end while loop
323
                GeneralPathX gpxInternal=new GeneralPathX();
324
                gpxInternal.moveTo(points.get(points.size()-1));
325
                for (int i=points.size()-2 ; i>=0 ; i--){
326
                        gpxInternal.lineTo(points.get(i));
327
                }
328
                gpxInternal.closePath();
329
                
330
                if (!gpxInternal.isCCW()) {
331
                        gpxInternal.flip();
332
                }
333
                newGp.append(gpxInternal.getPathIterator(null),false);
334

    
335
                return createSurface(newGp);
336
        }
337
        
338
        private Geometry createNewPolygonGC(MultiPrimitive multiPrimitive, List<Point> points) {
339
            MultiPrimitive multiPrimitiveAux = createMultiPrimitive();
340
                           
341
                for (int i = 0 ; i<multiPrimitive.getPrimitivesNumber() ; i++) {
342
                    multiPrimitiveAux.addPrimitive(multiPrimitive.getPrimitiveAt(i));
343
                }
344
                
345
                GeneralPathX gpx=new GeneralPathX();
346
                gpx.moveTo(points.get(points.size()-1));
347
                for (int i=points.size()-2 ; i>=0 ; i--){
348
                        gpx.lineTo(points.get(i));
349
                        multiPrimitiveAux.addPrimitive(createCurve(gpx));
350
                        gpx = new GeneralPathX();
351
                        gpx.moveTo(points.get(i));
352
                }
353
                gpx.closePath();
354
                multiPrimitiveAux.addPrimitive(createCurve(gpx));
355
                
356
                return multiPrimitiveAux;
357
        }
358
        
359
        public String getName() {
360
                return PluginServices.getText(this,"internal_polygon_");
361
        }
362

    
363
        public String toString() {
364
                return "_internalpolygon";
365
        }
366

    
367
        public boolean isApplicable(int shapeType) {
368
                switch (shapeType) {
369
                case Geometry.TYPES.POINT:
370
                case Geometry.TYPES.CURVE:
371
                case Geometry.TYPES.MULTIPOINT:
372
                        return false;
373
                }
374
                return true;
375
        }
376
}