root / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCADTool.java @ 24363
History | View | Annotate | Download (13.8 KB)
1 | 3883 | caballero | /* 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 | 4637 | caballero | import java.awt.Image; |
46 | 4313 | fjp | import java.awt.event.InputEvent; |
47 | 4637 | caballero | import java.awt.geom.AffineTransform; |
48 | 3883 | caballero | import java.awt.geom.Point2D; |
49 | import java.awt.geom.Point2D.Double; |
||
50 | 4637 | caballero | import java.awt.image.ImagingOpException; |
51 | 4522 | caballero | import java.util.ArrayList; |
52 | 23424 | vcaballero | import java.util.Iterator; |
53 | 3883 | caballero | |
54 | 23754 | jjdelcerro | import org.gvsig.fmap.data.exceptions.DataException; |
55 | import org.gvsig.fmap.data.exceptions.ReadException; |
||
56 | 23424 | vcaballero | import org.gvsig.fmap.data.feature.Feature; |
57 | 24263 | vcaballero | import org.gvsig.fmap.data.feature.FeatureSelection; |
58 | 23842 | jjdelcerro | import org.gvsig.fmap.data.feature.FeatureSet; |
59 | 23424 | vcaballero | import org.gvsig.fmap.data.feature.FeatureStore; |
60 | 21668 | vcaballero | import org.gvsig.fmap.geom.Geometry; |
61 | 23424 | vcaballero | import org.gvsig.fmap.geom.operation.Draw; |
62 | import org.gvsig.fmap.geom.operation.DrawOperationContext; |
||
63 | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
||
64 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
65 | 21668 | vcaballero | import org.gvsig.fmap.geom.util.UtilFunctions; |
66 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
67 | |||
68 | 3883 | caballero | import com.iver.andami.PluginServices; |
69 | 12739 | caballero | import com.iver.andami.messages.NotificationManager; |
70 | 3883 | caballero | import com.iver.cit.gvsig.CADExtension; |
71 | import com.iver.cit.gvsig.gui.cad.CADTool; |
||
72 | import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
||
73 | 5735 | caballero | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
74 | 3883 | caballero | import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext; |
75 | import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext.ScaleCADToolState; |
||
76 | 4637 | caballero | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
77 | 3883 | caballero | |
78 | |||
79 | /**
|
||
80 | * DOCUMENT ME!
|
||
81 | *
|
||
82 | * @author Vicente Caballero Navarro
|
||
83 | */
|
||
84 | public class ScaleCADTool extends DefaultCADTool { |
||
85 | private ScaleCADToolContext _fsm;
|
||
86 | private Point2D firstPoint; |
||
87 | private Point2D lastPoint; |
||
88 | private Point2D scalePoint; |
||
89 | private Double orr; |
||
90 | private Double frr; |
||
91 | private Double ore; |
||
92 | private Double fre; |
||
93 | |||
94 | /**
|
||
95 | * Crea un nuevo PolylineCADTool.
|
||
96 | */
|
||
97 | public ScaleCADTool() {
|
||
98 | } |
||
99 | |||
100 | /**
|
||
101 | * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
||
102 | * carga previa a la utilizaci?n de la herramienta.
|
||
103 | */
|
||
104 | public void init() { |
||
105 | _fsm = new ScaleCADToolContext(this); |
||
106 | } |
||
107 | |||
108 | /* (non-Javadoc)
|
||
109 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
||
110 | */
|
||
111 | 4313 | fjp | public void transition(double x, double y, InputEvent event) { |
112 | 4324 | caballero | _fsm.addPoint(x, y, event); |
113 | 3883 | caballero | } |
114 | |||
115 | /* (non-Javadoc)
|
||
116 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
||
117 | */
|
||
118 | public void transition(double d) { |
||
119 | _fsm.addValue(d); |
||
120 | } |
||
121 | |||
122 | /* (non-Javadoc)
|
||
123 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
||
124 | */
|
||
125 | 5735 | caballero | public void transition(String s) throws CommandException { |
126 | 4892 | caballero | if (!super.changeCommand(s)){ |
127 | _fsm.addOption(s); |
||
128 | } |
||
129 | 3883 | caballero | } |
130 | |||
131 | /**
|
||
132 | * DOCUMENT ME!
|
||
133 | */
|
||
134 | public void selection() { |
||
135 | 23842 | jjdelcerro | FeatureSet selection=null;
|
136 | 22986 | vcaballero | try {
|
137 | 23842 | jjdelcerro | selection = (FeatureSet)getVLE().getFeatureStore().getSelection(); |
138 | 22986 | vcaballero | } catch (ReadException e) {
|
139 | // TODO Auto-generated catch block
|
||
140 | e.printStackTrace(); |
||
141 | } |
||
142 | if (selection.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
||
143 | 5071 | caballero | CADExtension.setCADTool("_selection",false); |
144 | 4584 | caballero | ((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
145 | 5071 | caballero | "_scale");
|
146 | 3883 | caballero | } |
147 | } |
||
148 | |||
149 | /**
|
||
150 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
151 | * editableFeatureSource que ya estar? creado.
|
||
152 | *
|
||
153 | * @param x par?metro x del punto que se pase en esta transici?n.
|
||
154 | * @param y par?metro y del punto que se pase en esta transici?n.
|
||
155 | */
|
||
156 | 4365 | caballero | public void addPoint(double x, double y,InputEvent event) { |
157 | 3883 | caballero | ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState(); |
158 | String status = actualState.getName();
|
||
159 | |||
160 | 3978 | caballero | if (status.equals("Scale.PointMain")) { |
161 | 3883 | caballero | firstPoint = new Point2D.Double(x, y); |
162 | scalePoint = firstPoint; |
||
163 | 3978 | caballero | } else if (status.equals("Scale.ScaleFactorOrReference")) { |
164 | 3883 | caballero | PluginServices.getMDIManager().setWaitCursor(); |
165 | lastPoint = new Point2D.Double(x, y); |
||
166 | |||
167 | 4522 | caballero | //double w;
|
168 | //double h;
|
||
169 | //w = lastPoint.getX() - firstPoint.getX();
|
||
170 | //h = lastPoint.getY() - firstPoint.getY();
|
||
171 | 3883 | caballero | |
172 | try {
|
||
173 | double size=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(getCadToolAdapter().getMapControl().getWidth());
|
||
174 | scale(firstPoint.distance(lastPoint)/(size/40));
|
||
175 | 21668 | vcaballero | } catch (ReadException e) {
|
176 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
177 | 3883 | caballero | } |
178 | 21668 | vcaballero | |
179 | 3883 | caballero | PluginServices.getMDIManager().restoreCursor(); |
180 | 3978 | caballero | } else if (status.equals("Scale.PointOriginOrScaleFactor")) { |
181 | 3883 | caballero | orr = new Point2D.Double(x, y); |
182 | 3978 | caballero | } else if (status.equals("Scale.EndPointReference")) { |
183 | 3883 | caballero | frr = new Point2D.Double(x, y); |
184 | 3978 | caballero | } else if (status.equals("Scale.OriginPointScale")) { |
185 | 3883 | caballero | ore = new Point2D.Double(x, y); |
186 | firstPoint = ore; |
||
187 | 3978 | caballero | } else if (status.equals("Scale.EndPointScale")) { |
188 | 3883 | caballero | fre = new Point2D.Double(x, y); |
189 | |||
190 | double distrr = orr.distance(frr);
|
||
191 | double distre = ore.distance(fre);
|
||
192 | double escalado = distre / distrr;
|
||
193 | |||
194 | try {
|
||
195 | scale(escalado); |
||
196 | 21668 | vcaballero | } catch (ReadException e) {
|
197 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
198 | 21668 | vcaballero | } |
199 | 3883 | caballero | } |
200 | |||
201 | } |
||
202 | |||
203 | /**
|
||
204 | * M?todo para dibujar la lo necesario para el estado en el que nos
|
||
205 | * encontremos.
|
||
206 | *
|
||
207 | * @param g Graphics sobre el que dibujar.
|
||
208 | * @param x par?metro x del punto que se pase para dibujar.
|
||
209 | * @param y par?metro x del punto que se pase para dibujar.
|
||
210 | */
|
||
211 | public void drawOperation(Graphics g, double x, double y) { |
||
212 | 9121 | caballero | ScaleCADToolState actualState = _fsm.getState(); |
213 | 4522 | caballero | String status = actualState.getName();
|
214 | 23842 | jjdelcerro | FeatureSet selection=null;
|
215 | 23424 | vcaballero | try {
|
216 | 23842 | jjdelcerro | selection = (FeatureSet)getVLE().getFeatureStore().getSelection(); |
217 | 24263 | vcaballero | |
218 | 23424 | vcaballero | // ArrayList selectedRow = getSelectedRows();
|
219 | 4522 | caballero | Point2D currentPoint = new Point2D.Double(x, y); |
220 | 24263 | vcaballero | long selectionCount=selection.getSize();
|
221 | 4522 | caballero | if (status.equals("Scale.ScaleFactorOrReference")) { |
222 | 4637 | caballero | VectorialLayerEdited vle = getVLE(); |
223 | 6881 | cesar | ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
224 | 4637 | caballero | Point2D point = vp.fromMapPoint(firstPoint.getX(), firstPoint
|
225 | .getY()); |
||
226 | double size = getCadToolAdapter().getMapControl().getViewPort()
|
||
227 | .toMapDistance( |
||
228 | getCadToolAdapter().getMapControl().getWidth()); |
||
229 | double scale = firstPoint.distance(currentPoint) / (size / 40); |
||
230 | 11437 | caballero | drawLine((Graphics2D) g, firstPoint, currentPoint,DefaultCADTool.axisReferencesSymbol);
|
231 | 3883 | caballero | |
232 | 23424 | vcaballero | if (selectionCount < CADTool.TOPGEOMETRY) {
|
233 | Iterator iterator=selection.iterator();
|
||
234 | while (iterator.hasNext()) {
|
||
235 | Feature feature = (Feature) iterator.next(); |
||
236 | |||
237 | // }
|
||
238 | // for (int i = 0; i < selectionCount; i++) {
|
||
239 | // DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
|
||
240 | // .get(i)).getLinkedRow();
|
||
241 | Geometry geometry = ((Geometry)feature.getDefaultGeometry()).cloneGeometry(); |
||
242 | |||
243 | 4637 | caballero | UtilFunctions.scaleGeom(geometry, firstPoint, scale, scale); |
244 | |||
245 | 23424 | vcaballero | DrawOperationContext doc=new DrawOperationContext();
|
246 | doc.setGraphics((Graphics2D)g);
|
||
247 | doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
||
248 | doc.setSymbol(DefaultCADTool.axisReferencesSymbol); |
||
249 | try {
|
||
250 | geometry.invokeOperation(Draw.CODE,doc); |
||
251 | } catch (GeometryOperationNotSupportedException e) {
|
||
252 | e.printStackTrace(); |
||
253 | } catch (GeometryOperationException e) {
|
||
254 | e.printStackTrace(); |
||
255 | } |
||
256 | |||
257 | // geometry.draw((Graphics2D) g, getCadToolAdapter()
|
||
258 | // .getMapControl().getViewPort(),
|
||
259 | // DefaultCADTool.axisReferencesSymbol);
|
||
260 | |||
261 | 4637 | caballero | } |
262 | |||
263 | } else {
|
||
264 | AffineTransform at = new AffineTransform(); |
||
265 | at.setToTranslation(point.getX(), point.getY()); |
||
266 | at.scale(scale, scale); |
||
267 | at.translate(-point.getX(), -point.getY()); |
||
268 | Image imgSel = vle.getSelectionImage();
|
||
269 | try {
|
||
270 | ((Graphics2D) g).drawImage(imgSel, at, null); |
||
271 | } catch (ImagingOpException e) { |
||
272 | } |
||
273 | 3883 | caballero | } |
274 | 4637 | caballero | PluginServices.getMainFrame().getStatusBar().setMessage("5",
|
275 | "Factor = " + scale);
|
||
276 | 3978 | caballero | } else if (status.equals("Scale.EndPointScale")) { |
277 | 4637 | caballero | VectorialLayerEdited vle = getVLE(); |
278 | 6881 | cesar | ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
279 | 4637 | caballero | Point2D point = vp.fromMapPoint(scalePoint.getX(), scalePoint
|
280 | .getY()); |
||
281 | 3883 | caballero | |
282 | 4637 | caballero | double distrr = orr.distance(frr);
|
283 | double distre = ore.distance(currentPoint);
|
||
284 | double escalado = distre / distrr;
|
||
285 | 23424 | vcaballero | if (selectionCount < CADTool.TOPGEOMETRY) {
|
286 | Iterator iterator=selection.iterator();
|
||
287 | while (iterator.hasNext()) {
|
||
288 | Feature feature = (Feature) iterator.next(); |
||
289 | // for (int i = 0; i < selectedRow.size(); i++) {
|
||
290 | // DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
|
||
291 | // .get(i)).getLinkedRow();
|
||
292 | Geometry geometry = ((Geometry)feature.getDefaultGeometry()).cloneGeometry(); |
||
293 | 4637 | caballero | UtilFunctions.scaleGeom(geometry, scalePoint, escalado, |
294 | escalado); |
||
295 | 3883 | caballero | |
296 | 23424 | vcaballero | DrawOperationContext doc=new DrawOperationContext();
|
297 | doc.setGraphics((Graphics2D)g);
|
||
298 | doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
||
299 | doc.setSymbol(DefaultCADTool.axisReferencesSymbol); |
||
300 | try {
|
||
301 | geometry.invokeOperation(Draw.CODE,doc); |
||
302 | } catch (GeometryOperationNotSupportedException e) {
|
||
303 | e.printStackTrace(); |
||
304 | } catch (GeometryOperationException e) {
|
||
305 | e.printStackTrace(); |
||
306 | } |
||
307 | |||
308 | // geometry.draw((Graphics2D) g, getCadToolAdapter()
|
||
309 | // .getMapControl().getViewPort(),
|
||
310 | // DefaultCADTool.axisReferencesSymbol);
|
||
311 | |||
312 | 4637 | caballero | } |
313 | } else {
|
||
314 | AffineTransform at = new AffineTransform(); |
||
315 | at.setToTranslation(point.getX(), point.getY()); |
||
316 | at.scale(escalado, escalado); |
||
317 | at.translate(-point.getX(), -point.getY()); |
||
318 | Image imgSel = vle.getSelectionImage();
|
||
319 | try {
|
||
320 | ((Graphics2D) g).drawImage(imgSel, at, null); |
||
321 | } catch (ImagingOpException e) { |
||
322 | } |
||
323 | } |
||
324 | 11437 | caballero | drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.axisReferencesSymbol); |
325 | 3883 | caballero | } |
326 | 24263 | vcaballero | } catch (ReadException e1) {
|
327 | // TODO Auto-generated catch block
|
||
328 | e1.printStackTrace(); |
||
329 | } catch (DataException e) {
|
||
330 | // TODO Auto-generated catch block
|
||
331 | e.printStackTrace(); |
||
332 | } |
||
333 | 4522 | caballero | } |
334 | 3883 | caballero | |
335 | /**
|
||
336 | 4522 | caballero | * Add a diferent option.
|
337 | *
|
||
338 | * @param s
|
||
339 | * Diferent option.
|
||
340 | */
|
||
341 | 3883 | caballero | public void addOption(String s) { |
342 | ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState(); |
||
343 | 13869 | jaume | String status = actualState.getName();
|
344 | if (status.equals("Scale.ScaleFactorOrReference")) { |
||
345 | /* try {
|
||
346 | 3883 | caballero | scale(2);
|
347 | } catch (DriverIOException e) {
|
||
348 | // TODO Auto-generated catch block
|
||
349 | e.printStackTrace();
|
||
350 | } catch (IOException e) {
|
||
351 | // TODO Auto-generated catch block
|
||
352 | e.printStackTrace();
|
||
353 | }
|
||
354 | 13869 | jaume | */
|
355 | } |
||
356 | } |
||
357 | 3883 | caballero | |
358 | /* (non-Javadoc)
|
||
359 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
360 | */
|
||
361 | public void addValue(double d) { |
||
362 | ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState(); |
||
363 | 13869 | jaume | String status = actualState.getName();
|
364 | if (status.equals("Scale.ScaleFactorOrReference")) { |
||
365 | try {
|
||
366 | scale(d); |
||
367 | 21668 | vcaballero | } catch (ReadException e) {
|
368 | 13869 | jaume | NotificationManager.addError(e.getMessage(),e); |
369 | 21668 | vcaballero | } |
370 | 3883 | caballero | } |
371 | } |
||
372 | 23424 | vcaballero | private void scale(double scaleFactor) throws ReadException{ |
373 | 5985 | caballero | VectorialLayerEdited vle=getVLE(); |
374 | 23424 | vcaballero | FeatureStore featureStore=vle.getFeatureStore(); |
375 | |||
376 | featureStore.getCommandsRecord().startComplex(); |
||
377 | 24263 | vcaballero | Iterator iterator =((FeatureSelection)featureStore.getSelection()).iterator();
|
378 | 5985 | caballero | ArrayList selectedRowAux=new ArrayList(); |
379 | 23424 | vcaballero | while (iterator.hasNext()) {
|
380 | Feature feature = (Feature) iterator.next(); |
||
381 | |||
382 | // }
|
||
383 | // for (int i = 0; i < selectedRow.size(); i++) {
|
||
384 | // IRowEdited edRow = (IRowEdited) selectedRow.get(i);
|
||
385 | // DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
|
||
386 | Geometry geometry=((Geometry)feature.getDefaultGeometry()).cloneGeometry(); |
||
387 | UtilFunctions.scaleGeom(geometry, scalePoint, scaleFactor, scaleFactor); |
||
388 | try {
|
||
389 | feature.editing(); |
||
390 | 23754 | jjdelcerro | feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(), geometry); |
391 | 23424 | vcaballero | featureStore.update(feature); |
392 | } catch (DataException e) {
|
||
393 | // TODO Auto-generated catch block
|
||
394 | e.printStackTrace(); |
||
395 | } |
||
396 | // vea.modifyRow(edRow.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
|
||
397 | selectedRowAux.add(feature); |
||
398 | 3883 | caballero | } |
399 | 23424 | vcaballero | featureStore.getCommandsRecord().endComplex(getName()); |
400 | // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
|
||
401 | 5985 | caballero | //clearSelection();
|
402 | //selectedRow.addAll(selectedRowAux);
|
||
403 | } |
||
404 | 4118 | caballero | |
405 | 9121 | caballero | public String getName() { |
406 | 4584 | caballero | return PluginServices.getText(this,"scale_"); |
407 | 4118 | caballero | } |
408 | 4892 | caballero | |
409 | public String toString() { |
||
410 | return "_scale"; |
||
411 | } |
||
412 | 5170 | caballero | |
413 | 3883 | caballero | } |