root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RectangleCADTool.java @ 5878
History | View | Annotate | Download (10.2 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.andami.PluginServices; |
49 |
import com.iver.cit.gvsig.fmap.core.FShape; |
50 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
51 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
52 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
53 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
54 |
import com.iver.cit.gvsig.gui.cad.CADTool; |
55 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
56 |
import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
57 |
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext; |
58 |
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState; |
59 |
|
60 |
|
61 |
/**
|
62 |
* DOCUMENT ME!
|
63 |
*
|
64 |
* @author Vicente Caballero Navarro
|
65 |
*/
|
66 |
public class RectangleCADTool extends DefaultCADTool { |
67 |
private RectangleCADToolContext _fsm;
|
68 |
private Point2D firstPoint; |
69 |
private Point2D lastPoint; |
70 |
|
71 |
/**
|
72 |
* Crea un nuevo LineCADTool.
|
73 |
*/
|
74 |
public RectangleCADTool() {
|
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 RectangleCADToolContext(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 |
RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState(); |
119 |
|
120 |
String status = actualState.getName();
|
121 |
|
122 |
if (status.equals("Rectangle.FirstPoint")) { |
123 |
firstPoint = new Point2D.Double(x, y); |
124 |
} else if (status == "Rectangle.SecondPointOrSquare") { |
125 |
lastPoint = new Point2D.Double(x, y); |
126 |
|
127 |
GeneralPathX elShape = new GeneralPathX();
|
128 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
129 |
elShape.lineTo(lastPoint.getX(), firstPoint.getY()); |
130 |
elShape.lineTo(lastPoint.getX(), lastPoint.getY()); |
131 |
elShape.lineTo(firstPoint.getX(), lastPoint.getY()); |
132 |
//elShape.lineTo(firstPoint.getX(), firstPoint.getY());
|
133 |
elShape.closePath(); |
134 |
IGeometry geom=ShapeFactory.createPolygon2D(elShape); |
135 |
// try {
|
136 |
// if (getVLE().getVEA().getShapeType()==FShape.LINE)
|
137 |
// geom.setGeometryType(FShape.LINE);
|
138 |
// else
|
139 |
// geom.setGeometryType(FShape.POLYGON);
|
140 |
// } catch (DriverIOException e) {
|
141 |
// e.printStackTrace();
|
142 |
// }
|
143 |
addGeometry(geom); |
144 |
firstPoint = (Point2D) lastPoint.clone();
|
145 |
} else if (status == "Rectangle.SecondPointSquare") { |
146 |
lastPoint = new Point2D.Double(x, y); |
147 |
|
148 |
GeneralPathX elShape = new GeneralPathX();
|
149 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
150 |
elShape.lineTo(lastPoint.getX(), firstPoint.getY()); |
151 |
|
152 |
if (((lastPoint.getY() <= firstPoint.getY()) &&
|
153 |
(lastPoint.getX() <= firstPoint.getX())) || |
154 |
((lastPoint.getY() > firstPoint.getY()) && |
155 |
(lastPoint.getX() > firstPoint.getX()))) { |
156 |
elShape.lineTo(lastPoint.getX(), |
157 |
firstPoint.getY() + (lastPoint.getX() - firstPoint.getX())); |
158 |
elShape.lineTo(firstPoint.getX(), |
159 |
firstPoint.getY() + (lastPoint.getX() - firstPoint.getX())); |
160 |
} else {
|
161 |
elShape.lineTo(lastPoint.getX(), |
162 |
firstPoint.getY() - (lastPoint.getX() - firstPoint.getX())); |
163 |
elShape.lineTo(firstPoint.getX(), |
164 |
firstPoint.getY() - (lastPoint.getX() - firstPoint.getX())); |
165 |
} |
166 |
|
167 |
//elShape.lineTo(firstPoint.getX(), firstPoint.getY());
|
168 |
elShape.closePath(); |
169 |
IGeometry geom=ShapeFactory.createPolygon2D(elShape); |
170 |
// try {
|
171 |
// if (getVLE().getVEA().getShapeType()==FShape.LINE)
|
172 |
// geom.setGeometryType(FShape.LINE);
|
173 |
// else
|
174 |
// geom.setGeometryType(FShape.POLYGON);
|
175 |
// } catch (DriverIOException e) {
|
176 |
// e.printStackTrace();
|
177 |
// }
|
178 |
addGeometry(geom); |
179 |
firstPoint = (Point2D) lastPoint.clone();
|
180 |
} |
181 |
} |
182 |
|
183 |
/**
|
184 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
185 |
* encontremos.
|
186 |
*
|
187 |
* @param g Graphics sobre el que dibujar.
|
188 |
* @param selectedGeometries BitSet con las geometr?as seleccionadas.
|
189 |
* @param x par?metro x del punto que se pase para dibujar.
|
190 |
* @param y par?metro x del punto que se pase para dibujar.
|
191 |
*/
|
192 |
public void drawOperation(Graphics g, double x, |
193 |
double y) {
|
194 |
RectangleCADToolState actualState = _fsm.getState(); |
195 |
String status = actualState.getName();
|
196 |
|
197 |
if (status == "Rectangle.SecondPointOrSquare") { |
198 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
199 |
4);
|
200 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
201 |
elShape.lineTo(x, firstPoint.getY()); |
202 |
elShape.lineTo(x, y); |
203 |
elShape.lineTo(firstPoint.getX(), y); |
204 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
205 |
IGeometry geom=ShapeFactory.createPolyline2D(elShape); |
206 |
// try {
|
207 |
// if (getVLE().getVEA().getShapeType()==FShape.LINE)
|
208 |
// geom.setGeometryType(FShape.LINE);
|
209 |
// else
|
210 |
// geom.setGeometryType(FShape.POLYGON);
|
211 |
// } catch (DriverIOException e) {
|
212 |
// e.printStackTrace();
|
213 |
// }
|
214 |
geom.drawInts((Graphics2D) g,
|
215 |
getCadToolAdapter().getMapControl().getViewPort(), |
216 |
CADTool.drawingSymbol); |
217 |
} else if (status == "Rectangle.SecondPointSquare") { |
218 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
219 |
4);
|
220 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
221 |
elShape.lineTo(x, firstPoint.getY()); |
222 |
|
223 |
if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
|
224 |
((y > firstPoint.getY()) && (x > firstPoint.getX()))) { |
225 |
elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX())); |
226 |
elShape.lineTo(firstPoint.getX(), |
227 |
firstPoint.getY() + (x - firstPoint.getX())); |
228 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
229 |
} else {
|
230 |
elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX())); |
231 |
elShape.lineTo(firstPoint.getX(), |
232 |
firstPoint.getY() - (x - firstPoint.getX())); |
233 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
234 |
} |
235 |
|
236 |
IGeometry geom=ShapeFactory.createPolyline2D(elShape); |
237 |
// try {
|
238 |
// if (getVLE().getVEA().getShapeType()==FShape.LINE)
|
239 |
// geom.setGeometryType(FShape.LINE);
|
240 |
// else
|
241 |
// geom.setGeometryType(FShape.POLYGON);
|
242 |
// } catch (DriverIOException e) {
|
243 |
// e.printStackTrace();
|
244 |
// }
|
245 |
geom.drawInts((Graphics2D) g,
|
246 |
getCadToolAdapter().getMapControl().getViewPort(), |
247 |
CADTool.drawingSymbol); |
248 |
} |
249 |
} |
250 |
|
251 |
/**
|
252 |
* Add a diferent option.
|
253 |
*
|
254 |
* @param sel DOCUMENT ME!
|
255 |
* @param s Diferent option.
|
256 |
*/
|
257 |
public void addOption(String s) { |
258 |
RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState(); |
259 |
String status = actualState.getName();
|
260 |
|
261 |
if (status == "Rectangle.SecondPointOrSquare") { |
262 |
if (s.equals("C") || s.equals("c")) { |
263 |
//Opci?n correcta
|
264 |
} |
265 |
} |
266 |
} |
267 |
|
268 |
/* (non-Javadoc)
|
269 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
270 |
*/
|
271 |
public void addValue(double d) { |
272 |
} |
273 |
|
274 |
public String getName() { |
275 |
return PluginServices.getText(this,"rectangle_"); |
276 |
} |
277 |
|
278 |
public String toString() { |
279 |
return "_rectangle"; |
280 |
} |
281 |
public boolean isApplicable(int shapeType) { |
282 |
switch (shapeType) {
|
283 |
case FShape.POINT:
|
284 |
return false; |
285 |
} |
286 |
return true; |
287 |
} |
288 |
} |