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