root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / SymmetryCADTool.java @ 38539
History | View | Annotate | Download (12.5 KB)
1 | 37138 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | 5884 | caballero | *
|
3 | 37138 | cordinyana | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | 5884 | caballero | * 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 | 37138 | cordinyana | *
|
11 | 5884 | caballero | * 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 | 37138 | cordinyana | *
|
16 | 5884 | caballero | * 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 | 37138 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 5884 | caballero | */
|
22 | 29616 | jpiera | package org.gvsig.editing.gui.cad.tools; |
23 | 5884 | caballero | |
24 | import java.awt.event.InputEvent; |
||
25 | import java.awt.geom.Point2D; |
||
26 | |||
27 | 29616 | jpiera | import org.gvsig.andami.PluginServices; |
28 | import org.gvsig.andami.messages.NotificationManager; |
||
29 | import org.gvsig.editing.CADExtension; |
||
30 | import org.gvsig.editing.gui.cad.DefaultCADTool; |
||
31 | import org.gvsig.editing.gui.cad.exception.CommandException; |
||
32 | import org.gvsig.editing.gui.cad.tools.smc.SymmetryCADToolContext; |
||
33 | import org.gvsig.editing.gui.cad.tools.smc.SymmetryCADToolContext.SymmetryCADToolState; |
||
34 | import org.gvsig.editing.layers.VectorialLayerEdited; |
||
35 | 24500 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
36 | import org.gvsig.fmap.dal.exception.ReadException; |
||
37 | 24490 | jmvivo | import org.gvsig.fmap.dal.feature.EditableFeature; |
38 | import org.gvsig.fmap.dal.feature.Feature; |
||
39 | 38453 | cordinyana | import org.gvsig.fmap.dal.feature.FeatureSelection; |
40 | 24490 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureSet; |
41 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
42 | 21668 | vcaballero | import org.gvsig.fmap.geom.Geometry; |
43 | import org.gvsig.fmap.geom.handler.Handler; |
||
44 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
45 | 37328 | cordinyana | import org.gvsig.fmap.geom.type.GeometryType; |
46 | 21668 | vcaballero | import org.gvsig.fmap.geom.util.UtilFunctions; |
47 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
48 | 30335 | jpiera | import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
49 | 37138 | cordinyana | import org.gvsig.tools.dispose.DisposableIterator; |
50 | 21668 | vcaballero | |
51 | 5884 | caballero | /**
|
52 | * Herramienta para crear una geometr?a sim?trica a otra, con la posibilidad de
|
||
53 | * borrar la original.
|
||
54 | 37138 | cordinyana | *
|
55 | 5884 | caballero | * @author Vicente Caballero Navarro
|
56 | */
|
||
57 | public class SymmetryCADTool extends DefaultCADTool { |
||
58 | |||
59 | 37138 | cordinyana | protected SymmetryCADToolContext _fsm;
|
60 | protected Point2D firstPoint; |
||
61 | protected Point2D secondPoint; |
||
62 | 5884 | caballero | |
63 | 37138 | cordinyana | /**
|
64 | * M?todo de inicio, 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 SymmetryCADToolContext(this); |
||
69 | } |
||
70 | 5884 | caballero | |
71 | 37138 | cordinyana | /*
|
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 | 5884 | caballero | |
83 | 37138 | cordinyana | /*
|
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 | 5884 | caballero | |
95 | 37138 | cordinyana | /*
|
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 | 5884 | caballero | |
109 | 37138 | cordinyana | /**
|
110 | * DOCUMENT ME!
|
||
111 | */
|
||
112 | public void selection() { |
||
113 | FeatureSet selection = null;
|
||
114 | try {
|
||
115 | selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
||
116 | 5884 | caballero | |
117 | 37138 | cordinyana | if (selection.getSize() == 0 |
118 | && !CADExtension |
||
119 | .getCADTool() |
||
120 | .getClass() |
||
121 | .getName() |
||
122 | .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
||
123 | CADExtension.setCADTool("_selection", false); |
||
124 | ((SelectionCADTool) CADExtension.getCADTool()) |
||
125 | .setNextTool("_symmetry");
|
||
126 | } |
||
127 | } catch (ReadException e) {
|
||
128 | // TODO Auto-generated catch block
|
||
129 | e.printStackTrace(); |
||
130 | } catch (DataException e) {
|
||
131 | // TODO Auto-generated catch block
|
||
132 | e.printStackTrace(); |
||
133 | } |
||
134 | } |
||
135 | 5884 | caballero | |
136 | 37138 | cordinyana | /**
|
137 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
138 | * editableFeatureSource que ya estar? creado.
|
||
139 | *
|
||
140 | * @param x
|
||
141 | * par?metro x del punto que se pase en esta transici?n.
|
||
142 | * @param y
|
||
143 | * par?metro y del punto que se pase en esta transici?n.
|
||
144 | */
|
||
145 | public void addPoint(double x, double y, InputEvent event) { |
||
146 | SymmetryCADToolState actualState = |
||
147 | (SymmetryCADToolState) _fsm.getPreviousState(); |
||
148 | String status = actualState.getName();
|
||
149 | if (status.equals("Symmetry.FirstPoint")) { |
||
150 | firstPoint = new Point2D.Double(x, y); |
||
151 | } else
|
||
152 | if (status.equals("Symmetry.SecondPoint")) { |
||
153 | secondPoint = new Point2D.Double(x, y); |
||
154 | } |
||
155 | } |
||
156 | 24429 | vcaballero | |
157 | 37138 | cordinyana | /**
|
158 | * M?todo para dibujar la lo necesario para el estado en el que nos
|
||
159 | * encontremos.
|
||
160 | *
|
||
161 | * @param g
|
||
162 | * Graphics sobre el que dibujar.
|
||
163 | * @param x
|
||
164 | * par?metro x del punto que se pase para dibujar.
|
||
165 | * @param y
|
||
166 | * par?metro x del punto que se pase para dibujar.
|
||
167 | */
|
||
168 | public void drawOperation(MapControlDrawer renderer, double x, double y) { |
||
169 | SymmetryCADToolState actualState = _fsm.getState(); |
||
170 | String status = actualState.getName();
|
||
171 | 5884 | caballero | |
172 | 37138 | cordinyana | if (status.equals("Symmetry.SecondPoint")) { |
173 | Point2D pAux = new Point2D.Double(x, y); |
||
174 | drawSymmetry(renderer, pAux); |
||
175 | } else
|
||
176 | if (status.equals("Symmetry.CutOrCopy")) { |
||
177 | drawSymmetry(renderer, secondPoint); |
||
178 | } |
||
179 | } |
||
180 | 5884 | caballero | |
181 | 37138 | cordinyana | private void drawSymmetry(MapControlDrawer renderer, Point2D pAux) { |
182 | 23086 | vcaballero | |
183 | 37138 | cordinyana | VectorialLayerEdited vle = getVLE(); |
184 | FeatureSet selection = null;
|
||
185 | try {
|
||
186 | selection = (FeatureSet) vle.getFeatureStore().getSelection(); |
||
187 | } catch (ReadException e1) {
|
||
188 | // TODO Auto-generated catch block
|
||
189 | e1.printStackTrace(); |
||
190 | } catch (DataException e) {
|
||
191 | // TODO Auto-generated catch block
|
||
192 | e.printStackTrace(); |
||
193 | } |
||
194 | ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
||
195 | 30335 | jpiera | |
196 | 37138 | cordinyana | GeneralPathX gpx = new GeneralPathX();
|
197 | gpx.moveTo(firstPoint.getX(), firstPoint.getY()); |
||
198 | gpx.lineTo(pAux.getX(), pAux.getY()); |
||
199 | 30335 | jpiera | |
200 | 37138 | cordinyana | renderer.draw(createCurve(gpx), |
201 | mapControlManager.getAxisReferenceSymbol()); |
||
202 | 24429 | vcaballero | |
203 | 37138 | cordinyana | DisposableIterator iterator = null;
|
204 | try {
|
||
205 | iterator = selection.iterator(); |
||
206 | while (iterator.hasNext()) {
|
||
207 | Feature feature = (Feature) iterator.next(); |
||
208 | 24429 | vcaballero | |
209 | 37138 | cordinyana | // }
|
210 | // for (int i = 0; i < selectedRow.size(); i++) {
|
||
211 | // DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
|
||
212 | // DefaultFeature fea = (DefaultFeature) row.getLinkedRow();
|
||
213 | 24429 | vcaballero | |
214 | 37138 | cordinyana | Geometry geom = (feature.getDefaultGeometry()).cloneGeometry(); |
215 | Handler[] handlers = geom.getHandlers(Geometry.SELECTHANDLER); |
||
216 | 5884 | caballero | |
217 | 37138 | cordinyana | for (int j = 0; j < handlers.length; j++) { |
218 | Handler h = handlers[j];
|
||
219 | Point2D p = h.getPoint();
|
||
220 | Point2D[] ps = |
||
221 | UtilFunctions.getPerpendicular(firstPoint, pAux, p); |
||
222 | Point2D inter =
|
||
223 | UtilFunctions.getIntersection(ps[0], ps[1], firstPoint, |
||
224 | pAux); |
||
225 | if (inter != null) { |
||
226 | Point2D dif =
|
||
227 | new Point2D.Double(inter.getX() - p.getX(), |
||
228 | inter.getY() - p.getY()); |
||
229 | h.set(inter.getX() + dif.getX(), |
||
230 | inter.getY() + dif.getY()); |
||
231 | } |
||
232 | } |
||
233 | renderer.draw(geom, mapControlManager.getAxisReferenceSymbol()); |
||
234 | } |
||
235 | } catch (DataException e1) {
|
||
236 | // TODO Auto-generated catch block
|
||
237 | e1.printStackTrace(); |
||
238 | } finally {
|
||
239 | if (iterator != null) { |
||
240 | iterator.dispose(); |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | 5884 | caballero | |
245 | 37138 | cordinyana | /**
|
246 | * Add a diferent option.
|
||
247 | *
|
||
248 | * @param s
|
||
249 | * Diferent option.
|
||
250 | */
|
||
251 | public void addOption(String s) { |
||
252 | SymmetryCADToolState actualState = |
||
253 | (SymmetryCADToolState) _fsm.getPreviousState(); |
||
254 | String status = actualState.getName();
|
||
255 | VectorialLayerEdited vle = getVLE(); |
||
256 | FeatureStore featureStore = null;
|
||
257 | 38453 | cordinyana | FeatureSelection selection = null;
|
258 | 37138 | cordinyana | try {
|
259 | featureStore = vle.getFeatureStore(); |
||
260 | 38453 | cordinyana | selection = featureStore.getFeatureSelection(); |
261 | 37138 | cordinyana | } catch (DataException e1) {
|
262 | NotificationManager.addError(e1.getMessage(), e1); |
||
263 | return;
|
||
264 | } |
||
265 | // ArrayList selectedRowAux=new ArrayList();
|
||
266 | // VectorialEditableAdapter vea = vle.getVEA();
|
||
267 | if (status.equals("Symmetry.CutOrCopy")) { |
||
268 | PluginServices.getMDIManager().setWaitCursor(); |
||
269 | DisposableIterator iterator = null;
|
||
270 | try {
|
||
271 | featureStore.beginEditingGroup(getName()); |
||
272 | 38453 | cordinyana | // If not moved, deselect the previous features to select the new ones
|
273 | 37138 | cordinyana | try {
|
274 | 38453 | cordinyana | FeatureSelection newSelection = |
275 | featureStore.createFeatureSelection(); |
||
276 | 37138 | cordinyana | iterator = selection.iterator(); |
277 | while (iterator.hasNext()) {
|
||
278 | Feature feature = (Feature) iterator.next(); |
||
279 | 23086 | vcaballero | |
280 | 37138 | cordinyana | Geometry geom = |
281 | feature.getDefaultGeometry().cloneGeometry(); |
||
282 | Handler[] handlers = |
||
283 | geom.getHandlers(Geometry.SELECTHANDLER); |
||
284 | 5884 | caballero | |
285 | 37138 | cordinyana | for (int j = 0; j < handlers.length; j++) { |
286 | Handler h = handlers[j];
|
||
287 | Point2D p = h.getPoint();
|
||
288 | Point2D[] ps = |
||
289 | UtilFunctions.getPerpendicular(firstPoint, |
||
290 | secondPoint, p); |
||
291 | Point2D inter =
|
||
292 | UtilFunctions.getIntersection(ps[0], ps[1], |
||
293 | firstPoint, secondPoint); |
||
294 | Point2D dif =
|
||
295 | new Point2D.Double(inter.getX() - p.getX(), |
||
296 | inter.getY() - p.getY()); |
||
297 | h.set(inter.getX() + dif.getX(), |
||
298 | inter.getY() + dif.getY()); |
||
299 | } |
||
300 | 5884 | caballero | |
301 | 37138 | cordinyana | if (s.equals(PluginServices.getText(this, "cut")) |
302 | || s.equals("s") || s.equals("S")) { |
||
303 | EditableFeature eFeature = feature.getEditable(); |
||
304 | eFeature.setGeometry(featureStore |
||
305 | .getDefaultFeatureType() |
||
306 | .getDefaultGeometryAttributeName(), geom); |
||
307 | featureStore.update(eFeature); |
||
308 | 38453 | cordinyana | newSelection.select(feature); |
309 | 37138 | cordinyana | } else {
|
310 | 38453 | cordinyana | Feature newFeature = insertGeometry(geom); |
311 | newSelection.select(newFeature); |
||
312 | 37138 | cordinyana | } |
313 | refresh(); |
||
314 | 5884 | caballero | |
315 | 37138 | cordinyana | } |
316 | 38453 | cordinyana | featureStore.setSelection(newSelection); |
317 | 37138 | cordinyana | } finally {
|
318 | featureStore.endEditingGroup(); |
||
319 | } |
||
320 | } catch (DataException e) {
|
||
321 | NotificationManager.addError(e.getMessage(), e); |
||
322 | } finally {
|
||
323 | if (iterator != null) { |
||
324 | iterator.dispose(); |
||
325 | } |
||
326 | 38453 | cordinyana | PluginServices.getMDIManager().restoreCursor(); |
327 | 37138 | cordinyana | } |
328 | 5884 | caballero | |
329 | 37138 | cordinyana | } |
330 | } |
||
331 | 5884 | caballero | |
332 | 37138 | cordinyana | public void addValue(double d) { |
333 | 5884 | caballero | |
334 | 37138 | cordinyana | } |
335 | 5884 | caballero | |
336 | 37138 | cordinyana | public String getName() { |
337 | return PluginServices.getText(this, "symmetry_"); |
||
338 | } |
||
339 | 5884 | caballero | |
340 | 37138 | cordinyana | public String toString() { |
341 | return "_symmetry"; |
||
342 | } |
||
343 | |||
344 | 37328 | cordinyana | @Override
|
345 | public boolean isApplicable(GeometryType geometryType) { |
||
346 | return true; |
||
347 | } |
||
348 | |||
349 | @Override
|
||
350 | protected int[] getSupportedGeometryTypes() { |
||
351 | return null; |
||
352 | } |
||
353 | |||
354 | 5884 | caballero | } |