Statistics
| Revision:

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

History | View | Annotate | Download (7.19 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 org.gvsig.editing.gui.cad.tools;
42

    
43
import java.awt.Image;
44
import java.awt.event.InputEvent;
45
import java.awt.geom.Point2D;
46

    
47
import org.gvsig.andami.PluginServices;
48
import org.gvsig.editing.gui.cad.DefaultCADTool;
49
import org.gvsig.editing.gui.cad.exception.CommandException;
50
import org.gvsig.editing.gui.cad.tools.smc.LineCADToolContext;
51
import org.gvsig.editing.gui.cad.tools.smc.LineCADToolContext.LineCADToolState;
52
import org.gvsig.editing.layers.VectorialLayerEdited;
53
import org.gvsig.fmap.geom.Geometry;
54
import org.gvsig.fmap.geom.primitive.GeneralPathX;
55
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
56

    
57

    
58

    
59
/**
60
 * DOCUMENT ME!
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class LineCADTool extends DefaultCADTool {
65
    protected LineCADToolContext _fsm;
66
    protected Point2D firstPoint;
67
    protected Point2D lastPoint;
68
    protected double angle;
69
    protected double length;
70

    
71
    /**
72
     * Crea un nuevo LineCADTool.
73
     */
74
    public LineCADTool() {
75

    
76
    }
77

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

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

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

    
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
102
     */
103
    public void transition(String s) throws CommandException {
104
            if (!super.changeCommand(s)){
105
                    _fsm.addOption(s);
106
            }
107
    }
108

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

    
121
        if (status.equals("Line.FirstPoint")) {
122
            firstPoint = new Point2D.Double(x, y);
123
        } else if (status == "Line.SecondPointOrAngle") {
124
            lastPoint = new Point2D.Double(x, y);
125

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

    
135
            double w = (Math.cos(Math.toRadians(angle))) * length;
136
            double h = (Math.sin(Math.toRadians(angle))) * length;
137
            lastPoint = new Point2D.Double(firstPoint.getX() + w,
138
                    firstPoint.getY() + h);
139

    
140
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
141
                    2);
142
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
143
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
144
            insertAndSelectGeometry(createCurve(elShape));
145

    
146
            firstPoint = (Point2D) lastPoint.clone();
147
        }
148
    }
149

    
150
    /**
151
     * M?todo para dibujar la lo necesario para el estado en el que nos
152
     * encontremos.
153
     *
154
     * @param g Graphics sobre el que dibujar.
155
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
156
     * @param x par?metro x del punto que se pase para dibujar.
157
     * @param y par?metro x del punto que se pase para dibujar.
158
     */
159
    public void drawOperation(MapControlDrawer renderer,double x,
160
        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),mapControlManager.getGeometrySelectionSymbol());
168
            }
169
        }
170
    }
171

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

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

    
189
        if (status == "Line.SecondPointOrAngle") {
190
            angle = d;
191
        } else if (status == "Line.LenghtOrPoint") {
192
            length = d;
193

    
194
            double w = Math.cos(Math.toRadians(angle)) * length;
195
            double h = Math.sin(Math.toRadians(angle)) * length;
196
            lastPoint = new Point2D.Double(firstPoint.getX() + w,
197
                    firstPoint.getY() + h);
198

    
199
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
200
                    2);
201
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
202
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
203
            insertAndSelectGeometry(createCurve(elShape));
204
            firstPoint = (Point2D) lastPoint.clone();
205
        }
206
    }
207

    
208
        public String getName() {
209
                return PluginServices.getText(this,"line_");
210
        }
211

    
212
        public String toString() {
213
                return "_line";
214
        }
215
        public boolean isApplicable(int shapeType) {
216
                switch (shapeType) {
217
                case Geometry.TYPES.POINT:
218
                case Geometry.TYPES.SURFACE:
219
                case Geometry.TYPES.MULTIPOINT:
220
                        return false;
221
                }
222
                return true;
223
        }
224
}