Statistics
| Revision:

svn-gvsig-desktop / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / LineCadTool.java @ 1763

History | View | Annotate | Download (6.49 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 com.iver.cit.gvsig.fmap.core.GeneralPathX;
44
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
45
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
46
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
47
import com.iver.cit.gvsig.fmap.edition.cad.Status;
48
import com.iver.cit.gvsig.fmap.edition.cad.TrigonometricalFunctions;
49
import com.iver.cit.gvsig.fmap.layers.FBitSet;
50
import com.iver.cit.gvsig.gui.cad.automaton.Linea;
51

    
52
import com.iver.fsac.Automaton;
53

    
54
import java.awt.Graphics;
55
import java.awt.Graphics2D;
56
import java.awt.geom.Point2D;
57

    
58
import java.io.IOException;
59

    
60

    
61
/**
62
 * Herramienta para a?adir una geometr?a de tipo linea.
63
 *
64
 * @author Vicente Caballero Navarro
65
 */
66
public class LineCadTool extends AbstractCadTool {
67
        private static Status[] STATUS = {
68
                        new Status("Precise primer punto(x,y):"),
69
                        new Status("Precise punto siguiente(x,y) o ?ngulo(a):"),
70
                        new Status("Precise longitud(l) o punto(x,y):"), null, null,
71
                        new Status("Precise punto siguiente(x,y) o ?ngulo(a)")
72
                };
73
        private Linea lineStatus = new Linea();
74
        private Point2D firstPoint;
75
        private Point2D lastPoint;
76
        private double angle;
77
        private double length;
78

    
79
        /**
80
         * @see com.iver.cit.gvsig.gui.cad.CadTool#transition(java.lang.String,
81
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
82
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double[])
83
         */
84
        public int transition(String text, EditableFeatureSource editingSource,
85
                FBitSet selectedGeometries, double[] values) {
86
                int ret = lineStatus.transition(text);
87

    
88
                int status = lineStatus.getStatus();
89

    
90
                if (status == 0) {
91
                } else if (status == 1) {
92
                        if ((lineStatus.getPreviousStatus() == 5) ||
93
                                        (lineStatus.getPreviousStatus() == 1)) {
94
                                lastPoint = new Point2D.Double(values[0], values[1]);
95

    
96
                                try {
97
                                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
98
                                                        2);
99
                                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
100
                                        elShape.lineTo(lastPoint.getX(), lastPoint.getY());
101
                                        editingSource.addGeometry(ShapeFactory.createPolyline2D(
102
                                                        elShape));
103
                                } catch (DriverIOException e) {
104
                                        e.printStackTrace();
105
                                } catch (IOException e) {
106
                                        e.printStackTrace();
107
                                }
108

    
109
                                firstPoint = (Point2D) lastPoint.clone();
110
                        }
111
                } else if (status == 2) {
112
                        angle = values[0];
113
                } else if (status == 3) {
114
                        length = values[0];
115

    
116
                        double w = Math.cos(Math.toRadians(angle)) * length;
117
                        double h = Math.sin(Math.toRadians(angle)) * length;
118
                        lastPoint = new Point2D.Double(firstPoint.getX() + w,
119
                                        firstPoint.getY() + h);
120

    
121
                        try {
122
                                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
123
                                                2);
124
                                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
125
                                elShape.lineTo(lastPoint.getX(), lastPoint.getY());
126
                                editingSource.addGeometry(ShapeFactory.createPolyline2D(elShape));
127
                        } catch (DriverIOException e) {
128
                                e.printStackTrace();
129
                        } catch (IOException e) {
130
                                e.printStackTrace();
131
                        }
132

    
133
                        firstPoint = (Point2D) lastPoint.clone();
134
                        ret = ret | lineStatus.transition("end");
135
                } else if (status == 4) {
136
                        length = firstPoint.distance(values[0], values[1]);
137

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

    
143
                        try {
144
                                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
145
                                                2);
146
                                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
147
                                elShape.lineTo(lastPoint.getX(), lastPoint.getY());
148
                                editingSource.addGeometry(ShapeFactory.createPolyline2D(elShape));
149
                        } catch (DriverIOException e) {
150
                                e.printStackTrace();
151
                        } catch (IOException e) {
152
                                e.printStackTrace();
153
                        }
154

    
155
                        firstPoint = (Point2D) lastPoint.clone();
156
                        ret = ret | lineStatus.transition("end");
157
                } else if (status == 5) {
158
                        firstPoint = new Point2D.Double(values[0], values[1]);
159
                }
160

    
161
                return ret;
162
        }
163

    
164
        /**
165
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
166
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
167
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
168
         */
169
        public void drawOperation(Graphics g, EditableFeatureSource efs,
170
                FBitSet selectedGeometries, double x, double y) {
171
                int status = lineStatus.getStatus();
172

    
173
                if ((status == 1) || (status == 5)) {
174
                        drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
175
                        System.err.println("angulo: " +
176
                                TrigonometricalFunctions.getAngle(firstPoint,
177
                                        new Point2D.Double(x, y)));
178
                } else if (status == 2) {
179
                        length = firstPoint.distance(x, y);
180

    
181
                        double w = (Math.cos(Math.toRadians(angle))) * length;
182
                        double h = (Math.sin(Math.toRadians(angle))) * length;
183
                        drawLine((Graphics2D) g, firstPoint,
184
                                new Point2D.Double(firstPoint.getX() + w, firstPoint.getY() +
185
                                        h));
186
                }
187
        }
188

    
189
        /**
190
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
191
         */
192
        public String getQuestion() {
193
                return STATUS[lineStatus.getStatus()].getQuestion();
194
        }
195

    
196
        /**
197
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
198
         */
199
        public void initializeStatus() {
200
                lineStatus.initialize();
201
                firstPoint = null;
202
                lastPoint = null;
203
        }
204

    
205
        /**
206
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
207
         */
208
        public Automaton getAutomaton() {
209
                return lineStatus;
210
        }
211

    
212
        /**
213
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getName()
214
         */
215
        public String getName() {
216
                return "L?NEA";
217
        }
218
}