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