Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2057 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / PolygonCADTool.java @ 39154

History | View | Annotate | Download (12.1 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.event.InputEvent;
25
import java.awt.geom.Point2D;
26

    
27
import javax.swing.JOptionPane;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.app.ApplicationLocator;
31
import org.gvsig.editing.gui.cad.exception.CommandException;
32
import org.gvsig.editing.gui.cad.exception.ValueException;
33
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext;
34
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.GeometryLocator;
37
import org.gvsig.fmap.geom.exception.CreateGeometryException;
38
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
39
import org.gvsig.fmap.geom.util.UtilFunctions;
40
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
41
import org.gvsig.i18n.Messages;
42
import org.gvsig.tools.locator.LocatorException;
43

    
44
/**
45
 * DOCUMENT ME!
46
 * 
47
 * @author Vicente Caballero Navarro
48
 */
49
public class PolygonCADTool extends AbstractCurveSurfaceCADTool {
50

    
51
    protected PolygonCADToolContext _fsm;
52
    protected Point2D center;
53
    protected int numLines = 5;
54
    protected boolean isI = true;
55

    
56
    /**
57
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
58
     * carga previa a la utilizaci?n de la herramienta.
59
     */
60
    public void init() {
61
        _fsm = new PolygonCADToolContext(this);
62
    }
63

    
64
    public void transition(double x, double y, InputEvent event) {
65
        _fsm.addPoint(x, y, event);
66
    }
67

    
68
    public void transition(double d) {
69
        _fsm.addValue(d);
70
    }
71

    
72
    public void transition(String s) throws CommandException {
73
        if (!super.changeCommand(s)) {
74
            _fsm.addOption(s);
75
        }
76
    }
77

    
78
    /**
79
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
80
     * editableFeatureSource que ya estar? creado.
81
     * 
82
     * @param sel
83
     *            Bitset con las geometr?as que est?n seleccionadas.
84
     * @param x
85
     *            par?metro x del punto que se pase en esta transici?n.
86
     * @param y
87
     *            par?metro y del punto que se pase en esta transici?n.
88
     */
89
    public void addPoint(double x, double y, InputEvent event) {
90
        PolygonCADToolState actualState =
91
            (PolygonCADToolState) _fsm.getPreviousState();
92
        String status = actualState.getName();
93

    
94
        if (status.equals("Polygon.NumberOrCenterPoint")) {
95
            center = new Point2D.Double(x, y);
96
        } else
97
            if (status.equals("Polygon.CenterPoint")) {
98
                center = new Point2D.Double(x, y);
99
            } else
100
                if (status.equals("Polygon.OptionOrRadiusOrPoint")
101
                    || status.equals("Polygon.RadiusOrPoint")) {
102
                    Point2D point = new Point2D.Double(x, y);
103
                    // Pol?gono a partir de la circunferencia.
104
                    if (isI) {
105
                        insertAndSelectGeometry(getIPolygon(point,
106
                            point.distance(center)));
107
                    } else {
108
                        insertAndSelectGeometry(getCPolygon(point,
109
                            point.distance(center)));
110
                    }
111
                }
112
    }
113

    
114
    /**
115
     * M?todo para dibujar la lo necesario para el estado en el que nos
116
     * encontremos.
117
     * 
118
     * @param g
119
     *            Graphics sobre el que dibujar.
120
     * @param selectedGeometries
121
     *            BitSet con las geometr?as seleccionadas.
122
     * @param x
123
     *            par?metro x del punto que se pase para dibujar.
124
     * @param y
125
     *            par?metro x del punto que se pase para dibujar.
126
     */
127
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
128
        PolygonCADToolState actualState = _fsm.getState();
129
        String status = actualState.getName();
130

    
131
        if (status.equals("Polygon.OptionOrRadiusOrPoint")
132
            || status.equals("Polygon.RadiusOrPoint")) {
133
            Point2D point = new Point2D.Double(x, y);
134
            renderer.drawLine(center, point,
135
                mapControlManager.getGeometrySelectionSymbol());
136

    
137
            Geometry help_geom = null;
138
            Geometry help_geom_perim = null;
139
            
140
            /*
141
             * Get helping polygon
142
             */
143
            if (isI) {
144
                help_geom = getIPolygon(point, point.distance(center));
145
            } else {
146
                help_geom = getCPolygon(point, point.distance(center));
147
            }
148

    
149
            /*
150
             * Get and draw perimeter of polygon
151
             */
152
            try {
153
                help_geom_perim = GeometryLocator.
154
                    getGeometryManager().createCurve(
155
                        help_geom.getGeneralPath(),
156
                        Geometry.SUBTYPES.GEOM2D);
157
                renderer.draw(help_geom_perim,
158
                    mapControlManager.getGeometrySelectionSymbol());
159
            } catch (Exception e) {
160
                ApplicationLocator.getManager().message(
161
                    Messages.getText("_Unable_to_draw_help_geometry"),
162
                    JOptionPane.ERROR_MESSAGE);
163
            }
164

    
165

    
166
            /*
167
             * Create circle
168
             */
169
            help_geom = createCircle(center, point.distance(center));
170
            
171
            /*
172
             * Get and draw perimeter of circle
173
             */
174
            try {
175
                help_geom_perim = GeometryLocator.
176
                    getGeometryManager().createCurve(
177
                        help_geom.getGeneralPath(),
178
                        Geometry.SUBTYPES.GEOM2D);
179
                renderer.draw(help_geom_perim,
180
                    mapControlManager.getAxisReferenceSymbol());
181
            } catch (Exception e) {
182
                ApplicationLocator.getManager().message(
183
                    Messages.getText("_Unable_to_draw_help_geometry"),
184
                    JOptionPane.ERROR_MESSAGE);
185
            }
186

    
187
        }
188
    }
189

    
190
    /**
191
     * Add a diferent option.
192
     * 
193
     * @param sel
194
     *            DOCUMENT ME!
195
     * @param s
196
     *            Diferent option.
197
     */
198
    public void addOption(String s) {
199
        PolygonCADToolState actualState =
200
            (PolygonCADToolState) _fsm.getPreviousState();
201
        String status = actualState.getName();
202

    
203
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
204
            if (s.equalsIgnoreCase(PluginServices.getText(this,
205
                "PolygonCADTool.circumscribed"))
206
                || s.equals(PluginServices.getText(this, "circumscribed"))) {
207
                isI = false;
208
            } else
209
                if (s.equalsIgnoreCase(PluginServices.getText(this,
210
                    "PolygonCADTool.into_circle"))
211
                    || s.equals(PluginServices.getText(this, "into_circle"))) {
212
                    isI = true;
213
                }
214
        }
215
    }
