svn-gvsig-desktop / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 1494
History | View | Annotate | Download (10.5 KB)
1 |
package com.iver.cit.gvsig.gui.cad; |
---|---|
2 |
|
3 |
import com.iver.cit.gvsig.fmap.MapControl; |
4 |
import com.iver.cit.gvsig.fmap.ViewPort; |
5 |
import com.iver.cit.gvsig.fmap.core.Handler; |
6 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
7 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
8 |
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities; |
9 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
10 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
11 |
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource; |
12 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
13 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
14 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
15 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
16 |
import com.iver.cit.gvsig.gui.cad.automaton.Seleccion; |
17 |
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool; |
18 |
import com.iver.fsac.Automaton; |
19 |
|
20 |
import com.iver.utiles.console.InvalidResponseException; |
21 |
import com.iver.utiles.console.JConsole; |
22 |
import com.iver.utiles.console.ResponseListener; |
23 |
import com.vividsolutions.jts.geom.Envelope; |
24 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
25 |
|
26 |
import java.awt.AWTException; |
27 |
import java.awt.Color; |
28 |
import java.awt.Cursor; |
29 |
import java.awt.Graphics; |
30 |
import java.awt.Graphics2D; |
31 |
import java.awt.Point; |
32 |
import java.awt.Robot; |
33 |
import java.awt.event.ActionEvent; |
34 |
import java.awt.event.MouseEvent; |
35 |
import java.awt.event.MouseWheelEvent; |
36 |
import java.awt.geom.Point2D; |
37 |
import java.awt.geom.Rectangle2D; |
38 |
|
39 |
import java.io.IOException; |
40 |
import java.util.Stack; |
41 |
|
42 |
import javax.swing.JMenuItem; |
43 |
import javax.swing.JPopupMenu; |
44 |
import javax.swing.KeyStroke; |
45 |
import javax.swing.SwingUtilities; |
46 |
|
47 |
import org.apache.log4j.Logger; |
48 |
|
49 |
|
50 |
/**
|
51 |
* DOCUMENT ME!
|
52 |
*
|
53 |
* @author Fernando Gonz?lez Cort?s
|
54 |
*/
|
55 |
public class CADToolAdapter extends Behavior { |
56 |
private static Logger logger = Logger.getLogger(CADToolAdapter.class.getName()); |
57 |
|
58 |
private Stack cadToolStack = new Stack(); |
59 |
private FBitSet selection;
|
60 |
private EditableFeatureSource editableFeatureSource;
|
61 |
|
62 |
//Para pasarle las coordenadas cuando se produce un evento textEntered
|
63 |
private int lastX; |
64 |
private int lastY; |
65 |
private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED); |
66 |
private JConsole console;
|
67 |
private boolean grid = false; |
68 |
private int gridSize = 30; |
69 |
private Point gridPoint; |
70 |
private CadMapControl cadMapControl;
|
71 |
|
72 |
/**
|
73 |
* Pinta de alguna manera especial las geometrias seleccionadas para la
|
74 |
* edici?n. En caso de que el snapping est? activado, pintar? el efecto
|
75 |
* del mismo.
|
76 |
*
|
77 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
78 |
*/
|
79 |
public void paintComponent(Graphics g) { |
80 |
super.paintComponent(g);
|
81 |
/*
|
82 |
for (int i = selection.nextSetBit(0); i >= 0;
|
83 |
i = selection.nextSetBit(i + 1)) {
|
84 |
try {
|
85 |
IGeometry geom = editableFeatureSource.getGeometry(i);
|
86 |
Handler[] handlers=geom.getHandlers();
|
87 |
geom.draw((Graphics2D) g, getMapControl().getViewPort(), symbol);
|
88 |
FGraphicUtilities.DrawHandlers((Graphics2D)g,getMapControl().getViewPort().getAffineTransform(),handlers);
|
89 |
} catch (IOException e) {
|
90 |
e.printStackTrace();
|
91 |
} catch (DriverIOException e) {
|
92 |
e.printStackTrace();
|
93 |
}
|
94 |
}
|
95 |
*/
|
96 |
if (gridPoint != null){ |
97 |
g.drawLine(gridPoint.x, gridPoint.y, lastX, lastY); |
98 |
} |
99 |
Point2D p = getMapControl().getViewPort().toMapPoint(lastX, lastY);
|
100 |
((CadTool)cadToolStack.peek()).drawOperation(g, editableFeatureSource,selection,p.getX(), p.getY()); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* DOCUMENT ME!
|
105 |
*
|
106 |
* @return DOCUMENT ME!
|
107 |
*/
|
108 |
public Point getLastMousePosition() { |
109 |
return new Point(lastX, lastY); |
110 |
} |
111 |
|
112 |
/**
|
113 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
114 |
*/
|
115 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
116 |
} |
117 |
|
118 |
/**
|
119 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
120 |
*/
|
121 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
122 |
} |
123 |
|
124 |
/**
|
125 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
126 |
*/
|
127 |
public void mouseExited(MouseEvent e) throws BehaviorException { |
128 |
} |
129 |
|
130 |
/**
|
131 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
132 |
*/
|
133 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
134 |
if (e.getButton() == MouseEvent.BUTTON1){ |
135 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
136 |
Point2D p2;
|
137 |
if (gridPoint != null){ |
138 |
p2 = vp.toMapPoint(gridPoint); |
139 |
}else{
|
140 |
p2 = vp.toMapPoint(e.getPoint()); |
141 |
} |
142 |
|
143 |
transition("punto", editableFeatureSource, selection,
|
144 |
new double[] { p2.getX(), p2.getY() }); |
145 |
} |
146 |
} |
147 |
|
148 |
/**
|
149 |
* Ajusta un punto de la imagen que se pasa como
|
150 |
* par?metro al grid si ?ste est? activo y devuelve la distancia
|
151 |
* de un punto al punto ajustado
|
152 |
*
|
153 |
* @param point
|
154 |
*
|
155 |
* @return Distancia del punto que se pasa como par?metro al
|
156 |
* punto ajustado
|
157 |
*/
|
158 |
private double adjustToGrid(Point point) { |
159 |
if (grid){
|
160 |
Point p = new Point(point); |
161 |
point.x = (((int)point.getX()+ (gridSize/2))/gridSize) * gridSize; |
162 |
point.y = (((int)point.getY()+ (gridSize/2))/gridSize) * gridSize; |
163 |
return p.distance(point);
|
164 |
} |
165 |
return Double.MAX_VALUE; |
166 |
} |
167 |
|
168 |
/**
|
169 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
170 |
*/
|
171 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
172 |
} |
173 |
|
174 |
/**
|
175 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
176 |
*/
|
177 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
178 |
lastX = e.getX(); |
179 |
lastY = e.getY(); |
180 |
|
181 |
calculateSnapPoint(e.getPoint()); |
182 |
} |
183 |
|
184 |
/**
|
185 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
186 |
*/
|
187 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
188 |
lastX = e.getX(); |
189 |
lastY = e.getY(); |
190 |
|
191 |
calculateSnapPoint(e.getPoint()); |
192 |
|
193 |
getMapControl().repaint(); |
194 |
} |
195 |
|
196 |
/**
|
197 |
* @param point
|
198 |
*/
|
199 |
private void calculateSnapPoint(Point point) { |
200 |
double minDistance = adjustToGrid(point);
|
201 |
|
202 |
if (minDistance < Double.MAX_VALUE){ |
203 |
gridPoint = point; |
204 |
}else{
|
205 |
gridPoint = null;
|
206 |
} |
207 |
} |
208 |
|
209 |
/**
|
210 |
* @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
211 |
*/
|
212 |
public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
213 |
} |
214 |
|
215 |
/**
|
216 |
* DOCUMENT ME!
|
217 |
*
|
218 |
* @param text DOCUMENT ME!
|
219 |
*/
|
220 |
public void textEntered(String text) { |
221 |
text = text.trim(); |
222 |
|
223 |
String[] numbers = text.split(","); |
224 |
double[] values = null; |
225 |
|
226 |
try {
|
227 |
if (numbers.length == 2) { |
228 |
//punto
|
229 |
values = new double[] { |
230 |
Double.parseDouble(numbers[0]), |
231 |
Double.parseDouble(numbers[1]) |
232 |
}; |
233 |
transition("punto", editableFeatureSource, selection, values);
|
234 |
} else if (numbers.length == 1) { |
235 |
//valor
|
236 |
values = new double[] { Double.parseDouble(numbers[0]) }; |
237 |
transition("numero", editableFeatureSource, selection, values);
|
238 |
} |
239 |
} catch (NumberFormatException e) { |
240 |
transition(text, editableFeatureSource, selection, new double[0]); |
241 |
} |
242 |
} |
243 |
|
244 |
public void transition(String text){ |
245 |
transition(text,editableFeatureSource,selection,new double[0]); |
246 |
configureMenu(); |
247 |
} |
248 |
|
249 |
private void configureMenu(){ |
250 |
String[] desc = ((CadTool)cadToolStack.peek()).getAutomaton().getCurrentTransitionDescriptions(); |
251 |
String[] labels = ((CadTool)cadToolStack.peek()).getAutomaton().getCurrentTransitions(); |
252 |
getCadMapControl().clearMenu(); |
253 |
for (int i = 0; i < desc.length; i++) { |
254 |
if (desc[i] != null){ |
255 |
getCadMapControl().addMenuEntry(desc[i], labels[i]); |
256 |
} |
257 |
} |
258 |
} |
259 |
|
260 |
/**
|
261 |
* DOCUMENT ME!
|
262 |
*
|
263 |
* @param text DOCUMENT ME!
|
264 |
* @param source DOCUMENT ME!
|
265 |
* @param sel DOCUMENT ME!
|
266 |
* @param values DOCUMENT ME!
|
267 |
*/
|
268 |
private void transition(String text, EditableFeatureSource source, |
269 |
FBitSet sel, double[] values) { |
270 |
logger.debug(text); |
271 |
int ret = ((CadTool)cadToolStack.peek()).transition(text, source, sel, values);
|
272 |
|
273 |
if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED){
|
274 |
popCadTool(); |
275 |
if (cadToolStack.isEmpty()){
|
276 |
pushCadTool(new SelectionCadTool());
|
277 |
} |
278 |
getMapControl().drawMap(false);
|
279 |
}else{
|
280 |
if (((CadTool)cadToolStack.peek()).getAutomaton().checkState('c')){ |
281 |
getMapControl().drawMap(false);
|
282 |
} |
283 |
} |
284 |
console.askQuestion(((CadTool)cadToolStack.peek()).getQuestion(), |
285 |
new ResponseListener() {
|
286 |
public void acceptResponse(String response) |
287 |
throws InvalidResponseException {
|
288 |
//Si no se ha cancelado
|
289 |
if (response != null) { |
290 |
textEntered(response); |
291 |
} |
292 |
} |
293 |
}); |
294 |
|
295 |
|
296 |
configureMenu(); |
297 |
} |
298 |
|
299 |
public void setGrid(boolean value){ |
300 |
this.grid = value;
|
301 |
} |
302 |
|
303 |
/**
|
304 |
* DOCUMENT ME!
|
305 |
*
|
306 |
* @param activated DOCUMENT ME!
|
307 |
*/
|
308 |
public void setSnapping(boolean activated) { |
309 |
} |
310 |
|
311 |
/**
|
312 |
* DOCUMENT ME!
|
313 |
*
|
314 |
* @param x DOCUMENT ME!
|
315 |
* @param y DOCUMENT ME!
|
316 |
*/
|
317 |
public void getSnapPoint(double x, double y) { |
318 |
} |
319 |
|
320 |
/**
|
321 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
322 |
*/
|
323 |
public ToolListener getListener() {
|
324 |
return new ToolListener() { |
325 |
/**
|
326 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
327 |
*/
|
328 |
public Cursor getCursor() { |
329 |
return null; |
330 |
} |
331 |
|
332 |
/**
|
333 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
334 |
*/
|
335 |
public boolean cancelDrawing() { |
336 |
return false; |
337 |
} |
338 |
}; |
339 |
} |
340 |
|
341 |
/**
|
342 |
* DOCUMENT ME!
|
343 |
*
|
344 |
* @return DOCUMENT ME!
|
345 |
*/
|
346 |
public CadTool getCadTool() {
|
347 |
return (CadTool)cadToolStack.peek();
|
348 |
} |
349 |
|
350 |
public void pushCadTool(CadTool cadTool){ |
351 |
cadToolStack.push(cadTool); |
352 |
cadTool.setCadToolAdapter(this);
|
353 |
cadTool.initializeStatus(); |
354 |
cadTool.transition(null, editableFeatureSource, selection, new double[0]); |
355 |
console.askQuestion(cadTool.getQuestion(), |
356 |
new ResponseListener() {
|
357 |
public void acceptResponse(String response) |
358 |
throws InvalidResponseException {
|
359 |
if (response != null) { |
360 |
textEntered(response); |
361 |
} |
362 |
} |
363 |
}); |
364 |
} |
365 |
|
366 |
public void popCadTool(){ |
367 |
console.cancelQuestion(); |
368 |
cadToolStack.pop(); |
369 |
} |
370 |
|
371 |
/**
|
372 |
* DOCUMENT ME!
|
373 |
*
|
374 |
* @param cadTool DOCUMENT ME!
|
375 |
*/
|
376 |
public void setCadTool(CadTool cadTool) { |
377 |
cadToolStack.clear(); |
378 |
pushCadTool(cadTool); |
379 |
} |
380 |
|
381 |
/**
|
382 |
* DOCUMENT ME!
|
383 |
*
|
384 |
* @return DOCUMENT ME!
|
385 |
*/
|
386 |
public EditableFeatureSource getEditableFeatureSource() {
|
387 |
return editableFeatureSource;
|
388 |
} |
389 |
|
390 |
/**
|
391 |
* DOCUMENT ME!
|
392 |
*
|
393 |
* @param editableFeatureSource DOCUMENT ME!
|
394 |
*/
|
395 |
public void setEditableFeatureSource( |
396 |
EditableFeatureSource editableFeatureSource, FBitSet selection) { |
397 |
this.editableFeatureSource = editableFeatureSource;
|
398 |
this.selection = selection;
|
399 |
} |
400 |
|
401 |
/**
|
402 |
* DOCUMENT ME!
|
403 |
*
|
404 |
* @param console
|
405 |
*/
|
406 |
public void setConsole(JConsole console) { |
407 |
this.console = console;
|
408 |
} |
409 |
|
410 |
public CadMapControl getCadMapControl() {
|
411 |
return cadMapControl;
|
412 |
} |
413 |
public void setCadMapControl(CadMapControl cadMapControl) { |
414 |
this.cadMapControl = cadMapControl;
|
415 |
} |
416 |
} |