root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / RotateCADTool.java @ 30335
History | View | Annotate | Download (10.7 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 org.gvsig.editing.gui.cad.tools; |
42 |
|
43 |
import java.awt.Image; |
44 |
import java.awt.event.InputEvent; |
45 |
import java.awt.geom.AffineTransform; |
46 |
import java.awt.geom.Point2D; |
47 |
import java.util.ArrayList; |
48 |
|
49 |
import org.gvsig.andami.PluginServices; |
50 |
import org.gvsig.andami.messages.NotificationManager; |
51 |
import org.gvsig.editing.CADExtension; |
52 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
53 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
54 |
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext; |
55 |
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState; |
56 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
57 |
import org.gvsig.fmap.dal.exception.DataException; |
58 |
import org.gvsig.fmap.dal.exception.ReadException; |
59 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
60 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
61 |
import org.gvsig.fmap.dal.feature.Feature; |
62 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
63 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
64 |
import org.gvsig.fmap.geom.Geometry; |
65 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
66 |
import org.gvsig.fmap.mapcontext.ViewPort; |
67 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
68 |
|
69 |
|
70 |
/**
|
71 |
* DOCUMENT ME!
|
72 |
*
|
73 |
* @author Vicente Caballero Navarro
|
74 |
*/
|
75 |
public class RotateCADTool extends DefaultCADTool { |
76 |
private RotateCADToolContext _fsm;
|
77 |
private Point2D firstPoint; |
78 |
private Point2D lastPoint; |
79 |
|
80 |
/**
|
81 |
* Crea un nuevo PolylineCADTool.
|
82 |
*/
|
83 |
public RotateCADTool() {
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
88 |
* carga previa a la utilizaci?n de la herramienta.
|
89 |
*/
|
90 |
public void init() { |
91 |
_fsm = new RotateCADToolContext(this); |
92 |
} |
93 |
|
94 |
/*
|
95 |
* (non-Javadoc)
|
96 |
*
|
97 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
98 |
* double, double)
|
99 |
*/
|
100 |
public void transition(double x, double y, InputEvent event) { |
101 |
_fsm.addPoint(x, y, event); |
102 |
} |
103 |
|
104 |
/*
|
105 |
* (non-Javadoc)
|
106 |
*
|
107 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
108 |
* double)
|
109 |
*/
|
110 |
public void transition(double d) { |
111 |
_fsm.addValue(d); |
112 |
} |
113 |
|
114 |
/*
|
115 |
* (non-Javadoc)
|
116 |
*
|
117 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
118 |
* java.lang.String)
|
119 |
*/
|
120 |
public void transition(String s) throws CommandException { |
121 |
if (!super.changeCommand(s)) { |
122 |
_fsm.addOption(s); |
123 |
} |
124 |
} |
125 |
|
126 |
/**
|
127 |
* DOCUMENT ME!
|
128 |
*/
|
129 |
public void selection() { |
130 |
FeatureSet selection = null;
|
131 |
try {
|
132 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
133 |
|
134 |
if (selection.getSize() == 0 |
135 |
&& !CADExtension |
136 |
.getCADTool() |
137 |
.getClass() |
138 |
.getName() |
139 |
.equals( |
140 |
"com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
141 |
CADExtension.setCADTool("_selection", false); |
142 |
((SelectionCADTool) CADExtension.getCADTool()) |
143 |
.setNextTool("_rotate");
|
144 |
} |
145 |
} catch (ReadException e) {
|
146 |
// TODO Auto-generated catch block
|
147 |
e.printStackTrace(); |
148 |
} catch (DataException e) {
|
149 |
// TODO Auto-generated catch block
|
150 |
e.printStackTrace(); |
151 |
} |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
156 |
* editableFeatureSource que ya estar? creado.
|
157 |
*
|
158 |
* @param x
|
159 |
* par?metro x del punto que se pase en esta transici?n.
|
160 |
* @param y
|
161 |
* par?metro y del punto que se pase en esta transici?n.
|
162 |
*/
|
163 |
public void addPoint(double x, double y, InputEvent event) { |
164 |
RotateCADToolState actualState = (RotateCADToolState) _fsm |
165 |
.getPreviousState(); |
166 |
String status = actualState.getName();
|
167 |
VectorialLayerEdited vle = getVLE(); |
168 |
FeatureStore featureStore = null;
|
169 |
try {
|
170 |
featureStore = vle.getFeatureStore(); |
171 |
} catch (ReadException e1) {
|
172 |
// TODO Auto-generated catch block
|
173 |
e1.printStackTrace(); |
174 |
} |
175 |
DisposableIterator iterator = null;
|
176 |
try {
|
177 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
178 |
ArrayList selectedRowAux = new ArrayList(); |
179 |
|
180 |
if (status.equals("Rotate.PointMain")) { |
181 |
firstPoint = new Point2D.Double(x, y); |
182 |
} else if (status.equals("Rotate.AngleOrPoint")) { |
183 |
PluginServices.getMDIManager().setWaitCursor(); |
184 |
lastPoint = new Point2D.Double(x, y); |
185 |
|
186 |
double w;
|
187 |
double h;
|
188 |
w = lastPoint.getX() - firstPoint.getX(); |
189 |
h = lastPoint.getY() - firstPoint.getY(); |
190 |
|
191 |
featureStore.beginEditingGroup(getName()); |
192 |
try {
|
193 |
iterator = selection.iterator(); |
194 |
while (iterator.hasNext()) {
|
195 |
Feature feature = (Feature) iterator.next(); |
196 |
|
197 |
// }
|
198 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
199 |
// DefaultRowEdited row=(DefaultRowEdited)
|
200 |
// selectedRow.get(i);
|
201 |
// DefaultFeature fea = (DefaultFeature)
|
202 |
// row.getLinkedRow().cloneRow();
|
203 |
// Rotamos la geometry
|
204 |
Geometry geometry = (feature.getDefaultGeometry()) |
205 |
.cloneGeometry(); |
206 |
EditableFeature eFeature = feature.getEditable(); |
207 |
UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h)
|
208 |
+ (Math.PI / 2), firstPoint.getX(), firstPoint |
209 |
.getY()); |
210 |
eFeature.setGeometry(featureStore |
211 |
.getDefaultFeatureType() |
212 |
.getDefaultGeometryAttributeName(), geometry); |
213 |
featureStore.update(eFeature); |
214 |
// vea.modifyRow(row.getIndex(), fea,
|
215 |
// getName(),EditionEvent.GRAPHIC);
|
216 |
// selectedRowAux.add(new
|
217 |
// DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
|
218 |
} |
219 |
} finally {
|
220 |
|
221 |
featureStore.endEditingGroup(); |
222 |
} |
223 |
// vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
|
224 |
// selectedRowAux);
|
225 |
// clearSelection();
|
226 |
// selectedRow.addAll(selectedRowAux);
|
227 |
|
228 |
PluginServices.getMDIManager().restoreCursor(); |
229 |
} |
230 |
} catch (DataException e) {
|
231 |
NotificationManager.addError(e.getMessage(), e); |
232 |
} finally {
|
233 |
if (iterator != null) { |
234 |
iterator.dispose(); |
235 |
} |
236 |
} |
237 |
} |
238 |
|
239 |
/**
|
240 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
241 |
* encontremos.
|
242 |
*
|
243 |
* @param g
|
244 |
* Graphics sobre el que dibujar.
|
245 |
* @param x
|
246 |
* par?metro x del punto que se pase para dibujar.
|
247 |
* @param y
|
248 |
* par?metro x del punto que se pase para dibujar.
|
249 |
*/
|
250 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
251 |
RotateCADToolState actualState = _fsm.getState(); |
252 |
String status = actualState.getName();
|
253 |
VectorialLayerEdited vle = getVLE(); |
254 |
// ArrayList selectedRow=getSelectedRows();
|
255 |
|
256 |
// drawHandlers(g, selectedRow,
|
257 |
// getCadToolAdapter().getMapControl().getViewPort()
|
258 |
// .getAffineTransform());
|
259 |
if (status.equals("Rotate.AngleOrPoint")) { |
260 |
double w;
|
261 |
double h;
|
262 |
w = x - firstPoint.getX(); |
263 |
h = y - firstPoint.getY(); |
264 |
ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
265 |
Point2D point = vp.fromMapPoint(firstPoint.getX(), firstPoint
|
266 |
.getY()); |
267 |
AffineTransform at = AffineTransform.getRotateInstance(Math.atan2( |
268 |
w, h) |
269 |
- (Math.PI / 2), (int) point.getX(), (int) point.getY()); |
270 |
|
271 |
Image imgSel = vle.getSelectionImage();
|
272 |
renderer.drawImage(imgSel, at); |
273 |
renderer.drawLine(firstPoint, new Point2D.Double(x, y), |
274 |
mapControlManager.getAxisReferenceSymbol()); |
275 |
|
276 |
} else {
|
277 |
if (!vle.getLayer().isVisible()) {
|
278 |
return;
|
279 |
} |
280 |
Image imgSel = vle.getSelectionImage();
|
281 |
renderer.drawImage(imgSel, 0, 0); |
282 |
|
283 |
Image imgHand = vle.getHandlersImage();
|
284 |
renderer.drawImage(imgHand, 0, 0); |
285 |
} |
286 |
} |
287 |
|
288 |
/**
|
289 |
* Add a diferent option.
|
290 |
*
|
291 |
* @param s
|
292 |
* Diferent option.
|
293 |
*/
|
294 |
public void addOption(String s) { |
295 |
} |
296 |
|
297 |
/*
|
298 |
* (non-Javadoc)
|
299 |
*
|
300 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
301 |
*/
|
302 |
public void addValue(double d) { |
303 |
RotateCADToolState actualState = (RotateCADToolState) _fsm |
304 |
.getPreviousState(); |
305 |
String status = actualState.getName();
|
306 |
VectorialLayerEdited vle = getVLE(); |
307 |
FeatureStore featureStore = null;
|
308 |
try {
|
309 |
featureStore = vle.getFeatureStore(); |
310 |
} catch (ReadException e1) {
|
311 |
// TODO Auto-generated catch block
|
312 |
e1.printStackTrace(); |
313 |
} |
314 |
DisposableIterator iterator = null;
|
315 |
try {
|
316 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
317 |
|
318 |
if (status.equals("Rotate.AngleOrPoint")) { |
319 |
|
320 |
featureStore.beginEditingGroup(getName()); |
321 |
try {
|
322 |
// /ArrayList selectedRowAux=new ArrayList();
|
323 |
iterator = selection.iterator(); |
324 |
while (iterator.hasNext()) {
|
325 |
Feature feature = (Feature) iterator.next(); |
326 |
|
327 |
// }
|
328 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
329 |
// DefaultRowEdited row=(DefaultRowEdited)
|
330 |
// selectedRow.get(i);
|
331 |
// DefaultFeature fea = (DefaultFeature)
|
332 |
// row.getLinkedRow().cloneRow();
|
333 |
// Rotamos la geometry
|
334 |
AffineTransform at = new AffineTransform(); |
335 |
at.rotate(Math.toRadians(d), firstPoint.getX(),
|
336 |
firstPoint.getY()); |
337 |
Geometry geometry = (feature.getDefaultGeometry()) |
338 |
.cloneGeometry(); |
339 |
EditableFeature eFeature = feature.getEditable(); |
340 |
geometry.transform(at); |
341 |
eFeature.setGeometry(featureStore |
342 |
.getDefaultFeatureType() |
343 |
.getDefaultGeometryAttributeName(), geometry); |
344 |
featureStore.update(eFeature); |
345 |
// vea.modifyRow(row.getIndex(),
|
346 |
// fea,getName(),EditionEvent.GRAPHIC);
|
347 |
// /selectedRowAux.add(new
|
348 |
// DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
349 |
} |
350 |
} finally {
|
351 |
featureStore.endEditingGroup(); |
352 |
} |
353 |
clearSelection(); |
354 |
// /selectedRow=selectedRowAux;
|
355 |
|
356 |
} |
357 |
} catch (DataException e) {
|
358 |
NotificationManager.addError(e.getMessage(), e); |
359 |
} finally {
|
360 |
if (iterator != null) { |
361 |
iterator.dispose(); |
362 |
} |
363 |
} |
364 |
} |
365 |
|
366 |
public String getName() { |
367 |
return PluginServices.getText(this, "rotate_"); |
368 |
} |
369 |
|
370 |
public String toString() { |
371 |
return "_rotate"; |
372 |
} |
373 |
|
374 |
} |