216

    
217
    public void addValue(double d) {
218
        PolygonCADToolState actualState =
219
            (PolygonCADToolState) _fsm.getPreviousState();
220
        String status = actualState.getName();
221

    
222
        if (status.equals("Polygon.NumberOrCenterPoint")) {
223
            numLines = (int) d;
224
        } else
225
            if (status.equals("Polygon.OptionOrRadiusOrPoint")
226
                || status.equals("Polygon.RadiusOrPoint")) {
227
                double radio = d;
228

    
229
                // Pol?gono a partir de radio.
230
                Point2D point =
231
                    UtilFunctions.getPoint(center,
232
                        new Point2D.Double(center.getX(), center.getY() + 10),
233
                        radio);
234

    
235
                if (isI) {
236
                    insertAndSelectGeometry(getIPolygon(point, radio));
237
                } else {
238
                    insertAndSelectGeometry(getCPolygon(point, radio));
239
                }
240
            }
241
    }
242

    
243
    /**
244
     * Devuelve la geometr?a con el poligono regular circunscrito a la
245
     * circunferencia formada por el punto central y el radio que se froma
246
     * entre este y el puntero del rat?n..
247
     * 
248
     * @param point
249
     *            Puntero del rat?n.
250
     * @param radio
251
     *            Radio
252
     * 
253
     * @return GeometryCollection con las geometr?as del pol?gono.
254
     */
