svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / SelectionCADTool.java @ 4434
History | View | Annotate | Download (12 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 com.iver.andami.PluginServices; |
44 |
|
45 |
import com.iver.cit.gvsig.CADExtension; |
46 |
import com.iver.cit.gvsig.fmap.DriverException; |
47 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
48 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
49 |
import com.iver.cit.gvsig.fmap.core.Handler; |
50 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
51 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
52 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
53 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
54 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
55 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
56 |
import com.iver.cit.gvsig.gui.cad.CADTool; |
57 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
58 |
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext; |
59 |
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext.SelectionCADToolState; |
60 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
61 |
|
62 |
import java.awt.Color; |
63 |
import java.awt.Graphics; |
64 |
import java.awt.Graphics2D; |
65 |
import java.awt.event.InputEvent; |
66 |
import java.awt.geom.Point2D; |
67 |
import java.awt.geom.Rectangle2D; |
68 |
|
69 |
import java.util.ArrayList; |
70 |
|
71 |
/**
|
72 |
* DOCUMENT ME!
|
73 |
*
|
74 |
* @author Vicente Caballero Navarro
|
75 |
*/
|
76 |
public class SelectionCADTool extends DefaultCADTool { |
77 |
public final static int tolerance = 4; |
78 |
|
79 |
private SelectionCADToolContext _fsm;
|
80 |
|
81 |
private Point2D firstPoint; |
82 |
|
83 |
private Point2D lastPoint; |
84 |
|
85 |
private String tool = "selection"; |
86 |
|
87 |
private String state; |
88 |
|
89 |
/**
|
90 |
* Crea un nuevo LineCADTool.
|
91 |
*/
|
92 |
public SelectionCADTool() {
|
93 |
} |
94 |
|
95 |
/**
|
96 |
* M?todo de incio, 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 SelectionCADToolContext(this); |
101 |
setNextTool("selection");
|
102 |
} |
103 |
|
104 |
/*
|
105 |
* (non-Javadoc)
|
106 |
*
|
107 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
108 |
* double, double)
|
109 |
*/
|
110 |
public void transition(double x, double y, InputEvent event) { |
111 |
System.out.println("TRANSICION DESDE ESTADO " + getState() + " x= " + x |
112 |
+ " y=" + y);
|
113 |
_fsm.addPoint(x, y, event); |
114 |
} |
115 |
|
116 |
/*
|
117 |
* (non-Javadoc)
|
118 |
*
|
119 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
120 |
* double)
|
121 |
*/
|
122 |
public void transition(double d) { |
123 |
// _fsm.addValue(sel,d);
|
124 |
} |
125 |
|
126 |
/*
|
127 |
* (non-Javadoc)
|
128 |
*
|
129 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
130 |
* java.lang.String)
|
131 |
*/
|
132 |
public void transition(String s) { |
133 |
_fsm.addOption(s); |
134 |
} |
135 |
|
136 |
public String getState() { |
137 |
return state;
|
138 |
} |
139 |
|
140 |
public boolean select(double x, double y) { |
141 |
firstPoint = new Point2D.Double(x, y); |
142 |
|
143 |
FBitSet selection = getCadToolAdapter().getVectorialAdapter() |
144 |
.getSelection(); |
145 |
VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension |
146 |
.getEditionManager().getActiveLayerEdited(); |
147 |
ArrayList selectedHandler = vle.getSelectedHandler();
|
148 |
ArrayList selectedRow = vle.getSelectedRow();
|
149 |
ArrayList selectedRowIndex = vle.getSelectedRowIndex();
|
150 |
|
151 |
// Se comprueba si se pincha en una gemometr?a
|
152 |
PluginServices.getMDIManager().setWaitCursor(); |
153 |
|
154 |
double tam = getCadToolAdapter().getMapControl().getViewPort()
|
155 |
.toMapDistance(tolerance); |
156 |
Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam, |
157 |
firstPoint.getY() - tam, tam * 2, tam * 2); |
158 |
VectorialEditableAdapter vea = getCadToolAdapter() |
159 |
.getVectorialAdapter(); |
160 |
String strEPSG = getCadToolAdapter().getMapControl().getViewPort()
|
161 |
.getProjection().getAbrev().substring(5);
|
162 |
IRowEdited[] feats;
|
163 |
FBitSet prevSelection = (FBitSet) selection.clone(); |
164 |
|
165 |
try {
|
166 |
feats = vea.getFeatures(rect, strEPSG); |
167 |
selection.clear(); |
168 |
|
169 |
for (int i = 0; i < feats.length; i++) { |
170 |
IFeature feat = (IFeature) feats[i].getLinkedRow(); |
171 |
IGeometry geom = feat.getGeometry(); |
172 |
|
173 |
if (geom.intersects(rect)) { // , 0.1)){ |
174 |
selection.set(feats[i].getIndex(), true);
|
175 |
} |
176 |
} |
177 |
} catch (DriverException e1) {
|
178 |
e1.printStackTrace(); |
179 |
} |
180 |
|
181 |
boolean isTheSame = false; |
182 |
|
183 |
if ((prevSelection.cardinality() != 0) |
184 |
&& selection.intersects(prevSelection) |
185 |
&& !selection.equals(prevSelection)) { |
186 |
selection = prevSelection; |
187 |
getCadToolAdapter().getVectorialAdapter().setSelection(selection); |
188 |
isTheSame = true;
|
189 |
} |
190 |
|
191 |
if (selection.cardinality() > 0) { |
192 |
// Se comprueba si se pincha un handler. El m?s cercano (o los m?s
|
193 |
// cercanos si hay empate)
|
194 |
selectedRow.clear(); |
195 |
selectedRowIndex.clear(); |
196 |
selectedHandler.clear(); |
197 |
vle.refreshSelectionCache(firstPoint, getCadToolAdapter()); |
198 |
} |
199 |
|
200 |
PluginServices.getMDIManager().restoreCursor(); |
201 |
|
202 |
if (((selection.cardinality() > 0) && (selectedHandler.size() > 0)) |
203 |
|| isTheSame) { |
204 |
state = "Selection.EndPoint";
|
205 |
} else if (selection.cardinality() > 0) { |
206 |
state = "Selection.FirstPoint";
|
207 |
} else {
|
208 |
state = "Selection.SecondPoint";
|
209 |
} |
210 |
|
211 |
return true; |
212 |
} |
213 |
|
214 |
/**
|
215 |
* Equivale al transition del prototipo pero sin pasarle como par? metro el
|
216 |
* editableFeatureSource que ya estar? creado.
|
217 |
*
|
218 |
* @param selection
|
219 |
* Bitset con las geometr?as que est?n seleccionadas.
|
220 |
* @param x
|
221 |
* par?metro x del punto que se pase en esta transici?n.
|
222 |
* @param y
|
223 |
* par?metro y del punto que se pase en esta transici?n.
|
224 |
*/
|
225 |
public void addPoint(double x, double y, InputEvent event) { |
226 |
SelectionCADToolState actualState = (SelectionCADToolState) _fsm |
227 |
.getPreviousState(); |
228 |
String status = actualState.getName();
|
229 |
FBitSet selection = getCadToolAdapter().getVectorialAdapter() |
230 |
.getSelection(); |
231 |
VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension |
232 |
.getEditionManager().getActiveLayerEdited(); |
233 |
ArrayList selectedHandler = vle.getSelectedHandler();
|
234 |
ArrayList selectedRow = vle.getSelectedRow();
|
235 |
ArrayList selectedRowIndex = vle.getSelectedRowIndex();
|
236 |
|
237 |
try {
|
238 |
if (status.equals("Selection.FirstPoint")) { |
239 |
} else if (status.equals("Selection.SecondPoint")) { |
240 |
lastPoint = new Point2D.Double(x, y); |
241 |
selection.clear(); |
242 |
|
243 |
double x1;
|
244 |
double y1;
|
245 |
double w1;
|
246 |
double h1;
|
247 |
|
248 |
if (firstPoint.getX() < lastPoint.getX()) {
|
249 |
x1 = firstPoint.getX(); |
250 |
w1 = lastPoint.getX() - firstPoint.getX(); |
251 |
} else {
|
252 |
x1 = lastPoint.getX(); |
253 |
w1 = firstPoint.getX() - lastPoint.getX(); |
254 |
} |
255 |
|
256 |
if (firstPoint.getY() < lastPoint.getY()) {
|
257 |
y1 = firstPoint.getY(); |
258 |
h1 = lastPoint.getY() - firstPoint.getY(); |
259 |
} else {
|
260 |
y1 = lastPoint.getY(); |
261 |
h1 = firstPoint.getY() - lastPoint.getY(); |
262 |
} |
263 |
|
264 |
Rectangle2D rect = new Rectangle2D.Double(x1, y1, w1, h1); |
265 |
|
266 |
VectorialEditableAdapter vea = getCadToolAdapter() |
267 |
.getVectorialAdapter(); |
268 |
String strEPSG = getCadToolAdapter().getMapControl()
|
269 |
.getViewPort().getProjection().getAbrev().substring(5);
|
270 |
IRowEdited[] feats = vea.getFeatures(rect, strEPSG);
|
271 |
|
272 |
for (int i = 0; i < feats.length; i++) { |
273 |
IGeometry geom = ((IFeature) feats[i].getLinkedRow()) |
274 |
.getGeometry(); |
275 |
|
276 |
if (firstPoint.getX() < lastPoint.getX()) {
|
277 |
if (rect.contains(geom.getBounds2D())) {
|
278 |
selection.set(feats[i].getIndex(), true);
|
279 |
} |
280 |
} else {
|
281 |
if (geom.intersects(rect)) { // , 0.1)){ |
282 |
selection.set(feats[i].getIndex(), true);
|
283 |
} |
284 |
} |
285 |
} |
286 |
|
287 |
PluginServices.getMDIManager().restoreCursor(); |
288 |
} else if (status.equals("Selection.EndPoint")) { |
289 |
for (int i = 0; i < selectedRow.size(); i++) { |
290 |
DefaultFeature row = (DefaultFeature) selectedRow.get(i); |
291 |
int index = ((Integer) selectedRowIndex.get(i)).intValue(); |
292 |
|
293 |
// Movemos los handlers que hemos seleccionado
|
294 |
// previamente dentro del m?todo select()
|
295 |
for (int k = 0; k < selectedHandler.size(); k++) { |
296 |
Handler h = (Handler) selectedHandler.get(k); |
297 |
h.set(x, y); |
298 |
} |
299 |
|
300 |
modifyFeature(index, row); |
301 |
} |
302 |
} |
303 |
} catch (DriverException e) {
|
304 |
e.printStackTrace(); |
305 |
} |
306 |
} |
307 |
|
308 |
/**
|
309 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
310 |
* encontremos.
|
311 |
*
|
312 |
* @param g
|
313 |
* Graphics sobre el que dibujar.
|
314 |
* @param selectedGeometries
|
315 |
* BitSet con las geometr?as seleccionadas.
|
316 |
* @param x
|
317 |
* par?metro x del punto que se pase para dibujar.
|
318 |
* @param y
|
319 |
* par?metro x del punto que se pase para dibujar.
|
320 |
*/
|
321 |
public void drawOperation(Graphics g, double x, double y) { |
322 |
SelectionCADToolState actualState = _fsm.getState(); |
323 |
String status = actualState.getName();
|
324 |
FBitSet selection = getCadToolAdapter().getVectorialAdapter() |
325 |
.getSelection(); |
326 |
VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension |
327 |
.getEditionManager().getActiveLayerEdited(); |
328 |
ArrayList selectedHandler = vle.getSelectedHandler();
|
329 |
ArrayList selectedRow = vle.getSelectedRow();
|
330 |
ArrayList selectedRowIndex = vle.getSelectedRowIndex();
|
331 |
|
332 |
if (selection.cardinality() == 0) { |
333 |
selectedRow.clear(); |
334 |
selectedRowIndex.clear(); |
335 |
selectedHandler.clear(); |
336 |
} |
337 |
|
338 |
drawHandlers(g, selectedRow, getCadToolAdapter().getMapControl() |
339 |
.getViewPort().getAffineTransform()); |
340 |
|
341 |
if (status.equals("Selection.SecondPoint")) { |
342 |
// Dibuja el rect?ngulo de selecci?n
|
343 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
344 |
4);
|
345 |
elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
346 |
elShape.lineTo(x, firstPoint.getY()); |
347 |
elShape.lineTo(x, y); |
348 |
elShape.lineTo(firstPoint.getX(), y); |
349 |
elShape.lineTo(firstPoint.getX(), firstPoint.getY()); |
350 |
ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
|
351 |
getCadToolAdapter().getMapControl().getViewPort(), |
352 |
CADTool.selectSymbol); |
353 |
} else if (status.equals("Selection.EndPoint")) { |
354 |
// Movemos los handlers que hemos seleccionado
|
355 |
// previamente dentro del m?todo select()
|
356 |
for (int k = 0; k < selectedHandler.size(); k++) { |
357 |
Handler h = (Handler) selectedHandler.get(k); |
358 |
h.set(x, y); |
359 |
} |
360 |
|
361 |
// Y una vez movidos los v?rtices (handles)
|
362 |
// redibujamos la nueva geometr?a.
|
363 |
for (int i = 0; i < selectedRow.size(); i++) { |
364 |
IGeometry geom = ((IFeature) selectedRow.get(i)).getGeometry() |
365 |
.cloneGeometry(); |
366 |
g.setColor(Color.gray);
|
367 |
geom.draw((Graphics2D) g, getCadToolAdapter().getMapControl()
|
368 |
.getViewPort(), CADTool.modifySymbol); |
369 |
} |
370 |
} |
371 |
} |
372 |
|
373 |
/**
|
374 |
* Add a diferent option.
|
375 |
*
|
376 |
* @param sel
|
377 |
* DOCUMENT ME!
|
378 |
* @param s
|
379 |
* Diferent option.
|
380 |
*/
|
381 |
public void addOption(String s) { |
382 |
// TODO Auto-generated method stub
|
383 |
} |
384 |
|
385 |
/*
|
386 |
* (non-Javadoc)
|
387 |
*
|
388 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
389 |
*/
|
390 |
public void addValue(double d) { |
391 |
} |
392 |
|
393 |
public String getStatus() { |
394 |
try {
|
395 |
SelectionCADToolState actualState = (SelectionCADToolState) _fsm |
396 |
.getPreviousState(); |
397 |
String status = actualState.getName();
|
398 |
|
399 |
return status;
|
400 |
} catch (NullPointerException e) { |
401 |
return "ExecuteMap.Initial"; |
402 |
} |
403 |
} |
404 |
|
405 |
public String getTool() { |
406 |
return tool;
|
407 |
} |
408 |
|
409 |
public void setNextTool(String tool) { |
410 |
this.tool = tool;
|
411 |
} |
412 |
|
413 |
public void end() { |
414 |
CADExtension.setCADTool(getTool()); |
415 |
} |
416 |
|
417 |
public String getName() { |
418 |
return "SELECCION"; |
419 |
} |
420 |
} |