root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RotateCADTool.java @ 5880
History | View | Annotate | Download (10.1 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.Image; |
46 |
import java.awt.event.InputEvent; |
47 |
import java.awt.geom.AffineTransform; |
48 |
import java.awt.geom.Point2D; |
49 |
import java.io.IOException; |
50 |
import java.util.ArrayList; |
51 |
|
52 |
import com.iver.andami.PluginServices; |
53 |
import com.iver.cit.gvsig.CADExtension; |
54 |
import com.iver.cit.gvsig.fmap.ViewPort; |
55 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
56 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
57 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
58 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
59 |
import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
60 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
61 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
62 |
import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
63 |
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext; |
64 |
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState; |
65 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
66 |
|
67 |
|
68 |
/**
|
69 |
* DOCUMENT ME!
|
70 |
*
|
71 |
* @author Vicente Caballero Navarro
|
72 |
*/
|
73 |
public class RotateCADTool extends DefaultCADTool { |
74 |
private RotateCADToolContext _fsm;
|
75 |
private Point2D firstPoint; |
76 |
private Point2D lastPoint; |
77 |
|
78 |
/**
|
79 |
* Crea un nuevo PolylineCADTool.
|
80 |
*/
|
81 |
public RotateCADTool() {
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
86 |
* carga previa a la utilizaci?n de la herramienta.
|
87 |
*/
|
88 |
public void init() { |
89 |
_fsm = new RotateCADToolContext(this); |
90 |
} |
91 |
|
92 |
/* (non-Javadoc)
|
93 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
94 |
*/
|
95 |
public void transition(double x, double y, InputEvent event) { |
96 |
_fsm.addPoint(x, y, event); |
97 |
} |
98 |
|
99 |
/* (non-Javadoc)
|
100 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
101 |
*/
|
102 |
public void transition(double d) { |
103 |
_fsm.addValue(d); |
104 |
} |
105 |
|
106 |
/* (non-Javadoc)
|
107 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
108 |
*/
|
109 |
public void transition(String s) throws CommandException { |
110 |
if (!super.changeCommand(s)){ |
111 |
_fsm.addOption(s); |
112 |
} |
113 |
} |
114 |
|
115 |
/**
|
116 |
* DOCUMENT ME!
|
117 |
*/
|
118 |
public void selection() { |
119 |
ArrayList selectedRows=getSelectedRows();
|
120 |
if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
121 |
CADExtension.setCADTool("_selection",false); |
122 |
((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
123 |
"_rotate");
|
124 |
} |
125 |
} |
126 |
|
127 |
/**
|
128 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
129 |
* editableFeatureSource que ya estar? creado.
|
130 |
*
|
131 |
* @param x par?metro x del punto que se pase en esta transici?n.
|
132 |
* @param y par?metro y del punto que se pase en esta transici?n.
|
133 |
*/
|
134 |
public void addPoint(double x, double y,InputEvent event) { |
135 |
RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState(); |
136 |
String status = actualState.getName();
|
137 |
ArrayList selectedRow=getSelectedRows();
|
138 |
VectorialLayerEdited vle=getVLE(); |
139 |
VectorialEditableAdapter vea = vle.getVEA(); |
140 |
if (status.equals("Rotate.PointMain")) { |
141 |
firstPoint = new Point2D.Double(x, y); |
142 |
} else if (status.equals("Rotate.AngleOrPoint")) { |
143 |
PluginServices.getMDIManager().setWaitCursor(); |
144 |
lastPoint = new Point2D.Double(x,y); |
145 |
|
146 |
double w;
|
147 |
double h;
|
148 |
w = lastPoint.getX() - firstPoint.getX(); |
149 |
h = lastPoint.getY() - firstPoint.getY(); |
150 |
|
151 |
try {
|
152 |
vea.startComplexRow(); |
153 |
///ArrayList selectedRowAux=new ArrayList();
|
154 |
for (int i = 0; i < selectedRow.size(); i++) { |
155 |
DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i); |
156 |
DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow(); |
157 |
// Rotamos la geometry
|
158 |
UtilFunctions.rotateGeom(fea.getGeometry(), -Math.atan2(w,
|
159 |
h) |
160 |
+ (Math.PI / 2), firstPoint.getX(), firstPoint |
161 |
.getY()); |
162 |
|
163 |
vea.modifyRow(row.getIndex(), fea, |
164 |
getName(),EditionEvent.GRAPHIC); |
165 |
///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
166 |
} |
167 |
|
168 |
vea.endComplexRow(); |
169 |
clearSelection(); |
170 |
///selectedRow=selectedRowAux;
|
171 |
} catch (DriverIOException e) {
|
172 |
e.printStackTrace(); |
173 |
} catch (IOException e1) { |
174 |
e1.printStackTrace(); |
175 |
} |
176 |
|
177 |
PluginServices.getMDIManager().restoreCursor(); |
178 |
} |
179 |
} |
180 |
|
181 |
/**
|
182 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
183 |
* encontremos.
|
184 |
*
|
185 |
* @param g Graphics sobre el que dibujar.
|
186 |
* @param x par?metro x del punto que se pase para dibujar.
|
187 |
* @param y par?metro x del punto que se pase para dibujar.
|
188 |
*/
|
189 |
public void drawOperation(Graphics g, double x, double y) { |
190 |
RotateCADToolState actualState = ((RotateCADToolContext) _fsm).getState(); |
191 |
String status = actualState.getName();
|
192 |
VectorialLayerEdited vle=getVLE(); |
193 |
//ArrayList selectedRow=getSelectedRows();
|
194 |
|
195 |
// drawHandlers(g, selectedRow,
|
196 |
// getCadToolAdapter().getMapControl().getViewPort()
|
197 |
// .getAffineTransform());
|
198 |
if (status.equals("Rotate.AngleOrPoint")) { |
199 |
double w;
|
200 |
double h;
|
201 |
w = x - firstPoint.getX(); |
202 |
h = y - firstPoint.getY(); |
203 |
ViewPort vp=vle.getLayer().getFMap().getViewPort(); |
204 |
Point2D point = vp.fromMapPoint(firstPoint.getX(),
|
205 |
firstPoint.getY()); |
206 |
AffineTransform at = AffineTransform.getRotateInstance(Math.atan2( |
207 |
w, h) - (Math.PI / 2), (int) point.getX(), |
208 |
(int) point.getY());
|
209 |
|
210 |
Image imgSel = vle.getSelectionImage();
|
211 |
((Graphics2D)g).drawImage(imgSel, at, null); |
212 |
drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y)); |
213 |
///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
|
214 |
/// w, h) - (Math.PI / 2), (int) point.getX(),
|
215 |
/// (int) point.getY());
|
216 |
///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
|
217 |
|
218 |
///((Graphics2D) g).drawImage(img, at, null);
|
219 |
|
220 |
///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
|
221 |
|
222 |
/*
|
223 |
for (int i = 0; i < selectedRow.size(); i++) {
|
224 |
// IGeometry geometry =
|
225 |
// getCadToolAdapter().getVectorialAdapter().getShape(i);
|
226 |
IRowEdited edRow = (IRowEdited) selectedRow.get(i);
|
227 |
IFeature feat = (IFeature) edRow.getLinkedRow();
|
228 |
IGeometry geometry = feat.getGeometry().cloneGeometry();
|
229 |
// Rotamos la geometry
|
230 |
UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h) + Math.PI
|
231 |
/ 2, firstPoint.getX(), firstPoint.getY());
|
232 |
|
233 |
geometry.draw((Graphics2D) g, getCadToolAdapter()
|
234 |
.getMapControl().getViewPort(), CADTool.drawingSymbol);
|
235 |
GeneralPathX elShape = new GeneralPathX(
|
236 |
GeneralPathX.WIND_EVEN_ODD, 2);
|
237 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY());
|
238 |
elShape.lineTo(x, y);
|
239 |
ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
|
240 |
getCadToolAdapter().getMapControl().getViewPort(),
|
241 |
CADTool.drawingSymbol);
|
242 |
|
243 |
}
|
244 |
*/
|
245 |
}else{
|
246 |
Image imgSel = vle.getSelectionImage();
|
247 |
g.drawImage(imgSel, 0, 0, null); |
248 |
Image imgHand = vle.getHandlersImage();
|
249 |
g.drawImage(imgHand, 0, 0, null); |
250 |
} |
251 |
} |
252 |
|
253 |
/**
|
254 |
* Add a diferent option.
|
255 |
*
|
256 |
* @param s
|
257 |
* Diferent option.
|
258 |
*/
|
259 |
public void addOption(String s) { |
260 |
} |
261 |
|
262 |
/*
|
263 |
* (non-Javadoc)
|
264 |
*
|
265 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
266 |
*/
|
267 |
public void addValue(double d) { |
268 |
RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState(); |
269 |
String status = actualState.getName();
|
270 |
ArrayList selectedRow=getSelectedRows();
|
271 |
VectorialLayerEdited vle=getVLE(); |
272 |
VectorialEditableAdapter vea = vle.getVEA(); |
273 |
if (status.equals("Rotate.AngleOrPoint")) { |
274 |
try {
|
275 |
|
276 |
vea.startComplexRow(); |
277 |
///ArrayList selectedRowAux=new ArrayList();
|
278 |
for (int i = 0; i < selectedRow.size(); i++) { |
279 |
DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i); |
280 |
DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow(); |
281 |
// Rotamos la geometry
|
282 |
AffineTransform at = new AffineTransform(); |
283 |
at.rotate(Math.toRadians(d),
|
284 |
firstPoint.getX(), firstPoint.getY()); |
285 |
fea.getGeometry().transform(at); |
286 |
vea.modifyRow(row.getIndex(), fea,getName(),EditionEvent.GRAPHIC); |
287 |
///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
288 |
} |
289 |
vea.endComplexRow(); |
290 |
clearSelection(); |
291 |
///selectedRow=selectedRowAux;
|
292 |
} catch (DriverIOException e) {
|
293 |
e.printStackTrace(); |
294 |
} catch (IOException e1) { |
295 |
e1.printStackTrace(); |
296 |
} |
297 |
|
298 |
} |
299 |
} |
300 |
|
301 |
public String getName() { |
302 |
return PluginServices.getText(this,"rotate_"); |
303 |
} |
304 |
|
305 |
public String toString() { |
306 |
return "_rotate"; |
307 |
} |
308 |
|
309 |
} |