Statistics
| Revision:

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

History | View | Annotate | Download (13 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
     * Crea un nuevo PolylineCADTool.
70
     */
71
    public InternalPolygonCADTool() {
72
    }
73

    
74
    /**
75
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
76
     * carga previa a la utilizaci?n de la herramienta.
77
     */
78
    public void init() {
79
        _fsm = new InternalPolygonCADToolContext(this);
80
        points.clear();
81
    }
82

    
83
    /*
84
     * (non-Javadoc)
85
     * 
86
     * @see
87
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
88
     * .layers.FBitSet, double, double)
89
     */
90
    public void transition(double x, double y, InputEvent event) {
91
        _fsm.addPoint(x, y, event);
92
    }
93

    
94
    /*
95
     * (non-Javadoc)
96
     * 
97
     * @see
98
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
99
     * .layers.FBitSet, double)
100
     */
101
    public void transition(double d) {
102
        _fsm.addValue(d);
103
    }
104

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

    
118
    /**
119
     * DOCUMENT ME!
120
     */
121
    public void selection() {
122
        FeatureSet selection = null;
123
        try {
124
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
125

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

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

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

    
205
        if (points.size() > 0) {
206
            for (int i = 0; i < points.size(); i++) {
207
                if (i == 0) {
208
                    gpx.moveTo(points.get(i));
209
                    gpx1.moveTo(points.get(i));
210
                } else {
211
                    gpx.lineTo(points.get(i));
212
                    gpx1.lineTo(points.get(i));
213
                }
214

    
215
            }
216
            gpx.lineTo(createPoint(x, y));
217
            gpx.closePath();
218
            gpx1.closePath();
219

    
220
            Geometry geom = createSurface(gpx);
221
            Geometry geom1 = createSurface(gpx1);
222

    
223
            renderer.draw(geom1, mapControlManager.getSelectionSymbol());
224
            renderer.draw(geom, mapControlManager.getGeometrySelectionSymbol());
225
        }
226
    }
227

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

    
278
            } else
279
                if (s.equals(PluginServices.getText(this, "cancel"))) {
280
                    points.clear();
281
                }
282
        } catch (DataException e1) {
283
            e1.printStackTrace();
284
        } finally {
285
            if (iterator != null) {
286
                iterator.dispose();
287
            }
288
        }
289
    }
290

    
291
    /*
292
     * (non-Javadoc)
293
     * 
294
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
295
     */
296
    public void addValue(double d) {
297
    }
298

    
299
    private Geometry createNewPolygon(Geometry geometry, List<Point> points) {
300
        GeneralPathX newGp = new GeneralPathX();
301
        double[] theData = new double[6];
302

    
303
        PathIterator theIterator;
304
        int theType;
305
        int numParts = 0;
306

    
307
        theIterator = geometry.getPathIterator(null, geomManager.getFlatness());
308
        while (!theIterator.isDone()) {
309
            theType = theIterator.currentSegment(theData);
310
            switch (theType) {
311

    
312
            case PathIterator.SEG_MOVETO:
313
                numParts++;
314
                newGp.moveTo(theData[0], theData[1]);
315
                break;
316

    
317
            case PathIterator.SEG_LINETO:
318
                newGp.lineTo(theData[0], theData[1]);
319
                break;
320

    
321
            case PathIterator.SEG_QUADTO:
322
                newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
323
                break;
324

    
325
            case PathIterator.SEG_CUBICTO:
326
                newGp.curveTo(theData[0], theData[1], theData[2], theData[3],
327
                    theData[4], theData[5]);
328
                break;
329

    
330
            case PathIterator.SEG_CLOSE:
331
                newGp.closePath();
332
                break;
333
            } // end switch
334

    
335
            theIterator.next();
336
        } // end while loop
337
        GeneralPathX gpxInternal = new GeneralPathX();
338
        gpxInternal.moveTo(points.get(points.size() - 1));
339
        for (int i = points.size() - 2; i >= 0; i--) {
340
            gpxInternal.lineTo(points.get(i));
341
        }
342
        gpxInternal.closePath();
343

    
344
        if (!gpxInternal.isCCW()) {
345
            gpxInternal.flip();
346
        }
347
        newGp.append(gpxInternal.getPathIterator(null), false);
348

    
349
        return createSurface(newGp);
350
    }
351

    
352
    private Geometry createNewPolygonGC(MultiPrimitive multiPrimitive,
353
        List<Point> points) {
354
        MultiPrimitive multiPrimitiveAux = createMultiPrimitive();
355

    
356
        for (int i = 0; i < multiPrimitive.getPrimitivesNumber(); i++) {
357
            multiPrimitiveAux.addPrimitive(multiPrimitive.getPrimitiveAt(i));
358
        }
359

    
360
        GeneralPathX gpx = new GeneralPathX();
361
        gpx.moveTo(points.get(points.size() - 1));
362
        for (int i = points.size() - 2; i >= 0; i--) {
363
            gpx.lineTo(points.get(i));
364
            multiPrimitiveAux.addPrimitive(createCurve(gpx));
365
            gpx = new GeneralPathX();
366
            gpx.moveTo(points.get(i));
367
        }
368
        gpx.closePath();
369
        multiPrimitiveAux.addPrimitive(createCurve(gpx));
370

    
371
        return multiPrimitiveAux;
372
    }
373

    
374
    public String getName() {
375
        return PluginServices.getText(this, "internal_polygon_");
376
    }
377

    
378
    public String toString() {
379
        return "_internalpolygon";
380
    }
381

    
382
    public boolean isApplicable(int shapeType) {
383
        switch (shapeType) {
384
        case Geometry.TYPES.POINT:
385
        case Geometry.TYPES.CURVE:
386
        case Geometry.TYPES.MULTIPOINT:
387
            return false;
388
        }
389
        return true;
390
    }
391
}