Statistics
| Revision:

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

History | View | Annotate | Download (10.5 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 org.gvsig.andami.PluginServices;
28
import org.gvsig.editing.gui.cad.exception.CommandException;
29
import org.gvsig.editing.gui.cad.exception.ValueException;
30
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext;
31
import org.gvsig.editing.gui.cad.tools.smc.PolygonCADToolContext.PolygonCADToolState;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
34
import org.gvsig.fmap.geom.util.UtilFunctions;
35
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
36

    
37
/**
38
 * DOCUMENT ME!
39
 * 
40
 * @author Vicente Caballero Navarro
41
 */
42
public class PolygonCADTool extends AbstractCurveSurfaceCADTool {
43

    
44
    protected PolygonCADToolContext _fsm;
45
    protected Point2D center;
46
    protected int numLines = 5;
47
    protected boolean isI = true;
48

    
49
    /**
50
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
51
     * carga previa a la utilizaci?n de la herramienta.
52
     */
53
    public void init() {
54
        _fsm = new PolygonCADToolContext(this);
55
    }
56

    
57
    public void transition(double x, double y, InputEvent event) {
58
        _fsm.addPoint(x, y, event);
59
    }
60

    
61
    public void transition(double d) {
62
        _fsm.addValue(d);
63
    }
64

    
65
    public void transition(String s) throws CommandException {
66
        if (!super.changeCommand(s)) {
67
            _fsm.addOption(s);
68
        }
69
    }
70

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

    
87
        if (status.equals("Polygon.NumberOrCenterPoint")) {
88
            center = new Point2D.Double(x, y);
89
        } else
90
            if (status.equals("Polygon.CenterPoint")) {
91
                center = new Point2D.Double(x, y);
92
            } else
93
                if (status.equals("Polygon.OptionOrRadiusOrPoint")
94
                    || status.equals("Polygon.RadiusOrPoint")) {
95
                    Point2D point = new Point2D.Double(x, y);
96
                    // Pol?gono a partir de la circunferencia.
97
                    if (isI) {
98
                        insertAndSelectGeometry(getIPolygon(point,
99
                            point.distance(center)));
100
                    } else {
101
                        insertAndSelectGeometry(getCPolygon(point,
102
                            point.distance(center)));
103
                    }
104
                }
105
    }
106

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

    
124
        if (status.equals("Polygon.OptionOrRadiusOrPoint")
125
            || status.equals("Polygon.RadiusOrPoint")) {
126
            Point2D point = new Point2D.Double(x, y);
127
            renderer.drawLine(center, point,
128
                mapControlManager.getGeometrySelectionSymbol());
129

    
130
            if (isI) {
131
                renderer.draw(getIPolygon(point, point.distance(center)),
132
                    mapControlManager.getGeometrySelectionSymbol());
133
            } else {
134
                renderer.draw(getCPolygon(point, point.distance(center)),
135
                    mapControlManager.getGeometrySelectionSymbol());
136
            }
137
            renderer.draw(createCircle(center, point.distance(center)),
138
                mapControlManager.getAxisReferenceSymbol());
139
        }
140
    }
141

    
142
    /**
143
     * Add a diferent option.
144
     * 
145
     * @param sel
146
     *            DOCUMENT ME!
147
     * @param s
148
     *            Diferent option.
149
     */
150
    public void addOption(String s) {
151
        PolygonCADToolState actualState =
152
            (PolygonCADToolState) _fsm.getPreviousState();
153
        String status = actualState.getName();
154

    
155
        if (status.equals("Polygon.OptionOrRadiusOrPoint")) {
156
            if (s.equalsIgnoreCase(PluginServices.getText(this,
157
                "PolygonCADTool.circumscribed"))
158
                || s.equals(PluginServices.getText(this, "circumscribed"))) {
159
                isI = false;
160
            } else
161
                if (s.equalsIgnoreCase(PluginServices.getText(this,
162
                    "PolygonCADTool.into_circle"))
163
                    || s.equals(PluginServices.getText(this, "into_circle"))) {
164
                    isI = true;
165
                }
166
        }
167
    }
168

    
169
    public void addValue(double d) {
170
        PolygonCADToolState actualState =
171
            (PolygonCADToolState) _fsm.getPreviousState();
172
        String status = actualState.getName();
173

    
174
        if (status.equals("Polygon.NumberOrCenterPoint")) {
175
            numLines = (int) d;
176
        } else
177
            if (status.equals("Polygon.OptionOrRadiusOrPoint")
178
                || status.equals("Polygon.RadiusOrPoint")) {
179
                double radio = d;
180

    
181
                // Pol?gono a partir de radio.
182
                Point2D point =
183
                    UtilFunctions.getPoint(center,
184
                        new Point2D.Double(center.getX(), center.getY() + 10),
185
                        radio);
186

    
187
                if (isI) {
188
                    insertAndSelectGeometry(getIPolygon(point, radio));
189
                } else {
190
                    insertAndSelectGeometry(getCPolygon(point, radio));
191
                }
192
            }
193
    }
