Statistics
| Revision:

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

History | View | Annotate | Download (6.51 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.tools.smc.LineCADToolContext;
30
import org.gvsig.editing.gui.cad.tools.smc.LineCADToolContext.LineCADToolState;
31
import org.gvsig.fmap.geom.primitive.GeneralPathX;
32
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
33

    
34
/**
35
 * DOCUMENT ME!
36
 * 
37
 * @author Vicente Caballero Navarro
38
 */
39
public class LineCADTool extends AbstractCurveCADTool {
40

    
41
    protected LineCADToolContext _fsm;
42
    protected Point2D firstPoint;
43
    protected Point2D lastPoint;
44
    protected double angle;
45
    protected double length;
46

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

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

    
59
    public void transition(double d) {
60
        _fsm.addValue(d);
61
    }
62

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

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

    
85
        if (status.equals("Line.FirstPoint")) {
86
            firstPoint = new Point2D.Double(x, y);
87
        } else
88
            if (status == "Line.SecondPointOrAngle") {
89
                lastPoint = new Point2D.Double(x, y);
90

    
91
                GeneralPathX elShape =
92
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
93
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
94
                elShape.lineTo(lastPoint.getX(), lastPoint.getY());
95
                insertAndSelectGeometry(createCurve(elShape));
96
                firstPoint = (Point2D) lastPoint.clone();
97
            } else
98
                if (status == "Line.LenghtOrPoint") {
99
                    length = firstPoint.distance(x, y);
100

    
101
                    double w = (Math.cos(Math.toRadians(angle))) * length;
102
                    double h = (Math.sin(Math.toRadians(angle))) * length;
103
                    lastPoint =
104
                        new Point2D.Double(firstPoint.getX() + w,
105
                            firstPoint.getY() + h);
106

    
107
                    GeneralPathX elShape =
108
                        new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
109
                    elShape.moveTo(firstPoint.getX(), firstPoint.getY());
110
                    elShape.lineTo(lastPoint.getX(), lastPoint.getY());
111
                    insertAndSelectGeometry(createCurve(elShape));
112

    
113
                    firstPoint = (Point2D) lastPoint.clone();
114
                }
115
    }
116

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

    
134
        if ((status != "Line.FirstPoint")) { // || (status == "5")) {
135

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

    
143
    /**
144
     * Add a diferent option.
145
     * 
146
     * @param sel
147
     *            DOCUMENT ME!
148
     * @param s
149
     *            Diferent option.
150
     */
151
    public void addOption(String s) {
152
        // Nothing to do
153
    }
154

    
155
    public void addValue(double d) {
156
        LineCADToolState actualState =
157
            (LineCADToolState) _fsm.getPreviousState();
158
        String status = actualState.getName();
159

    
160
        if (status == "Line.SecondPointOrAngle") {
161
            angle = d;
162
        } else
163
            if (status == "Line.LenghtOrPoint") {
164
                length = d;
165

    
166
                double w = Math.cos(Math.toRadians(angle)) * length;
167
                double h = Math.sin(Math.toRadians(angle)) * length;
168
                lastPoint =
169
                    new Point2D.Double(firstPoint.getX() + w, firstPoint.getY()
170
                        + h);
171

    
172
                GeneralPathX elShape =
173
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
174
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
175
                elShape.lineTo(lastPoint.getX(), lastPoint.getY());
176
                insertAndSelectGeometry(createCurve(elShape));
177
                firstPoint = (Point2D) lastPoint.clone();
178
            }
179
    }
180

    
181
    public String getName() {
182
        return PluginServices.getText(this, "line_");
183
    }
184

    
185
    public String toString() {
186
        return "_line";
187
    }
188

    
189
    @Override
190
    protected int getSupportedPrimitiveGeometryType() {
191
        return CURVE;
192
    }
193
}