255
    private Geometry getCPolygon(Point2D point, double radio) {
256
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
257

    
258
        double initangle = UtilFunctions.getAngle(center, point);
259
        Point2D antPoint = p1;
260
        Point2D antInter = null;
261
        double an = (Math.PI * 2) / numLines;
262
        boolean firstTime = true;
263

    
264
        OrientablePrimitive orientablePrimitive = createOrientablePrimitive();
265

    
266
        for (int i = numLines - 1; i >= 0; i--) {
267
            Point2D p2 =
268
                UtilFunctions.getPoint(center, (an * i) + initangle, radio);
269
            Point2D[] ps1 =
270
                UtilFunctions.getPerpendicular(antPoint, center, antPoint);
271
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
272
            Point2D inter =
273
                UtilFunctions.getIntersection(ps1[0], ps1[1], ps2[0], ps2[1]);
274

    
275
            if (antInter != null) {
276

    
277
                if (firstTime) {
278
                    orientablePrimitive.addMoveToVertex(createPoint(
279
                        antInter.getX(), antInter.getY()));
280
                    firstTime = false;
281
                }
282
                orientablePrimitive.addVertex(createPoint(inter.getX(),
283
                    inter.getY()));
284
            }
285

    
286
            antInter = inter;
287
            antPoint = p2;
288
        }
289
        orientablePrimitive.closePrimitive();
290

    
291
        return orientablePrimitive;
292
    }
293

    
294
    /**
295
     * Devuelve la geometr?a con el poligono regular inscrito en la
296
     * circunferencia.
297
     * 
298
     * @param point
299
     *            Puntero del rat?n.
300
     * @param radio
301
     *            Radio
302
     * 
303
     * @return GeometryCollection con las geometr?as del pol?gono.
304
     * @throws ValueException
305
     */
306
    private Geometry getIPolygon(Point2D point, double radio) {
307
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
308
        double initangle = UtilFunctions.getAngle(center, point);
309
        Point2D antPoint = p1;
310
        double an = (Math.PI * 2) / numLines;
311
        boolean firstTime = true;
312

    
313
        OrientablePrimitive orientablePrimitive = createOrientablePrimitive();
314

    
315
        for (int i = numLines - 1; i > 0; i--) {
316
            Point2D p2 =
317
                UtilFunctions.getPoint(center, (an * i) + initangle, radio);
318

    
319
            if (firstTime) {
320
                orientablePrimitive.addMoveToVertex(createPoint(
321
                    antPoint.getX(), antPoint.getY()));
322
                firstTime = false;
323
            }
324

    
325
            orientablePrimitive.addVertex(createPoint(p2.getX(), p2.getY()));
326

    
327
            antPoint = p2;
328
        }
329
        orientablePrimitive.closePrimitive();
330

    
331
        return orientablePrimitive;
332
    }
333

    
334
    public String getName() {
335
        return PluginServices.getText(this, "polygon_");
336
    }
337

    
338
    /**
339
     * Devuelve la cadena que corresponde al estado en el que nos encontramos.
340
     * 
341
     * @return Cadena para mostrar por consola.
342
     */
343
    public String getQuestion() {
344
        PolygonCADToolState actualState = _fsm.getState();
345
        String status = actualState.getName();
346

    
347
        if (status.equals("Polygon.NumberOrCenterPoint")) {
348
            return super.getQuestion() + "<" + numLines + ">";
349
        }
350
        return super.getQuestion();
351

    
352
    }
353

    
354
    public String toString() {
355
        return "_polygon";
356
    }
357

    
358
}