Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / CircleCADTool.java @ 39348

History | View | Annotate | Download (8.23 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.tools.smc.CircleCADToolContext;
33
import org.gvsig.editing.gui.cad.tools.smc.CircleCADToolContext.CircleCADToolState;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.primitive.Circle;
37
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
38
import org.gvsig.i18n.Messages;
39

    
40
/**
41
 * DOCUMENT ME!
42
 * 
43
 * @author Vicente Caballero Navarro
44
 */
45
public class CircleCADTool extends AbstractSurfaceCADTool {
46

    
47
    protected CircleCADToolContext _fsm;
48
    protected Point2D center;
49
    protected Point2D firstPoint;
50
    protected Point2D secondPoint;
51
    protected Point2D thirdPoint;
52

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

    
61
    public void transition(double x, double y, InputEvent event) {
62
        _fsm.addPoint(x, y, event);
63
    }
64

    
65
    public void transition(double d) {
66
        _fsm.addValue(d);
67
    }
68

    
69
    public void transition(String s) throws CommandException {
70
        if (!super.changeCommand(s)) {
71
            _fsm.addOption(s);
72
        }
73
    }
74

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

    
91
        if (status.equals("Circle.CenterPointOr3p")) {
92
            center = new Point2D.Double(x, y);
93
        } else
94
            if (status == "Circle.PointOrRadius") {
95
                insertAndSelectGeometry(createCircle(createPoint(center),
96
                    createPoint(x, y)));
97
            } else
98
                if (status == "Circle.FirstPoint") {
99
                    firstPoint = new Point2D.Double(x, y);
100
                } else
101
                    if (status == "Circle.SecondPoint") {
102
                        secondPoint = new Point2D.Double(x, y);
103
                    } else
104
                        if (status == "Circle.ThirdPoint") {
105
                            thirdPoint = new Point2D.Double(x, y);
106
                            insertAndSelectGeometry(createCircle(
107
                                createPoint(firstPoint),
108
                                createPoint(secondPoint),
109
                                createPoint(thirdPoint)));
110
                        }
111
    }
112

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

    
130
        Geometry help_geom_perim = null; 
131
        
132
        if ((status == "Circle.CenterPointOr3p")) { // || (status == "5")) {
133

    
134
            if (firstPoint != null) {
135
                renderer.drawLine(firstPoint, new Point2D.Double(x, y),
136
                    mapControlManager.getGeometrySelectionSymbol());
137
            }
138
        }
139

    
140
        if (status == "Circle.PointOrRadius") {
141
            Point2D currentPoint = new Point2D.Double(x, y);
142
            Circle circle =
143
                createCircle(createPoint(center), createPoint(currentPoint));
144
            
145
            try {
146
                help_geom_perim = GeometryLocator.
147
                    getGeometryManager().createCurve(
148
                        circle.getGeneralPath(),
149
                        Geometry.SUBTYPES.GEOM2D);
150
                renderer.draw(help_geom_perim,
151
                    mapControlManager.getAxisReferenceSymbol());
152
            } catch (Exception e) {
153
                ApplicationLocator.getManager().message(
154
                    Messages.getText("_Unable_to_draw_help_geometry"),
155
                    JOptionPane.ERROR_MESSAGE);
156
            }            
157
            
158
            
159
        } else
160
            if (status == "Circle.SecondPoint") {
161
                renderer.drawLine(firstPoint, new Point2D.Double(x, y),
162
                    mapControlManager.getGeometrySelectionSymbol());
163
            } else
164
                if (status == "Circle.ThirdPoint") {
165
                    Point2D currentPoint = new Point2D.Double(x, y);
166
                    Geometry geom =
167
                        createCircle(createPoint(firstPoint),
168
                            createPoint(secondPoint), createPoint(currentPoint));
169

    
170
                    if (geom != null) {
171
                        
172
                        try {
173
                            help_geom_perim = GeometryLocator.
174
                                getGeometryManager().createCurve(
175
                                    geom.getGeneralPath(),
176
                                    Geometry.SUBTYPES.GEOM2D);
177
                            renderer.draw(help_geom_perim,
178
                                mapControlManager.getAxisReferenceSymbol());
179
                        } catch (Exception e) {
180
                            ApplicationLocator.getManager().message(
181
                                Messages.getText("_Unable_to_draw_help_geometry"),
182
                                JOptionPane.ERROR_MESSAGE);
183
                        }            
184
                    }
185
                }
186
    }
187

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

    
201
        if (status == "Circle.CenterPointOr3p") {
202
            if (s.equalsIgnoreCase(PluginServices.getText(this,
203
                "CircleCADTool.3p"))) {
204
                // Opci?n correcta.
205
            }
206
        }
207
    }
208

    
209
    /*
210
     * (non-Javadoc)
211
     * 
212
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
213
     */
214
    public void addValue(double d) {
215
        CircleCADToolState actualState =
216
            (CircleCADToolState) _fsm.getPreviousState();
217
        String status = actualState.getName();
218

    
219
        if (status == "Circle.PointOrRadius") {
220
            insertAndSelectGeometry(createCircle(createPoint(center), d));
221
        }
222
    }
223

    
224
    public String getName() {
225
        return PluginServices.getText(this, "circle_");
226
    }
227

    
228
    public String toString() {
229
        return "_circle";
230
    }
231

    
232
    @Override
233
    protected int getSupportedPrimitiveGeometryType() {
234
        return CIRCLE;
235
    }
236
}