svn-gvsig-desktop / tags / v2_0_0_Build_2055 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / SymmetryCADTool.java @ 38948
History | View | Annotate | Download (12.5 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.tools.dispose.DisposableIterator; |
50 |
|
51 |
/**
|
52 |
* Herramienta para crear una geometr?a sim?trica a otra, con la posibilidad de
|
53 |
* borrar la original.
|
54 |
*
|
55 |
* @author Vicente Caballero Navarro
|
56 |
*/
|
57 |
public class SymmetryCADTool extends DefaultCADTool { |
58 |
|
59 |
protected SymmetryCADToolContext _fsm;
|
60 |
protected Point2D firstPoint; |
61 |
protected Point2D secondPoint; |
62 |
|
63 |
/**
|
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 |
|
71 |
/*
|
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 |
|
83 |
/*
|
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 |
|
95 |
/*
|
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 |
|
109 |
/**
|
110 |
* DOCUMENT ME!
|
111 |
*/
|
112 |
public void selection() { |
113 |
FeatureSet selection = null;
|
114 |
try {
|
115 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
116 |
|
117 |
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 |
|
136 |
/**
|
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 |
|
157 |
/**
|
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 |
|
172 |
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 |
|
181 |
private void drawSymmetry(MapControlDrawer renderer, Point2D pAux) { |
182 |
|
183 |
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 |
|
196 |
GeneralPathX gpx = new GeneralPathX();
|
197 |
gpx.moveTo(firstPoint.getX(), firstPoint.getY()); |
198 |
gpx.lineTo(pAux.getX(), pAux.getY()); |
199 |
|
200 |
renderer.draw(createCurve(gpx), |
201 |
mapControlManager.getAxisReferenceSymbol()); |
202 |
|
203 |
DisposableIterator iterator = null;
|
204 |
try {
|
205 |
iterator = selection.iterator(); |
206 |
while (iterator.hasNext()) {
|
207 |
Feature feature = (Feature) iterator.next(); |
208 |
|
209 |
// }
|
210 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
211 |
// DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
|
212 |
// DefaultFeature fea = (DefaultFeature) row.getLinkedRow();
|
213 |
|
214 |
Geometry geom = (feature.getDefaultGeometry()).cloneGeometry(); |
215 |
Handler[] handlers = geom.getHandlers(Geometry.SELECTHANDLER); |
216 |
|
217 |
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 |
|
245 |
/**
|
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 |
FeatureSelection selection = null;
|
258 |
try {
|
259 |
featureStore = vle.getFeatureStore(); |
260 |
selection = featureStore.getFeatureSelection(); |
261 |
} 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 |
// If not moved, deselect the previous features to select the new ones
|
273 |
try {
|
274 |
FeatureSelection newSelection = |
275 |
featureStore.createFeatureSelection(); |
276 |
iterator = selection.iterator(); |
277 |
while (iterator.hasNext()) {
|
278 |
Feature feature = (Feature) iterator.next(); |
279 |
|
280 |
Geometry geom = |
281 |
feature.getDefaultGeometry().cloneGeometry(); |
282 |
Handler[] handlers = |
283 |
geom.getHandlers(Geometry.SELECTHANDLER); |
284 |
|
285 |
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 |
|
301 |
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 |
newSelection.select(feature); |
309 |
} else {
|
310 |
Feature newFeature = insertGeometry(geom); |
311 |
newSelection.select(newFeature); |
312 |
} |
313 |
refresh(); |
314 |
|
315 |
} |
316 |
featureStore.setSelection(newSelection); |
317 |
} 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 |
PluginServices.getMDIManager().restoreCursor(); |
327 |
} |
328 |
|
329 |
} |
330 |
} |
331 |
|
332 |
public void addValue(double d) { |
333 |
|
334 |
} |
335 |
|
336 |
public String getName() { |
337 |
return PluginServices.getText(this, "symmetry_"); |
338 |
} |
339 |
|
340 |
public String toString() { |
341 |
return "_symmetry"; |
342 |
} |
343 |
|
344 |
@Override
|
345 |
public boolean isApplicable(GeometryType geometryType) { |
346 |
return true; |
347 |
} |
348 |
|
349 |
@Override
|
350 |
protected int[] getSupportedGeometryTypes() { |
351 |
return null; |
352 |
} |
353 |
|
354 |
} |