svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 1822
History | View | Annotate | Download (13 KB)
1 |
package com.iver.cit.gvsig.gui.cad; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Cursor; |
5 |
import java.awt.Graphics; |
6 |
import java.awt.Point; |
7 |
import java.awt.event.MouseEvent; |
8 |
import java.awt.event.MouseWheelEvent; |
9 |
import java.awt.geom.AffineTransform; |
10 |
import java.awt.geom.Point2D; |
11 |
import java.awt.geom.Rectangle2D; |
12 |
import java.io.IOException; |
13 |
import java.util.Stack; |
14 |
|
15 |
import org.apache.log4j.Logger; |
16 |
|
17 |
import com.iver.andami.PluginServices; |
18 |
import com.iver.cit.gvsig.fmap.core.FGeometry; |
19 |
import com.iver.cit.gvsig.fmap.core.Handler; |
20 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
21 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
22 |
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource; |
23 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
24 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
25 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
26 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
27 |
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool; |
28 |
import com.iver.fsac.Automaton; |
29 |
|
30 |
|
31 |
/**
|
32 |
* DOCUMENT ME!
|
33 |
*
|
34 |
* @author Fernando Gonz?lez Cort?s
|
35 |
*/
|
36 |
public abstract class CADToolAdapter extends Behavior { |
37 |
private static Logger logger = Logger.getLogger(CADToolAdapter.class.getName()); |
38 |
protected Stack cadToolStack = new Stack(); |
39 |
protected FBitSet selection;
|
40 |
protected EditableFeatureSource editableFeatureSource;
|
41 |
|
42 |
//Para pasarle las coordenadas cuando se produce un evento textEntered
|
43 |
private int lastX; |
44 |
private int lastY; |
45 |
//private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
|
46 |
private Point2D mapAdjustedPoint; |
47 |
private CadMapControl cadMapControl;
|
48 |
//private boolean questionAsked = false;
|
49 |
protected CadGrid grid;// = new CadGrid(); |
50 |
protected Point2D adjustedPoint; |
51 |
private boolean snapping=true; |
52 |
private boolean adjustSnapping=false; |
53 |
|
54 |
public CADToolAdapter(){
|
55 |
grid= new CadGrid(this); |
56 |
} |
57 |
public void paintComponent(Graphics g){ |
58 |
super.paintComponent(g);
|
59 |
} |
60 |
/**
|
61 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
62 |
*/
|
63 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
64 |
} |
65 |
|
66 |
/**
|
67 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
68 |
*/
|
69 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
70 |
} |
71 |
|
72 |
/**
|
73 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
74 |
*/
|
75 |
public void mouseExited(MouseEvent e) throws BehaviorException { |
76 |
} |
77 |
|
78 |
/**
|
79 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
80 |
*/
|
81 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
82 |
if (e.getButton() == MouseEvent.BUTTON1) { |
83 |
//ViewPort vp = getMapControl().getMapContext().getViewPort();
|
84 |
Point2D p;
|
85 |
|
86 |
if (mapAdjustedPoint != null) { |
87 |
p = mapAdjustedPoint; |
88 |
} else {
|
89 |
p = toPoint(adjustedPoint); |
90 |
} |
91 |
|
92 |
transition("punto", editableFeatureSource, selection,
|
93 |
new double[] { p.getX(), p.getY() }); |
94 |
} |
95 |
} |
96 |
|
97 |
|
98 |
/**
|
99 |
* Ajusta un punto de la imagen que se pasa como par?metro al grid si ?ste
|
100 |
* est? activo y devuelve la distancia de un punto al punto ajustado
|
101 |
*
|
102 |
* @param point
|
103 |
*
|
104 |
* @return Distancia del punto que se pasa como par?metro al punto ajustado
|
105 |
*/
|
106 |
private double adjustToHandler(Point2D point, Point2D mapHandlerAdjustedPoint) { |
107 |
//if (selection.cardinality() > 0) {
|
108 |
double rw=toDistance(5); |
109 |
try {
|
110 |
Point2D mapPoint = point;
|
111 |
|
112 |
Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw/2, |
113 |
mapPoint.getY() - rw/2, rw, rw);
|
114 |
|
115 |
int[] indexes = editableFeatureSource.getGeometriesIndexes(r); |
116 |
|
117 |
double min = Double.MAX_VALUE; |
118 |
Point2D argmin = null; |
119 |
Point2D mapArgmin = null; |
120 |
|
121 |
for (int i = 0; i < indexes.length; i++) { |
122 |
//if (!selection.get(indexes[i])) continue;
|
123 |
IGeometry geom; |
124 |
geom = editableFeatureSource.getGeometry(indexes[i]); |
125 |
|
126 |
Handler[] handlers = geom.getHandlers(FGeometry.SELECTHANDLER); |
127 |
|
128 |
for (int j = 0; j < handlers.length; j++) { |
129 |
Point2D handlerPoint = handlers[j].getPoint();
|
130 |
Point2D handlerImagePoint = handlerPoint;
|
131 |
double dist = handlerImagePoint.distance(point);
|
132 |
|
133 |
if ((dist < toDistance(SelectionCadTool.tolerance)) &&
|
134 |
(dist < min)) { |
135 |
min = dist; |
136 |
argmin = handlerImagePoint; |
137 |
mapArgmin = handlerPoint; |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
if (argmin != null) { |
143 |
point.setLocation(argmin); |
144 |
|
145 |
//Se hace el casting porque no se quiere redondeo
|
146 |
//point.setLocation((int)argmin.getX(),
|
147 |
// (int) argmin.getY());
|
148 |
|
149 |
mapHandlerAdjustedPoint.setLocation(mapArgmin); |
150 |
return min;
|
151 |
} |
152 |
} catch (IOException e) { |
153 |
e.printStackTrace(); |
154 |
} catch (DriverIOException e) {
|
155 |
e.printStackTrace(); |
156 |
} |
157 |
//}
|
158 |
|
159 |
return Double.MAX_VALUE; |
160 |
} |
161 |
|
162 |
/**
|
163 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
164 |
*/
|
165 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
166 |
} |
167 |
|
168 |
/**
|
169 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
170 |
*/
|
171 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
172 |
lastX = e.getX(); |
173 |
lastY = e.getY(); |
174 |
|
175 |
calculateSnapPoint(e.getPoint()); |
176 |
} |
177 |
|
178 |
/**
|
179 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
180 |
*/
|
181 |
public abstract void mouseMoved(MouseEvent e) throws BehaviorException; |
182 |
|
183 |
/**
|
184 |
* DOCUMENT ME!
|
185 |
*
|
186 |
* @param g DOCUMENT ME!
|
187 |
*/
|
188 |
public void drawCursor(Graphics g) { |
189 |
g.setColor(Color.black);
|
190 |
Point2D p = adjustedPoint;
|
191 |
if (p==null) grid.setAT(getAT()); |
192 |
//else{
|
193 |
int size1 = 15; |
194 |
int size2 = 3; |
195 |
g.drawLine((int) (p.getX() - size1), (int) (p.getY()), |
196 |
(int) (p.getX() + size1), (int) (p.getY())); |
197 |
g.drawLine((int) (p.getX()), (int) (p.getY() - size1), |
198 |
(int) (p.getX()), (int) (p.getY() + size1)); |
199 |
if (adjustedPoint!=null){ |
200 |
if (adjustSnapping){
|
201 |
g.setColor(Color.ORANGE);
|
202 |
g.drawRect((int)(adjustedPoint.getX()-5),(int)(adjustedPoint.getY()-5),10,10); |
203 |
g.drawRect((int)(adjustedPoint.getX()-3),(int)(adjustedPoint.getY()-3),6,6); |
204 |
g.setColor(Color.MAGENTA);
|
205 |
g.drawRect((int)(adjustedPoint.getX()-4),(int)(adjustedPoint.getY()-4),8,8); |
206 |
|
207 |
adjustSnapping=false;
|
208 |
}else{
|
209 |
g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2), |
210 |
(int) (size2 * 2), (int) (size2 * 2)); |
211 |
} |
212 |
} |
213 |
//}
|
214 |
} |
215 |
/**
|
216 |
* DOCUMENT ME!
|
217 |
*
|
218 |
* @param point
|
219 |
*/
|
220 |
protected void calculateSnapPoint(Point point) { |
221 |
//Se comprueba el ajuste a rejilla
|
222 |
|
223 |
Point2D gridAdjustedPoint = toPoint(point);
|
224 |
double minDistance = Double.MAX_VALUE; |
225 |
|
226 |
if (!cadToolStack.isEmpty() && cadToolStack.peek() instanceof SelectionCadTool && ((SelectionCadTool)cadToolStack.peek()).getAutomaton().getStatus()==0){ |
227 |
mapAdjustedPoint=gridAdjustedPoint; |
228 |
adjustedPoint=(Point2D)point.clone();
|
229 |
}else{
|
230 |
minDistance= grid.adjustToGrid(gridAdjustedPoint); |
231 |
if (minDistance < Double.MAX_VALUE) { |
232 |
adjustedPoint = fromPoint(gridAdjustedPoint); |
233 |
mapAdjustedPoint = gridAdjustedPoint; |
234 |
} else {
|
235 |
mapAdjustedPoint = null;
|
236 |
} |
237 |
} |
238 |
Point2D handlerAdjustedPoint = null; |
239 |
|
240 |
//Se comprueba el ajuste a los handlers
|
241 |
if (mapAdjustedPoint != null) { |
242 |
handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); //getMapControl().getViewPort().toMapPoint(point); |
243 |
} else {
|
244 |
handlerAdjustedPoint = toPoint(point); |
245 |
} |
246 |
|
247 |
Point2D mapPoint = new Point2D.Double(); |
248 |
double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
|
249 |
|
250 |
if (distance < minDistance) {
|
251 |
adjustSnapping = true;
|
252 |
adjustedPoint = fromPoint(handlerAdjustedPoint); |
253 |
mapAdjustedPoint = mapPoint; |
254 |
minDistance = distance; |
255 |
} |
256 |
|
257 |
//Si no hay ajuste
|
258 |
if (minDistance == Double.MAX_VALUE) { |
259 |
adjustedPoint = point; |
260 |
mapAdjustedPoint = null;
|
261 |
} |
262 |
} |
263 |
/*protected void calculateSnapPoint(Point point) {
|
264 |
//Se comprueba el ajuste a rejilla
|
265 |
Point gridAdjustedPoint = (Point) point.clone();
|
266 |
double minDistance = grid.adjustToGrid(gridAdjustedPoint);
|
267 |
if (minDistance < Double.MAX_VALUE) {
|
268 |
adjustedPoint = gridAdjustedPoint;
|
269 |
mapAdjustedPoint = toPoint(adjustedPoint);
|
270 |
}
|
271 |
|
272 |
//Se comprueba el ajuste a los handlers
|
273 |
Point handlerAdjustedPoint = (Point) point.clone();
|
274 |
Point2D mapPoint = new Point2D.Double();
|
275 |
double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
|
276 |
if (distance < minDistance) {
|
277 |
adjustSnapping=true;
|
278 |
adjustedPoint = handlerAdjustedPoint;
|
279 |
mapAdjustedPoint = mapPoint;
|
280 |
minDistance = distance;
|
281 |
}
|
282 |
|
283 |
//Si no hay ajuste
|
284 |
if (minDistance == Double.MAX_VALUE) {
|
285 |
adjustedPoint = point;
|
286 |
mapAdjustedPoint = null;
|
287 |
}
|
288 |
}
|
289 |
|
290 |
*/
|
291 |
/**
|
292 |
* @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
293 |
*/
|
294 |
public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
295 |
} |
296 |
|
297 |
/**
|
298 |
* DOCUMENT ME!
|
299 |
*
|
300 |
* @param text DOCUMENT ME!
|
301 |
*/
|
302 |
public void textEntered(String text) { |
303 |
text = text.trim(); |
304 |
|
305 |
String[] numbers = text.split(","); |
306 |
double[] values = null; |
307 |
|
308 |
try {
|
309 |
if (numbers.length == 2) { |
310 |
//punto
|
311 |
values = new double[] { |
312 |
Double.parseDouble(numbers[0]), |
313 |
Double.parseDouble(numbers[1]) |
314 |
}; |
315 |
transition("punto", editableFeatureSource, selection, values);
|
316 |
} else if (numbers.length == 1) { |
317 |
//valor
|
318 |
values = new double[] { Double.parseDouble(numbers[0]) }; |
319 |
transition("numero", editableFeatureSource, selection, values);
|
320 |
} |
321 |
} catch (NumberFormatException e) { |
322 |
transition(text, editableFeatureSource, selection, new double[0]); |
323 |
} |
324 |
} |
325 |
|
326 |
/**
|
327 |
* DOCUMENT ME!
|
328 |
*
|
329 |
* @param text DOCUMENT ME!
|
330 |
*/
|
331 |
public void transition(String text) { |
332 |
transition(text, editableFeatureSource, selection, new double[0]); |
333 |
configureMenu(); |
334 |
} |
335 |
|
336 |
public abstract void configureMenu(); |
337 |
|
338 |
|
339 |
/**
|
340 |
* DOCUMENT ME!
|
341 |
*
|
342 |
* @param value DOCUMENT ME!
|
343 |
*/
|
344 |
public abstract void setGrid(boolean value); |
345 |
|
346 |
|
347 |
/**
|
348 |
* DOCUMENT ME!
|
349 |
*
|
350 |
* @param activated DOCUMENT ME!
|
351 |
*/
|
352 |
public void setSnapping(boolean activated) { |
353 |
snapping=activated; |
354 |
} |
355 |
|
356 |
/**
|
357 |
* DOCUMENT ME!
|
358 |
*
|
359 |
* @param x DOCUMENT ME!
|
360 |
* @param y DOCUMENT ME!
|
361 |
*/
|
362 |
public void getSnapPoint(double x, double y,double dist) { |
363 |
} |
364 |
|
365 |
/**
|
366 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
367 |
*/
|
368 |
public ToolListener getListener() {
|
369 |
return new ToolListener() { |
370 |
/**
|
371 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
372 |
*/
|
373 |
public Cursor getCursor() { |
374 |
return null; |
375 |
} |
376 |
|
377 |
/**
|
378 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
379 |
*/
|
380 |
public boolean cancelDrawing() { |
381 |
return false; |
382 |
} |
383 |
}; |
384 |
} |
385 |
|
386 |
/**
|
387 |
* DOCUMENT ME!
|
388 |
*
|
389 |
* @return DOCUMENT ME!
|
390 |
*/
|
391 |
public CadTool getCadTool() {
|
392 |
return (CadTool) cadToolStack.peek();
|
393 |
} |
394 |
|
395 |
/**
|
396 |
* DOCUMENT ME!
|
397 |
*
|
398 |
* @param cadTool DOCUMENT ME!
|
399 |
*/
|
400 |
public void pushCadTool(CadTool cadTool) { |
401 |
cadToolStack.push(cadTool); |
402 |
cadTool.setCadToolAdapter(this);
|
403 |
cadTool.initializeStatus(); |
404 |
int ret = cadTool.transition(null, editableFeatureSource, selection, new double[0]); |
405 |
if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
|
406 |
popCadTool(); |
407 |
|
408 |
if (cadToolStack.isEmpty()) {
|
409 |
pushCadTool(new SelectionCadTool());
|
410 |
PluginServices.getMainFrame().selectTool("selection");
|
411 |
} |
412 |
|
413 |
//askQuestion();
|
414 |
|
415 |
//getMapControl().drawMap(false);
|
416 |
} |
417 |
} |
418 |
|
419 |
/**
|
420 |
* DOCUMENT ME!
|
421 |
*/
|
422 |
public void popCadTool() { |
423 |
cadToolStack.pop(); |
424 |
} |
425 |
|
426 |
|
427 |
|
428 |
/**
|
429 |
* DOCUMENT ME!
|
430 |
*
|
431 |
* @param cadTool DOCUMENT ME!
|
432 |
*/
|
433 |
public void setCadTool(CadTool cadTool) { |
434 |
cadToolStack.clear(); |
435 |
pushCadTool(cadTool); |
436 |
//if (console!=null)
|
437 |
//askQuestion();
|
438 |
} |
439 |
|
440 |
/**
|
441 |
* DOCUMENT ME!
|
442 |
*
|
443 |
* @return DOCUMENT ME!
|
444 |
*/
|
445 |
public EditableFeatureSource getEditableFeatureSource() {
|
446 |
return editableFeatureSource;
|
447 |
} |
448 |
|
449 |
/**
|
450 |
* DOCUMENT ME!
|
451 |
*
|
452 |
* @param editableFeatureSource DOCUMENT ME!
|
453 |
* @param selection DOCUMENT ME!
|
454 |
*/
|
455 |
public void setEditableFeatureSource( |
456 |
EditableFeatureSource editableFeatureSource, FBitSet selection) { |
457 |
this.editableFeatureSource = editableFeatureSource;
|
458 |
this.selection = selection;
|
459 |
} |
460 |
|
461 |
/**
|
462 |
* DOCUMENT ME!
|
463 |
*
|
464 |
* @param console
|
465 |
*/
|
466 |
/*public void setConsole(JConsole console) {
|
467 |
this.console = console;
|
468 |
}
|
469 |
*/
|
470 |
/**
|
471 |
* DOCUMENT ME!
|
472 |
*
|
473 |
* @return DOCUMENT ME!
|
474 |
*/
|
475 |
public CadMapControl getCadMapControl() {
|
476 |
return cadMapControl;
|
477 |
} |
478 |
|
479 |
/**
|
480 |
* DOCUMENT ME!
|
481 |
*
|
482 |
* @param cadMapControl DOCUMENT ME!
|
483 |
*/
|
484 |
public void setCadMapControl(CadMapControl cadMapControl) { |
485 |
this.cadMapControl = cadMapControl;
|
486 |
} |
487 |
|
488 |
|
489 |
/**
|
490 |
* @param b
|
491 |
*/
|
492 |
public void setAdjustGrid(boolean b) { |
493 |
grid.setAdjustGrid(b); |
494 |
} |
495 |
|
496 |
|
497 |
public abstract AffineTransform getAT(); |
498 |
public abstract double toDistance(int i); |
499 |
public FBitSet getSelection(){
|
500 |
return selection;
|
501 |
} |
502 |
public abstract void refresh(); |
503 |
public abstract Point2D toPoint(Point2D p); |
504 |
public abstract Rectangle2D getExtent(); |
505 |
public abstract double fromDistance(double d); |
506 |
public abstract Point2D fromPoint(Point2D p); |
507 |
public void setGridSizeX(double i1){ |
508 |
grid.setGridSizeX(i1); |
509 |
} |
510 |
public void setGridSizeY(double i1){ |
511 |
grid.setGridSizeY(i1); |
512 |
} |
513 |
public double getGridSizeX(){ |
514 |
return grid.getGridSizeX();
|
515 |
} |
516 |
public double getGridSizeY(){ |
517 |
return grid.getGridSizeY();
|
518 |
} |
519 |
public Point2D getAdjustedPoint(){ |
520 |
return adjustedPoint;
|
521 |
} |
522 |
protected abstract void transition(String text, EditableFeatureSource source, |
523 |
FBitSet sel, double[] values); |
524 |
public abstract void keyPressed(String actionCommand); |
525 |
public boolean isSelected(IGeometry g){ |
526 |
return true; |
527 |
} |
528 |
} |