svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RotateCADTool.java @ 4899
History | View | Annotate | Download (10 KB)
1 | 3883 | caballero | /* 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 | 4637 | caballero | import java.awt.Image; |
46 | 4313 | fjp | import java.awt.event.InputEvent; |
47 | 3883 | caballero | import java.awt.geom.AffineTransform; |
48 | import java.awt.geom.Point2D; |
||
49 | import java.io.IOException; |
||
50 | 4458 | caballero | import java.util.ArrayList; |
51 | 3883 | caballero | |
52 | import com.iver.andami.PluginServices; |
||
53 | import com.iver.cit.gvsig.CADExtension; |
||
54 | 4637 | caballero | import com.iver.cit.gvsig.fmap.ViewPort; |
55 | 3883 | caballero | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
56 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
57 | 4584 | caballero | import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
58 | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
||
59 | 3904 | fjp | import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
60 | 4637 | caballero | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
61 | 3883 | caballero | 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 | 4637 | caballero | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
65 | 3883 | caballero | |
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 | 4313 | fjp | public void transition(double x, double y, InputEvent event) { |
95 | 4324 | caballero | _fsm.addPoint(x, y, event); |
96 | 3883 | caballero | } |
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 | 4892 | caballero | if (!super.changeCommand(s)){ |
110 | _fsm.addOption(s); |
||
111 | } |
||
112 | 3883 | caballero | } |
113 | |||
114 | /**
|
||
115 | * DOCUMENT ME!
|
||
116 | */
|
||
117 | public void selection() { |
||
118 | 4584 | caballero | ArrayList selectedRows=getSelectedRows();
|
119 | if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
||
120 | 4892 | caballero | CADExtension.setCADTool("selection",false); |
121 | 4584 | caballero | ((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
122 | 3883 | caballero | "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 | 4365 | caballero | public void addPoint(double x, double y,InputEvent event) { |
134 | 3883 | caballero | RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState(); |
135 | String status = actualState.getName();
|
||
136 | 4584 | caballero | ArrayList selectedRow=getSelectedRows();
|
137 | 4637 | caballero | VectorialLayerEdited vle=getVLE(); |
138 | VectorialEditableAdapter vea = vle.getVEA(); |
||
139 | 3978 | caballero | if (status.equals("Rotate.PointMain")) { |
140 | 3883 | caballero | firstPoint = new Point2D.Double(x, y); |
141 | 3978 | caballero | } else if (status.equals("Rotate.AngleOrPoint")) { |
142 | 3883 | caballero | 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 | 4637 | caballero | vea.startComplexRow(); |
152 | ///ArrayList selectedRowAux=new ArrayList();
|
||
153 | 4584 | caballero | for (int i = 0; i < selectedRow.size(); i++) { |
154 | 4637 | caballero | DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i); |
155 | DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow(); |
||
156 | 4584 | caballero | // Rotamos la geometry
|
157 | UtilFunctions.rotateGeom(fea.getGeometry(), -Math.atan2(w,
|
||
158 | h) |
||
159 | + (Math.PI / 2), firstPoint.getX(), firstPoint |
||
160 | .getY()); |
||
161 | 3978 | caballero | |
162 | 4637 | caballero | int index=vea.modifyRow(row.getIndex(), fea,
|
163 | 4584 | caballero | getName()); |
164 | 4637 | caballero | ///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
165 | 4584 | caballero | } |
166 | 4637 | caballero | |
167 | vea.endComplexRow(); |
||
168 | 4584 | caballero | clearSelection(); |
169 | 4637 | caballero | ///selectedRow=selectedRowAux;
|
170 | 4584 | caballero | } catch (DriverIOException e) {
|
171 | e.printStackTrace(); |
||
172 | } catch (IOException e1) { |
||
173 | e1.printStackTrace(); |
||
174 | } |
||
175 | 3883 | caballero | |
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 | 4637 | caballero | VectorialLayerEdited vle=getVLE(); |
192 | //ArrayList selectedRow=getSelectedRows();
|
||
193 | 3883 | caballero | |
194 | 4637 | caballero | // drawHandlers(g, selectedRow,
|
195 | // getCadToolAdapter().getMapControl().getViewPort()
|
||
196 | // .getAffineTransform());
|
||
197 | 3978 | caballero | if (status.equals("Rotate.AngleOrPoint")) { |
198 | 3883 | caballero | double w;
|
199 | double h;
|
||
200 | w = x - firstPoint.getX(); |
||
201 | h = y - firstPoint.getY(); |
||
202 | 4637 | caballero | 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 | 3883 | caballero | |
209 | 4637 | caballero | Image imgSel = vle.getSelectionImage();
|
210 | ((Graphics2D)g).drawImage(imgSel, at, null); |
||
211 | drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y)); |
||
212 | 3883 | caballero | ///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 | 4637 | caballero | /*
|
222 | 4584 | caballero | 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 | 3883 | caballero | |
232 | 4584 | caballero | 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 | 3883 | caballero | |
242 | 4584 | caballero | }
|
243 | 4637 | caballero | */
|
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 | 3883 | caballero | } |
250 | } |
||
251 | |||
252 | /**
|
||
253 | 4584 | caballero | * Add a diferent option.
|
254 | *
|
||
255 | * @param s
|
||
256 | * Diferent option.
|
||
257 | */
|
||
258 | 3883 | caballero | public void addOption(String s) { |
259 | } |
||
260 | |||
261 | 4584 | caballero | /*
|
262 | * (non-Javadoc)
|
||
263 | *
|
||
264 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
265 | */
|
||
266 | 3883 | caballero | public void addValue(double d) { |
267 | RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState(); |
||
268 | String status = actualState.getName();
|
||
269 | 4584 | caballero | ArrayList selectedRow=getSelectedRows();
|
270 | 4637 | caballero | VectorialLayerEdited vle=getVLE(); |
271 | VectorialEditableAdapter vea = vle.getVEA(); |
||
272 | 3978 | caballero | if (status.equals("Rotate.AngleOrPoint")) { |
273 | 3883 | caballero | try {
|
274 | 4637 | caballero | |
275 | vea.startComplexRow(); |
||
276 | ///ArrayList selectedRowAux=new ArrayList();
|
||
277 | 4522 | caballero | for (int i = 0; i < selectedRow.size(); i++) { |
278 | 4637 | caballero | DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i); |
279 | DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow(); |
||
280 | 3904 | fjp | // 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 | 4637 | caballero | int index=vea.modifyRow(row.getIndex(), fea,getName());
|
286 | ///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
||
287 | 3883 | caballero | } |
288 | 4637 | caballero | vea.endComplexRow(); |
289 | 4584 | caballero | clearSelection(); |
290 | 4637 | caballero | ///selectedRow=selectedRowAux;
|
291 | 3883 | caballero | } catch (DriverIOException e) {
|
292 | e.printStackTrace(); |
||
293 | } catch (IOException e1) { |
||
294 | e1.printStackTrace(); |
||
295 | } |
||
296 | |||
297 | } |
||
298 | } |
||
299 | 4118 | caballero | |
300 | public String getName() { |
||
301 | 4584 | caballero | return PluginServices.getText(this,"rotate_"); |
302 | 4118 | caballero | } |
303 | 4892 | caballero | |
304 | public String toString() { |
||
305 | return "_rotate"; |
||
306 | } |
||
307 | 3883 | caballero | } |