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