Statistics
| Revision:

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

History | View | Annotate | Download (7.49 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.DefaultCADTool;
29
import org.gvsig.editing.gui.cad.exception.CommandException;
30
import org.gvsig.editing.gui.cad.tools.smc.LineCADToolContext;
31
import org.gvsig.editing.gui.cad.tools.smc.LineCADToolContext.LineCADToolState;
32
import org.gvsig.fmap.geom.Geometry;
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 DefaultCADTool {
42

    
43
    protected LineCADToolContext _fsm;
44
    protected Point2D firstPoint;
45
    protected Point2D lastPoint;
46
    protected double angle;
47
    protected double length;
48

    
49
    /**
50
     * Crea un nuevo LineCADTool.
51
     */
52
    public LineCADTool() {
53

    
54
    }
55

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

    
64
    /*
65
     * (non-Javadoc)
66
     * 
67
     * @see
68
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
69
     * .layers.FBitSet, double, double)
70
     */
71
    public void transition(double x, double y, InputEvent event) {
72
        _fsm.addPoint(x, y, event);
73
    }
74

    
75
    /*
76
     * (non-Javadoc)
77
     * 
78
     * @see
79
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
80
     * .layers.FBitSet, double)
81
     */
82
    public void transition(double d) {
83
        _fsm.addValue(d);
84
    }
85

    
86
    /*
87
     * (non-Javadoc)
88
     * 
89
     * @see
90
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
91
     * .layers.FBitSet, java.lang.String)
92
     */
93
    public void transition(String s) throws CommandException {
94
        if (!super.changeCommand(s)) {
95
            _fsm.addOption(s);
96
        }
97
    }
98

    
99
    /**
100
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
101
     * editableFeatureSource que ya estar? creado.
102
     * 
103
     * @param sel
104
     *            Bitset con las geometr?as que est?n seleccionadas.
105
     * @param x
106
     *            par?metro x del punto que se pase en esta transici?n.
107
     * @param y
108
     *            par?metro y del punto que se pase en esta transici?n.
109
     */
110
    public void addPoint(double x, double y, InputEvent event) {
111
        LineCADToolState actualState =
112
            (LineCADToolState) _fsm.getPreviousState();
113
        String status = actualState.getName();
114

    
115
        if (status.equals("Line.FirstPoint")) {
116
            firstPoint = new Point2D.Double(x, y);
117
        } else
118
            if (status == "Line.SecondPointOrAngle") {
119
                lastPoint = new Point2D.Double(x, y);
120

    
121
                GeneralPathX elShape =
122
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
123
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
124
                elShape.lineTo(lastPoint.getX(), lastPoint.getY());
125
                insertAndSelectGeometry(createCurve(elShape));
126
                firstPoint = (Point2D) lastPoint.clone();
127
            } else
128
                if (status == "Line.LenghtOrPoint") {
129
                    length = firstPoint.distance(x, y);
130

    
131
                    double w = (Math.cos(Math.toRadians(angle))) * length;
132
                    double h = (Math.sin(Math.toRadians(angle))) * length;
133
                    lastPoint =
134
                        new Point2D.Double(firstPoint.getX() + w,
135
                            firstPoint.getY() + h);
136

    
137
                    GeneralPathX elShape =
138
                        new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
139
                    elShape.moveTo(firstPoint.getX(), firstPoint.getY());
140
                    elShape.lineTo(lastPoint.getX(), lastPoint.getY());
141
                    insertAndSelectGeometry(createCurve(elShape));
142

    
143
                    firstPoint = (Point2D) lastPoint.clone();
144
                }
145
    }
146

    
147
    /**
148
     * M?todo para dibujar la lo necesario para el estado en el que nos
149
     * encontremos.
150
     * 
151
     * @param g
152
     *            Graphics sobre el que dibujar.
153
     * @param selectedGeometries
154
     *            BitSet con las geometr?as seleccionadas.
155
     * @param x
156
     *            par?metro x del punto que se pase para dibujar.
157
     * @param y
158
     *            par?metro x del punto que se pase para dibujar.
159
     */
160
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
161
        LineCADToolState actualState = _fsm.getState();
162
        String status = actualState.getName();
163

    
164
        if ((status != "Line.FirstPoint")) { // || (status == "5")) {
165

    
166
            if (firstPoint != null) {
167
                renderer.drawLine(firstPoint, new Point2D.Double(x, y),
168
                    mapControlManager.getGeometrySelectionSymbol());
169
            }
170
        }
171
    }
172

    
173
    /**
174
     * Add a diferent option.
175
     * 
176
     * @param sel
177
     *            DOCUMENT ME!
178
     * @param s
179
     *            Diferent option.
180
     */
181
    public void addOption(String s) {
182
        // TODO Auto-generated method stub
183
    }
184

    
185
    /*
186
     * (non-Javadoc)
187
     * 
188
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
189
     */
190
    public void addValue(double d) {
191
        LineCADToolState actualState =
192
            (LineCADToolState) _fsm.getPreviousState();
193
        String status = actualState.getName();
194

    
195
        if (status == "Line.SecondPointOrAngle") {
196
            angle = d;
197
        } else
198
            if (status == "Line.LenghtOrPoint") {
199
                length = d;
200

    
201
                double w = Math.cos(Math.toRadians(angle)) * length;
202
                double h = Math.sin(Math.toRadians(angle)) * length;
203
                lastPoint =
204
                    new Point2D.Double(firstPoint.getX() + w, firstPoint.getY()
205
                        + h);
206

    
207
                GeneralPathX elShape =
208
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
209
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
210
                elShape.lineTo(lastPoint.getX(), lastPoint.getY());
211
                insertAndSelectGeometry(createCurve(elShape));
212
                firstPoint = (Point2D) lastPoint.clone();
213
            }
214
    }
215

    
216
    public String getName() {
217
        return PluginServices.getText(this, "line_");
218
    }
219

    
220
    public String toString() {
221
        return "_line";
222
    }
223

    
224
    public boolean isApplicable(int shapeType) {
225
        switch (shapeType) {
226
        case Geometry.TYPES.POINT:
227
        case Geometry.TYPES.MULTIPOINT:
228
        case Geometry.TYPES.SURFACE:
229
        case Geometry.TYPES.MULTISURFACE:
230

    
231
            return false;
232
        }
233
        return true;
234
    }
235
}