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