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 / SymmetryCADTool.java @ 40557
History | View | Annotate | Download (14.4 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.Point2D; |
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.SymmetryCADToolContext; |
35 |
import org.gvsig.editing.gui.cad.tools.smc.SymmetryCADToolContext.SymmetryCADToolState; |
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.FeatureSelection; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException; |
45 |
import org.gvsig.fmap.geom.Geometry; |
46 |
import org.gvsig.fmap.geom.handler.Handler; |
47 |
import org.gvsig.fmap.geom.operation.perpendicular.Perpendicular; |
48 |
import org.gvsig.fmap.geom.operation.perpendicular.PerpendicularOperationContext; |
49 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
50 |
import org.gvsig.fmap.geom.primitive.Point; |
51 |
import org.gvsig.fmap.geom.type.GeometryType; |
52 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
53 |
import org.gvsig.fmap.mapcontext.ViewPort; |
54 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
55 |
import org.gvsig.i18n.Messages; |
56 |
import org.gvsig.tools.dispose.DisposableIterator; |
57 |
|
58 |
/**
|
59 |
* Herramienta para crear una geometr?a sim?trica a otra, con la posibilidad de
|
60 |
* borrar la original.
|
61 |
*
|
62 |
* @author Vicente Caballero Navarro
|
63 |
*/
|
64 |
public class SymmetryCADTool extends DefaultCADTool { |
65 |
|
66 |
protected SymmetryCADToolContext _fsm;
|
67 |
protected Point2D firstPoint; |
68 |
protected Point2D secondPoint; |
69 |
|
70 |
/**
|
71 |
* M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
72 |
* carga previa a la utilizaci?n de la herramienta.
|
73 |
*/
|
74 |
public void init() { |
75 |
_fsm = new SymmetryCADToolContext(this); |
76 |
} |
77 |
|
78 |
/*
|
79 |
* (non-Javadoc)
|
80 |
*
|
81 |
* @see
|
82 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
83 |
* .layers.FBitSet,
|
84 |
* double, double)
|
85 |
*/
|
86 |
public void transition(double x, double y, InputEvent event) { |
87 |
_fsm.addPoint(x, y, event); |
88 |
} |
89 |
|
90 |
/*
|
91 |
* (non-Javadoc)
|
92 |
*
|
93 |
* @see
|
94 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
95 |
* .layers.FBitSet,
|
96 |
* double)
|
97 |
*/
|
98 |
public void transition(double d) { |
99 |
_fsm.addValue(d); |
100 |
} |
101 |
|
102 |
/*
|
103 |
* (non-Javadoc)
|
104 |
*
|
105 |
* @see
|
106 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
107 |
* .layers.FBitSet,
|
108 |
* java.lang.String)
|
109 |
*/
|
110 |
public void transition(String s) throws CommandException { |
111 |
// if (!super.changeCommand(s)) {
|
112 |
_fsm.addOption(s); |
113 |
// }
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* DOCUMENT ME!
|
118 |
*/
|
119 |
public void selection() { |
120 |
FeatureSet selection = null;
|
121 |
try {
|
122 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
123 |
|
124 |
if (selection.getSize() == 0 |
125 |
&& !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
|
126 |
CADExtension.setCADTool("_selection", false); |
127 |
((SelectionCADTool) CADExtension.getCADTool()) |
128 |
.setNextTool("_symmetry");
|
129 |
} |
130 |
} catch (ReadException e) {
|
131 |
// TODO Auto-generated catch block
|
132 |
e.printStackTrace(); |
133 |
} catch (DataException e) {
|
134 |
// TODO Auto-generated catch block
|
135 |
e.printStackTrace(); |
136 |
} |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
141 |
* editableFeatureSource que ya estar? creado.
|
142 |
*
|
143 |
* @param x
|
144 |
* par?metro x del punto que se pase en esta transici?n.
|
145 |
* @param y
|
146 |
* par?metro y del punto que se pase en esta transici?n.
|
147 |
*/
|
148 |
public void addPoint(double x, double y, InputEvent event) { |
149 |
SymmetryCADToolState actualState = |
150 |
(SymmetryCADToolState) _fsm.getPreviousState(); |
151 |
String status = actualState.getName();
|
152 |
if (status.equals("Symmetry.FirstPoint")) { |
153 |
firstPoint = new Point2D.Double(x, y); |
154 |
} else
|
155 |
if (status.equals("Symmetry.SecondPoint")) { |
156 |
secondPoint = new Point2D.Double(x, y); |
157 |
} |
158 |
} |
159 |
|
160 |
/**
|
161 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
162 |
* encontremos.
|
163 |
*
|
164 |
* @param g
|
165 |
* Graphics sobre el que dibujar.
|
166 |
* @param x
|
167 |
* par?metro x del punto que se pase para dibujar.
|
168 |
* @param y
|
169 |
* par?metro x del punto que se pase para dibujar.
|
170 |
*/
|
171 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
172 |
SymmetryCADToolState actualState = _fsm.getState(); |
173 |
String status = actualState.getName();
|
174 |
|
175 |
if (status.equals("Symmetry.SecondPoint")) { |
176 |
Point2D pAux = new Point2D.Double(x, y); |
177 |
drawSymmetry(renderer, pAux); |
178 |
} else
|
179 |
if (status.equals("Symmetry.CutOrCopy")) { |
180 |
drawSymmetry(renderer, secondPoint); |
181 |
} |
182 |
} |
183 |
|
184 |
private void drawSymmetry(MapControlDrawer renderer, Point2D pAux) { |
185 |
|
186 |
VectorialLayerEdited vle = getVLE(); |
187 |
FeatureSet selection = null;
|
188 |
try {
|
189 |
selection = (FeatureSet) vle.getFeatureStore().getSelection(); |
190 |
} catch (ReadException e1) {
|
191 |
// TODO Auto-generated catch block
|
192 |
e1.printStackTrace(); |
193 |
} catch (DataException e) {
|
194 |
// TODO Auto-generated catch block
|
195 |
e.printStackTrace(); |
196 |
} |
197 |
ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
198 |
|
199 |
GeneralPathX gpx = new GeneralPathX();
|
200 |
gpx.moveTo(firstPoint.getX(), firstPoint.getY()); |
201 |
gpx.lineTo(pAux.getX(), pAux.getY()); |
202 |
|
203 |
renderer.draw(createCurve(gpx), |
204 |
mapControlManager.getAxisReferenceSymbol()); |
205 |
|
206 |
DisposableIterator iterator = null;
|
207 |
try {
|
208 |
iterator = selection.iterator(); |
209 |
while (iterator.hasNext()) {
|
210 |
Feature feature = (Feature) iterator.next(); |
211 |
|
212 |
// }
|
213 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
214 |
// DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
|
215 |
// DefaultFeature fea = (DefaultFeature) row.getLinkedRow();
|
216 |
|
217 |
Geometry geom = (feature.getDefaultGeometry()).cloneGeometry(); |
218 |
Handler[] handlers = geom.getHandlers(Geometry.SELECTHANDLER); |
219 |
|
220 |
for (int j = 0; j < handlers.length; j++) { |
221 |
Handler h = handlers[j];
|
222 |
Point2D p = h.getPoint();
|
223 |
Point2D[] ps = |
224 |
UtilFunctions.getPerpendicular(firstPoint, pAux, p); |
225 |
Point2D inter =
|
226 |
UtilFunctions.getIntersection(ps[0], ps[1], firstPoint, |
227 |
pAux); |
228 |
if (inter != null) { |
229 |
Point2D dif =
|
230 |
new Point2D.Double(inter.getX() - p.getX(), |
231 |
inter.getY() - p.getY()); |
232 |
h.set(inter.getX() + dif.getX(), |
233 |
inter.getY() + dif.getY()); |
234 |
} |
235 |
} |
236 |
renderer.draw(geom, mapControlManager.getAxisReferenceSymbol()); |
237 |
} |
238 |
} catch (DataException e1) {
|
239 |
// TODO Auto-generated catch block
|
240 |
e1.printStackTrace(); |
241 |
} finally {
|
242 |
if (iterator != null) { |
243 |
iterator.dispose(); |
244 |
} |
245 |
} |
246 |
} |
247 |
|
248 |
/**
|
249 |
* Add a diferent option.
|
250 |
*
|
251 |
* @param s
|
252 |
* Diferent option.
|
253 |
*/
|
254 |
public void addOption(String s) { |
255 |
|
256 |
if (s.equals(Messages.getText("cancel"))) { |
257 |
refresh(); |
258 |
/*
|
259 |
* Nothing to do
|
260 |
*/
|
261 |
return;
|
262 |
} |
263 |
|
264 |
SymmetryCADToolState actualState = |
265 |
(SymmetryCADToolState) _fsm.getPreviousState(); |
266 |
String status = actualState.getName();
|
267 |
VectorialLayerEdited vle = getVLE(); |
268 |
FeatureStore featureStore = null;
|
269 |
FeatureSelection selection = null;
|
270 |
try {
|
271 |
featureStore = vle.getFeatureStore(); |
272 |
selection = featureStore.getFeatureSelection(); |
273 |
} catch (DataException e1) {
|
274 |
NotificationManager.addError(e1.getMessage(), e1); |
275 |
return;
|
276 |
} |
277 |
// ArrayList selectedRowAux=new ArrayList();
|
278 |
// VectorialEditableAdapter vea = vle.getVEA();
|
279 |
if (status.equals("Symmetry.CutOrCopy")) { |
280 |
PluginServices.getMDIManager().setWaitCursor(); |
281 |
DisposableIterator iterator = null;
|
282 |
try {
|
283 |
featureStore.beginEditingGroup(getName()); |
284 |
// If not moved, deselect the previous features to select the new ones
|
285 |
try {
|
286 |
FeatureSelection newSelection = |
287 |
featureStore.createFeatureSelection(); |
288 |
iterator = selection.iterator(); |
289 |
while (iterator.hasNext()) {
|
290 |
Feature feature = (Feature) iterator.next(); |
291 |
|
292 |
Geometry geom = |
293 |
feature.getDefaultGeometry().cloneGeometry(); |
294 |
Handler[] handlers = |
295 |
geom.getHandlers(Geometry.SELECTHANDLER); |
296 |
|
297 |
for (int j = 0; j < handlers.length; j++) { |
298 |
Handler h = handlers[j];
|
299 |
Point2D p = h.getPoint();
|
300 |
Point2D[] ps = getPerpendicularLine( |
301 |
firstPoint, secondPoint, p); |
302 |
Point2D inter =
|
303 |
UtilFunctions.getIntersection(ps[0], ps[1], |
304 |
firstPoint, secondPoint); |
305 |
Point2D dif =
|
306 |
new Point2D.Double(inter.getX() - p.getX(), |
307 |
inter.getY() - p.getY()); |
308 |
h.set(inter.getX() + dif.getX(), |
309 |
inter.getY() + dif.getY()); |
310 |
} |
311 |
|
312 |
// geom.invokeOperation(index, ctx)
|
313 |
|
314 |
String yes_low_case =
|
315 |
Messages.getText("SymmetryCADTool.yes").toLowerCase();
|
316 |
String yes_up_case = yes_low_case.toUpperCase();
|
317 |
|
318 |
if (s.equals(PluginServices.getText(this, "cut")) |
319 |
|| s.equals(yes_low_case) || s.equals(yes_up_case)) { |
320 |
super.updateGeometry(featureStore, feature, geom);
|
321 |
// EditableFeature eFeature = feature.getEditable();
|
322 |
// eFeature.setGeometry(featureStore
|
323 |
// .getDefaultFeatureType()
|
324 |
// .getDefaultGeometryAttributeName(), geom);
|
325 |
// featureStore.update(eFeature);
|
326 |
newSelection.select(feature); |
327 |
} else {
|
328 |
Feature newFeature = insertGeometry(geom); |
329 |
newSelection.select(newFeature); |
330 |
} |
331 |
refresh(); |
332 |
|
333 |
} |
334 |
featureStore.setSelection(newSelection); |
335 |
} finally {
|
336 |
featureStore.endEditingGroup(); |
337 |
} |
338 |
} catch (DataException e) {
|
339 |
NotificationManager.addError(e.getMessage(), e); |
340 |
} finally {
|
341 |
if (iterator != null) { |
342 |
iterator.dispose(); |
343 |
} |
344 |
PluginServices.getMDIManager().restoreCursor(); |
345 |
} |
346 |
|
347 |
} |
348 |
} |
349 |
|
350 |
public void addValue(double d) { |
351 |
|
352 |
} |
353 |
|
354 |
public String getName() { |
355 |
return PluginServices.getText(this, "symmetry_"); |
356 |
} |
357 |
|
358 |
public String toString() { |
359 |
return "_symmetry"; |
360 |
} |
361 |
|
362 |
@Override
|
363 |
public boolean isApplicable(GeometryType geometryType) { |
364 |
return true; |
365 |
} |
366 |
|
367 |
@Override
|
368 |
protected int[] getSupportedGeometryTypes() { |
369 |
return null; |
370 |
} |
371 |
|
372 |
private Point2D[] getPerpendicularLine( |
373 |
Point2D p1, Point2D p2, Point2D pp) throws CreateGeometryException { |
374 |
|
375 |
try {
|
376 |
Point g_p1 = geomManager.createPoint(
|
377 |
p1.getX(), p1.getY(), Geometry.SUBTYPES.GEOM2D); |
378 |
Point g_p2 = geomManager.createPoint(
|
379 |
p2.getX(), p2.getY(), Geometry.SUBTYPES.GEOM2D); |
380 |
Point g_pp = geomManager.createPoint(
|
381 |
pp.getX(), pp.getY(), Geometry.SUBTYPES.GEOM2D); |
382 |
|
383 |
PerpendicularOperationContext poc = new PerpendicularOperationContext(
|
384 |
g_p2, g_pp); |
385 |
|
386 |
Point[] res = (Point[]) g_p1.invokeOperation( |
387 |
Perpendicular.CODE, poc); |
388 |
Point2D[] resp = new Point2D[2]; |
389 |
resp[0] = new Point2D.Double(res[0].getX(), res[0].getY()); |
390 |
resp[1] = new Point2D.Double(res[1].getX(), res[1].getY()); |
391 |
return resp;
|
392 |
|
393 |
} catch (Exception ex) { |
394 |
throw new CreateGeometryException( |
395 |
Geometry.TYPES.POINT, |
396 |
Geometry.SUBTYPES.GEOM2D, |
397 |
ex); |
398 |
} |
399 |
} |
400 |
|
401 |
|
402 |
} |