Statistics
| Revision:

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

History | View | Annotate | Download (6.52 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.geom.Point2D;
46

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

    
53

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

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

    
71
    }
72

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

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

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

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

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

    
114
        if (status.equals("ExecuteMap.Initial")) {
115
            firstPoint = new Point2D.Double(x, y);
116
        } else if (status == "ExecuteMap.First") {
117
            lastPoint = new Point2D.Double(x, y);
118

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

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

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

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

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

    
157
        if ((status != "ExecuteMap.Initial")) { // || (status == "5")) {
158

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

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

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

    
182
        if (status == "ExecuteMap.Second") {
183
            angle = d;
184
        } else if (status == "ExecuteMap.Third") {
185
            length = d;
186

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

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

    
201
}