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