Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1004 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / LineCADTool.java @ 12319

History | View | Annotate | Download (7.14 KB)

1 3702 caballero
/* 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 3782 caballero
package com.iver.cit.gvsig.gui.cad.tools;
42
43 3832 caballero
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45 4313 fjp
import java.awt.event.InputEvent;
46 3832 caballero
import java.awt.geom.Point2D;
47
48 4584 caballero
import com.iver.andami.PluginServices;
49 5170 caballero
import com.iver.cit.gvsig.fmap.core.FShape;
50 3782 caballero
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
51
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
52 3832 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
53 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
54 3782 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.LineCADToolContext;
55
import com.iver.cit.gvsig.gui.cad.tools.smc.LineCADToolContext.LineCADToolState;
56
57
58
/**
59
 * DOCUMENT ME!
60
 *
61
 * @author Vicente Caballero Navarro
62
 */
63 3712 caballero
public class LineCADTool extends DefaultCADTool {
64
    private LineCADToolContext _fsm;
65 3702 caballero
    private Point2D firstPoint;
66
    private Point2D lastPoint;
67
    private double angle;
68 3744 caballero
    private double length;
69
70 3702 caballero
    /**
71 3744 caballero
     * Crea un nuevo LineCADTool.
72
     */
73
    public LineCADTool() {
74 3883 caballero
75 3744 caballero
    }
76
77
    /**
78 3702 caballero
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
79
     * carga previa a la utilizaci?n de la herramienta.
80
     */
81
    public void init() {
82 3883 caballero
             _fsm = new LineCADToolContext(this);
83 3744 caballero
    }
84 3712 caballero
85 3744 caballero
    /* (non-Javadoc)
86 3782 caballero
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
87
     */
88 4313 fjp
    public void transition(double x, double y, InputEvent event) {
89 4324 caballero
        _fsm.addPoint(x, y, event);
90 3744 caballero
    }
91 3782 caballero
92 3766 caballero
    /* (non-Javadoc)
93 3782 caballero
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
94
     */
95 3832 caballero
    public void transition(double d) {
96
        _fsm.addValue(d);
97 3782 caballero
    }
98 3744 caballero
99 3782 caballero
    /* (non-Javadoc)
100
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
101
     */
102 5735 caballero
    public void transition(String s) throws CommandException {
103 4892 caballero
            if (!super.changeCommand(s)){
104
                    _fsm.addOption(s);
105
            }
106 3782 caballero
    }
107
108 3744 caballero
    /**
109 3702 caballero
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
110
     * editableFeatureSource que ya estar? creado.
111
     *
112
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
113
     * @param x par?metro x del punto que se pase en esta transici?n.
114
     * @param y par?metro y del punto que se pase en esta transici?n.
115
     */
116 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
117 3744 caballero
        LineCADToolState actualState = (LineCADToolState) _fsm.getPreviousState();
118 3702 caballero
        String status = actualState.getName();
119 3782 caballero
120 3978 caballero
        if (status.equals("Line.FirstPoint")) {
121 3744 caballero
            firstPoint = new Point2D.Double(x, y);
122 3978 caballero
        } else if (status == "Line.SecondPointOrAngle") {
123 3744 caballero
            lastPoint = new Point2D.Double(x, y);
124 3782 caballero
125 3766 caballero
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
126 3782 caballero
                    2);
127 3766 caballero
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
128
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
129
            addGeometry(ShapeFactory.createPolyline2D(elShape));
130 3744 caballero
            firstPoint = (Point2D) lastPoint.clone();
131 3978 caballero
        } else if (status == "Line.LenghtOrPoint") {
132 3744 caballero
            length = firstPoint.distance(x, y);
133
134
            double w = (Math.cos(Math.toRadians(angle))) * length;
135
            double h = (Math.sin(Math.toRadians(angle))) * length;
136
            lastPoint = new Point2D.Double(firstPoint.getX() + w,
137
                    firstPoint.getY() + h);
138
139 3766 caballero
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
140 3782 caballero
                    2);
141
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
142
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
143
            addGeometry(ShapeFactory.createPolyline2D(elShape));
144 3744 caballero
145
            firstPoint = (Point2D) lastPoint.clone();
146
        }
147 3702 caballero
    }
148
149
    /**
150 3744 caballero
     * M?todo para dibujar la lo necesario para el estado en el que nos
151
     * encontremos.
152 3702 caballero
     *
153
     * @param g Graphics sobre el que dibujar.
154
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
155
     * @param x par?metro x del punto que se pase para dibujar.
156
     * @param y par?metro x del punto que se pase para dibujar.
157
     */
158 3832 caballero
    public void drawOperation(Graphics g,double x,
159 3702 caballero
        double y) {
160 3744 caballero
        LineCADToolState actualState = _fsm.getState();
161
        String status = actualState.getName();
162 3782 caballero
163 3978 caballero
        if ((status != "Line.FirstPoint")) { // || (status == "5")) {
164 3702 caballero
165 3744 caballero
            if (firstPoint != null) {
166 11436 caballero
                drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.geometrySelectSymbol);
167 3744 caballero
            }
168
        }
169
    }
170
171 3702 caballero
    /**
172
     * Add a diferent option.
173
     *
174 3782 caballero
     * @param sel DOCUMENT ME!
175 3702 caballero
     * @param s Diferent option.
176
     */
177 3832 caballero
    public void addOption(String s) {
178 3702 caballero
        // TODO Auto-generated method stub
179
    }
180 3712 caballero
181 3744 caballero
    /* (non-Javadoc)
182
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
183
     */
184 3832 caballero
    public void addValue(double d) {
185 3744 caballero
        LineCADToolState actualState = (LineCADToolState) _fsm.getPreviousState();
186
        String status = actualState.getName();
187
188 3978 caballero
        if (status == "Line.SecondPointOrAngle") {
189 3744 caballero
            angle = d;
190 3978 caballero
        } else if (status == "Line.LenghtOrPoint") {
191 3744 caballero
            length = d;
192
193
            double w = Math.cos(Math.toRadians(angle)) * length;
194
            double h = Math.sin(Math.toRadians(angle)) * length;
195
            lastPoint = new Point2D.Double(firstPoint.getX() + w,
196
                    firstPoint.getY() + h);
197
198 3766 caballero
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
199 3782 caballero
                    2);
200 3766 caballero
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
201
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
202
            addGeometry(ShapeFactory.createPolyline2D(elShape));
203 3744 caballero
            firstPoint = (Point2D) lastPoint.clone();
204 3782 caballero
        }
205 3744 caballero
    }
206 3883 caballero
207 4118 caballero
        public String getName() {
208 4584 caballero
                return PluginServices.getText(this,"line_");
209 4118 caballero
        }
210
211 4892 caballero
        public String toString() {
212
                return "_line";
213
        }
214 5170 caballero
        public boolean isApplicable(int shapeType) {
215
                switch (shapeType) {
216
                case FShape.POINT:
217
                case FShape.POLYGON:
218 6024 caballero
                case FShape.MULTIPOINT:
219 5170 caballero
                        return false;
220
                }
221
                return true;
222
        }
223 3702 caballero
}