194

    
195
    /**
196
     * Devuelve la geometr?a con el poligono regular circunscrito a la
197
     * circunferencia formada por el punto central y el radio que se froma
198
     * entre este y el puntero del rat?n..
199
     * 
200
     * @param point
201
     *            Puntero del rat?n.
202
     * @param radio
203
     *            Radio
204
     * 
205
     * @return GeometryCollection con las geometr?as del pol?gono.
206
     */
207
    private Geometry getCPolygon(Point2D point, double radio) {
208
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
209

    
210
        double initangle = UtilFunctions.getAngle(center, point);
211
        Point2D antPoint = p1;
212
        Point2D antInter = null;
213
        double an = (Math.PI * 2) / numLines;
214
        boolean firstTime = true;
215

    
216
        OrientablePrimitive orientablePrimitive = createOrientablePrimitive();
217

    
218
        for (int i = numLines - 1; i >= 0; i--) {
219
            Point2D p2 =
220
                UtilFunctions.getPoint(center, (an * i) + initangle, radio);
221
            Point2D[] ps1 =
222
                UtilFunctions.getPerpendicular(antPoint, center, antPoint);
223
            Point2D[] ps2 = UtilFunctions.getPerpendicular(p2, center, p2);
224
            Point2D inter =
225
                UtilFunctions.getIntersection(ps1[0], ps1[1], ps2[0], ps2[1]);
226

    
227
            if (antInter != null) {
228

    
229
                if (firstTime) {
230
                    orientablePrimitive.addMoveToVertex(createPoint(
231
                        antInter.getX(), antInter.getY()));
232
                    firstTime = false;
233
                }
234
                orientablePrimitive.addVertex(createPoint(inter.getX(),
235
                    inter.getY()));
236
            }
237

    
238
            antInter = inter;
239
            antPoint = p2;
240
        }
241
        orientablePrimitive.closePrimitive();
242

    
243
        return orientablePrimitive;
244
    }
245

    
246
    /**
247
     * Devuelve la geometr?a con el poligono regular inscrito en la
248
     * circunferencia.
249
     * 
250
     * @param point
251
     *            Puntero del rat?n.
252
     * @param radio
253
     *            Radio
254
     * 
255
     * @return GeometryCollection con las geometr?as del pol?gono.
256
     * @throws ValueException
257
     */
258
    private Geometry getIPolygon(Point2D point, double radio) {
259
        Point2D p1 = UtilFunctions.getPoint(center, point, radio);
260
        double initangle = UtilFunctions.getAngle(center, point);
261
        Point2D antPoint = p1;
262
        double an = (Math.PI * 2) / numLines;
263
        boolean firstTime = true;
264

    
265
        OrientablePrimitive orientablePrimitive = createOrientablePrimitive();
266

    
267
        for (int i = numLines - 1; i > 0; i--) {
268
            Point2D p2 =
269
                UtilFunctions.getPoint(center, (an * i) + initangle, radio);
270

    
271
            if (firstTime) {
272
                orientablePrimitive.addMoveToVertex(createPoint(
273
                    antPoint.getX(), antPoint.getY()));
274
                firstTime = false;
275
            }
276

    
277
            orientablePrimitive.addVertex(createPoint(p2.getX(), p2.getY()));
278

    
279
            antPoint = p2;
280
        }
281
        orientablePrimitive.closePrimitive();
282

    
283
        return orientablePrimitive;
284
    }
285

    
286
    public String getName() {
287
        return PluginServices.getText(this, "polygon_");
288
    }
289

    
290
    /**
291
     * Devuelve la cadena que corresponde al estado en el que nos encontramos.
292
     * 
293
     * @return Cadena para mostrar por consola.
294
     */
295
    public String getQuestion() {
296
        PolygonCADToolState actualState = _fsm.getState();
297
        String status = actualState.getName();
298

    
299
        if (status.equals("Polygon.NumberOrCenterPoint")) {
300
            return super.getQuestion() + "<" + numLines + ">";
301
        }
302
        return super.getQuestion();
303

    
304
    }
305

    
306
    public String toString() {
307
        return "_polygon";
308
    }
309

    
310
}