Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / LineCADTool.java @ 40557

History | View | Annotate | Download (6.56 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing.gui.cad.tools;
25

    
26
import java.awt.event.InputEvent;
27
import java.awt.geom.Point2D;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.editing.gui.cad.exception.CommandException;
31
import org.gvsig.editing.gui.cad.tools.smc.LineCADToolContext;
32
import org.gvsig.editing.gui.cad.tools.smc.LineCADToolContext.LineCADToolState;
33
import org.gvsig.fmap.geom.primitive.GeneralPathX;
34
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
35

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

    
43
    protected LineCADToolContext _fsm;
44
    protected Point2D firstPoint;
45
    protected Point2D lastPoint;
46
    protected double angle;
47
    protected double length;
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 LineCADToolContext(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
        LineCADToolState actualState =
84
            (LineCADToolState) _fsm.getPreviousState();
85
        String status = actualState.getName();
86

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

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

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

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

    
115
                    firstPoint = (Point2D) lastPoint.clone();
116
                }
117
    }
118

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

    
136
        if ((status != "Line.FirstPoint")) { // || (status == "5")) {
137

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

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

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

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

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

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

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

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

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