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