svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / RotateCADTool.java @ 40557
History | View | Annotate | Download (12.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.editing.gui.cad.tools; |
25 |
|
26 |
import java.awt.event.InputEvent; |
27 |
import java.awt.geom.AffineTransform; |
28 |
import java.awt.geom.Point2D; |
29 |
import java.util.ArrayList; |
30 |
|
31 |
import org.gvsig.andami.PluginServices; |
32 |
import org.gvsig.andami.messages.NotificationManager; |
33 |
import org.gvsig.editing.CADExtension; |
34 |
import org.gvsig.editing.IEditionManager; |
35 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
36 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
37 |
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext; |
38 |
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState; |
39 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
40 |
import org.gvsig.fmap.dal.exception.DataException; |
41 |
import org.gvsig.fmap.dal.exception.ReadException; |
42 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
43 |
import org.gvsig.fmap.dal.feature.Feature; |
44 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.geom.Geometry; |
47 |
import org.gvsig.fmap.geom.type.GeometryType; |
48 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
49 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
50 |
import org.gvsig.tools.dispose.DisposableIterator; |
51 |
|
52 |
/**
|
53 |
* DOCUMENT ME!
|
54 |
*
|
55 |
* @author Vicente Caballero Navarro
|
56 |
*/
|
57 |
public class RotateCADTool extends DefaultCADTool { |
58 |
|
59 |
private RotateCADToolContext _fsm;
|
60 |
private Point2D firstPoint; |
61 |
private Point2D lastPoint; |
62 |
|
63 |
/**
|
64 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
65 |
* carga previa a la utilizaci?n de la herramienta.
|
66 |
*/
|
67 |
public void init() { |
68 |
_fsm = new RotateCADToolContext(this); |
69 |
} |
70 |
|
71 |
/*
|
72 |
* (non-Javadoc)
|
73 |
*
|
74 |
* @see
|
75 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
76 |
* .layers.FBitSet,
|
77 |
* double, double)
|
78 |
*/
|
79 |
public void transition(double x, double y, InputEvent event) { |
80 |
_fsm.addPoint(x, y, event); |
81 |
} |
82 |
|
83 |
/*
|
84 |
* (non-Javadoc)
|
85 |
*
|
86 |
* @see
|
87 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
88 |
* .layers.FBitSet,
|
89 |
* double)
|
90 |
*/
|
91 |
public void transition(double d) { |
92 |
_fsm.addValue(d); |
93 |
} |
94 |
|
95 |
/*
|
96 |
* (non-Javadoc)
|
97 |
*
|
98 |
* @see
|
99 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
100 |
* .layers.FBitSet,
|
101 |
* java.lang.String)
|
102 |
*/
|
103 |
public void transition(String s) throws CommandException { |
104 |
if (!super.changeCommand(s)) { |
105 |
_fsm.addOption(s); |
106 |
} |
107 |
} |
108 |
|
109 |
/**
|
110 |
* DOCUMENT ME!
|
111 |
*/
|
112 |
public void selection() { |
113 |
FeatureSet selection = null;
|
114 |
try {
|
115 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
116 |
|
117 |
if (selection.getSize() == 0 |
118 |
&& !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
|
119 |
CADExtension.setCADTool("_selection", false); |
120 |
((SelectionCADTool) CADExtension.getCADTool()) |
121 |
.setNextTool("_rotate");
|
122 |
} |
123 |
} catch (ReadException e) {
|
124 |
// TODO Auto-generated catch block
|
125 |
e.printStackTrace(); |
126 |
} catch (DataException e) {
|
127 |
// TODO Auto-generated catch block
|
128 |
e.printStackTrace(); |
129 |
} |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
134 |
* editableFeatureSource que ya estar? creado.
|
135 |
*
|
136 |
* @param x
|
137 |
* par?metro x del punto que se pase en esta transici?n.
|
138 |
* @param y
|
139 |
* par?metro y del punto que se pase en esta transici?n.
|
140 |
*/
|
141 |
public void addPoint(double x, double y, InputEvent event) { |
142 |
RotateCADToolState actualState = |
143 |
(RotateCADToolState) _fsm.getPreviousState(); |
144 |
String status = actualState.getName();
|
145 |
VectorialLayerEdited vle = getVLE(); |
146 |
FeatureStore featureStore = null;
|
147 |
try {
|
148 |
featureStore = vle.getFeatureStore(); |
149 |
} catch (ReadException e1) {
|
150 |
// TODO Auto-generated catch block
|
151 |
e1.printStackTrace(); |
152 |
} |
153 |
DisposableIterator iterator = null;
|
154 |
try {
|
155 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
156 |
ArrayList selectedRowAux = new ArrayList(); |
157 |
|
158 |
if (status.equals("Rotate.PointMain")) { |
159 |
firstPoint = new Point2D.Double(x, y); |
160 |
} else
|
161 |
if (status.equals("Rotate.AngleOrPoint")) { |
162 |
PluginServices.getMDIManager().setWaitCursor(); |
163 |
lastPoint = new Point2D.Double(x, y); |
164 |
|
165 |
double w;
|
166 |
double h;
|
167 |
w = lastPoint.getX() - firstPoint.getX(); |
168 |
h = lastPoint.getY() - firstPoint.getY(); |
169 |
|
170 |
featureStore.beginEditingGroup(getName()); |
171 |
try {
|
172 |
iterator = selection.iterator(); |
173 |
while (iterator.hasNext()) {
|
174 |
Feature feature = (Feature) iterator.next(); |
175 |
|
176 |
// }
|
177 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
178 |
// DefaultRowEdited row=(DefaultRowEdited)
|
179 |
// selectedRow.get(i);
|
180 |
// DefaultFeature fea = (DefaultFeature)
|
181 |
// row.getLinkedRow().cloneRow();
|
182 |
// Rotamos la geometry
|
183 |
Geometry geometry = |
184 |
(feature.getDefaultGeometry()).cloneGeometry(); |
185 |
EditableFeature eFeature = feature.getEditable(); |
186 |
|
187 |
geometry.rotate(-Math.atan2(w, h) + (Math.PI / 2), |
188 |
firstPoint.getX(), firstPoint.getY()); |
189 |
|
190 |
super.updateGeometry(featureStore, feature, geometry);
|
191 |
// eFeature.setGeometry(featureStore
|
192 |
// .getDefaultFeatureType()
|
193 |
// .getDefaultGeometryAttributeName(), geometry);
|
194 |
// featureStore.update(eFeature);
|
195 |
// vea.modifyRow(row.getIndex(), fea,
|
196 |
// getName(),EditionEvent.GRAPHIC);
|
197 |
// selectedRowAux.add(new
|
198 |
// DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
|
199 |
} |
200 |
} finally {
|
201 |
|
202 |
featureStore.endEditingGroup(); |
203 |
} |
204 |
// vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
|
205 |
// selectedRowAux);
|
206 |
// clearSelection();
|
207 |
// selectedRow.addAll(selectedRowAux);
|
208 |
|
209 |
PluginServices.getMDIManager().restoreCursor(); |
210 |
} |
211 |
} catch (DataException e) {
|
212 |
NotificationManager.addError(e.getMessage(), e); |
213 |
} finally {
|
214 |
if (iterator != null) { |
215 |
iterator.dispose(); |
216 |
} |
217 |
} |
218 |
} |
219 |
|
220 |
/**
|
221 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
222 |
* encontremos.
|
223 |
*
|
224 |
* @param g
|
225 |
* Graphics sobre el que dibujar.
|
226 |
* @param x
|
227 |
* par?metro x del punto que se pase para dibujar.
|
228 |
* @param y
|
229 |
* par?metro x del punto que se pase para dibujar.
|
230 |
*/
|
231 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
232 |
RotateCADToolState actualState = _fsm.getState(); |
233 |
String status = actualState.getName();
|
234 |
|
235 |
if (status.equals("Rotate.AngleOrPoint")) { |
236 |
drawAndRotateSelectedGeometries(renderer, firstPoint, x, y); |
237 |
renderer.drawLine(firstPoint, new Point2D.Double(x, y), |
238 |
mapControlManager.getAxisReferenceSymbol()); |
239 |
} |
240 |
} |
241 |
|
242 |
/**
|
243 |
* Add a diferent option.
|
244 |
*
|
245 |
* @param s
|
246 |
* Diferent option.
|
247 |
*/
|
248 |
public void addOption(String s) { |
249 |
} |
250 |
|
251 |
/*
|
252 |
* (non-Javadoc)
|
253 |
*
|
254 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
255 |
*/
|
256 |
public void addValue(double d) { |
257 |
RotateCADToolState actualState = |
258 |
(RotateCADToolState) _fsm.getPreviousState(); |
259 |
String status = actualState.getName();
|
260 |
VectorialLayerEdited vle = getVLE(); |
261 |
FeatureStore featureStore = null;
|
262 |
IEditionManager ed_man = this.getEditionManager();
|
263 |
|
264 |
try {
|
265 |
featureStore = vle.getFeatureStore(); |
266 |
} catch (ReadException e1) {
|
267 |
// TODO Auto-generated catch block
|
268 |
e1.printStackTrace(); |
269 |
} |
270 |
DisposableIterator iterator = null;
|
271 |
try {
|
272 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
273 |
|
274 |
if (status.equals("Rotate.AngleOrPoint")) { |
275 |
|
276 |
featureStore.beginEditingGroup(getName()); |
277 |
try {
|
278 |
// /ArrayList selectedRowAux=new ArrayList();
|
279 |
iterator = selection.iterator(); |
280 |
while (iterator.hasNext()) {
|
281 |
Feature feature = (Feature) iterator.next(); |
282 |
|
283 |
// }
|
284 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
285 |
// DefaultRowEdited row=(DefaultRowEdited)
|
286 |
// selectedRow.get(i);
|
287 |
// DefaultFeature fea = (DefaultFeature)
|
288 |
// row.getLinkedRow().cloneRow();
|
289 |
// Rotamos la geometry
|
290 |
AffineTransform at = new AffineTransform(); |
291 |
at.rotate(Math.toRadians(d), firstPoint.getX(),
|
292 |
firstPoint.getY()); |
293 |
Geometry geometry = |
294 |
(feature.getDefaultGeometry()).cloneGeometry(); |
295 |
EditableFeature eFeature = feature.getEditable(); |
296 |
geometry.transform(at); |
297 |
eFeature.setGeometry(featureStore |
298 |
.getDefaultFeatureType() |
299 |
.getDefaultGeometryAttributeName(), geometry); |
300 |
|
301 |
ed_man.updateFeature(featureStore, eFeature); |
302 |
// vea.modifyRow(row.getIndex(),
|
303 |
// fea,getName(),EditionEvent.GRAPHIC);
|
304 |
// /selectedRowAux.add(new
|
305 |
// DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
306 |
} |
307 |
} finally {
|
308 |
featureStore.endEditingGroup(); |
309 |
} |
310 |
clearSelection(); |
311 |
// /selectedRow=selectedRowAux;
|
312 |
|
313 |
} |
314 |
} catch (DataException e) {
|
315 |
NotificationManager.addError(e.getMessage(), e); |
316 |
} finally {
|
317 |
if (iterator != null) { |
318 |
iterator.dispose(); |
319 |
} |
320 |
} |
321 |
} |
322 |
|
323 |
public String getName() { |
324 |
return PluginServices.getText(this, "rotate_"); |
325 |
} |
326 |
|
327 |
public String toString() { |
328 |
return "_rotate"; |
329 |
} |
330 |
|
331 |
@Override
|
332 |
public boolean isApplicable(GeometryType geometryType) { |
333 |
return true; |
334 |
} |
335 |
|
336 |
@Override
|
337 |
protected int[] getSupportedGeometryTypes() { |
338 |
return null; |
339 |
} |
340 |
} |