Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2049 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / EllipseCADTool.java @ 38468

History | View | Annotate | Download (6.77 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.EllipseCADToolContext;
33
import org.gvsig.editing.gui.cad.tools.smc.EllipseCADToolContext.EllipseCADToolState;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.primitive.Ellipse;
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 EllipseCADTool extends AbstractSurfaceCADTool {
46

    
47
    protected EllipseCADToolContext _fsm;
48
    protected Point2D startAxis;
49
    protected Point2D endAxis;
50

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

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

    
63
    public void transition(double d) {
64
        _fsm.addValue(d);
65
    }
66

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

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

    
89
        if (status.equals("Ellipse.FirstPointAxis")) {
90
            startAxis = new Point2D.Double(x, y);
91
        } else
92
            if (status.equals("Ellipse.SecondPointAxis")) {
93
                endAxis = new Point2D.Double(x, y);
94
            } else
95
                if (status.equals("Ellipse.DistanceOtherAxis")) {
96
                    Point2D middle =
97
                        new Point2D.Double(
98
                            (startAxis.getX() + endAxis.getX()) / 2,
99
                            (startAxis.getY() + endAxis.getY()) / 2);
100
                    Point2D third = new Point2D.Double(x, y);
101
                    double distance = middle.distance(third);
102
                    insertAndSelectGeometry(createEllipse(startAxis, endAxis,
103
                        distance));
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
        EllipseCADToolState actualState = _fsm.getState();
122
        String status = actualState.getName();
123

    
124
        if (status.equals("Ellipse.SecondPointAxis")) {
125
            renderer.drawLine(startAxis, new Point2D.Double(x, y),
126
                mapControlManager.getGeometrySelectionSymbol());
127
        } else
128
            if (status.equals("Ellipse.DistanceOtherAxis")) {
129
                Point2D middle =
130
                    new Point2D.Double((startAxis.getX() + endAxis.getX()) / 2,
131
                        (startAxis.getY() + endAxis.getY()) / 2);
132

    
133
                Point2D third = new Point2D.Double(x, y);
134

    
135
                double distance = middle.distance(third);
136
                Ellipse ellipse = createEllipse(startAxis, endAxis, distance);
137
                
138
                Geometry help_geom_perim = null;
139
                try {
140
                    help_geom_perim = GeometryLocator.
141
                        getGeometryManager().createCurve(
142
                            ellipse.getGeneralPath(),
143
                            Geometry.SUBTYPES.GEOM2D);
144
                    renderer.draw(help_geom_perim,
145
                        mapControlManager.getAxisReferenceSymbol());
146
                } catch (Exception e) {
147
                    ApplicationLocator.getManager().message(
148
                        Messages.getText("_Unable_to_draw_help_geometry"),
149
                        JOptionPane.ERROR_MESSAGE);
150
                }  
151

    
152
                Point2D mediop =
153
                    new Point2D.Double((startAxis.getX() + endAxis.getX()) / 2,
154
                        (startAxis.getY() + endAxis.getY()) / 2);
155
                renderer.drawLine(mediop, third,
156
                    mapControlManager.getGeometrySelectionSymbol());
157
            }
158
    }
159

    
160
    /**
161
     * Add a diferent option.
162
     * 
163
     * @param sel
164
     *            DOCUMENT ME!
165
     * @param s
166
     *            Diferent option.
167
     */
168
    public void addOption(String s) {
169
        // TODO Auto-generated method stub
170
    }
171

    
172
    public void addValue(double d) {
173
        EllipseCADToolState actualState =
174
            (EllipseCADToolState) _fsm.getPreviousState();
175
        String status = actualState.getName();
176

    
177
        if (status.equals("Ellipse.DistanceOtherAxis")) {
178
            double distance = d;
179
            insertAndSelectGeometry(createEllipse(startAxis, endAxis, distance));
180
        }
181
    }
182

    
183
    public String getName() {
184
        return PluginServices.getText(this, "ellipse_");
185
    }
186

    
187
    public String toString() {
188
        return "_ellipse";
189
    }
190

    
191
    @Override
192
    protected int getSupportedPrimitiveGeometryType() {
193
        return ELLIPSE;
194
    }
195
}