svn-gvsig-desktop / tags / v2_0_0_Build_2058 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / SymmetryCADTool.java @ 39229
History | View | Annotate | Download (14.2 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 |
SymmetryCADToolState actualState = |
258 |
(SymmetryCADToolState) _fsm.getPreviousState(); |
259 |
String status = actualState.getName();
|
260 |
VectorialLayerEdited vle = getVLE(); |
261 |
FeatureStore featureStore = null;
|
262 |
FeatureSelection selection = null;
|
263 |
try {
|
264 |
featureStore = vle.getFeatureStore(); |
265 |
selection = featureStore.getFeatureSelection(); |
266 |
} catch (DataException e1) {
|
267 |
NotificationManager.addError(e1.getMessage(), e1); |
268 |
return;
|
269 |
} |
270 |
// ArrayList selectedRowAux=new ArrayList();
|
271 |
// VectorialEditableAdapter vea = vle.getVEA();
|
272 |
if (status.equals("Symmetry.CutOrCopy")) { |
273 |
PluginServices.getMDIManager().setWaitCursor(); |
274 |
DisposableIterator iterator = null;
|
275 |
try {
|
276 |
featureStore.beginEditingGroup(getName()); |
277 |
// If not moved, deselect the previous features to select the new ones
|
278 |
try {
|
279 |
FeatureSelection newSelection = |
280 |
featureStore.createFeatureSelection(); |
281 |
iterator = selection.iterator(); |
282 |
while (iterator.hasNext()) {
|
283 |
Feature feature = (Feature) iterator.next(); |
284 |
|
285 |
Geometry geom = |
286 |
feature.getDefaultGeometry().cloneGeometry(); |
287 |
Handler[] handlers = |
288 |
geom.getHandlers(Geometry.SELECTHANDLER); |
289 |
|
290 |
for (int j = 0; j < handlers.length; j++) { |
291 |
Handler h = handlers[j];
|
292 |
Point2D p = h.getPoint();
|
293 |
Point2D[] ps = getPerpendicularLine( |
294 |
firstPoint, secondPoint, p); |
295 |
Point2D inter =
|
296 |
UtilFunctions.getIntersection(ps[0], ps[1], |
297 |
firstPoint, secondPoint); |
298 |
Point2D dif =
|
299 |
new Point2D.Double(inter.getX() - p.getX(), |
300 |
inter.getY() - p.getY()); |
301 |
h.set(inter.getX() + dif.getX(), |
302 |
inter.getY() + dif.getY()); |
303 |
} |
304 |
|
305 |
// geom.invokeOperation(index, ctx)
|
306 |
|
307 |
String yes_low_case =
|
308 |
Messages.getText("SymmetryCADTool.yes").toLowerCase();
|
309 |
String yes_up_case = yes_low_case.toUpperCase();
|
310 |
|
311 |
if (s.equals(PluginServices.getText(this, "cut")) |
312 |
|| s.equals(yes_low_case) || s.equals(yes_up_case)) { |
313 |
EditableFeature eFeature = feature.getEditable(); |
314 |
eFeature.setGeometry(featureStore |
315 |
.getDefaultFeatureType() |
316 |
.getDefaultGeometryAttributeName(), geom); |
317 |
featureStore.update(eFeature); |
318 |
newSelection.select(feature); |
319 |
} else {
|
320 |
Feature newFeature = insertGeometry(geom); |
321 |
newSelection.select(newFeature); |
322 |
} |
323 |
refresh(); |
324 |
|
325 |
} |
326 |
featureStore.setSelection(newSelection); |
327 |
} finally {
|
328 |
featureStore.endEditingGroup(); |
329 |
} |
330 |
} catch (DataException e) {
|
331 |
NotificationManager.addError(e.getMessage(), e); |
332 |
} finally {
|
333 |
if (iterator != null) { |
334 |
iterator.dispose(); |
335 |
} |
336 |
PluginServices.getMDIManager().restoreCursor(); |
337 |
} |
338 |
|
339 |
} |
340 |
} |
341 |
|
342 |
public void addValue(double d) { |
343 |
|
344 |
} |
345 |
|
346 |
public String getName() { |
347 |
return PluginServices.getText(this, "symmetry_"); |
348 |
} |
349 |
|
350 |
public String toString() { |
351 |
return "_symmetry"; |
352 |
} |
353 |
|
354 |
@Override
|
355 |
public boolean isApplicable(GeometryType geometryType) { |
356 |
return true; |
357 |
} |
358 |
|
359 |
@Override
|
360 |
protected int[] getSupportedGeometryTypes() { |
361 |
return null; |
362 |
} |
363 |
|
364 |
private Point2D[] getPerpendicularLine( |
365 |
Point2D p1, Point2D p2, Point2D pp) throws CreateGeometryException { |
366 |
|
367 |
try {
|
368 |
Point g_p1 = geomManager.createPoint(
|
369 |
p1.getX(), p1.getY(), Geometry.SUBTYPES.GEOM2D); |
370 |
Point g_p2 = geomManager.createPoint(
|
371 |
p2.getX(), p2.getY(), Geometry.SUBTYPES.GEOM2D); |
372 |
Point g_pp = geomManager.createPoint(
|
373 |
pp.getX(), pp.getY(), Geometry.SUBTYPES.GEOM2D); |
374 |
|
375 |
PerpendicularOperationContext poc = new PerpendicularOperationContext(
|
376 |
g_p2, g_pp); |
377 |
|
378 |
Point[] res = (Point[]) g_p1.invokeOperation( |
379 |
Perpendicular.CODE, poc); |
380 |
Point2D[] resp = new Point2D[2]; |
381 |
resp[0] = new Point2D.Double(res[0].getX(), res[0].getY()); |
382 |
resp[1] = new Point2D.Double(res[1].getX(), res[1].getY()); |
383 |
return resp;
|
384 |
|
385 |
} catch (Exception ex) { |
386 |
throw new CreateGeometryException( |
387 |
Geometry.TYPES.POINT, |
388 |
Geometry.SUBTYPES.GEOM2D, |
389 |
ex); |
390 |
} |
391 |
} |
392 |
|
393 |
|
394 |
} |