root / tags / v1_1_Build_1003 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RectangleCADTool.java @ 12271
History | View | Annotate | Download (9.33 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.CADExtension; |
50 |
import com.iver.cit.gvsig.fmap.core.FShape; |
51 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
52 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
53 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
54 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
55 |
import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
56 |
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext; |
57 |
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState; |
58 |
|
59 |
|
60 |
/**
|
61 |
* DOCUMENT ME!
|
62 |
*
|
63 |
* @author Vicente Caballero Navarro
|
64 |
*/
|
65 |
public class RectangleCADTool extends DefaultCADTool { |
66 |
private RectangleCADToolContext _fsm;
|
67 |
private Point2D firstPoint; |
68 |
private Point2D lastPoint; |
69 |
|
70 |
/**
|
71 |
* Crea un nuevo LineCADTool.
|
72 |
*/
|
73 |
public RectangleCADTool() {
|
74 |
|
75 |
} |
76 |
|
77 |
/**
|
78 |
* 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 |
_fsm = new RectangleCADToolContext(this); |
83 |
} |
84 |
|
85 |
/* (non-Javadoc)
|
86 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
87 |
*/
|
88 |
public void transition(double x, double y, InputEvent event) { |
89 |
_fsm.addPoint(x, y, event); |
90 |
} |
91 |
|
92 |
/* (non-Javadoc)
|
93 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
94 |
*/
|
95 |
public void transition(double d) { |
96 |
_fsm.addValue(d); |
97 |
} |
98 |
|
99 |
/* (non-Javadoc)
|
100 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
101 |
*/
|
102 |
public void transition(String s) throws CommandException { |
103 |
if (!super.changeCommand(s)){ |
104 |
_fsm.addOption(s); |
105 |
} |
106 |
} |
107 |
|
108 |
/**
|
109 |
* 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 |
public void addPoint(double x, double y,InputEvent event) { |
117 |
RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState(); |
118 |
|
119 |
String status = actualState.getName();
|
120 |
|
121 |
if (status.equals("Rectangle.FirstPoint")) { |
122 |
firstPoint = new Point2D.Double(x, y); |
123 |
} else if (status == "Rectangle.SecondPointOrSquare") { |
124 |
lastPoint = new Point2D.Double(x, y); |
125 |
|
126 |
GeneralPathX elShape = new GeneralPathX();
|
127 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
128 |
elShape.lineTo(lastPoint.getX(), firstPoint.getY()); |
129 |
elShape.lineTo(lastPoint.getX(), lastPoint.getY()); |
130 |
elShape.lineTo(firstPoint.getX(), lastPoint.getY()); |
131 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
132 |
elShape.closePath(); |
133 |
int type=getCadToolAdapter().getActiveLayerType();
|
134 |
if (type==FShape.POLYGON){
|
135 |
addGeometry(ShapeFactory.createPolygon2D(elShape)); |
136 |
}else{
|
137 |
addGeometry(ShapeFactory.createPolyline2D(elShape)); |
138 |
} |
139 |
firstPoint = (Point2D) lastPoint.clone();
|
140 |
} else if (status == "Rectangle.SecondPointSquare") { |
141 |
lastPoint = new Point2D.Double(x, y); |
142 |
|
143 |
GeneralPathX elShape = new GeneralPathX();
|
144 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
145 |
elShape.lineTo(lastPoint.getX(), firstPoint.getY()); |
146 |
|
147 |
if (((lastPoint.getY() <= firstPoint.getY()) &&
|
148 |
(lastPoint.getX() <= firstPoint.getX())) || |
149 |
((lastPoint.getY() > firstPoint.getY()) && |
150 |
(lastPoint.getX() > firstPoint.getX()))) { |
151 |
elShape.lineTo(lastPoint.getX(), |
152 |
firstPoint.getY() + (lastPoint.getX() - firstPoint.getX())); |
153 |
elShape.lineTo(firstPoint.getX(), |
154 |
firstPoint.getY() + (lastPoint.getX() - firstPoint.getX())); |
155 |
} else {
|
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 |
} |
161 |
|
162 |
//elShape.lineTo(firstPoint.getX(), firstPoint.getY());
|
163 |
elShape.closePath(); |
164 |
int type=getCadToolAdapter().getActiveLayerType();
|
165 |
if (type==FShape.POLYGON){
|
166 |
addGeometry(ShapeFactory.createPolygon2D(elShape)); |
167 |
}else{
|
168 |
addGeometry(ShapeFactory.createPolyline2D(elShape)); |
169 |
} |
170 |
firstPoint = (Point2D) lastPoint.clone();
|
171 |
} |
172 |
} |
173 |
|
174 |
/**
|
175 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
176 |
* encontremos.
|
177 |
*
|
178 |
* @param g Graphics sobre el que dibujar.
|
179 |
* @param selectedGeometries BitSet con las geometr?as seleccionadas.
|
180 |
* @param x par?metro x del punto que se pase para dibujar.
|
181 |
* @param y par?metro x del punto que se pase para dibujar.
|
182 |
*/
|
183 |
public void drawOperation(Graphics g, double x, |
184 |
double y) {
|
185 |
RectangleCADToolState actualState = _fsm.getState(); |
186 |
String status = actualState.getName();
|
187 |
|
188 |
if (status == "Rectangle.SecondPointOrSquare") { |
189 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
190 |
4);
|
191 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
192 |
elShape.lineTo(x, firstPoint.getY()); |
193 |
elShape.lineTo(x, y); |
194 |
elShape.lineTo(firstPoint.getX(), y); |
195 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
196 |
ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
|
197 |
getCadToolAdapter().getMapControl().getViewPort(), |
198 |
DefaultCADTool.geometrySelectSymbol); |
199 |
} else if (status == "Rectangle.SecondPointSquare") { |
200 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
201 |
4);
|
202 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
203 |
elShape.lineTo(x, firstPoint.getY()); |
204 |
|
205 |
if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
|
206 |
((y > firstPoint.getY()) && (x > firstPoint.getX()))) { |
207 |
elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX())); |
208 |
elShape.lineTo(firstPoint.getX(), |
209 |
firstPoint.getY() + (x - firstPoint.getX())); |
210 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
211 |
} else {
|
212 |
elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX())); |
213 |
elShape.lineTo(firstPoint.getX(), |
214 |
firstPoint.getY() - (x - firstPoint.getX())); |
215 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
216 |
} |
217 |
|
218 |
ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
|
219 |
getCadToolAdapter().getMapControl().getViewPort(), |
220 |
DefaultCADTool.geometrySelectSymbol); |
221 |
} |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Add a diferent option.
|
226 |
*
|
227 |
* @param sel DOCUMENT ME!
|
228 |
* @param s Diferent option.
|
229 |
*/
|
230 |
public void addOption(String s) { |
231 |
RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState(); |
232 |
String status = actualState.getName();
|
233 |
|
234 |
if (status == "Rectangle.SecondPointOrSquare") { |
235 |
if (s.equals("C") || s.equals("c")) { |
236 |
//Opci?n correcta
|
237 |
} |
238 |
} |
239 |
} |
240 |
|
241 |
/* (non-Javadoc)
|
242 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
243 |
*/
|
244 |
public void addValue(double d) { |
245 |
} |
246 |
|
247 |
public String getName() { |
248 |
return PluginServices.getText(this,"rectangle_"); |
249 |
} |
250 |
|
251 |
public String toString() { |
252 |
return "_rectangle"; |
253 |
} |
254 |
public boolean isApplicable(int shapeType) { |
255 |
switch (shapeType) {
|
256 |
case FShape.POINT:
|
257 |
case FShape.MULTIPOINT:
|
258 |
return false; |
259 |
} |
260 |
return true; |
261 |
} |
262 |
} |