svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / RotateCADTool.java @ 39573
History | View | Annotate | Download (11.8 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 |
&& !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
|
116 |
CADExtension.setCADTool("_selection", false); |
117 |
((SelectionCADTool) CADExtension.getCADTool()) |
118 |
.setNextTool("_rotate");
|
119 |
} |
120 |
} catch (ReadException e) {
|
121 |
// TODO Auto-generated catch block
|
122 |
e.printStackTrace(); |
123 |
} catch (DataException e) {
|
124 |
// TODO Auto-generated catch block
|
125 |
e.printStackTrace(); |
126 |
} |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
131 |
* editableFeatureSource que ya estar? creado.
|
132 |
*
|
133 |
* @param x
|
134 |
* par?metro x del punto que se pase en esta transici?n.
|
135 |
* @param y
|
136 |
* par?metro y del punto que se pase en esta transici?n.
|
137 |
*/
|
138 |
public void addPoint(double x, double y, InputEvent event) { |
139 |
RotateCADToolState actualState = |
140 |
(RotateCADToolState) _fsm.getPreviousState(); |
141 |
String status = actualState.getName();
|
142 |
VectorialLayerEdited vle = getVLE(); |
143 |
FeatureStore featureStore = null;
|
144 |
try {
|
145 |
featureStore = vle.getFeatureStore(); |
146 |
} catch (ReadException e1) {
|
147 |
// TODO Auto-generated catch block
|
148 |
e1.printStackTrace(); |
149 |
} |
150 |
DisposableIterator iterator = null;
|
151 |
try {
|
152 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
153 |
ArrayList selectedRowAux = new ArrayList(); |
154 |
|
155 |
if (status.equals("Rotate.PointMain")) { |
156 |
firstPoint = new Point2D.Double(x, y); |
157 |
} else
|
158 |
if (status.equals("Rotate.AngleOrPoint")) { |
159 |
PluginServices.getMDIManager().setWaitCursor(); |
160 |
lastPoint = new Point2D.Double(x, y); |
161 |
|
162 |
double w;
|
163 |
double h;
|
164 |
w = lastPoint.getX() - firstPoint.getX(); |
165 |
h = lastPoint.getY() - firstPoint.getY(); |
166 |
|
167 |
featureStore.beginEditingGroup(getName()); |
168 |
try {
|
169 |
iterator = selection.iterator(); |
170 |
while (iterator.hasNext()) {
|
171 |
Feature feature = (Feature) iterator.next(); |
172 |
|
173 |
// }
|
174 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
175 |
// DefaultRowEdited row=(DefaultRowEdited)
|
176 |
// selectedRow.get(i);
|
177 |
// DefaultFeature fea = (DefaultFeature)
|
178 |
// row.getLinkedRow().cloneRow();
|
179 |
// Rotamos la geometry
|
180 |
Geometry geometry = |
181 |
(feature.getDefaultGeometry()).cloneGeometry(); |
182 |
EditableFeature eFeature = feature.getEditable(); |
183 |
|
184 |
geometry.rotate(-Math.atan2(w, h) + (Math.PI / 2), |
185 |
firstPoint.getX(), firstPoint.getY()); |
186 |
|
187 |
eFeature.setGeometry(featureStore |
188 |
.getDefaultFeatureType() |
189 |
.getDefaultGeometryAttributeName(), geometry); |
190 |
featureStore.update(eFeature); |
191 |
// vea.modifyRow(row.getIndex(), fea,
|
192 |
// getName(),EditionEvent.GRAPHIC);
|
193 |
// selectedRowAux.add(new
|
194 |
// DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
|
195 |
} |
196 |
} finally {
|
197 |
|
198 |
featureStore.endEditingGroup(); |
199 |
} |
200 |
// vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
|
201 |
// selectedRowAux);
|
202 |
// clearSelection();
|
203 |
// selectedRow.addAll(selectedRowAux);
|
204 |
|
205 |
PluginServices.getMDIManager().restoreCursor(); |
206 |
} |
207 |
} catch (DataException e) {
|
208 |
NotificationManager.addError(e.getMessage(), e); |
209 |
} finally {
|
210 |
if (iterator != null) { |
211 |
iterator.dispose(); |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
/**
|
217 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
218 |
* encontremos.
|
219 |
*
|
220 |
* @param g
|
221 |
* Graphics sobre el que dibujar.
|
222 |
* @param x
|
223 |
* par?metro x del punto que se pase para dibujar.
|
224 |
* @param y
|
225 |
* par?metro x del punto que se pase para dibujar.
|
226 |
*/
|
227 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
228 |
RotateCADToolState actualState = _fsm.getState(); |
229 |
String status = actualState.getName();
|
230 |
|
231 |
if (status.equals("Rotate.AngleOrPoint")) { |
232 |
drawAndRotateSelectedGeometries(renderer, firstPoint, x, y); |
233 |
renderer.drawLine(firstPoint, new Point2D.Double(x, y), |
234 |
mapControlManager.getAxisReferenceSymbol()); |
235 |
} |
236 |
} |
237 |
|
238 |
/**
|
239 |
* Add a diferent option.
|
240 |
*
|
241 |
* @param s
|
242 |
* Diferent option.
|
243 |
*/
|
244 |
public void addOption(String s) { |
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
*
|
250 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
251 |
*/
|
252 |
public void addValue(double d) { |
253 |
RotateCADToolState actualState = |
254 |
(RotateCADToolState) _fsm.getPreviousState(); |
255 |
String status = actualState.getName();
|
256 |
VectorialLayerEdited vle = getVLE(); |
257 |
FeatureStore featureStore = null;
|
258 |
try {
|
259 |
featureStore = vle.getFeatureStore(); |
260 |
} catch (ReadException e1) {
|
261 |
// TODO Auto-generated catch block
|
262 |
e1.printStackTrace(); |
263 |
} |
264 |
DisposableIterator iterator = null;
|
265 |
try {
|
266 |
FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
|
267 |
|
268 |
if (status.equals("Rotate.AngleOrPoint")) { |
269 |
|
270 |
featureStore.beginEditingGroup(getName()); |
271 |
try {
|
272 |
// /ArrayList selectedRowAux=new ArrayList();
|
273 |
iterator = selection.iterator(); |
274 |
while (iterator.hasNext()) {
|
275 |
Feature feature = (Feature) iterator.next(); |
276 |
|
277 |
// }
|
278 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
279 |
// DefaultRowEdited row=(DefaultRowEdited)
|
280 |
// selectedRow.get(i);
|
281 |
// DefaultFeature fea = (DefaultFeature)
|
282 |
// row.getLinkedRow().cloneRow();
|
283 |
// Rotamos la geometry
|
284 |
AffineTransform at = new AffineTransform(); |
285 |
at.rotate(Math.toRadians(d), firstPoint.getX(),
|
286 |
firstPoint.getY()); |
287 |
Geometry geometry = |
288 |
(feature.getDefaultGeometry()).cloneGeometry(); |
289 |
EditableFeature eFeature = feature.getEditable(); |
290 |
geometry.transform(at); |
291 |
eFeature.setGeometry(featureStore |
292 |
.getDefaultFeatureType() |
293 |
.getDefaultGeometryAttributeName(), geometry); |
294 |
featureStore.update(eFeature); |
295 |
// vea.modifyRow(row.getIndex(),
|
296 |
// fea,getName(),EditionEvent.GRAPHIC);
|
297 |
// /selectedRowAux.add(new
|
298 |
// DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
|
299 |
} |
300 |
} finally {
|
301 |
featureStore.endEditingGroup(); |
302 |
} |
303 |
clearSelection(); |
304 |
// /selectedRow=selectedRowAux;
|
305 |
|
306 |
} |
307 |
} catch (DataException e) {
|
308 |
NotificationManager.addError(e.getMessage(), e); |
309 |
} finally {
|
310 |
if (iterator != null) { |
311 |
iterator.dispose(); |
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
public String getName() { |
317 |
return PluginServices.getText(this, "rotate_"); |
318 |
} |
319 |
|
320 |
public String toString() { |
321 |
return "_rotate"; |
322 |
} |
323 |
|
324 |
@Override
|
325 |
public boolean isApplicable(GeometryType geometryType) { |
326 |
return true; |
327 |
} |
328 |
|
329 |
@Override
|
330 |
protected int[] getSupportedGeometryTypes() { |
331 |
return null; |
332 |
} |
333 |
} |