Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / LineCADTool.java @ 4313

History | View | Annotate | Download (6.63 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.cad.tools;
42

    
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.Point2D;
47

    
48
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
49
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
50
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
51
import com.iver.cit.gvsig.gui.cad.tools.smc.LineCADToolContext;
52
import com.iver.cit.gvsig.gui.cad.tools.smc.LineCADToolContext.LineCADToolState;
53

    
54

    
55
/**
56
 * DOCUMENT ME!
57
 *
58
 * @author Vicente Caballero Navarro
59
 */
60
public class LineCADTool extends DefaultCADTool {
61
    private LineCADToolContext _fsm;
62
    private Point2D firstPoint;
63
    private Point2D lastPoint;
64
    private double angle;
65
    private double length;
66

    
67
    /**
68
     * Crea un nuevo LineCADTool.
69
     */
70
    public LineCADTool() {
71

    
72
    }
73

    
74
    /**
75
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
76
     * carga previa a la utilizaci?n de la herramienta.
77
     */
78
    public void init() {
79
             _fsm = new LineCADToolContext(this);
80
    }
81

    
82
    /* (non-Javadoc)
83
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
84
     */
85
    public void transition(double x, double y, InputEvent event) {
86
        _fsm.addPoint(x, y);
87
    }
88

    
89
    /* (non-Javadoc)
90
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
91
     */
92
    public void transition(double d) {
93
        _fsm.addValue(d);
94
    }
95

    
96
    /* (non-Javadoc)
97
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
98
     */
99
    public void transition(String s) {
100
        _fsm.addOption(s);
101
    }
102

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

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

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

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

    
134
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
135
                    2);
136
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
137
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
138
            addGeometry(ShapeFactory.createPolyline2D(elShape));
139

    
140
            firstPoint = (Point2D) lastPoint.clone();
141
        }
142
    }
143

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

    
158
        if ((status != "Line.FirstPoint")) { // || (status == "5")) {
159

    
160
            if (firstPoint != null) {
161
                drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
162
            }
163
        }
164
    }
165

    
166
    /**
167
     * Add a diferent option.
168
     *
169
     * @param sel DOCUMENT ME!
170
     * @param s Diferent option.
171
     */
172
    public void addOption(String s) {
173
        // TODO Auto-generated method stub
174
    }
175

    
176
    /* (non-Javadoc)
177
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
178
     */
179
    public void addValue(double d) {
180
        LineCADToolState actualState = (LineCADToolState) _fsm.getPreviousState();
181
        String status = actualState.getName();
182

    
183
        if (status == "Line.SecondPointOrAngle") {
184
            angle = d;
185
        } else if (status == "Line.LenghtOrPoint") {
186
            length = d;
187

    
188
            double w = Math.cos(Math.toRadians(angle)) * length;
189
            double h = Math.sin(Math.toRadians(angle)) * length;
190
            lastPoint = new Point2D.Double(firstPoint.getX() + w,
191
                    firstPoint.getY() + h);
192

    
193
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
194
                    2);
195
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
196
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
197
            addGeometry(ShapeFactory.createPolyline2D(elShape));
198
            firstPoint = (Point2D) lastPoint.clone();
199
        }
200
    }
201

    
202
        public String getName() {
203
                return "LINEA";
204
        }
205

    
206
}