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