root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / MapControl.java @ 30788
History | View | Annotate | Download (72.7 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 org.gvsig.fmap.mapcontrol; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Cursor; |
45 |
import java.awt.Dimension; |
46 |
import java.awt.Graphics; |
47 |
import java.awt.Graphics2D; |
48 |
import java.awt.Image; |
49 |
import java.awt.Point; |
50 |
import java.awt.Toolkit; |
51 |
import java.awt.event.ActionEvent; |
52 |
import java.awt.event.ActionListener; |
53 |
import java.awt.event.ComponentEvent; |
54 |
import java.awt.event.ComponentListener; |
55 |
import java.awt.event.MouseEvent; |
56 |
import java.awt.event.MouseListener; |
57 |
import java.awt.event.MouseMotionListener; |
58 |
import java.awt.event.MouseWheelEvent; |
59 |
import java.awt.event.MouseWheelListener; |
60 |
import java.awt.geom.Point2D; |
61 |
import java.awt.image.BufferedImage; |
62 |
import java.awt.image.MemoryImageSource; |
63 |
import java.util.ArrayList; |
64 |
import java.util.Comparator; |
65 |
import java.util.HashMap; |
66 |
import java.util.Set; |
67 |
import java.util.TreeMap; |
68 |
import java.util.prefs.Preferences; |
69 |
|
70 |
import javax.swing.JComponent; |
71 |
import javax.swing.Timer; |
72 |
|
73 |
import org.cresques.cts.IProjection; |
74 |
import org.gvsig.fmap.crs.CRSFactory; |
75 |
import org.gvsig.fmap.dal.DataStoreNotification; |
76 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
77 |
import org.gvsig.fmap.geom.Geometry; |
78 |
import org.gvsig.fmap.geom.GeometryLocator; |
79 |
import org.gvsig.fmap.geom.GeometryManager; |
80 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
81 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
82 |
import org.gvsig.fmap.geom.primitive.Envelope; |
83 |
import org.gvsig.fmap.geom.util.Converter; |
84 |
import org.gvsig.fmap.mapcontext.MapContext; |
85 |
import org.gvsig.fmap.mapcontext.ViewPort; |
86 |
import org.gvsig.fmap.mapcontext.events.AtomicEvent; |
87 |
import org.gvsig.fmap.mapcontext.events.listeners.AtomicEventListener; |
88 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
89 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
90 |
import org.gvsig.fmap.mapcontext.layers.LayerEvent; |
91 |
import org.gvsig.fmap.mapcontext.layers.SpatialCache; |
92 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
93 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
94 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
95 |
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior; |
96 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
97 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
98 |
import org.gvsig.fmap.mapcontrol.tools.grid.Grid; |
99 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper; |
100 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperRaster; |
101 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial; |
102 |
import org.gvsig.tools.observer.Observable; |
103 |
import org.gvsig.tools.observer.Observer; |
104 |
import org.gvsig.tools.task.Cancellable; |
105 |
import org.gvsig.utils.exceptionHandling.ExceptionHandlingSupport; |
106 |
import org.gvsig.utils.exceptionHandling.ExceptionListener; |
107 |
import org.slf4j.Logger; |
108 |
import org.slf4j.LoggerFactory; |
109 |
|
110 |
|
111 |
|
112 |
/**
|
113 |
* <p>A component that includes a {@link MapContext MapContext} with support for use it as a particular {@link Behavior Behavior}.</p>
|
114 |
*
|
115 |
* <p>A developer can register a set of <code>Behavior</code>, but only one (that can be a composition of several) of them can be active. The active one
|
116 |
* defines the way to work and access with its <code>MapContext</code>'s layers. The active behavior, in combination with the appropriate
|
117 |
* {@link ToolListener ToolListener} will allow user work with a particular <i>tool</i>.</p>
|
118 |
*
|
119 |
* <p>All mouse events produced on this component will be delegated to the current active behavior, the <i>currentMapTool</i>.</p>
|
120 |
*
|
121 |
* <p><b>Drawing process:</b></p>
|
122 |
*
|
123 |
* <p>Uses a double buffer for the drawing process of <code>MapContext</code>'s information.</p>
|
124 |
*
|
125 |
* <p>If the double buffer wasn't created, creates a new one.</p>
|
126 |
*
|
127 |
* <p>Paints the component according the following algorithm:
|
128 |
* <br>
|
129 |
*   If <i>status</i> is <i>UPDATED</i>:<br>
|
130 |
*     If there is a <i>double buffer</i>:<br>
|
131 |
*       If there is a <i>behavior</i> for managing the <code>MapControl</code> instance, delegates
|
132 |
* the drawing process to that behavior, calling: <code><i>behavior_instance</i>.paintComponent(g)</code>.<br>
|
133 |
*       Else, repaints the current graphical information quickly calling: <code>g.drawImage(image,0,0,null)</code>.<br>
|
134 |
*   Else, (<i>status</i> is <i>OUTDATED</i>, or <i>ONLY_GRAPHICS</i>): executes a quickly repaint of the previous information calling <code>g.drawImage(image,0,0,null)</code>, and creates
|
135 |
* a <i>painting request</i> to delegate the heavy drawing process to the {@link Drawer2 Drawer2}'s worker thread, according the <i>SingleWorketThread</i> pattern, starting a timer to update
|
136 |
* (invoking <code>repaint()</code>) the view every delay of <code>1000 / drawFrameRate</code> ms. during that heavy drawing process, and if its enabled <code>drawAnimationEnabled</code>. The <i>painting request</i> once is being attended, invokes <code>MapContext</code> to
|
137 |
* draw the layers: <code>mapContext.draw(image, g, cancel,mapContext.getScaleView());</code>
|
138 |
* <br>
|
139 |
* <p>Some notes:
|
140 |
* <ul>
|
141 |
* <li>The painting process can be cancelled calling {@link #cancelDrawing() #cancelDrawing()}.</li>
|
142 |
* <li>At last resort, the particular implementation of each layer in a <code>MapControl</code>'s <code>MapContrext</code>
|
143 |
* will be that one which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</li>
|
144 |
* <li>It's possible to force repaint all layers, calling {@link #drawMap(boolean doClear) #drawMap(boolean)}.</li>
|
145 |
* <li>It's possible repaint only the dirty layers, calling {@link #rePaintDirtyLayers() #rePaintDirtyLayers()}.</li>
|
146 |
* <li>It's possible repaint only the {@link GraphicLayer GraphicLayer}, calling {@link #drawGraphics() #drawGraphics()}.</li>
|
147 |
* </ul>
|
148 |
* </p>
|
149 |
*
|
150 |
* <p><b>Tools:</b></p>
|
151 |
*
|
152 |
* <p>A developer can:
|
153 |
* <ul>
|
154 |
* <li>Register each tool as:
|
155 |
* <ul>
|
156 |
* <li>A single behavior: {@link #addBehavior(String, Behavior) #addMapTool(String, Behavior)}.</li>
|
157 |
* <li>Or, a compound behavior: {@link #addBehavior(String, Behavior) #addMapTool(String, Behavior)}.</li>
|
158 |
* </ul>
|
159 |
* </li>
|
160 |
* <li>Get the current active tool: {@link #getCurrentMapTool() #getCurrentMapTool()}.</li>
|
161 |
* <li>Get the current active tool name: {@link #getCurrentTool() #getCurrentTool()}.</li>
|
162 |
* <li>Get a registered tool: {@link #getMapTool(String) #getMapTool(String)}.</li>
|
163 |
* <li>Get the name of all tools registered: {@link #getMapToolsKeySet() #getMapToolsKeySet()}.</li>
|
164 |
* <li>Get all tools registered, including the name they were registered: {@link #getNamesMapTools() #getNamesMapTools()}.</li>
|
165 |
* <li>Determine if has a tool registered: {@link #hasTool(String) #hasTool(String)}.</li>
|
166 |
* <li>Set as an active tool, one of the registered: {@link #setTool(String) #setTool(String)}.</li>
|
167 |
* <li>Set as active tool, the previous used: {@link #setPrevTool() #setPrevTool()}.</li>
|
168 |
* <li>Set the current tool: {@link #setCurrentMapTool(Behavior) #setCurrentMapTool(Behavior)}.</li>
|
169 |
* <li>Change the draw frame rate: {@link #setDrawFrameRate(int) #setDrawFrameRate(int)} and {@link #applyFrameRate() #applyFrameRate()}.</li>
|
170 |
* <li>Get the draw frame rate: {@link #getDrawFrameRate() #getDrawFrameRate()}.</li>
|
171 |
* <li>Determine if will repaint this component each time timer finishes: {@link #isDrawAnimationEnabled() #isDrawAnimationEnabled()}.</li>
|
172 |
* <li>Change if will repaint this component each time timer finishes: {@link #setDrawAnimationEnabled(boolean) #setDrawAnimationEnabled(boolean)}.</li>
|
173 |
* <li>Get the shared object that determines if a drawing process must be cancelled or can continue: {@link #getCanceldraw() #getCanceldraw()}.</li>
|
174 |
* <li>Get the combined tool: {@link #getCombinedTool() #getCombinedTool()}.</li>
|
175 |
* <li>Set a combined tool: {@link #setCombinedTool(Behavior) #setCombinedTool(Behavior)}.</li>
|
176 |
* <li>Remove the combined tool: {@link #removeCombinedTool() #removeCombinedTool()}.</li>
|
177 |
* </ul>
|
178 |
* </p>
|
179 |
*
|
180 |
* <p><b>Exception listener:</b></p>
|
181 |
*
|
182 |
* <p> Adding an <code>ExceptionListener</code>, can get notification about any exception produced:
|
183 |
* <ul>
|
184 |
* <li>Attending a <i>painting request</i>.</li>
|
185 |
* <li>Working with the active tool.</li>
|
186 |
* <li>Applying a <i>zoom in</i> or <i>zoom out</i> operation.</li>
|
187 |
* </ul>
|
188 |
* </p>
|
189 |
*
|
190 |
* <p><b>Other:</b></p>
|
191 |
*
|
192 |
* <p>Other useful capabilities of <code>MapControl</code>:
|
193 |
* <ul>
|
194 |
* <li>Cancel the current drawing process (notifying it also to the inner
|
195 |
* <code>MapContext</code> instance and its layers): {@link #cancelDrawing() #cancelDrawing()}.</li>
|
196 |
* <li>Applying a <i>zoom in</i> operation centered at mouse position (without a <code>ToolListener</code>): {@link #zoomIn() #zoomIn()}.</li>
|
197 |
* <li>Applying a <i>zoom out</i> operation centered at mouse position (without a <code>ToolListener</code>): {@link #zoomOut() #zoomOut()}.</li>
|
198 |
* </ul>
|
199 |
* </p>
|
200 |
*
|
201 |
* @see CancelDraw
|
202 |
* @see Drawer
|
203 |
* @see MapContextListener
|
204 |
* @see MapToolListener
|
205 |
*
|
206 |
* @author Fernando Gonz?lez Cort?s
|
207 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
208 |
*/
|
209 |
public class MapControl extends JComponent implements ComponentListener, Observer { |
210 |
protected static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
211 |
protected static final Logger logger = LoggerFactory.getLogger(GeometryManager.class); |
212 |
|
213 |
/**
|
214 |
* <p>One of the possible status of <code>MapControl</code>. Determines that all visible information has been
|
215 |
* drawn and its updated.</p>
|
216 |
*/
|
217 |
public static final int ACTUALIZADO = 0; |
218 |
|
219 |
/**
|
220 |
* <p>One of the possible status of <code>MapControl</code>. Determines that not all visible information has been
|
221 |
* drawn or isn't updated.</p>
|
222 |
*/
|
223 |
public static final int DESACTUALIZADO = 1; |
224 |
|
225 |
/**
|
226 |
* <p>One of the possible status of <code>MapControl</code>. Determines that only the graphical layer must
|
227 |
* be drawn / updated.</p>
|
228 |
*/
|
229 |
public static final int ONLY_GRAPHICS = 2; |
230 |
|
231 |
/**
|
232 |
* <p>Determines if the drawer can update this <code>MapControl</code> instance when the timer launches an event.</p>
|
233 |
*/
|
234 |
private static boolean drawAnimationEnabled = true; |
235 |
|
236 |
/**
|
237 |
* <p>Inner model with the layers, event support for drawing them, and the <code>ViewPort</code>
|
238 |
* with information to adapt to the bounds available in <i>image coordinates</i>.</p>
|
239 |
*
|
240 |
* @see #getMapContext()
|
241 |
* @see #setMapContext(MapContext)
|
242 |
*/
|
243 |
private MapContext mapContext = null; |
244 |
|
245 |
/**
|
246 |
* <p>All registered <code>Behavior</code> that can define a way to work with this <code>MapControl</code>.</p>
|
247 |
*
|
248 |
* <p>Only one of them can be active at a given moment.</p>
|
249 |
*
|
250 |
* @see #addBehavior(String, Behavior)
|
251 |
* @see #addBehavior(String, Behavior[])
|
252 |
* @see #getMapTool(String)
|
253 |
* @see #getMapToolsKeySet()
|
254 |
* @see #getNamesMapTools()
|
255 |
*/
|
256 |
protected HashMap namesMapTools = new HashMap(); |
257 |
|
258 |
/**
|
259 |
* <p>Active {@link Behavior Behavior} that will generate events according a criterion, and then, with a {@link ToolListener ToolListener}
|
260 |
* associated, will simulate to user that works with this component as a particular tool.</p>
|
261 |
*
|
262 |
* @see #getCurrentMapTool()
|
263 |
* @see #getCurrentTool()
|
264 |
* @see #setTool(String)
|
265 |
*/
|
266 |
protected Behavior currentMapTool = null; |
267 |
|
268 |
/**
|
269 |
* <p>Determines which's the current drawn status of this component:
|
270 |
* <ul>
|
271 |
* <li><b>OUTDATED</b>: all visible information has been drawn or isn't updated.</li>
|
272 |
* <li><b>UTDATED</b>: all visible information has been drawn and its updated.</li>
|
273 |
* <li><b>ONLY_GRAPHICS</b>: only the graphical layer must be drawn / updated.</li>
|
274 |
* </ul>
|
275 |
* </p>
|
276 |
*
|
277 |
* <p>The <code>MapControl</code> drawing process will consider the value of this parameter to decide which elements will
|
278 |
* be updated or drawn.</p>
|
279 |
*/
|
280 |
private int status = DESACTUALIZADO; |
281 |
|
282 |
/**
|
283 |
* <p>Image with a buffer to accelerate the draw the changes of the graphical items in this component.</p>
|
284 |
*
|
285 |
* <p>Firstly, information will be drawn in the buffer, and, when is outright drawn, that information will be displayed.
|
286 |
* Meanwhile, the previous image can be kept showed.</p>
|
287 |
*
|
288 |
* @see BufferedImage
|
289 |
*
|
290 |
* @see #getImage()
|
291 |
*/
|
292 |
private BufferedImage image = null; |
293 |
|
294 |
/**
|
295 |
* <p>Name of the tool used currently to interact with this component.</p>
|
296 |
*
|
297 |
* @see #getCurrentTool()
|
298 |
* @see #setTool(String)
|
299 |
*/
|
300 |
protected String currentTool; |
301 |
|
302 |
/**
|
303 |
* <p>Object to store the flag that notifies a drawing thread task and <code>MapContext</code>'s layers,
|
304 |
* that must be canceled or can continue with the process.</p>
|
305 |
*
|
306 |
* @see #cancelDrawing()
|
307 |
*/
|
308 |
private CancelDraw canceldraw;
|
309 |
|
310 |
//private boolean isCancelled = true;
|
311 |
|
312 |
/**
|
313 |
* <p>Fires an action events after a specified delay.</p>
|
314 |
*
|
315 |
* <p><code>MapControl</code> will use the timer to update its visible graphical information during
|
316 |
* a drawing process, or allowing to cancel that process.</p>
|
317 |
*
|
318 |
* <p>This is very useful to pretend faster interactivity to user when <code>MapControl</code> has
|
319 |
* lots of layers, and / or layers with heavy graphical elements, that need a long time to finish
|
320 |
* drawing all its data.</p>
|
321 |
*/
|
322 |
private Timer timer; |
323 |
|
324 |
/**
|
325 |
* <p>Reference to the {@link ViewPort ViewPort} of the {@link MapContext MapContext} of this component.</p>
|
326 |
*
|
327 |
* <p>The view port once is created an instance of <code>MapControl</code>,
|
328 |
* is obtained from the <i>EPSG:23030</i> projection, that's the default projection for this component.</p>
|
329 |
*
|
330 |
* <p>After, the view port will change adapting itself according the current projection and the extent.</p>
|
331 |
*
|
332 |
* @see #getViewPort()
|
333 |
*
|
334 |
* @see ViewPort
|
335 |
*/
|
336 |
protected ViewPort vp;
|
337 |
|
338 |
/**
|
339 |
* <p>Manager of all <code>MapControl</code> painting requests.</p>
|
340 |
*/
|
341 |
private Drawer drawer;
|
342 |
|
343 |
/**
|
344 |
* <p>Listener of all kind of mouse events produced in this component.</p>
|
345 |
*
|
346 |
* <p>Delegates each mouse event to the current map tool.</p>
|
347 |
*
|
348 |
* @see #addBehavior(String, Behavior)
|
349 |
* @see #addBehavior(String, Behavior[])
|
350 |
* @see #getMapTool(String)
|
351 |
* @see #getMapToolsKeySet()
|
352 |
* @see #getNamesMapTools()
|
353 |
* @see #setTool(String)
|
354 |
*/
|
355 |
protected MapToolListener mapToolListener = new MapToolListener(); |
356 |
|
357 |
/**
|
358 |
* <p>Listener of all events produced in a this component's <code>MapContext</code>
|
359 |
* object during an atomic period of time.</p>
|
360 |
*/
|
361 |
private MapContextListener mapContextListener = new MapContextListener(); |
362 |
|
363 |
/**
|
364 |
* <p>Group of <code>ExceptionListener</code> that, in whatever moment could be notified a Throwable Java error or exception.</p>
|
365 |
*
|
366 |
* @see #addExceptionListener(ExceptionListener)
|
367 |
* @see #removeExceptionListener(ExceptionListener)
|
368 |
*/
|
369 |
private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport(); |
370 |
|
371 |
/**
|
372 |
* <p>Name of the previous tool used.</p>
|
373 |
*/
|
374 |
protected String prevTool; |
375 |
|
376 |
/**
|
377 |
* <p>Tool that will be used combined with the current tool of this <code>MapControl</code>.</p>
|
378 |
*/
|
379 |
private Behavior combinedTool = null; |
380 |
|
381 |
/**
|
382 |
* Optional grid that could be applied on the <code>MapControl</code>'s view port.
|
383 |
*
|
384 |
* @see #getGrid()
|
385 |
* @see #setAdjustGrid(boolean)
|
386 |
*/
|
387 |
private Grid cadgrid = new Grid(); |
388 |
/**
|
389 |
* Represents the cursor's point selected in <i>screen coordinates</i>.
|
390 |
*
|
391 |
* @see ViewPort#fromMapPoint(Point2D)
|
392 |
*/
|
393 |
private Point2D adjustedPoint; |
394 |
/**
|
395 |
* <p>Determines if the position of the snap of the mouse's cursor on the <code>MapControl</code>
|
396 |
* is within the area around a control point of a geometry.</p>
|
397 |
*
|
398 |
* <p>The area is calculated as a circle centered at the control point and with radius the pixels tolerance
|
399 |
* defined in the preferences.</p>
|
400 |
*/
|
401 |
private boolean bForceCoord = false; |
402 |
|
403 |
/**
|
404 |
* Kind of geometry drawn to identify the kind of control point selected by the cursor's mouse.
|
405 |
*/
|
406 |
private ISnapper usedSnap = null; |
407 |
|
408 |
/**
|
409 |
* Determines if the snap tools are enabled or disabled.
|
410 |
*
|
411 |
* @see #isRefentEnabled()
|
412 |
* @see #setRefentEnabled(boolean)
|
413 |
*/
|
414 |
private boolean bRefent = true; |
415 |
|
416 |
/**
|
417 |
* Stores the 2D map coordinates of the last point added.
|
418 |
*/
|
419 |
private double[] previousPoint = null; |
420 |
|
421 |
protected static MapControlManager mapControlManager = MapControlLocator.getMapControlManager(); |
422 |
|
423 |
private static TreeMap selected = new TreeMap(new Comparator(){ |
424 |
|
425 |
public int compare(Object o1, Object o2) { |
426 |
if (o1.getClass().equals(o2.getClass()))
|
427 |
return 0; |
428 |
if (((ISnapper)o1).getPriority()>((ISnapper)o2).getPriority())
|
429 |
return 1; |
430 |
else
|
431 |
return -1; |
432 |
} |
433 |
|
434 |
}); |
435 |
|
436 |
/**
|
437 |
* Represents the cursor's point selected in <i>map coordinates</i>.
|
438 |
*
|
439 |
* @see MapControl#toMapPoint
|
440 |
*/
|
441 |
private Point2D mapAdjustedPoint; |
442 |
|
443 |
/**
|
444 |
* Renderer used to draw the layers.
|
445 |
*/
|
446 |
private MapControlDrawer mapControlDrawer = null; |
447 |
|
448 |
/**
|
449 |
* <p>Creates a new <code>MapControl</code> instance with the following characteristics:
|
450 |
* <ul>
|
451 |
* <li><i>Name</i>: MapControl .</li>
|
452 |
* <li>Disables the double buffer of <code>JComponent</code> .</li>
|
453 |
* <li>Sets opaque <i>(see {@link JComponent#setOpaque(boolean)} )</i>. </li>
|
454 |
* <li>Sets its status to <code>OUTDATED</code> .</li>
|
455 |
* <li>Creates a new {@link CancelDraw CancelDraw} object to notify <code>MapContext</code>'s layers if can continue processing the drawn or must cancel it.</li>
|
456 |
* <li>Creates a new {@link MapContext MapContext} with a new {@link ViewPort ViewPort} in the projection <i>"EPSG:23030"</i> .</li>
|
457 |
* <li>Creates a new {@link CommandListener CommandListener} for edition operations.</li>
|
458 |
* <li>Creates a new {@link MapToolListener MapToolListener}, and associates it as a listener of whatever kind of mouse events produced in this component.</li>
|
459 |
* <li>Creates a new {@link Drawer2 Drawer2} for managing the painting requests.</li>
|
460 |
* <li>Creates a new timer that will invoke refresh this component <code>drawFrameRate</code> per second, when is running a drawing process, and its enabled
|
461 |
* <code>drawAnimationEnabled</code>.</li>
|
462 |
* </ul>
|
463 |
* </p>
|
464 |
*/
|
465 |
public MapControl() {
|
466 |
this.setName("MapControl"); |
467 |
setDoubleBuffered(false);
|
468 |
setOpaque(true);
|
469 |
status = DESACTUALIZADO; |
470 |
|
471 |
//Clase usada para cancelar el dibujado
|
472 |
canceldraw = new CancelDraw();
|
473 |
|
474 |
//Modelo de datos y ventana del mismo
|
475 |
// TODO: Cuando creamos un mapControl, deber?amos asignar
|
476 |
// la projecci?n por defecto con la que vayamos a trabajar.
|
477 |
// 23030 es el c?digo EPSG del UTM30 elipsoide ED50
|
478 |
vp = new ViewPort(CRSFactory.getCRS("EPSG:23030")); |
479 |
setMapContext(new MapContext(vp));
|
480 |
|
481 |
//eventos
|
482 |
this.addComponentListener(this); |
483 |
this.addMouseListener(mapToolListener);
|
484 |
this.addMouseMotionListener(mapToolListener);
|
485 |
this.addMouseWheelListener(mapToolListener);
|
486 |
|
487 |
this.drawer = new Drawer(); |
488 |
//Timer para mostrar el redibujado mientras se dibuja
|
489 |
timer = new Timer(1000/MapContext.getDrawFrameRate(), |
490 |
new ActionListener() { |
491 |
public void actionPerformed(ActionEvent e) { |
492 |
|
493 |
if (drawAnimationEnabled) {
|
494 |
MapControl.this.repaint(); |
495 |
} |
496 |
} |
497 |
}); |
498 |
initializeGrid(); |
499 |
} |
500 |
|
501 |
/**
|
502 |
* <p>Sets a <code>MapContext</code> to this component.</p>
|
503 |
*
|
504 |
* <p>The <code>MapContext</code> has the <i>model</i>, and most of the <i>view</i>,
|
505 |
* and <i>control</i> logic of the layers of this component, including a {@link ViewPort ViewPort} to adapt the
|
506 |
* information to the projection, and to display it in the available area.</p>
|
507 |
*
|
508 |
* <p>If <code>model</code> hadn't a <code>ViewPort</code>, assigns the current one to it, otherwise, use its <code>ViewPort</code>.</p>
|
509 |
*
|
510 |
* <p>After assigning the <code>MapContext</code> and <code>ViewPort</code>, sets the same {@link MapContextListener MapContextListener}
|
511 |
* that was using, and changes the <i>status</i> to <code>OUTDATED</code>.</p>
|
512 |
*
|
513 |
* @param model this component's <code>MapContext</code>, that includes the <code>ViewPort</code>.
|
514 |
*
|
515 |
* @see MapContext
|
516 |
*
|
517 |
* @see #getMapContext()
|
518 |
*/
|
519 |
public void setMapContext(MapContext model) { |
520 |
if (mapContext != null) { |
521 |
mapContext.removeAtomicEventListener(mapContextListener); |
522 |
} |
523 |
|
524 |
mapContext = model; |
525 |
|
526 |
if (mapContext.getViewPort() == null) { |
527 |
mapContext.setViewPort(vp); |
528 |
} else {
|
529 |
vp = mapContext.getViewPort(); |
530 |
cadgrid.setViewPort(vp); |
531 |
} |
532 |
|
533 |
mapContext.addAtomicEventListener(mapContextListener); |
534 |
|
535 |
status = DESACTUALIZADO; |
536 |
} |
537 |
|
538 |
/**
|
539 |
* @return the mapControlDrawer
|
540 |
*/
|
541 |
public MapControlDrawer getMapControlDrawer() {
|
542 |
return mapControlDrawer;
|
543 |
} |
544 |
|
545 |
/**
|
546 |
* @param mapControlDrawer the mapControlDrawer to set
|
547 |
*/
|
548 |
public void setMapControlDrawer(MapControlDrawer mapControlDrawer) { |
549 |
this.mapControlDrawer = mapControlDrawer;
|
550 |
this.mapControlDrawer.setViewPort(vp);
|
551 |
} |
552 |
|
553 |
/**
|
554 |
* <p>Gets this component's {@link MapContext MapContext} projection.</p>
|
555 |
*
|
556 |
* @return this component's {@link MapContext MapContext} projection
|
557 |
*
|
558 |
* @see MapContext#getProjection()
|
559 |
* @see MapControl#setProjection(IProjection)
|
560 |
*/
|
561 |
public IProjection getProjection() {
|
562 |
return getMapContext().getProjection();
|
563 |
} |
564 |
|
565 |
/**
|
566 |
* <p>Sets the projection to this component's {@link MapContext MapContext}.</p>
|
567 |
*
|
568 |
* @param proj the kind of projection to this component's {@link MapContext MapContext}
|
569 |
*
|
570 |
* @see MapContext#setProjection(IProjection)
|
571 |
* @see MapControl#getProjection()
|
572 |
*/
|
573 |
public void setProjection(IProjection proj) { |
574 |
getMapContext().setProjection(proj); |
575 |
} |
576 |
|
577 |
/**
|
578 |
* <p>Gets this component's <code>MapContext</code>, with the <i>model</i>, and most of the <i>view</i>,
|
579 |
* and <i>control</i> logic of the layers of this component, including a {@link ViewPort ViewPort} to adapt the
|
580 |
* information to the projection, and display it in the available area.</p>
|
581 |
*
|
582 |
* @return this component's <code>MapContext</code>, that includes the <code>ViewPort</code> used to project the
|
583 |
* graphical information, and display it in the available area
|
584 |
*
|
585 |
* @see MapContext
|
586 |
*
|
587 |
* @see MapControl#setMapContext(MapContext)
|
588 |
*/
|
589 |
public MapContext getMapContext() {
|
590 |
return mapContext;
|
591 |
} |
592 |
|
593 |
/**
|
594 |
* <p>Registers a new behavior to this component.</p>
|
595 |
*
|
596 |
* <p>According the nature of the {@link Behavior Behavior}, different events will be generated. Those
|
597 |
* events can be caught by a particular {@link ToolListener ToolListener}, allowing user to interact with this
|
598 |
* <code>MapControl</code> object as a <i>tool</i>.</p>
|
599 |
*
|
600 |
* @param name name to identify the behavior to add
|
601 |
* @param tool the behavior to add
|
602 |
*
|
603 |
* @see #addBehavior(String, Behavior[])
|
604 |
* @see #getNamesMapTools()
|
605 |
* @see #getMapToolsKeySet()
|
606 |
* @see #hasTool(String)
|
607 |
*/
|
608 |
public void addBehavior(String name, Behavior tool) { |
609 |
namesMapTools.put(name, tool); |
610 |
tool.setMapControl(this);
|
611 |
} |
612 |
|
613 |
/**
|
614 |
* <p>Registers a new behavior to this component as a {@link CompoundBehavior CompoundBehavior} made up of <code>tools</code>.</p>
|
615 |
*
|
616 |
* <p>According the nature of the behaviors registered, different events will be generated. Those
|
617 |
* events can be caught by a particular {@link ToolListener ToolListener}, allowing user to interact with this
|
618 |
* <code>MapControl</code> object as a <i>tool</i>.</p>
|
619 |
*
|
620 |
* @param name name to identify the compound behavior to add
|
621 |
* @param tools the compound behavior to add
|
622 |
*
|
623 |
* @see #addBehavior(String, Behavior)
|
624 |
* @see #getNamesMapTools()
|
625 |
* @see #getMapToolsKeySet()
|
626 |
* @see #hasTool(String)
|
627 |
*/
|
628 |
public void addBehavior(String name, Behavior[] tools){ |
629 |
CompoundBehavior tool = new CompoundBehavior(tools);
|
630 |
addBehavior(name, tool); |
631 |
} |
632 |
|
633 |
/**
|
634 |
* <p>Gets the <code>Behavior</code> registered in this component, identified
|
635 |
* by <code>name</code>.</p>
|
636 |
*
|
637 |
* @param name name of a registered behavior
|
638 |
*
|
639 |
* @return tool the registered behavior in this component as <code>name</code>, or <code>null</code> if
|
640 |
* no one has that identifier
|
641 |
*
|
642 |
* @see #addBehavior(String, Behavior)
|
643 |
* @see #addBehavior(String, Behavior[])
|
644 |
* @see #hasTool(String)
|
645 |
*/
|
646 |
public Behavior getMapTool(String name) { |
647 |
return (Behavior)namesMapTools.get(name);
|
648 |
} |
649 |
|
650 |
/**
|
651 |
* <p>Returns a set view of the keys that identified the tools
|
652 |
* registered.</p>
|
653 |
*
|
654 |
* @return a set view of the keys that identified the tools registered
|
655 |
*
|
656 |
* @see HashMap#keySet()
|
657 |
*
|
658 |
* @see #getNamesMapTools()
|
659 |
* @see #addBehavior(String, Behavior)
|
660 |
* @see #addBehavior(String, Behavior[])
|
661 |
*/
|
662 |
public Set getMapToolsKeySet() { |
663 |
return namesMapTools.keySet();
|
664 |
} |
665 |
|
666 |
/**
|
667 |
* <p>Returns <code>true</code> if this component contains a tool identified by <code>toolName</code>.</p>
|
668 |
*
|
669 |
* @param toolName identifier of the tool
|
670 |
*
|
671 |
* @return <code>true</code> if this component contains a tool identified by <code>toolName</code>; otherwise <code>false</code>
|
672 |
*
|
673 |
* @see #addBehavior(String, Behavior)
|
674 |
* @see #addBehavior(String, Behavior[])
|
675 |
*/
|
676 |
public boolean hasTool(String toolName) { |
677 |
return namesMapTools.containsKey(toolName);
|
678 |
} |
679 |
|
680 |
/**
|
681 |
* <p>Sets as current active <code>Behavior</code> associated to this component, that one which
|
682 |
* is registered and identified by <code>toolName</code>.</p>
|
683 |
*
|
684 |
* <p>Changing the current active behavior for this <code>MapControl</code>, implies also updating the
|
685 |
* previous <i>behavior</i> tool, and the current cursor.</p>
|
686 |
*
|
687 |
* @param toolName name of a registered behavior
|
688 |
*
|
689 |
* @see #getCurrentMapTool()
|
690 |
* @see #getCurrentTool()
|
691 |
*/
|
692 |
public void setTool(String toolName) { |
693 |
prevTool=getCurrentTool(); |
694 |
Behavior mapTool = (Behavior) namesMapTools.get(toolName); |
695 |
currentMapTool = mapTool; |
696 |
currentTool = toolName; |
697 |
|
698 |
if (combinedTool != null) { |
699 |
if (mapTool instanceof CompoundBehavior) { |
700 |
((CompoundBehavior)mapTool).addMapBehavior(combinedTool, true);
|
701 |
} |
702 |
else {
|
703 |
currentMapTool = new CompoundBehavior(new Behavior[] {currentMapTool}); |
704 |
((CompoundBehavior)currentMapTool).addMapBehavior(combinedTool, true);
|
705 |
} |
706 |
} |
707 |
|
708 |
// this.setCursor(mapTool.getCursor());
|
709 |
} |
710 |
|
711 |
/**
|
712 |
* <p>Gets as current active <code>Behavior</code> associated to this component, that one which
|
713 |
* is registered and identified by <code>toolName</code>.</p>
|
714 |
*
|
715 |
* <p>Changing the current active behavior for this <code>MapControl</code>, implies also updating the
|
716 |
* previous <i>behavior</i> tool, and the current cursor.</p>
|
717 |
*
|
718 |
* @param toolName name of a registered behavior
|
719 |
*
|
720 |
* @see #getCurrentTool()
|
721 |
* @see #setTool(String)
|
722 |
*/
|
723 |
public Behavior getCurrentMapTool(){
|
724 |
return currentMapTool;
|
725 |
} |
726 |
|
727 |
/**
|
728 |
* <p>Returns the name of the current selected tool on this MapControl</p>
|
729 |
*
|
730 |
* @return the name of the current's behavior tool associated to this component
|
731 |
*
|
732 |
* @see #getCurrentMapTool()
|
733 |
* @see #setTool(String)
|
734 |
*/
|
735 |
public String getCurrentTool() { |
736 |
return currentTool;
|
737 |
} |
738 |
|
739 |
/**
|
740 |
* <p>Determines that current drawing process of <code>MapControl</code>'s <code>MapContext</code>'s data must be canceled.</p>
|
741 |
*
|
742 |
* <p>It has no effects if now isn't drawing that graphical information.</p>
|
743 |
*
|
744 |
* <p>At last resort, the particular implementation of each layer in this <code>MapControl</code>'s <code>MapContrext</code>
|
745 |
* will be that one which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</p>
|
746 |
*/
|
747 |
public void cancelDrawing() { |
748 |
/* if (drawer != null) {
|
749 |
if (!drawer.isAlive()) {
|
750 |
return;
|
751 |
}
|
752 |
}
|
753 |
*/
|
754 |
canceldraw.setCanceled(true);
|
755 |
|
756 |
/* while (!isCancelled) {
|
757 |
if (!drawer.isAlive()) {
|
758 |
// Si hemos llegado aqu? con un thread vivo, seguramente
|
759 |
// no estamos actualizados.
|
760 |
|
761 |
break;
|
762 |
}
|
763 |
|
764 |
}
|
765 |
canceldraw.setCancel(false);
|
766 |
isCancelled = false;
|
767 |
drawerAlive = false; */
|
768 |
} |
769 |
|
770 |
/**
|
771 |
* <p>Creates a {@link BufferedImage BufferedImage} image if there was no buffered image, or if
|
772 |
* its viewport's image height or width is different from this component's size. Once has created
|
773 |
* a double-buffer, fills it with the vieport's background color, or with <i>white</i> if it had no background color.</p>
|
774 |
*
|
775 |
* <p>If no double-buffered existed, creates a {@link BufferedImage BufferedImage} with the size of this component,
|
776 |
* and as an image with 8-bit RGBA color components packed into integer pixels. That image has a <code>DirectColorModel</code> with alpha.
|
777 |
* The color data in that image is considered not to be premultiplied with alpha.</p>
|
778 |
*
|
779 |
* <p>Once has created and filled the new inner <code>MapControl</code>'s double-buffer, changes the status to
|
780 |
* <code>OUTDATED</code>.</p>
|
781 |
*
|
782 |
* @return <code>true</code> if has created and filled a new double-buffer for this <code>MapControl</code> instance; otherwise <code>false</code>
|
783 |
*/
|
784 |
private boolean adaptToImageSize() |
785 |
{ |
786 |
if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight())) |
787 |
{ |
788 |
image = new BufferedImage(this.getWidth(), this.getHeight(), |
789 |
BufferedImage.TYPE_INT_ARGB);
|
790 |
// ESTILO MAC
|
791 |
// image = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
792 |
// .getDefaultScreenDevice().getDefaultConfiguration()
|
793 |
// .createCompatibleImage(this.getWidth(), this.getHeight());
|
794 |
vp.setImageSize(new Dimension(getWidth(), getHeight())); |
795 |
getMapContext().getViewPort().refreshExtent(); |
796 |
|
797 |
|
798 |
Graphics gTemp = image.createGraphics();
|
799 |
Color theBackColor = vp.getBackColor();
|
800 |
if (theBackColor == null) { |
801 |
gTemp.setColor(Color.WHITE);
|
802 |
} else {
|
803 |
gTemp.setColor(theBackColor); |
804 |
} |
805 |
|
806 |
gTemp.fillRect(0,0,getWidth(), getHeight()); |
807 |
gTemp.dispose(); |
808 |
status = DESACTUALIZADO; |
809 |
// g.drawImage(image,0,0,null);
|
810 |
return true; |
811 |
} |
812 |
return false; |
813 |
} |
814 |
|
815 |
/**
|
816 |
* <p>Paints the graphical information of this component using a double buffer.</p>
|
817 |
*
|
818 |
* <p>If the double buffer wasn't created, creates a new one.</p>
|
819 |
*
|
820 |
* <p>Paints the component according the following algorithm:
|
821 |
* <br>
|
822 |
*   If <i>status</i> is <i>UPDATED</i>:<br>
|
823 |
*     If there is no <i>double buffer</i>:<br>
|
824 |
*       If there is a <i>behavior</i> for managing the <code>MapControl</code> instance, delegates
|
825 |
* the drawing process to that behavior, calling: <code><i>behavior_instance</i>.paintComponent(g)</code>   .<br>
|
826 |
*       Else, repaints the current graphical information quickly calling: <code>g.drawImage(image,0,0,null)</code>   .<br>
|
827 |
*   Else, (<i>status</i> is <i>OUTDATED</i>, or <i>ONLY_GRAPHICS</i>): executes a quickly repaint of the previous information calling <code>g.drawImage(image,0,0,null)</code>, and creates
|
828 |
* a <i>painting request</i> to delegate the heavy drawing process to the {@link Drawer2 Drawer2}'s worker thread, according the <i>SingleWorketThread</i> pattern, starting a timer to update
|
829 |
* (invoking <code>repaint()</code> that comprises invoke this method) the view every delay of 360 ms. during the the process drawing.</p>
|
830 |
*
|
831 |
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
832 |
* @see Drawer2
|
833 |
*/
|
834 |
protected void paintComponent(Graphics g) { |
835 |
adaptToImageSize(); |
836 |
|
837 |
try {
|
838 |
mapControlDrawer.startDrawing(this);
|
839 |
} catch (InterruptedException e) { |
840 |
logger.error("Error locking the MapControlDrawer", e);
|
841 |
} |
842 |
mapControlDrawer.setGraphics(g); |
843 |
mapControlDrawer.stopDrawing(this);
|
844 |
mapControlDrawer.setViewPort(getMapContext().getViewPort()); |
845 |
|
846 |
if (status == ACTUALIZADO) {
|
847 |
/*
|
848 |
* Si hay un behaviour y la imagen es distinta de null se delega el dibujado
|
849 |
* en dicho behaviour
|
850 |
*/
|
851 |
if (image != null) |
852 |
{ |
853 |
if (currentMapTool != null) { |
854 |
currentMapTool.paintComponent(mapControlDrawer); |
855 |
} else {
|
856 |
mapControlDrawer.drawImage(image,0,0); |
857 |
} |
858 |
} |
859 |
} else if ((status == DESACTUALIZADO) |
860 |
|| (status == ONLY_GRAPHICS)) { |
861 |
|
862 |
mapControlDrawer.drawImage(image,0,0); |
863 |
|
864 |
drawer.put(new PaintingRequest());
|
865 |
timer.start(); |
866 |
} |
867 |
cadgrid.drawGrid(mapControlDrawer); |
868 |
drawCursor(); |
869 |
} |
870 |
|
871 |
/**
|
872 |
* <p>Gets the {@link BufferedImage BufferedImage} used to accelerate the draw of new ''frames'' with changes,
|
873 |
* or new graphical items in this component.</p>
|
874 |
*
|
875 |
* @return double buffered image used by this component to accelerate the draw of its graphical information, or
|
876 |
* <code>null</code> if isn't already created
|
877 |
*
|
878 |
* @see BufferedImage
|
879 |
*/
|
880 |
public BufferedImage getImage() { |
881 |
return image;
|
882 |
} |
883 |
|
884 |
/**
|
885 |
* <p>Forces repaint all visible graphical information in this component.</p>
|
886 |
*
|
887 |
* <p>If <code>doClear == true</code>, before repainting, clears the background color, with the
|
888 |
* inner viewport's background color.</p>
|
889 |
*
|
890 |
* @param doClear <code>true</code> if needs clearing the background color before drawing the map
|
891 |
*
|
892 |
* @see #cancelDrawing()
|
893 |
* @see FLayers#setDirty(boolean)
|
894 |
*/
|
895 |
public void drawMap(boolean doClear) { |
896 |
cancelDrawing(); |
897 |
//System.out.println("drawMap con doClear=" + doClear);
|
898 |
status = DESACTUALIZADO; |
899 |
if (doClear)
|
900 |
{ |
901 |
// image = null; // Se usa para el PAN
|
902 |
if (image != null) |
903 |
{ |
904 |
Graphics2D g = image.createGraphics();
|
905 |
Color theBackColor = vp.getBackColor();
|
906 |
if (theBackColor == null) { |
907 |
g.setColor(Color.WHITE);
|
908 |
} else {
|
909 |
g.setColor(theBackColor); |
910 |
} |
911 |
g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight()); |
912 |
g.dispose(); |
913 |
} |
914 |
} |
915 |
repaint(); |
916 |
} |
917 |
|
918 |
|
919 |
/**
|
920 |
* <p>Cancels any current drawing process, changing the status to <code>OUTDATED</code>, and forcing
|
921 |
* repaint only the layers dirty.</p>
|
922 |
*
|
923 |
* @see #cancelDrawing()
|
924 |
*/
|
925 |
public void rePaintDirtyLayers() |
926 |
{ |
927 |
cancelDrawing(); |
928 |
status = DESACTUALIZADO; |
929 |
repaint(); |
930 |
} |
931 |
|
932 |
/**
|
933 |
* <p>Cancels any current drawing process, changing the status to <code>ONLY_GRAPHICS</code>, and forcing
|
934 |
* repaint only the graphical layer of the <code>MapContext</code>.</p>
|
935 |
*/
|
936 |
public void drawGraphics() { |
937 |
status = ONLY_GRAPHICS; |
938 |
repaint(); |
939 |
} |
940 |
|
941 |
/**
|
942 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
943 |
*/
|
944 |
public void componentHidden(ComponentEvent e) { |
945 |
} |
946 |
|
947 |
/**
|
948 |
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
949 |
*/
|
950 |
public void componentMoved(ComponentEvent e) { |
951 |
} |
952 |
|
953 |
/**
|
954 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
955 |
*/
|
956 |
public void componentResized(ComponentEvent e) { |
957 |
/* image = new BufferedImage(this.getWidth(), this.getHeight(),
|
958 |
BufferedImage.TYPE_INT_ARGB);
|
959 |
Graphics gTemp = image.createGraphics();
|
960 |
gTemp.setColor(vp.getBackColor());
|
961 |
gTemp.fillRect(0,0,getWidth(), getHeight());
|
962 |
System.out.println("MapControl resized");
|
963 |
// image = null;
|
964 |
vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
965 |
getMapContext().getViewPort().setScale(); */
|
966 |
// drawMap(true);
|
967 |
} |
968 |
|
969 |
/**
|
970 |
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
971 |
*/
|
972 |
public void componentShown(ComponentEvent e) { |
973 |
} |
974 |
|
975 |
/**
|
976 |
* @see ExceptionHandlingSupport#addExceptionListener(ExceptionListener)
|
977 |
*/
|
978 |
public void addExceptionListener(ExceptionListener o) { |
979 |
exceptionHandlingSupport.addExceptionListener(o); |
980 |
} |
981 |
|
982 |
/**
|
983 |
* @see ExceptionHandlingSupport#removeExceptionListener(ExceptionListener)
|
984 |
*/
|
985 |
public boolean removeExceptionListener(ExceptionListener o) { |
986 |
return exceptionHandlingSupport.removeExceptionListener(o);
|
987 |
} |
988 |
|
989 |
/**
|
990 |
* @see ExceptionHandlingSupport#throwException(Throwable)
|
991 |
*/
|
992 |
protected void throwException(Throwable t) { |
993 |
exceptionHandlingSupport.throwException(t); |
994 |
} |
995 |
|
996 |
/**
|
997 |
* <p>Represents each <code>MapControl</code>'s data painting request.</p>
|
998 |
*
|
999 |
* <p>The request will be attended by a <code>Drawer2</code>, which will hold it since the <code>Drawer2</code>'s worker
|
1000 |
* takes it, or arrives a new painting request, which will replace it.</p>
|
1001 |
*/
|
1002 |
private class PaintingRequest |
1003 |
{ |
1004 |
/**
|
1005 |
* <p>Creates a new <code>PaintingRequest</p> instance.</p>
|
1006 |
*/
|
1007 |
public PaintingRequest()
|
1008 |
{ |
1009 |
} |
1010 |
|
1011 |
/**
|
1012 |
* <p><code>MapControl</code> paint process:</p>
|
1013 |
*
|
1014 |
* <p>
|
1015 |
* <ul>
|
1016 |
* <li><i>1.- </i>Cancels all previous <code>MapControl</code>'s drawing processes.</li>
|
1017 |
* <li><i>2.- </i>If <i>status</i> was OUTDATED:
|
1018 |
* <ul>
|
1019 |
* <li><i>2.1.- </i>Fills the background color with viewport's background color, or <i>white</i> if it was undefined.</li>
|
1020 |
* <li><i>2.2.- </i>Notifies <i>MapContext</i> to be drawn invoking: <code>mapContext.draw(double-buffer, double-buffer's buffer, shared cancel-draw object, mapContext.getScaleView());</code>.</li>
|
1021 |
* <li><i>2.3.- </i>If <code>canceldraw.isCanceled()</code>
|
1022 |
* <ul>
|
1023 |
* <li><i>2.3.1.- </i>Sets <i>status</i> to OUTDATED.</li>
|
1024 |
* <li><i>2.3.2.- </i>Sets <i>dirty</i> all layers stored in <i>MapContext</i>.</li>
|
1025 |
* </ul>
|
1026 |
* </li>
|
1027 |
* <li><i>2.4.- </i>Else, sets <i>status</i> to UPDATED.</li>
|
1028 |
* </ul>
|
1029 |
* </li>
|
1030 |
* <li><i>3.- </i>Else, if <i>status</i> was ONLY_GRAPHICS:
|
1031 |
* <ul>
|
1032 |
* <li><i>3.1.- </i>Sets <i>status</i> to UPDATED.</li>
|
1033 |
* <li><i>3.2.- </i>Notifies <i>MapContext</i> to be drawn invoking: <code>mapContext.drawGraphics(double-buffer, double-buffer's buffer, shared cancel-draw object, mapContext.getScaleView());</code>.</li>
|
1034 |
* </ul>
|
1035 |
* </li>
|
1036 |
* <li><i>4.- </i>Stops the <i>timer</i>.</li>
|
1037 |
* <li><i>5.- </i>Repaints this component invoking: <code>repaint();</code></li>
|
1038 |
* </ul>
|
1039 |
* </p>
|
1040 |
*
|
1041 |
* @see #cancelDrawing()
|
1042 |
* @see MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)
|
1043 |
* @see MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)
|
1044 |
*
|
1045 |
* @see ViewPort
|
1046 |
*/
|
1047 |
public void paint() |
1048 |
{ |
1049 |
try
|
1050 |
{ |
1051 |
canceldraw.setCanceled(false);
|
1052 |
/* if (image == null)
|
1053 |
{
|
1054 |
image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
|
1055 |
BufferedImage.TYPE_INT_ARGB);
|
1056 |
Graphics gTemp = image.createGraphics();
|
1057 |
Color theBackColor = vp.getBackColor();
|
1058 |
if (theBackColor == null)
|
1059 |
gTemp.setColor(Color.WHITE);
|
1060 |
else
|
1061 |
gTemp.setColor(theBackColor);
|
1062 |
|
1063 |
gTemp.fillRect(0,0,getWidth(), getHeight());
|
1064 |
gTemp.dispose();
|
1065 |
// g.drawImage(image,0,0,null);
|
1066 |
System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
|
1067 |
} */
|
1068 |
Graphics2D g = image.createGraphics();
|
1069 |
|
1070 |
ViewPort viewPort = mapContext.getViewPort(); |
1071 |
|
1072 |
if (status == DESACTUALIZADO)
|
1073 |
{ |
1074 |
Graphics2D gTemp = image.createGraphics();
|
1075 |
Color theBackColor = viewPort.getBackColor();
|
1076 |
if (theBackColor == null) { |
1077 |
gTemp.setColor(Color.WHITE);
|
1078 |
} else {
|
1079 |
gTemp.setColor(theBackColor); |
1080 |
} |
1081 |
gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight()); |
1082 |
// ESTILO MAC
|
1083 |
// BufferedImage imgMac = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
|
1084 |
// BufferedImage.TYPE_INT_ARGB);
|
1085 |
//
|
1086 |
// mapContext.draw(imgMac, g, canceldraw, mapContext.getScaleView());
|
1087 |
// g.drawImage(imgMac, 0, 0, null);
|
1088 |
// FIN ESTILO MAC
|
1089 |
// SIN MAC:
|
1090 |
|
1091 |
mapContext.draw(image, g, canceldraw, mapContext.getScaleView()); |
1092 |
if (!canceldraw.isCanceled()){
|
1093 |
status=ACTUALIZADO; |
1094 |
}else{
|
1095 |
status=DESACTUALIZADO; |
1096 |
// getMapContext().getLayers().setDirty(true);
|
1097 |
} |
1098 |
|
1099 |
} |
1100 |
else if (status == ONLY_GRAPHICS) |
1101 |
{ |
1102 |
status = ACTUALIZADO; |
1103 |
mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView()); |
1104 |
|
1105 |
} |
1106 |
|
1107 |
|
1108 |
|
1109 |
// status = FAST_PAINT;
|
1110 |
// drawerAlive = false;
|
1111 |
timer.stop(); |
1112 |
repaint(); |
1113 |
|
1114 |
|
1115 |
} catch (Throwable e) { |
1116 |
timer.stop(); |
1117 |
// isCancelled = true;
|
1118 |
e.printStackTrace(); |
1119 |
throwException(e); |
1120 |
} finally {
|
1121 |
} |
1122 |
} |
1123 |
} |
1124 |
|
1125 |
/**
|
1126 |
* <p>An instance of <code>Drawer2</code> could manage all <code>MapControl</code> painting requests.</p>
|
1127 |
*
|
1128 |
* <p>Based on the <i>WorkerThread</i> software pattern, creates a worker thread that will attend sequentially
|
1129 |
* the current waiting painting request, after finishing the previous (that could be by a cancel action).</p>
|
1130 |
*
|
1131 |
* <p>All new {@link PaintingRequest PaintingRequest} generated will be stored as <i>waiting requests</i> since the worker
|
1132 |
* attends it.</p>
|
1133 |
*
|
1134 |
* <p>If a worker finished and there was no <i>painting request</i>, the worker would be set to wait until any
|
1135 |
* <i>painting request</i> would be put.</p>
|
1136 |
*
|
1137 |
* @author fjp
|
1138 |
*/
|
1139 |
public class Drawer |
1140 |
{ |
1141 |
// Una mini cola de 2. No acumulamos peticiones de dibujado
|
1142 |
// dibujamos solo lo ?ltimo que nos han pedido.
|
1143 |
|
1144 |
|
1145 |
/**
|
1146 |
* <p>Painting request that's been attended by the <code>Drawer2</code>'s worker.</p>
|
1147 |
*
|
1148 |
* @see #put(org.gvsig.fmap.mapcontrol.MapControl.PaintingRequest)
|
1149 |
* @see #take()
|
1150 |
*/
|
1151 |
private PaintingRequest paintingRequest;
|
1152 |
|
1153 |
/**
|
1154 |
* <p>Painting request waiting to be attended by the <code>Drawer2</code>'s worker.</p>
|
1155 |
*
|
1156 |
* @see #put(org.gvsig.fmap.mapcontrol.MapControl.PaintingRequest)
|
1157 |
* @see #take()
|
1158 |
*/
|
1159 |
private PaintingRequest waitingRequest;
|
1160 |
|
1161 |
/**
|
1162 |
* <p>Determines that the <code>Drawer2</code>'s worker is busy attending a painting request.</p>
|
1163 |
*
|
1164 |
* @see #put(org.gvsig.fmap.mapcontrol.MapControl.PaintingRequest)
|
1165 |
* @see #take()
|
1166 |
*/
|
1167 |
private boolean waiting; |
1168 |
|
1169 |
/**
|
1170 |
* <p>Notifies the <code>Drawer2</code>'s worker to finish or continue with its process.</p>
|
1171 |
*
|
1172 |
* @see #setShutdown(boolean)
|
1173 |
*/
|
1174 |
private boolean shutdown; |
1175 |
|
1176 |
/**
|
1177 |
* <p>Sets this <code>Drawer2</code>'s worker to finish or continue with its process.</p>
|
1178 |
*
|
1179 |
* @param isShutdown a boolean value
|
1180 |
*/
|
1181 |
public void setShutdown(boolean isShutdown) |
1182 |
{ |
1183 |
shutdown = isShutdown; |
1184 |
} |
1185 |
|
1186 |
/**
|
1187 |
* <p>Creates a new drawer for managing all data painting requests in <code>MapControl</code>.</p>
|
1188 |
*
|
1189 |
* <p>Includes the following steps:
|
1190 |
* <ul>
|
1191 |
* <li>By default, there is no <i>current painting request</i>.</li>
|
1192 |
* <li>By default, there is no <i>waiting painting request</i>.</li>
|
1193 |
* <li>By default, the worker thread is waiting no <i>painting request</i>.</li>
|
1194 |
* <li>By default, the worker thread is running.</li>
|
1195 |
* <li>Creates and starts a worker thread for attending the <i>painting requests</i>.</li>
|
1196 |
* </ul>
|
1197 |
* </p>
|
1198 |
*/
|
1199 |
public Drawer()
|
1200 |
{ |
1201 |
paintingRequest = null;
|
1202 |
waitingRequest = null;
|
1203 |
waiting = false;
|
1204 |
shutdown = false;
|
1205 |
new Thread(new Worker()).start(); |
1206 |
} |
1207 |
|
1208 |
/**
|
1209 |
* <p>Sets a <code>PaintingRequest</code> to be attended by the worker thread of this object. If
|
1210 |
* this one was waiting, wakes up.</p>
|
1211 |
*
|
1212 |
* <p>All waiting threads will be notified synchronized.</p>
|
1213 |
*
|
1214 |
* @param newPaintRequest
|
1215 |
*
|
1216 |
* @see #take()
|
1217 |
*/
|
1218 |
public void put(PaintingRequest newPaintRequest) |
1219 |
{ |
1220 |
waitingRequest = newPaintRequest; |
1221 |
if (waiting)
|
1222 |
{ |
1223 |
synchronized (this) { |
1224 |
notifyAll(); |
1225 |
} |
1226 |
} |
1227 |
} |
1228 |
|
1229 |
/**
|
1230 |
* <p>Used by this object's worker, returns the current waiting drawing request, causing current thread
|
1231 |
* to wait until another thread invokes {@link #put(org.gvsig.fmap.mapcontrol.MapControl.PaintingRequest) #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)},
|
1232 |
* if there was no waiting request.</p>
|
1233 |
*
|
1234 |
* <p>All threads will access synchronized to the waiting request.</p>
|
1235 |
*
|
1236 |
* @return <code>PaintingRequest</code> that was waiting to be attended
|
1237 |
*
|
1238 |
* @see #put(org.gvsig.fmap.mapcontrol.MapControl.PaintingRequest)
|
1239 |
*/
|
1240 |
public PaintingRequest take()
|
1241 |
{ |
1242 |
if (waitingRequest == null) |
1243 |
{ |
1244 |
synchronized (this) { |
1245 |
waiting = true;
|
1246 |
try {
|
1247 |
wait(); |
1248 |
} |
1249 |
catch (InterruptedException ie) |
1250 |
{ |
1251 |
waiting = false;
|
1252 |
} |
1253 |
} |
1254 |
} |
1255 |
paintingRequest = waitingRequest; |
1256 |
waitingRequest = null;
|
1257 |
return paintingRequest;
|
1258 |
} |
1259 |
|
1260 |
/**
|
1261 |
* <p>Thread for attending painting requests.</p>
|
1262 |
*
|
1263 |
* <p>If there was no double buffer, sets the status to <code>OUTDATED</code> and finishes, otherwise
|
1264 |
* takes the painting request (it's probably that would wait some time), cancel the previous drawing
|
1265 |
* process, and starts processing the request.</p>
|
1266 |
*
|
1267 |
* @see Thread
|
1268 |
*/
|
1269 |
private class Worker implements Runnable |
1270 |
{ |
1271 |
/*
|
1272 |
* (non-Javadoc)
|
1273 |
* @see java.lang.Runnable#run()
|
1274 |
*/
|
1275 |
public void run() |
1276 |
{ |
1277 |
while (!shutdown)
|
1278 |
{ |
1279 |
PaintingRequest p = take(); |
1280 |
//System.out.println("Pintando");
|
1281 |
if (image != null){ |
1282 |
cancelDrawing(); |
1283 |
p.paint(); |
1284 |
} else{
|
1285 |
status = DESACTUALIZADO; |
1286 |
} |
1287 |
} |
1288 |
} |
1289 |
} |
1290 |
} |
1291 |
|
1292 |
/**
|
1293 |
* <p>An instance of <code>CancelDraw</code> will be shared by all this <code>MapControl</code>'s <code>MapContext</code> layers,
|
1294 |
* allowing receive a notification that, when they're been drawn, to be cancelled.</p>
|
1295 |
*
|
1296 |
* @see Cancellable
|
1297 |
*
|
1298 |
* @author Fernando Gonz?lez Cort?s
|
1299 |
*/
|
1300 |
public class CancelDraw implements Cancellable { |
1301 |
/**
|
1302 |
* <p>Determines if the drawing task must be canceled or not.</p>
|
1303 |
*
|
1304 |
* @see #isCanceled()
|
1305 |
* @see #setCanceled(boolean)
|
1306 |
*/
|
1307 |
private boolean cancel = false; |
1308 |
|
1309 |
/**
|
1310 |
* Creates a new <code>CancelDraw</code> object.
|
1311 |
*/
|
1312 |
public CancelDraw() {
|
1313 |
} |
1314 |
|
1315 |
/*
|
1316 |
* (non-Javadoc)
|
1317 |
* @see com.iver.utiles.swing.threads.Cancellable#setCanceled(boolean)
|
1318 |
*/
|
1319 |
public void setCanceled(boolean b) { |
1320 |
cancel = b; |
1321 |
} |
1322 |
|
1323 |
/*
|
1324 |
* (non-Javadoc)
|
1325 |
* @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
|
1326 |
*/
|
1327 |
public boolean isCanceled() { |
1328 |
return cancel;
|
1329 |
} |
1330 |
} |
1331 |
|
1332 |
/**
|
1333 |
* <p>Listens all kind of mouse events produced in {@link MapControl MapControl}, and invokes its current
|
1334 |
* map tool <i>({@link MapControl#getCurrentMapTool() MapControl#getCurrentMapTool()}</i> to simulate a behavior.</p>
|
1335 |
*
|
1336 |
* <p>Mouse wheel moved events produce a <i>zoom in</i> operation if wheel rotation is negative, or a <i>zoom out</i>
|
1337 |
* if its positive. Both will be centered in the position of the mouse, but, meanwhile <i>zoom in</i> operation
|
1338 |
* applies a factor of 0.9, <i>zoom out</i> operation applies a factor of 1.2</p>
|
1339 |
*
|
1340 |
* <p>Mouse wheel moved events can be produced as much frequently, that between each one, the drawing process could
|
1341 |
* hadn't finished. This is the reason that, in this situation, cancels always the previous drawing process before
|
1342 |
* applying a <i>zoom</i> operation, and ignores all new mouse positions that are produced before 1 second.</p>
|
1343 |
*
|
1344 |
* @author Fernando Gonz?lez Cort?s
|
1345 |
*/
|
1346 |
public class MapToolListener implements MouseListener, MouseWheelListener, |
1347 |
MouseMotionListener {
|
1348 |
|
1349 |
/**
|
1350 |
* <p>Used to avoid mouse wheel move events closed.</p>
|
1351 |
*
|
1352 |
* <p>If a mouse wheel move event is produced
|
1353 |
*/
|
1354 |
long t1;
|
1355 |
|
1356 |
/**
|
1357 |
* <p>Position of the mouse, in map coordinates.</p>
|
1358 |
*
|
1359 |
* <p>This point coordinates will be used as center of the <i>zoom</i> operation.</p>
|
1360 |
*/
|
1361 |
Point2D pReal;
|
1362 |
|
1363 |
/**
|
1364 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
1365 |
* @see Behavior#mouseClicked(MouseEvent)
|
1366 |
*/
|
1367 |
public void mouseClicked(MouseEvent e) { |
1368 |
try {
|
1369 |
if (currentMapTool != null) { |
1370 |
currentMapTool.mouseClicked(e); |
1371 |
} |
1372 |
} catch (BehaviorException t) {
|
1373 |
throwException(t); |
1374 |
} |
1375 |
} |
1376 |
|
1377 |
/**
|
1378 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
1379 |
* @see Behavior#mouseEntered(MouseEvent)
|
1380 |
*/
|
1381 |
public void mouseEntered(MouseEvent e) { |
1382 |
setToolMouse(); |
1383 |
try {
|
1384 |
if (currentMapTool != null) { |
1385 |
currentMapTool.mouseEntered(e); |
1386 |
} |
1387 |
} catch (BehaviorException t) {
|
1388 |
throwException(t); |
1389 |
} |
1390 |
} |
1391 |
|
1392 |
/**
|
1393 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
1394 |
* @see Behavior#mouseExited(MouseEvent)
|
1395 |
*/
|
1396 |
public void mouseExited(MouseEvent e) { |
1397 |
try {
|
1398 |
if (currentMapTool != null) { |
1399 |
currentMapTool.mouseExited(e); |
1400 |
} |
1401 |
} catch (BehaviorException t) {
|
1402 |
throwException(t); |
1403 |
} |
1404 |
//Remove the snapping image if exist
|
1405 |
usedSnap = null;
|
1406 |
repaint(); |
1407 |
} |
1408 |
|
1409 |
/**
|
1410 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
1411 |
* @see Behavior#mousePressed(MouseEvent)
|
1412 |
*/
|
1413 |
public void mousePressed(MouseEvent e) { |
1414 |
try {
|
1415 |
if (currentMapTool != null) { |
1416 |
currentMapTool.mousePressed(e); |
1417 |
} |
1418 |
} catch (BehaviorException t) {
|
1419 |
throwException(t); |
1420 |
} |
1421 |
} |
1422 |
|
1423 |
/**
|
1424 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
1425 |
* @see Behavior#mouseReleased(MouseEvent)
|
1426 |
*/
|
1427 |
public void mouseReleased(MouseEvent e) { |
1428 |
try {
|
1429 |
if (currentMapTool != null) { |
1430 |
currentMapTool.mouseReleased(e); |
1431 |
} |
1432 |
} catch (BehaviorException t) {
|
1433 |
throwException(t); |
1434 |
} |
1435 |
} |
1436 |
|
1437 |
/**
|
1438 |
* @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
1439 |
* @see Behavior#mouseWheelMoved(MouseWheelEvent)
|
1440 |
*/
|
1441 |
public void mouseWheelMoved(MouseWheelEvent e) { |
1442 |
try {
|
1443 |
if (currentMapTool == null) { |
1444 |
return;
|
1445 |
} |
1446 |
|
1447 |
currentMapTool.mouseWheelMoved(e); |
1448 |
|
1449 |
// Si el tool actual no ha consumido el evento
|
1450 |
// entendemos que quiere el comportamiento por defecto.
|
1451 |
if (!e.isConsumed())
|
1452 |
{ |
1453 |
// Para usar el primer punto sobre el que queremos centrar
|
1454 |
// el mapa, dejamos pasar un segundo para considerar el siguiente
|
1455 |
// punto como v?lido.
|
1456 |
if (t1 == 0) |
1457 |
{ |
1458 |
t1= System.currentTimeMillis();
|
1459 |
pReal = vp.toMapPoint(e.getPoint()); |
1460 |
} |
1461 |
else
|
1462 |
{ |
1463 |
long t2 = System.currentTimeMillis(); |
1464 |
if ((t2-t1) > 1000) { |
1465 |
t1=0;
|
1466 |
} |
1467 |
} |
1468 |
cancelDrawing(); |
1469 |
ViewPort vp = getViewPort(); |
1470 |
|
1471 |
|
1472 |
/* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
|
1473 |
vp.getAdjustedExtent().getCenterY()); */
|
1474 |
int amount = e.getWheelRotation();
|
1475 |
double nuevoX;
|
1476 |
double nuevoY;
|
1477 |
double factor;
|
1478 |
|
1479 |
if (amount < 0) // nos acercamos |
1480 |
{ |
1481 |
factor = 0.9;
|
1482 |
} else // nos alejamos |
1483 |
{ |
1484 |
factor = 1.2;
|
1485 |
} |
1486 |
if (vp.getExtent() != null) { |
1487 |
nuevoX = pReal.getX() |
1488 |
- ((vp.getExtent().getWidth() * factor) / 2.0);
|
1489 |
nuevoY = pReal.getY() |
1490 |
- ((vp.getExtent().getHeight() * factor) / 2.0);
|
1491 |
double x = nuevoX;
|
1492 |
double y = nuevoY;
|
1493 |
double width = vp.getExtent().getWidth() * factor;
|
1494 |
double height = vp.getExtent().getHeight() * factor;
|
1495 |
|
1496 |
try {
|
1497 |
vp.setEnvelope(geomManager.createEnvelope(x,y,x+width,y+height, SUBTYPES.GEOM2D)); |
1498 |
} catch (CreateEnvelopeException e1) {
|
1499 |
logger.error("Error creating the envelope", e);
|
1500 |
} |
1501 |
} |
1502 |
|
1503 |
|
1504 |
} |
1505 |
} catch (BehaviorException t) {
|
1506 |
throwException(t); |
1507 |
} |
1508 |
} |
1509 |
|
1510 |
/**
|
1511 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
1512 |
* @see Behavior#mouseDragged(MouseEvent)
|
1513 |
*/
|
1514 |
public void mouseDragged(MouseEvent e) { |
1515 |
calculateSnapPoint(e.getPoint()); |
1516 |
try {
|
1517 |
if (currentMapTool != null) { |
1518 |
currentMapTool.mouseDragged(e); |
1519 |
} |
1520 |
} catch (BehaviorException t) {
|
1521 |
throwException(t); |
1522 |
} |
1523 |
repaint(); |
1524 |
} |
1525 |
|
1526 |
/**
|
1527 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
1528 |
* @see Behavior#mouseMoved(MouseEvent)
|
1529 |
*/
|
1530 |
public void mouseMoved(MouseEvent e) { |
1531 |
calculateSnapPoint(e.getPoint()); |
1532 |
try {
|
1533 |
if (currentMapTool != null) { |
1534 |
currentMapTool.mouseMoved(e); |
1535 |
} |
1536 |
} catch (BehaviorException t) {
|
1537 |
throwException(t); |
1538 |
} |
1539 |
repaint(); |
1540 |
} |
1541 |
} |
1542 |
|
1543 |
/**
|
1544 |
* <p<code>MapContextListener</code> listens all events produced in a <code>MapControl</code>'s <code>MapContext</code>
|
1545 |
* object during an atomic period of time, and sets it to dirty, <i>executing <code>drawMap(false)</code>, if any of the
|
1546 |
* following conditions is accomplished</i>:
|
1547 |
* <ul>
|
1548 |
* <li>Any of the <code>LayerEvent</code> in the <code>AtomicEvent</code> parameter notifies a <i>visibility change</i>.</li>
|
1549 |
* <li>There is at least one <code>ColorEvent</code> in the <code>AtomicEvent</code> parameter.</li>
|
1550 |
* <li>There is at least one <code>ExtentEvent</code> in the <code>AtomicEvent</code> parameter.</li>
|
1551 |
* <li>Any of the <code>LayerCollectionEvent</code> in the <code>AtomicEvent</code> parameter notifies that a driver's layer has reloaded it successfully.</li>
|
1552 |
* <li>There is at least one <code>LegendEvent</code> in the <code>AtomicEvent</code> parameter.</li>
|
1553 |
* <li>There is at least one <code>SelectionEvent</code> in the <code>AtomicEvent</code> parameter.</li>
|
1554 |
* </ul>
|
1555 |
* </p>
|
1556 |
*
|
1557 |
* @author Fernando Gonz?lez Cort?s
|
1558 |
*/
|
1559 |
public class MapContextListener implements AtomicEventListener { |
1560 |
/**
|
1561 |
* @see org.gvsig.fmap.mapcontext.events.listeners.AtomicEventListener#atomicEvent(org.gvsig.fmap.mapcontext.events.AtomicEvent)
|
1562 |
*/
|
1563 |
public void atomicEvent(AtomicEvent e) { |
1564 |
LayerEvent[] layerEvents = e.getLayerEvents();
|
1565 |
|
1566 |
int eType;
|
1567 |
for (int i = 0; i < layerEvents.length; i++) { |
1568 |
eType=layerEvents[i].getEventType(); |
1569 |
// if (eType == LayerEvent.DRAW_VALUES_CHANGED || eType == LayerEvent.VISIBILITY_CHANGED) {
|
1570 |
// MapControl.this.drawMap(false);
|
1571 |
// return;
|
1572 |
// }
|
1573 |
if (layerEvents[i].getProperty().equals("visible")) { |
1574 |
MapControl.this.drawMap(false);
|
1575 |
return;
|
1576 |
} else if (layerEvents[i].getEventType() == LayerEvent.EDITION_CHANGED){ |
1577 |
MapControl.this.drawMap(false);
|
1578 |
return;
|
1579 |
} |
1580 |
} |
1581 |
|
1582 |
if (e.getColorEvents().length > 0) { |
1583 |
MapControl.this.drawMap(false);
|
1584 |
return;
|
1585 |
} |
1586 |
|
1587 |
if (e.getExtentEvents().length > 0) { |
1588 |
MapControl.this.drawMap(false);
|
1589 |
return;
|
1590 |
} |
1591 |
|
1592 |
if (e.getProjectionEvents().length > 0) { |
1593 |
//redraw = true;
|
1594 |
} |
1595 |
|
1596 |
|
1597 |
LayerCollectionEvent[] aux = e.getLayerCollectionEvents();
|
1598 |
if (aux.length > 0) { |
1599 |
for (int i=0; i < aux.length; i++) |
1600 |
{ |
1601 |
if (aux[i].getAffectedLayer().getFLayerStatus().isDriverLoaded())
|
1602 |
{ |
1603 |
MapControl.this.drawMap(false);
|
1604 |
return;
|
1605 |
} |
1606 |
} |
1607 |
|
1608 |
} |
1609 |
|
1610 |
if (e.getLegendEvents().length > 0) { |
1611 |
MapControl.this.drawMap(false);
|
1612 |
return;
|
1613 |
} |
1614 |
|
1615 |
if (e.getSelectionEvents().length > 0) { |
1616 |
MapControl.this.drawMap(false);
|
1617 |
return;
|
1618 |
} |
1619 |
} |
1620 |
} |
1621 |
|
1622 |
/**
|
1623 |
* <p>Gets the <code>ViewPort</code> of this component's {@link MapContext MapContext} .</p>
|
1624 |
*
|
1625 |
* @see MapContext#getViewPort()
|
1626 |
*/
|
1627 |
public ViewPort getViewPort() {
|
1628 |
return vp;
|
1629 |
} |
1630 |
|
1631 |
/**
|
1632 |
* <p>Returns all registered <code>Behavior</code> that can define a way to work with this <code>MapControl</code>.</p>
|
1633 |
*
|
1634 |
* @return registered <code>Behavior</code> to this <code>MapControl</code>
|
1635 |
*
|
1636 |
* @see #addBehavior(String, Behavior)
|
1637 |
* @see #addBehavior(String, Behavior[])
|
1638 |
* @see #getMapToolsKeySet()
|
1639 |
* @see #hasTool(String)
|
1640 |
*/
|
1641 |
public HashMap getNamesMapTools() { |
1642 |
return namesMapTools;
|
1643 |
} |
1644 |
|
1645 |
/*
|
1646 |
* (non-Javadoc)
|
1647 |
* @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRepaint()
|
1648 |
*/
|
1649 |
public void commandRepaint() { |
1650 |
drawMap(false);
|
1651 |
} |
1652 |
|
1653 |
/*
|
1654 |
* (non-Javadoc)
|
1655 |
* @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRefresh()
|
1656 |
*/
|
1657 |
public void commandRefresh() { |
1658 |
// TODO Auto-generated method stub
|
1659 |
} |
1660 |
|
1661 |
/**
|
1662 |
* <p>Equivalent operation to <i>undo</i>.</p>
|
1663 |
*
|
1664 |
* <p>Exchanges the previous tool with the current one.</p>
|
1665 |
*
|
1666 |
* @see #addBehavior(String, Behavior)
|
1667 |
* @see #addBehavior(String, Behavior[])
|
1668 |
* @see #setTool(String)
|
1669 |
*/
|
1670 |
public void setPrevTool() { |
1671 |
setTool(prevTool); |
1672 |
} |
1673 |
|
1674 |
/**
|
1675 |
* <p>Executes a <i>zoom in</i> operation centered at the center of the extent.</p>
|
1676 |
*
|
1677 |
* <p>This implementation is designed for being invoked outside a <code>Behavior</code>, for example
|
1678 |
* by an action of pressing a button; and simulates that the event has been produced by
|
1679 |
* releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
|
1680 |
* <code>MapControl</code> object that's responsible for the <i>zoom in</i> operation.</p>
|
1681 |
*
|
1682 |
* @see #zoomOut()
|
1683 |
*/
|
1684 |
public void zoomIn() { |
1685 |
Behavior mapTool = (Behavior) namesMapTools.get("zoomIn");
|
1686 |
ViewPort vp=getViewPort(); |
1687 |
Envelope r=getViewPort().getAdjustedExtent(); |
1688 |
Point2D pCenter=vp.fromMapPoint(r.getCenter(0),r.getCenter(1)); |
1689 |
MouseEvent e=new MouseEvent(this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1); |
1690 |
try {
|
1691 |
mapTool.mousePressed(e); |
1692 |
mapTool.mouseReleased(e); |
1693 |
} catch (BehaviorException t) {
|
1694 |
throwException(t); |
1695 |
} |
1696 |
} |
1697 |
|
1698 |
/**
|
1699 |
* <p>Executes a <i>zoom out</i> operation centered at the center of the extent.</p>
|
1700 |
*
|
1701 |
* <p>This implementation is thought for being invoked outside a <code>Behavior</code>, for example
|
1702 |
* by an action of pressing a button, and simulates that the event has been produced by
|
1703 |
* releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
|
1704 |
* <code>MapControl</code> object that's responsible for the <i>zoom out</i> operation.</p>
|
1705 |
*
|
1706 |
* @see #zoomIn()
|
1707 |
*/
|
1708 |
public void zoomOut() { |
1709 |
Behavior mapTool = (Behavior) namesMapTools.get("zoomOut");
|
1710 |
ViewPort vp=getViewPort(); |
1711 |
Envelope r=getViewPort().getAdjustedExtent(); |
1712 |
Point2D pCenter=vp.fromMapPoint(r.getCenter(0),r.getCenter(1)); |
1713 |
MouseEvent e=new MouseEvent(this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1); |
1714 |
try {
|
1715 |
mapTool.mousePressed(e); |
1716 |
mapTool.mouseReleased(e); |
1717 |
} catch (BehaviorException t) {
|
1718 |
throwException(t); |
1719 |
} |
1720 |
} |
1721 |
|
1722 |
/**
|
1723 |
* <p>Returns the listener used to catch all mouse events produced in this <code>MapControl</code> instance
|
1724 |
* and that redirects the calls to the current map tool.</p>
|
1725 |
*
|
1726 |
* @return the map tool listener used
|
1727 |
*/
|
1728 |
public MapToolListener getMapToolListener() {
|
1729 |
return mapToolListener;
|
1730 |
} |
1731 |
|
1732 |
// mapTool can be null, for instance, in 3D's navigation tools
|
1733 |
/**
|
1734 |
* <p>Sets <code>mapTool</code> as this <code>MapControl</code>'s current map tool.
|
1735 |
*
|
1736 |
* @param mapTool a map tool, or <code>null</code> to disable the interaction with the user
|
1737 |
*
|
1738 |
* @see #getCurrentMapTool()
|
1739 |
* @see #getCurrentTool()
|
1740 |
* @see #setTool(String)
|
1741 |
* @see #setPrevTool()
|
1742 |
* @see #addBehavior(String, Behavior)
|
1743 |
* @see #addBehavior(String, Behavior[])
|
1744 |
*/
|
1745 |
public void setCurrentMapTool(Behavior mapTool ){ |
1746 |
currentMapTool = mapTool; |
1747 |
} |
1748 |
|
1749 |
/**
|
1750 |
* <p>Sets the delay to the timer that refreshes this <code>MapControl</code> instance.</p>
|
1751 |
*
|
1752 |
* <p><code>Delay (in ms) = 1000 / getDrawFrameRate()</code></p>
|
1753 |
*
|
1754 |
* @see #getDrawFrameRate()
|
1755 |
* @see #setDrawFrameRate(int)
|
1756 |
*/
|
1757 |
public void applyFrameRate() { |
1758 |
if (MapContext.getDrawFrameRate()>0) { |
1759 |
timer.setDelay(1000/MapContext.getDrawFrameRate());
|
1760 |
} |
1761 |
} |
1762 |
|
1763 |
|
1764 |
|
1765 |
/**
|
1766 |
* <p>Determines if its enabled the repaint that invokes the timer according to {@link #getDrawFrameRate() #getDrawFrameRate()}.</p>
|
1767 |
*
|
1768 |
* @return <code>true</code> if its enabled; otherwise <code>false</code>
|
1769 |
*/
|
1770 |
public static boolean isDrawAnimationEnabled() { |
1771 |
return drawAnimationEnabled;
|
1772 |
} |
1773 |
|
1774 |
/**
|
1775 |
* <p>Sets if its enabled the repaint that invokes the timer according to {@link #getDrawFrameRate() #getDrawFrameRate()}.</p>
|
1776 |
*
|
1777 |
* @param drawAnimationEnabled <code>true</code> to enable the mode; otherwise <code>false</code>
|
1778 |
*/
|
1779 |
public static void setDrawAnimationEnabled(boolean drawAnimationEnabled) { |
1780 |
MapControl.drawAnimationEnabled = drawAnimationEnabled; |
1781 |
} |
1782 |
|
1783 |
/**
|
1784 |
* <p>Gets the shared object that determines if a drawing process must be cancelled or can continue.</p>
|
1785 |
*
|
1786 |
* @return the shared object that determines if a drawing process must be cancelled or can continue
|
1787 |
*/
|
1788 |
public CancelDraw getCanceldraw() {
|
1789 |
return canceldraw;
|
1790 |
} |
1791 |
|
1792 |
/**
|
1793 |
* <p>Gets the tool used in combination with the current tool of this <code>MapControl</code>.</p>
|
1794 |
*
|
1795 |
* @return the tool used in combination with the <code>currentMapTool</code>; <code>null</code> if there is
|
1796 |
* no combined tool
|
1797 |
*/
|
1798 |
public Behavior getCombinedTool() {
|
1799 |
return combinedTool;
|
1800 |
} |
1801 |
|
1802 |
/**
|
1803 |
* <p>Sets a tool to be used in combination with the current tool of this <code>MapControl</code>.</p>
|
1804 |
*
|
1805 |
* @param combinedTool a tool to be used in combination with the current tool of <code>MapControl</code>
|
1806 |
*/
|
1807 |
public void setCombinedTool(Behavior combinedTool) { |
1808 |
this.combinedTool = combinedTool;
|
1809 |
|
1810 |
if (currentMapTool == null) { |
1811 |
return;
|
1812 |
} |
1813 |
|
1814 |
if (currentMapTool instanceof CompoundBehavior) { |
1815 |
((CompoundBehavior)currentMapTool).addMapBehavior(combinedTool, true);
|
1816 |
} |
1817 |
else {
|
1818 |
currentMapTool = new CompoundBehavior(new Behavior[] {currentMapTool}); |
1819 |
((CompoundBehavior)currentMapTool).addMapBehavior(combinedTool, true);
|
1820 |
} |
1821 |
|
1822 |
} |
1823 |
/**
|
1824 |
* <p>Adds a new tool as combined tool.</p>
|
1825 |
* <p>The new tool will be stored with the previous combined tools, and will be combined with
|
1826 |
* the current tool.</p>
|
1827 |
* <p>If <code>tool</code> was already stored as a combined tool, doesn't adds it.</p>
|
1828 |
*
|
1829 |
* @param tool a new tool to be used combined with the current tool
|
1830 |
*/
|
1831 |
public void addCombinedBehavior(Behavior tool) { |
1832 |
if (combinedTool == null) { |
1833 |
combinedTool = tool; |
1834 |
} |
1835 |
else {
|
1836 |
if (combinedTool instanceof CompoundBehavior) { |
1837 |
if (((CompoundBehavior)combinedTool).containsBehavior(tool)) {
|
1838 |
return;
|
1839 |
} |
1840 |
|
1841 |
((CompoundBehavior)combinedTool).addMapBehavior(tool, true);
|
1842 |
} |
1843 |
else {
|
1844 |
if (combinedTool.equals(tool)) {
|
1845 |
return;
|
1846 |
} |
1847 |
|
1848 |
combinedTool = new CompoundBehavior(new Behavior[] {combinedTool}); |
1849 |
((CompoundBehavior)combinedTool).addMapBehavior(tool, true);
|
1850 |
} |
1851 |
} |
1852 |
|
1853 |
if (currentMapTool == null) { |
1854 |
return;
|
1855 |
} |
1856 |
|
1857 |
if (currentMapTool instanceof CompoundBehavior) { |
1858 |
((CompoundBehavior)currentMapTool).addMapBehavior(tool, true);
|
1859 |
} |
1860 |
else {
|
1861 |
currentMapTool = new CompoundBehavior(new Behavior[] {currentMapTool}); |
1862 |
((CompoundBehavior)currentMapTool).addMapBehavior(tool, true);
|
1863 |
} |
1864 |
} |
1865 |
/**
|
1866 |
* <p>Removes the tool <code>tool</code> used in combination with the current tool of this <code>MapControl</code>.</p>
|
1867 |
*/
|
1868 |
public void removeCombinedTool(Behavior tool) { |
1869 |
if ((currentMapTool != null) && (currentMapTool instanceof CompoundBehavior)) { |
1870 |
((CompoundBehavior)currentMapTool).removeMapBehavior(tool); |
1871 |
} |
1872 |
|
1873 |
if (combinedTool == null) { |
1874 |
return;
|
1875 |
} |
1876 |
|
1877 |
if (combinedTool instanceof CompoundBehavior) { |
1878 |
((CompoundBehavior)combinedTool).removeMapBehavior(tool); |
1879 |
} |
1880 |
else {
|
1881 |
combinedTool = null;
|
1882 |
} |
1883 |
} |
1884 |
|
1885 |
|
1886 |
/**
|
1887 |
* <p>Updates the grid on the <code>ViewPort</code> of the associated <code>MapControl</code>
|
1888 |
* object according the values in the {@link com.iver.cit.gvsig.gui.cad.CADToolAdapter.prefs.Preferences com.iver.cit.gvsig.gui.cad.CADToolAdapter.prefs.Preferences}.</p>
|
1889 |
*
|
1890 |
* <p>The preferences are:
|
1891 |
* <ul>
|
1892 |
* <li>Show/hide the grid.</li>
|
1893 |
* <li>Adjust or not the grid.</li>
|
1894 |
* <li>Horizontal ( X ) line separation.</li>
|
1895 |
* <li>Vertical ( Y ) line separation.</li>
|
1896 |
* </ul>
|
1897 |
* </p>
|
1898 |
*/
|
1899 |
public void initializeGrid(){ |
1900 |
Preferences prefs = mapControlManager.getEditionPreferences();
|
1901 |
boolean showGrid = prefs.getBoolean("grid.showgrid",cadgrid.isShowGrid()); |
1902 |
boolean adjustGrid = prefs.getBoolean("grid.adjustgrid",cadgrid.isAdjustGrid()); |
1903 |
|
1904 |
double dx = prefs.getDouble("grid.distancex",cadgrid.getGridSizeX()); |
1905 |
double dy = prefs.getDouble("grid.distancey",cadgrid.getGridSizeY()); |
1906 |
|
1907 |
setGridVisibility(showGrid); |
1908 |
setAdjustGrid(adjustGrid); |
1909 |
cadgrid.setGridSizeX(dx); |
1910 |
cadgrid.setGridSizeY(dy); |
1911 |
} |
1912 |
|
1913 |
public void setAdjustGrid(boolean adjustGrid) { |
1914 |
cadgrid.setAdjustGrid(adjustGrid); |
1915 |
} |
1916 |
|
1917 |
public void setGridVisibility(boolean showGrid) { |
1918 |
cadgrid.setShowGrid(showGrid); |
1919 |
cadgrid.setViewPort(getViewPort()); |
1920 |
this.repaint();
|
1921 |
} |
1922 |
|
1923 |
/**
|
1924 |
* Uses like a mouse pointer the image that provides the
|
1925 |
* selected tool.
|
1926 |
*/
|
1927 |
private void setToolMouse(){ |
1928 |
Image cursor=getCurrentMapTool().getImageCursor();
|
1929 |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
1930 |
Cursor c = toolkit.createCustomCursor(cursor ,
|
1931 |
new Point(16, 16), "img"); |
1932 |
setCursor (c); |
1933 |
} |
1934 |
|
1935 |
/**
|
1936 |
* Makes the mouse pointer transparent.
|
1937 |
*/
|
1938 |
private void setTransparentMouse(){ |
1939 |
Image cursor=getCurrentMapTool().getImageCursor();
|
1940 |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
1941 |
Cursor transparentCursor = Toolkit.getDefaultToolkit() |
1942 |
.createCustomCursor(Toolkit.getDefaultToolkit().createImage(new MemoryImageSource( |
1943 |
16, 16, new int[16 * 16], 0, 16)), |
1944 |
new Point(0, 0), "invisiblecursor"); |
1945 |
setCursor (transparentCursor); |
1946 |
} |
1947 |
|
1948 |
/**
|
1949 |
* <p>Draws a 31x31 pixels cross round the mouse's cursor with an small geometry centered:
|
1950 |
* <ul>
|
1951 |
* <li><i>an square centered</i>: if isn't over a <i>control point</i>.
|
1952 |
* <li><i>an small geometry centered according to the kind of control point</i>: if it's over a control
|
1953 |
* point. In this case, the small geometry is drawn by a {@link ISnapper ISnapper} type object.<br>
|
1954 |
* On the other hand, a light-yellowed background tool tip text with the type of <i>control point</i> will
|
1955 |
* be displayed.</li>
|
1956 |
* </p>
|
1957 |
*
|
1958 |
* @param g <code>MapControl</code>'s graphics where the data will be drawn
|
1959 |
*/
|
1960 |
private void drawCursor() { |
1961 |
if (adjustedPoint == null){ |
1962 |
return;
|
1963 |
} |
1964 |
|
1965 |
if (usedSnap != null){ |
1966 |
usedSnap.draw(mapControlDrawer, adjustedPoint); |
1967 |
setTransparentMouse(); |
1968 |
}else{
|
1969 |
setToolMouse(); |
1970 |
} |
1971 |
} |
1972 |
|
1973 |
/**
|
1974 |
* <p>Adjusts the <code>point</code> to the grid if its enabled, and
|
1975 |
* sets <code>mapHandlerAdjustedPoint</code> with that new value.</p>
|
1976 |
*
|
1977 |
* <p>The value returned is the distance between those points: the original and
|
1978 |
* the adjusted one.</p>
|
1979 |
*
|
1980 |
* @param point point to adjust
|
1981 |
* @param mapHandlerAdjustedPoint <code>point</code> adjusted
|
1982 |
*
|
1983 |
* @return distance from <code>point</code> to the adjusted one. If there is no
|
1984 |
* adjustment, returns <code>Double.MAX_VALUE</code>.
|
1985 |
*/
|
1986 |
|
1987 |
|
1988 |
private double adjustToHandler(Point2D point, |
1989 |
Point2D mapHandlerAdjustedPoint) {
|
1990 |
|
1991 |
if (!isRefentEnabled())
|
1992 |
return Double.MAX_VALUE; |
1993 |
|
1994 |
ArrayList layersToSnap = getMapContext().getLayersToSnap();
|
1995 |
|
1996 |
double mapTolerance = vp.toMapDistance(mapControlManager.getTolerance());
|
1997 |
double minDist = mapTolerance;
|
1998 |
double middleTol=mapTolerance * 0.5; |
1999 |
Point2D mapPoint = point;
|
2000 |
org.gvsig.fmap.geom.primitive.Envelope r; |
2001 |
com.vividsolutions.jts.geom.Envelope e = null;
|
2002 |
try {
|
2003 |
r = geomManager.createEnvelope(mapPoint.getX() - middleTol, |
2004 |
mapPoint.getY() - middleTol, |
2005 |
mapPoint.getX() + middleTol, |
2006 |
mapPoint.getY() + middleTol, |
2007 |
SUBTYPES.GEOM2D); |
2008 |
|
2009 |
e = Converter.convertEnvelopeToJTS(r); |
2010 |
} catch (CreateEnvelopeException e1) {
|
2011 |
logger.error("Error creating the envelope", e1);
|
2012 |
} |
2013 |
|
2014 |
usedSnap = null;
|
2015 |
Point2D lastPoint = null; |
2016 |
if (previousPoint != null) |
2017 |
{ |
2018 |
lastPoint = new Point2D.Double(previousPoint[0], previousPoint[1]); |
2019 |
} |
2020 |
for (int j = 0; j < layersToSnap.size(); j++) |
2021 |
{ |
2022 |
FLyrVect lyrVect = (FLyrVect) layersToSnap.get(j); |
2023 |
SpatialCache cache = lyrVect.getSpatialCache(); |
2024 |
if (lyrVect.isVisible())
|
2025 |
{ |
2026 |
// La lista de snappers est? siempre ordenada por prioridad. Los de mayor
|
2027 |
// prioridad est?n primero.
|
2028 |
java.util.List geoms = cache.query(e); |
2029 |
for (int n=0; n < geoms.size(); n++) { |
2030 |
Geometry geom = (Geometry) geoms.get(n); |
2031 |
for (int i = 0; i < mapControlManager.getSnapperCount(); i++) |
2032 |
{ |
2033 |
ISnapper theSnapper = mapControlManager.getSnapperAt(i); |
2034 |
|
2035 |
if (usedSnap != null) |
2036 |
{ |
2037 |
// Si ya tenemos un snap y es de alta prioridad, cogemos ese. (A no ser que en otra capa encontremos un snapper mejor)
|
2038 |
if (theSnapper.getPriority() > usedSnap.getPriority())
|
2039 |
break;
|
2040 |
} |
2041 |
// SnappingVisitor snapVisitor = null;
|
2042 |
Point2D theSnappedPoint = null; |
2043 |
if (theSnapper instanceof ISnapperVectorial) |
2044 |
{ |
2045 |
theSnappedPoint = ((ISnapperVectorial) theSnapper).getSnapPoint(point, geom, mapTolerance, lastPoint); |
2046 |
} |
2047 |
if (theSnapper instanceof ISnapperRaster) |
2048 |
{ |
2049 |
ISnapperRaster snapRaster = (ISnapperRaster) theSnapper; |
2050 |
theSnappedPoint = snapRaster.getSnapPoint(this, point, mapTolerance, lastPoint);
|
2051 |
} |
2052 |
|
2053 |
|
2054 |
if (theSnappedPoint != null) { |
2055 |
double distAux = theSnappedPoint.distance(point);
|
2056 |
if (minDist > distAux)
|
2057 |
{ |
2058 |
minDist = distAux; |
2059 |
usedSnap = theSnapper; |
2060 |
mapHandlerAdjustedPoint.setLocation(theSnappedPoint); |
2061 |
} |
2062 |
} |
2063 |
} |
2064 |
} // for n
|
2065 |
} // visible
|
2066 |
} |
2067 |
if (usedSnap != null) |
2068 |
return minDist;
|
2069 |
return Double.MAX_VALUE; |
2070 |
|
2071 |
} |
2072 |
/**
|
2073 |
* Determines if snap tools are enabled or disabled.
|
2074 |
*
|
2075 |
* @return <code>true</code> to enable the snap tools; <code>false</code> to disable them
|
2076 |
*
|
2077 |
* @see #setRefentEnabled(boolean)
|
2078 |
*/
|
2079 |
public boolean isRefentEnabled() |
2080 |
{ |
2081 |
return bRefent;
|
2082 |
} |
2083 |
|
2084 |
/**
|
2085 |
* <p>Tries to find the nearest geometry or grid control point by the position of the current snap tool.</p>
|
2086 |
*
|
2087 |
* <p>Prioritizes the grid control points than the geometries ones.</p>
|
2088 |
*
|
2089 |
* <p>If finds any near, stores the <i>map</i> and <i>pixel</i> coordinates for the snap, and enables
|
2090 |
* the <code>bForceCoord</code> attribute for the next draw of the mouse's cursor.</p>
|
2091 |
*
|
2092 |
* @param point current mouse 2D position
|
2093 |
*/
|
2094 |
public void calculateSnapPoint(Point point) { |
2095 |
// Se comprueba el ajuste a rejilla
|
2096 |
|
2097 |
Point2D gridAdjustedPoint = vp.toMapPoint(
|
2098 |
point); |
2099 |
double minDistance = Double.MAX_VALUE; |
2100 |
|
2101 |
minDistance = cadgrid.adjustToGrid(gridAdjustedPoint); |
2102 |
if (minDistance < Double.MAX_VALUE) { |
2103 |
adjustedPoint = vp.fromMapPoint( |
2104 |
gridAdjustedPoint); |
2105 |
mapAdjustedPoint = gridAdjustedPoint; |
2106 |
} else {
|
2107 |
mapAdjustedPoint = null;
|
2108 |
} |
2109 |
|
2110 |
Point2D handlerAdjustedPoint = null; |
2111 |
|
2112 |
// Se comprueba el ajuste a los handlers
|
2113 |
if (mapAdjustedPoint != null) { |
2114 |
handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); // getMapControl().getViewPort().toMapPoint(point); |
2115 |
} else {
|
2116 |
handlerAdjustedPoint = vp.toMapPoint( |
2117 |
point); |
2118 |
} |
2119 |
|
2120 |
Point2D mapPoint = new Point2D.Double(); |
2121 |
double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
|
2122 |
|
2123 |
if (distance < minDistance) {
|
2124 |
bForceCoord = true;
|
2125 |
adjustedPoint = vp.fromMapPoint(mapPoint); |
2126 |
mapAdjustedPoint = mapPoint; |
2127 |
minDistance = distance; |
2128 |
} |
2129 |
|
2130 |
// Si no hay ajuste
|
2131 |
if (minDistance == Double.MAX_VALUE) { |
2132 |
adjustedPoint = point; |
2133 |
mapAdjustedPoint = null;
|
2134 |
} |
2135 |
} |
2136 |
|
2137 |
/**
|
2138 |
* Sets the snap tools enabled or disabled.
|
2139 |
*
|
2140 |
* @param activated <code>true</code> to enable the snap tools; <code>false</code> to disable them
|
2141 |
*
|
2142 |
* @see #isRefentEnabled()
|
2143 |
*/
|
2144 |
public void setRefentEnabled(boolean activated) { |
2145 |
bRefent = activated; |
2146 |
} |
2147 |
|
2148 |
public Grid getGrid() {
|
2149 |
return cadgrid;
|
2150 |
} |
2151 |
|
2152 |
public void update(Observable observable, Object notification) { |
2153 |
DataStoreNotification ddsn=(DataStoreNotification)notification; |
2154 |
String type=ddsn.getType();
|
2155 |
if (type.equals(FeatureStoreNotification.AFTER_UNDO) ||
|
2156 |
type.equals(FeatureStoreNotification.AFTER_REDO)){ |
2157 |
repaint(); |
2158 |
} |
2159 |
} |
2160 |
|
2161 |
/**
|
2162 |
* @return the adjustedPoint
|
2163 |
*/
|
2164 |
public Point2D getAdjustedPoint() { |
2165 |
return adjustedPoint;
|
2166 |
} |
2167 |
|
2168 |
/**
|
2169 |
* @return the mapAdjustedPoint
|
2170 |
*/
|
2171 |
public Point2D getMapAdjustedPoint() { |
2172 |
return mapAdjustedPoint;
|
2173 |
} |
2174 |
|
2175 |
/**
|
2176 |
* Gets the selected point. If the snapping is enabled
|
2177 |
* it returns the selected point.
|
2178 |
* @return
|
2179 |
* The selected point
|
2180 |
*/
|
2181 |
public Point2D getPoint(){ |
2182 |
if (mapAdjustedPoint != null) { |
2183 |
return mapAdjustedPoint;
|
2184 |
} else {
|
2185 |
return getViewPort().toMapPoint(adjustedPoint);
|
2186 |
} |
2187 |
} |
2188 |
} |