Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / InternalPolygonCADTool.java @ 37853

History | View | Annotate | Download (12.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
24
import java.awt.Component;
25
import java.awt.event.InputEvent;
26
import java.awt.event.MouseEvent;
27
import java.awt.geom.PathIterator;
28
import java.util.ArrayList;
29
import java.util.List;
30

    
31
import javax.swing.JOptionPane;
32

    
33
import com.vividsolutions.jts.geom.GeometryCollection;
34

    
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.editing.CADExtension;
38
import org.gvsig.editing.gui.cad.DefaultCADTool;
39
import org.gvsig.editing.gui.cad.exception.CommandException;
40
import org.gvsig.editing.gui.cad.tools.smc.InternalPolygonCADToolContext;
41
import org.gvsig.editing.layers.VectorialLayerEdited;
42
import org.gvsig.fmap.dal.exception.DataException;
43
import org.gvsig.fmap.dal.exception.ReadException;
44
import org.gvsig.fmap.dal.feature.EditableFeature;
45
import org.gvsig.fmap.dal.feature.Feature;
46
import org.gvsig.fmap.dal.feature.FeatureSelection;
47
import org.gvsig.fmap.dal.feature.FeatureSet;
48
import org.gvsig.fmap.dal.feature.FeatureStore;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
51
import org.gvsig.fmap.geom.primitive.GeneralPathX;
52
import org.gvsig.fmap.geom.primitive.Point;
53
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
54
import org.gvsig.tools.dispose.DisposableIterator;
55
import org.gvsig.tools.dispose.DisposeUtils;
56

    
57
/**
58
 * DOCUMENT ME!
59
 * 
60
 * @author Vicente Caballero Navarro
61
 */
62
public class InternalPolygonCADTool extends DefaultCADTool {
63

    
64
    protected InternalPolygonCADToolContext _fsm;
65
    protected List<Point> points = new ArrayList<Point>();
66
    protected Geometry geometry = null;
67

    
68
    /**
69
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
70
     * carga previa a la utilizaci?n de la herramienta.
71
     */
72
    public void init() {
73
        _fsm = new InternalPolygonCADToolContext(this);
74
        points.clear();
75
    }
76

    
77
    /*
78
     * (non-Javadoc)
79
     * 
80
     * @see
81
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
82
     * .layers.FBitSet, double, double)
83
     */
84
    public void transition(double x, double y, InputEvent event) {
85
        _fsm.addPoint(x, y, event);
86
    }
87

    
88
    /*
89
     * (non-Javadoc)
90
     * 
91
     * @see
92
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
93
     * .layers.FBitSet, double)
94
     */
95
    public void transition(double d) {
96
        _fsm.addValue(d);
97
    }
98

    
99
    /*
100
     * (non-Javadoc)
101
     * 
102
     * @see
103
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
104
     * .layers.FBitSet, java.lang.String)
105
     */
106
    public void transition(String s) throws CommandException {
107
        if (!super.changeCommand(s)) {
108
            _fsm.addOption(s);
109
        }
110
    }
111

    
112
    /**
113
     * DOCUMENT ME!
114
     */
115
    public void selection() {
116
        FeatureSet selection = null;
117
        try {
118
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
119

    
120
            if (selection.getSize() == 0
121
                && !CADExtension
122
                    .getCADTool()
123
                    .getClass()
124
                    .getName()
125
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
126
                CADExtension.setCADTool("_selection", false);
127
                ((SelectionCADTool) CADExtension.getCADTool())
128
                    .setNextTool("_internalpolygon");
129
            }
130
        } catch (ReadException e) {
131
            // TODO Auto-generated catch block
132
            e.printStackTrace();
133
        } catch (DataException e) {
134
            // TODO Auto-generated catch block
135
            e.printStackTrace();
136
        }
137
    }
138

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

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

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

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

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

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

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

    
272
            } else
273
                if (s.equals(PluginServices.getText(this, "cancel"))) {
274
                    points.clear();
275
                }
276
        } catch (DataException e1) {
277
            e1.printStackTrace();
278
        } finally {
279
            if (iterator != null) {
280
                iterator.dispose();
281
            }
282
        }
283
    }
284

    
285
    /*
286
     * (non-Javadoc)
287
     * 
288
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
289
     */
290
    public void addValue(double d) {
291
    }
292

    
293
    private Geometry createNewPolygon(Geometry geometry, List<Point> points) {
294
        GeneralPathX newGp = new GeneralPathX();
295
        double[] theData = new double[6];
296

    
297
        PathIterator theIterator;
298
        int theType;
299
        int numParts = 0;
300

    
301
        theIterator = geometry.getPathIterator(null, geomManager.getFlatness());
302
        while (!theIterator.isDone()) {
303
            theType = theIterator.currentSegment(theData);
304
            switch (theType) {
305

    
306
            case PathIterator.SEG_MOVETO:
307
                numParts++;
308
                newGp.moveTo(theData[0], theData[1]);
309
                break;
310

    
311
            case PathIterator.SEG_LINETO:
312
                newGp.lineTo(theData[0], theData[1]);
313
                break;
314

    
315
            case PathIterator.SEG_QUADTO:
316
                newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
317
                break;
318

    
319
            case PathIterator.SEG_CUBICTO:
320
                newGp.curveTo(theData[0], theData[1], theData[2], theData[3],
321
                    theData[4], theData[5]);
322
                break;
323

    
324
            case PathIterator.SEG_CLOSE:
325
                newGp.closePath();
326
                break;
327
            } // end switch
328

    
329
            theIterator.next();
330
        } // end while loop
331
        GeneralPathX gpxInternal = new GeneralPathX();
332
        gpxInternal.moveTo(points.get(points.size() - 1));
333
        for (int i = points.size() - 2; i >= 0; i--) {
334
            gpxInternal.lineTo(points.get(i));
335
        }
336
        gpxInternal.closePath();
337

    
338
        if (!gpxInternal.isCCW()) {
339
            gpxInternal.flip();
340
        }
341
        newGp.append(gpxInternal.getPathIterator(null), false);
342

    
343
        return createSurface(newGp);
344
    }
345

    
346
    private Geometry createNewPolygonGC(MultiPrimitive multiPrimitive,
347
        List<Point> points) {
348
        MultiPrimitive multiPrimitiveAux = createMultiPrimitive();
349

    
350
        for (int i = 0; i < multiPrimitive.getPrimitivesNumber(); i++) {
351
            multiPrimitiveAux.addPrimitive(multiPrimitive.getPrimitiveAt(i));
352
        }
353

    
354
        GeneralPathX gpx = new GeneralPathX();
355
        gpx.moveTo(points.get(points.size() - 1));
356
        for (int i = points.size() - 2; i >= 0; i--) {
357
            gpx.lineTo(points.get(i));
358
            multiPrimitiveAux.addPrimitive(createCurve(gpx));
359
            gpx = new GeneralPathX();
360
            gpx.moveTo(points.get(i));
361
        }
362
        gpx.closePath();
363
        multiPrimitiveAux.addPrimitive(createCurve(gpx));
364

    
365
        return multiPrimitiveAux;
366
    }
367

    
368
    public String getName() {
369
        return PluginServices.getText(this, "internal_polygon_");
370
    }
371

    
372
    public String toString() {
373
        return "_internalpolygon";
374
    }
375

    
376
    @Override
377
    protected int[] getSupportedGeometryTypes() {
378
        return new int[] { SURFACE, MULTISURFACE };
379
    }
380
}