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