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