svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / RotateCADTool.java @ 39296
History | View | Annotate | Download (12 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.editing.gui.cad.tools; |
23 |
|
24 |
import java.awt.event.InputEvent; |
25 |
import java.awt.geom.AffineTransform; |
26 |
import java.awt.geom.Point2D; |
27 |
import java.util.ArrayList; |
28 |
|
29 |
import org.gvsig.andami.PluginServices; |
30 |
import org.gvsig.andami.messages.NotificationManager; |
31 |
import org.gvsig.editing.CADExtension; |
32 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
33 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
34 |
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext; |
35 |
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState; |
36 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
37 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.exception.ReadException; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.fmap.geom.Geometry; |
44 |
import org.gvsig.fmap.geom.type.GeometryType; |
45 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
46 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
47 |
import org.gvsig.tools.dispose.DisposableIterator; |
48 |
|
49 |
/**
|
50 |
* DOCUMENT ME!
|
51 |
*
|
52 |
* @author Vicente Caballero Navarro
|
53 |
*/
|
54 |
public class RotateCADTool extends DefaultCADTool { |
55 |
|
56 |
private RotateCADToolContext _fsm;
|
57 |
private Point2D firstPoint; |
58 |
private Point2D lastPoint; |
59 |
|
60 |
/**
|
61 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
62 |
* carga previa a la utilizaci?n de la herramienta.
|
63 |
*/
|
64 |
public void init() { |
65 |
_fsm = new RotateCADToolContext(this); |
66 |
} |
67 |
|
68 |
/*
|
69 |
* (non-Javadoc)
|
70 |
*
|
71 |
* @see
|
72 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
73 |
* .layers.FBitSet,
|
74 |
* double, double)
|
75 |
*/
|
76 |
public void transition(double x, double y, InputEvent event) { |
77 |
_fsm.addPoint(x, y, event); |
78 |
} |
79 |
|
80 |
/*
|
81 |
* (non-Javadoc)
|
82 |
*
|
83 |
* @see
|
84 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
85 |
* .layers.FBitSet,
|
86 |
* double)
|
87 |
*/
|
88 |
public void transition(double d) { |
89 |
_fsm.addValue(d); |
90 |
} |
91 |
|
92 |
/*
|
93 |
* (non-Javadoc)
|
94 |
*
|
95 |
* @see
|
96 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
97 |
* .layers.FBitSet,
|
98 |
* java.lang.String)
|
99 |
*/
|
100 |
public void transition(String s) throws CommandException { |
101 |
if (!super.changeCommand(s)) { |
102 |
_fsm.addOption(s); |
103 |
} |
104 |
} |
105 |
|
106 |
/**
|
107 |
* DOCUMENT ME!
|
108 |
*/
|
109 |
public void selection() { |
110 |
FeatureSet selection = null;
|
111 |
try {
|
112 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
113 |
|
114 |
if (selection.getSize() == 0 |
115 |
&& !CADExtension |
116 |
.getCADTool() |
117 |
.getClass() |
118 |
.getName() |
119 |
.equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
120 |
CADExtension.setCADTool("_selection", false); |
121 |
((SelectionCADTool) CADExtension.getCADTool()) |
122 |
.setNextTool("_rotate");
|
123 |
} |
124 |
} catch (ReadException e) {
|
125 |
// TODO Auto-generated catch block
|
126 |
e.printStackTrace(); |
127 |
} catch (DataException e) {
|
128 |
// TODO Auto-generated catch block
|
129 |
e.printStackTrace(); |
130 |
} |
131 |
} |
132 |
|
133 |
/**
|
134 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
135 |
* editableFeatureSource que ya estar? creado.
|
136 |
*
|
137 |
* @param x
|
138 |
* par?metro x del punto que se pase en esta transici?n.
|
139 |
* @param y
|
140 |
* par?metro y del punto que se pase en esta transici?n.
|
141 |
*/
|
142 |
public void addPoint(double x, double y, InputEvent event) { |
143 |
RotateCADToolState actualState = |
144 |
(RotateCADToolState) _fsm.getPreviousState(); |
145 |
String status = actualState.getName();
|
146 |
VectorialLayerEdited vle = getVLE(); |
147 |
FeatureStore featureStore = null;
|
148 |
try {
|
149 |
featureStore = vle.getFeatureStore(); |
150 |
} catch (ReadException e1) {
|
151 |
// TODO Auto-generated catch block
|
152 |
e1.printStackTrace(); |
153 |
} |
154 |
DisposableIterator iterator = null;
|
155 |
try {
|
156 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
157 |
ArrayList selectedRowAux = new ArrayList(); |
158 |
|
159 |
if (status.equals("Rotate.PointMain")) { |
160 |
firstPoint = new Point2D.Double(x, y); |
161 |
} else
|
162 |
if (status.equals("Rotate.AngleOrPoint")) { |
163 |
PluginServices.getMDIManager().setWaitCursor(); |
164 |
lastPoint = new Point2D.Double(x, y); |
165 |
|
166 |
double w;
|
167 |
double h;
|
168 |
w = lastPoint.getX() - firstPoint.getX(); |
169 |
h = lastPoint.getY() - firstPoint.getY(); |
170 |
|
171 |
featureStore.beginEditingGroup(getName()); |
172 |
try {
|
173 |
iterator = selection.iterator(); |
174 |
while (iterator.hasNext()) {
|
175 |
Feature feature = (Feature) iterator.next(); |
176 |
|
177 |
// }
|
178 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
179 |
// DefaultRowEdited row=(DefaultRowEdited)
|
180 |
// selectedRow.get(i);
|
181 |
// DefaultFeature fea = (DefaultFeature)
|
182 |
// row.getLinkedRow().cloneRow();
|
183 |
// Rotamos la geometry
|
184 |
Geometry geometry = |
185 |
(feature.getDefaultGeometry()).cloneGeometry(); |
186 |
EditableFeature eFeature = feature.getEditable(); |
187 |
|
188 |
geometry.rotate(-Math.atan2(w, h) + (Math.PI / 2), |
189 |
firstPoint.getX(), firstPoint.getY()); |
190 |
|
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 |
try {
|
263 |
featureStore = vle.getFeatureStore(); |
264 |
} catch (ReadException e1) {
|
265 |
// TODO Auto-generated catch block
|
266 |
e1.printStackTrace(); |
267 |
} |
268 |
DisposableIterator iterator = null;
|
269 |
try {
|
270 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
271 |
|
272 |
if (status.equals("Rotate.AngleOrPoint")) { |
273 |
|
274 |
featureStore.beginEditingGroup(getName()); |
275 |
try {
|
276 |
// /ArrayList selectedRowAux=new ArrayList();
|
277 |
iterator = selection.iterator(); |
278 |
while (iterator.hasNext()) {
|
279 |
Feature feature = (Feature) iterator.next(); |
280 |
|
281 |
// }
|
282 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
283 |
// DefaultRowEdited row=(DefaultRowEdited)
|
284 |
// selectedRow.get(i);
|
285 |
// DefaultFeature fea = (DefaultFeature)
|
286 |
// row.getLinkedRow().cloneRow();
|
287 |
// Rotamos la geometry
|
288 |
AffineTransform at = new AffineTransform(); |
289 |
at.rotate(Math.toRadians(d), firstPoint.getX(),
|
290 |
firstPoint.getY()); |
291 |
Geometry geometry = |
292 |
(feature.getDefaultGeometry()).cloneGeometry(); |
293 |
EditableFeature eFeature = feature.getEditable(); |
294 |
geometry.transform(at); |
295 |
eFeature.setGeometry(featureStore |
296 |
.getDefaultFeatureType() |
297 |
.getDefaultGeometryAttributeName(), geometry); |
298 |
featureStore.update(eFeature); |
299 |
// vea.modifyRow(row.getIndex(),
|
300 |
// fea,getName(),EditionEvent.GRAPHIC);
|
301 |
// /selectedRowAux.add(new
|
302 |
// DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
303 |
} |
304 |
} finally {
|
305 |
featureStore.endEditingGroup(); |
306 |
} |
307 |
clearSelection(); |
308 |
// /selectedRow=selectedRowAux;
|
309 |
|
310 |
} |
311 |
} catch (DataException e) {
|
312 |
NotificationManager.addError(e.getMessage(), e); |
313 |
} finally {
|
314 |
if (iterator != null) { |
315 |
iterator.dispose(); |
316 |
} |
317 |
} |
318 |
} |
319 |
|
320 |
public String getName() { |
321 |
return PluginServices.getText(this, "rotate_"); |
322 |
} |
323 |
|
324 |
public String toString() { |
325 |
return "_rotate"; |
326 |
} |
327 |
|
328 |
@Override
|
329 |
public boolean isApplicable(GeometryType geometryType) { |
330 |
return true; |
331 |
} |
332 |
|
333 |
@Override
|
334 |
protected int[] getSupportedGeometryTypes() { |
335 |
return null; |
336 |
} |
337 |
} |