root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / SymmetryCADTool.java @ 29616
History | View | Annotate | Download (12.6 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.editing.gui.cad.tools; |
42 |
|
43 |
import java.awt.Graphics; |
44 |
import java.awt.Graphics2D; |
45 |
import java.awt.Image; |
46 |
import java.awt.event.InputEvent; |
47 |
import java.awt.geom.Point2D; |
48 |
|
49 |
import org.gvsig.andami.PluginServices; |
50 |
import org.gvsig.andami.messages.NotificationManager; |
51 |
import org.gvsig.editing.CADExtension; |
52 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
53 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
54 |
import org.gvsig.editing.gui.cad.tools.smc.SymmetryCADToolContext; |
55 |
import org.gvsig.editing.gui.cad.tools.smc.SymmetryCADToolContext.SymmetryCADToolState; |
56 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
57 |
import org.gvsig.fmap.dal.exception.DataException; |
58 |
import org.gvsig.fmap.dal.exception.ReadException; |
59 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
60 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
61 |
import org.gvsig.fmap.dal.feature.Feature; |
62 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
63 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
64 |
import org.gvsig.fmap.geom.Geometry; |
65 |
import org.gvsig.fmap.geom.handler.Handler; |
66 |
import org.gvsig.fmap.geom.operation.Draw; |
67 |
import org.gvsig.fmap.geom.operation.DrawOperationContext; |
68 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
69 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
70 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
71 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
72 |
import org.gvsig.fmap.mapcontext.ViewPort; |
73 |
|
74 |
|
75 |
/**
|
76 |
* Herramienta para crear una geometr?a sim?trica a otra, con la posibilidad de
|
77 |
* borrar la original.
|
78 |
*
|
79 |
* @author Vicente Caballero Navarro
|
80 |
*/
|
81 |
public class SymmetryCADTool extends DefaultCADTool { |
82 |
protected SymmetryCADToolContext _fsm;
|
83 |
protected Point2D firstPoint; |
84 |
protected Point2D secondPoint; |
85 |
|
86 |
/**
|
87 |
* Crea un nuevo SymmetryCADTool.
|
88 |
*/
|
89 |
public SymmetryCADTool() {
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
94 |
* carga previa a la utilizaci?n de la herramienta.
|
95 |
*/
|
96 |
public void init() { |
97 |
_fsm = new SymmetryCADToolContext(this); |
98 |
} |
99 |
|
100 |
/*
|
101 |
* (non-Javadoc)
|
102 |
*
|
103 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
104 |
* double, double)
|
105 |
*/
|
106 |
public void transition(double x, double y, InputEvent event) { |
107 |
_fsm.addPoint(x, y, event); |
108 |
} |
109 |
|
110 |
/*
|
111 |
* (non-Javadoc)
|
112 |
*
|
113 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
114 |
* double)
|
115 |
*/
|
116 |
public void transition(double d) { |
117 |
_fsm.addValue(d); |
118 |
} |
119 |
|
120 |
/*
|
121 |
* (non-Javadoc)
|
122 |
*
|
123 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
124 |
* java.lang.String)
|
125 |
*/
|
126 |
public void transition(String s) throws CommandException { |
127 |
if (!super.changeCommand(s)) { |
128 |
_fsm.addOption(s); |
129 |
} |
130 |
} |
131 |
|
132 |
/**
|
133 |
* DOCUMENT ME!
|
134 |
*/
|
135 |
public void selection() { |
136 |
FeatureSet selection = null;
|
137 |
try {
|
138 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
139 |
|
140 |
if (selection.getSize() == 0 |
141 |
&& !CADExtension |
142 |
.getCADTool() |
143 |
.getClass() |
144 |
.getName() |
145 |
.equals( |
146 |
"com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
147 |
CADExtension.setCADTool("_selection", false); |
148 |
((SelectionCADTool) CADExtension.getCADTool()) |
149 |
.setNextTool("_symmetry");
|
150 |
} |
151 |
} catch (ReadException e) {
|
152 |
// TODO Auto-generated catch block
|
153 |
e.printStackTrace(); |
154 |
} catch (DataException e) {
|
155 |
// TODO Auto-generated catch block
|
156 |
e.printStackTrace(); |
157 |
} |
158 |
} |
159 |
|
160 |
/**
|
161 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
162 |
* editableFeatureSource que ya estar? creado.
|
163 |
*
|
164 |
* @param x
|
165 |
* par?metro x del punto que se pase en esta transici?n.
|
166 |
* @param y
|
167 |
* par?metro y del punto que se pase en esta transici?n.
|
168 |
*/
|
169 |
public void addPoint(double x, double y, InputEvent event) { |
170 |
SymmetryCADToolState actualState = (SymmetryCADToolState) _fsm |
171 |
.getPreviousState(); |
172 |
String status = actualState.getName();
|
173 |
if (status.equals("Symmetry.FirstPoint")) { |
174 |
firstPoint = new Point2D.Double(x, y); |
175 |
} else if (status.equals("Symmetry.SecondPoint")) { |
176 |
secondPoint = new Point2D.Double(x, y); |
177 |
} |
178 |
} |
179 |
|
180 |
/**
|
181 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
182 |
* encontremos.
|
183 |
*
|
184 |
* @param g
|
185 |
* Graphics sobre el que dibujar.
|
186 |
* @param x
|
187 |
* par?metro x del punto que se pase para dibujar.
|
188 |
* @param y
|
189 |
* par?metro x del punto que se pase para dibujar.
|
190 |
*/
|
191 |
public void drawOperation(Graphics g, double x, double y) { |
192 |
SymmetryCADToolState actualState = _fsm.getState(); |
193 |
String status = actualState.getName();
|
194 |
|
195 |
if (status.equals("Symmetry.SecondPoint")) { |
196 |
Point2D pAux = new Point2D.Double(x, y); |
197 |
drawSymmetry(g, pAux); |
198 |
} else if (status.equals("Symmetry.CutOrCopy")) { |
199 |
drawSymmetry(g, secondPoint); |
200 |
}else{
|
201 |
VectorialLayerEdited vle=getVLE(); |
202 |
if (!vle.getLayer().isVisible()) {
|
203 |
return;
|
204 |
} |
205 |
try{
|
206 |
Image imgSel = vle.getSelectionImage();
|
207 |
if (imgSel!=null) { |
208 |
g.drawImage(imgSel, 0, 0, null); |
209 |
} |
210 |
Image imgHand = vle.getHandlersImage();
|
211 |
if (imgHand!=null) { |
212 |
g.drawImage(imgHand, 0, 0, null); |
213 |
} |
214 |
}catch (Exception e) { |
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
private void drawSymmetry(Graphics g, Point2D pAux) { |
220 |
|
221 |
VectorialLayerEdited vle = getVLE(); |
222 |
FeatureSet selection = null;
|
223 |
try {
|
224 |
selection = (FeatureSet) vle.getFeatureStore().getSelection(); |
225 |
} catch (ReadException e1) {
|
226 |
// TODO Auto-generated catch block
|
227 |
e1.printStackTrace(); |
228 |
} catch (DataException e) {
|
229 |
// TODO Auto-generated catch block
|
230 |
e.printStackTrace(); |
231 |
} |
232 |
ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
233 |
|
234 |
GeneralPathX gpx = new GeneralPathX();
|
235 |
gpx.moveTo(firstPoint.getX(), firstPoint.getY()); |
236 |
gpx.lineTo(pAux.getX(), pAux.getY()); |
237 |
DrawOperationContext doc = new DrawOperationContext();
|
238 |
doc.setGraphics((Graphics2D) g);
|
239 |
doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
240 |
doc.setSymbol(DefaultCADTool.axisReferencesSymbol); |
241 |
try {
|
242 |
createCurve(gpx).invokeOperation(Draw.CODE, doc); |
243 |
} catch (GeometryOperationNotSupportedException e) {
|
244 |
e.printStackTrace(); |
245 |
} catch (GeometryOperationException e) {
|
246 |
e.printStackTrace(); |
247 |
} |
248 |
// geomFactory.createPolyline2D(gpx).draw((Graphics2D)g,vp,DefaultCADTool.axisReferencesSymbol);
|
249 |
DisposableIterator iterator = null;
|
250 |
try {
|
251 |
iterator = selection.iterator(); |
252 |
while (iterator.hasNext()) {
|
253 |
Feature feature = (Feature) iterator.next(); |
254 |
|
255 |
// }
|
256 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
257 |
// DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
|
258 |
// DefaultFeature fea = (DefaultFeature) row.getLinkedRow();
|
259 |
|
260 |
Geometry geom = (feature.getDefaultGeometry()) |
261 |
.cloneGeometry(); |
262 |
Handler[] handlers = geom.getHandlers(Geometry.SELECTHANDLER); |
263 |
|
264 |
for (int j = 0; j < handlers.length; j++) { |
265 |
Handler h = handlers[j];
|
266 |
Point2D p = h.getPoint();
|
267 |
Point2D[] ps = UtilFunctions.getPerpendicular(firstPoint, pAux, |
268 |
p); |
269 |
Point2D inter = UtilFunctions.getIntersection(ps[0], ps[1], |
270 |
firstPoint, pAux); |
271 |
if (inter != null) { |
272 |
Point2D dif = new Point2D.Double(inter.getX() - p.getX(), |
273 |
inter.getY() - p.getY()); |
274 |
h.set(inter.getX() + dif.getX(), inter.getY() + dif.getY()); |
275 |
} |
276 |
} |
277 |
// DrawOperationContext doc=new DrawOperationContext();
|
278 |
doc.setGraphics((Graphics2D) g);
|
279 |
doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
280 |
doc.setSymbol(DefaultCADTool.axisReferencesSymbol); |
281 |
try {
|
282 |
geom.cloneGeometry().invokeOperation(Draw.CODE, doc); |
283 |
} catch (GeometryOperationNotSupportedException e) {
|
284 |
e.printStackTrace(); |
285 |
} catch (GeometryOperationException e) {
|
286 |
e.printStackTrace(); |
287 |
} |
288 |
// geom.draw((Graphics2D)g,vp,DefaultCADTool.selectionSymbol);
|
289 |
} |
290 |
} catch (DataException e1) {
|
291 |
// TODO Auto-generated catch block
|
292 |
e1.printStackTrace(); |
293 |
} finally {
|
294 |
if (iterator != null) { |
295 |
iterator.dispose(); |
296 |
} |
297 |
} |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Add a diferent option.
|
302 |
*
|
303 |
* @param s
|
304 |
* Diferent option.
|
305 |
*/
|
306 |
public void addOption(String s) { |
307 |
SymmetryCADToolState actualState = (SymmetryCADToolState) _fsm |
308 |
.getPreviousState(); |
309 |
String status = actualState.getName();
|
310 |
VectorialLayerEdited vle = getVLE(); |
311 |
FeatureStore featureStore = null;
|
312 |
FeatureSet selection = null;
|
313 |
try {
|
314 |
featureStore = vle.getFeatureStore(); |
315 |
selection = (FeatureSet) featureStore.getSelection(); |
316 |
} catch (DataException e1) {
|
317 |
NotificationManager.addError(e1.getMessage(), e1); |
318 |
return;
|
319 |
} |
320 |
// ArrayList selectedRowAux=new ArrayList();
|
321 |
// VectorialEditableAdapter vea = vle.getVEA();
|
322 |
if (status.equals("Symmetry.CutOrCopy")) { |
323 |
PluginServices.getMDIManager().setWaitCursor(); |
324 |
DisposableIterator iterator = null;
|
325 |
try {
|
326 |
featureStore.beginEditingGroup(getName()); |
327 |
try {
|
328 |
iterator = selection.iterator(); |
329 |
while (iterator.hasNext()) {
|
330 |
Feature feature = (Feature) iterator.next(); |
331 |
|
332 |
// }
|
333 |
// for (int i = 0; i < selectedRow.size(); i++) {
|
334 |
// DefaultRowEdited row = (DefaultRowEdited) selectedRow
|
335 |
// .get(i);
|
336 |
// DefaultFeature fea = (DefaultFeature)
|
337 |
// row.getLinkedRow()
|
338 |
// .cloneRow();
|
339 |
|
340 |
Geometry geom = feature.getDefaultGeometry() |
341 |
.cloneGeometry(); |
342 |
Handler[] handlers = geom |
343 |
.getHandlers(Geometry.SELECTHANDLER); |
344 |
|
345 |
for (int j = 0; j < handlers.length; j++) { |
346 |
Handler h = handlers[j];
|
347 |
Point2D p = h.getPoint();
|
348 |
Point2D[] ps = UtilFunctions.getPerpendicular( |
349 |
firstPoint, secondPoint, p); |
350 |
Point2D inter = UtilFunctions.getIntersection(
|
351 |
ps[0], ps[1], firstPoint, secondPoint); |
352 |
Point2D dif = new Point2D.Double(inter.getX() |
353 |
- p.getX(), inter.getY() - p.getY()); |
354 |
h.set(inter.getX() + dif.getX(), inter.getY() |
355 |
+ dif.getY()); |
356 |
} |
357 |
|
358 |
if (s.equals(PluginServices.getText(this, "cut")) |
359 |
|| s.equals("s") || s.equals("S")) { |
360 |
EditableFeature eFeature = feature.getEditable(); |
361 |
eFeature.setGeometry(featureStore |
362 |
.getDefaultFeatureType() |
363 |
.getDefaultGeometryAttributeName(), geom); |
364 |
featureStore.update(eFeature); |
365 |
// vea.modifyRow(row.getIndex(), fea,
|
366 |
// getName(), EditionEvent.GRAPHIC);
|
367 |
|
368 |
// selectedRowAux.add(new DefaultRowEdited(fea,
|
369 |
// IRowEdited.STATUS_MODIFIED, row.getIndex()));
|
370 |
} else {
|
371 |
// EditableFeature eFeature = featureStore
|
372 |
// .createNewFeature(featureStore
|
373 |
// .getDefaultFeatureType(), feature);
|
374 |
// eFeature.setGeometry(featureStore
|
375 |
// .getDefaultFeatureType()
|
376 |
// .getDefaultGeometryAttributeName(), geom);
|
377 |
// featureStore.insert(eFeature);
|
378 |
insertAndSelectGeometry(geom); |
379 |
// int index=addGeometry(geom,fea.getAttributes());
|
380 |
// selectedRowAux.add(new DefaultRowEdited(fea,
|
381 |
// IRowEdited.STATUS_ADDED, index));
|
382 |
|
383 |
} |
384 |
refresh(); |
385 |
|
386 |
} |
387 |
} finally {
|
388 |
featureStore.endEditingGroup(); |
389 |
|
390 |
} |
391 |
|
392 |
// vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS,
|
393 |
// selectedRowAux);
|
394 |
// clearSelection();
|
395 |
// selectedRow.addAll(selectedRowAux);
|
396 |
} catch (DataException e) {
|
397 |
NotificationManager.addError(e.getMessage(), e); |
398 |
} finally {
|
399 |
if (iterator != null) { |
400 |
iterator.dispose(); |
401 |
} |
402 |
} |
403 |
|
404 |
PluginServices.getMDIManager().restoreCursor(); |
405 |
} |
406 |
} |
407 |
|
408 |
/*
|
409 |
* (non-Javadoc)
|
410 |
*
|
411 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
412 |
*/
|
413 |
public void addValue(double d) { |
414 |
|
415 |
} |
416 |
|
417 |
public String getName() { |
418 |
return PluginServices.getText(this, "symmetry_"); |
419 |
} |
420 |
|
421 |
public String toString() { |
422 |
return "_symmetry"; |
423 |
} |
424 |
|
425 |
} |