Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapControl.java @ 20098

History | View | Annotate | Download (62.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap;
42

    
43
import java.awt.Color;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.Graphics;
47
import java.awt.Graphics2D;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.ComponentEvent;
51
import java.awt.event.ComponentListener;
52
import java.awt.event.MouseEvent;
53
import java.awt.event.MouseListener;
54
import java.awt.event.MouseMotionListener;
55
import java.awt.event.MouseWheelEvent;
56
import java.awt.event.MouseWheelListener;
57
import java.awt.geom.Point2D;
58
import java.awt.geom.Rectangle2D;
59
import java.awt.image.BufferedImage;
60
import java.util.HashMap;
61
import java.util.Set;
62

    
63
import javax.swing.JComponent;
64
import javax.swing.Timer;
65

    
66
import org.cresques.cts.IProjection;
67

    
68
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
69
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener;
70
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
71
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
72
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
73
import com.iver.cit.gvsig.fmap.tools.CompoundBehavior;
74
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
75
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
76
import com.iver.utiles.exceptionHandling.ExceptionListener;
77
import com.iver.utiles.swing.threads.Cancellable;
78

    
79

    
80
/**
81
 * <p>A component that includes a {@link MapContext MapContext} with support for use it as a particular {@link Behavior Behavior}.</p>
82
 *
83
 * <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
84
 *  defines the way to work and access with its <code>MapContext</code>'s layers. The active behavior, in combination with the appropriate
85
 *  {@link ToolListener ToolListener} will allow user work with a particular <i>tool</i>.</p>
86
 *
87
 * <p>All mouse events produced on this component will be delegated to the current active behavior, the <i>currentMapTool</i>.</p>
88
 * 
89
 * <p><b>Drawing process:</b></p>
90
 * 
91
 * <p>Uses a double buffer for the drawing process of <code>MapContext</code>'s information.</p>
92
 * 
93
 * <p>If the double buffer wasn't created, creates a new one.</p>
94
 * 
95
 * <p>Paints the component according the following algorithm:
96
 * <br>
97
 *  &nbsp If <i>status</i> is <i>UPDATED</i>:<br>
98
 *  &nbsp &nbsp If there is a <i>double buffer</i>:<br>
99
 *  &nbsp &nbsp &nbsp If there is a <i>behavior</i> for managing the <code>MapControl</code> instance, delegates
100
 *   the drawing process to that behavior, calling: <code><i>behavior_instance</i>.paintComponent(g)</code>.<br>
101
 *  &nbsp &nbsp &nbsp Else, repaints the current graphical information quickly calling: <code>g.drawImage(image,0,0,null)</code>.<br>
102
 *  &nbsp 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
103
 *   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
104
 *   (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
105
 *   draw the layers: <code>mapContext.draw(image, g, cancel,mapContext.getScaleView());</code> 
106
 * <br>
107
 * <p>Some notes:
108
 *  <ul>
109
 *   <li>The painting process can be cancelled calling {@link #cancelDrawing() #cancelDrawing()}.</li>
110
 *   <li>At last resort, the particular implementation of each layer in a <code>MapControl</code>'s <code>MapContrext</code>
111
 *    will be that one which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</li>
112
 *   <li>It's possible to force repaint all layers, calling {@link #drawMap(boolean doClear) #drawMap(boolean)}.</li>
113
 *   <li>It's possible repaint only the dirty layers, calling {@link #rePaintDirtyLayers() #rePaintDirtyLayers()}.</li>
114
 *   <li>It's possible repaint only the {@link GraphicLayer GraphicLayer}, calling {@link #drawGraphics() #drawGraphics()}.</li>
115
 *  </ul> 
116
 * </p>
117
 * 
118
 * <p><b>Tools:</b></p>
119
 * 
120
 * <p>A developer can:
121
 *   <ul>
122
 *    <li>Register each tool as:
123
 *     <ul>
124
 *      <li>A single behavior: {@link #addMapTool(String, Behavior) #addMapTool(String, Behavior)}.</li>
125
 *      <li>Or, a compound behavior: {@link #addMapTool(String, Behavior) #addMapTool(String, Behavior)}.</li>
126
 *     </ul>
127
 *    </li>
128
 *    <li>Get the current active tool: {@link #getCurrentMapTool() #getCurrentMapTool()}.</li>
129
 *    <li>Get the current active tool name: {@link #getCurrentTool() #getCurrentTool()}.</li>
130
 *    <li>Get a registered tool: {@link #getMapTool(String) #getMapTool(String)}.</li>
131
 *    <li>Get the name of all tools registered: {@link #getMapToolsKeySet() #getMapToolsKeySet()}.</li>
132
 *    <li>Get all tools registered, including the name they were registered: {@link #getNamesMapTools() #getNamesMapTools()}.</li>
133
 *    <li>Determine if has a tool registered: {@link #hasTool(String) #hasTool(String)}.</li>
134
 *    <li>Set as an active tool, one of the registered: {@link #setTool(String) #setTool(String)}.</li>
135
 *    <li>Set as active tool, the previous used: {@link #setPrevTool() #setPrevTool()}.</li>
136
 *    <li>Set the current tool: {@link #setCurrentMapTool(Behavior) #setCurrentMapTool(Behavior)}.</li>
137
 *    <li>Change the draw frame rate: {@link #setDrawFrameRate(int) #setDrawFrameRate(int)} and {@link #applyFrameRate() #applyFrameRate()}.</li>
138
 *    <li>Get the draw frame rate: {@link #getDrawFrameRate() #getDrawFrameRate()}.</li>
139
 *    <li>Determine if will repaint this component each time timer finishes: {@link #isDrawAnimationEnabled() #isDrawAnimationEnabled()}.</li>
140
 *    <li>Change if will repaint this component each time timer finishes: {@link #setDrawAnimationEnabled(boolean) #setDrawAnimationEnabled(boolean)}.</li>
141
 *    <li>Get the shared object that determines if a drawing process must be cancelled or can continue: {@link #getCanceldraw() #getCanceldraw()}.</li>
142
 *   </ul>
143
 * </p>
144
 *
145
 * <p><b>Exception listener:</b></p>
146
 * 
147
 * <p> Adding an <code>ExceptionListener</code>, can get notification about any exception produced: 
148
 *  <ul>
149
 *   <li>Attending a <i>painting request</i>.</li>
150
 *   <li>Working with the active tool.</li>
151
 *   <li>Applying a <i>zoom in</i> or <i>zoom out</i> operation.</li>
152
 *  </ul>
153
 * </p>
154
 * 
155
 * <p><b>Other:</b></p>
156
 * 
157
 * <p>Other useful capabilities of <code>MapControl</code>:
158
 *   <ul>
159
 *    <li>Cancel the current drawing process (notifying it also to the inner
160
 *     <code>MapContext</code> instance and its layers): {@link #cancelDrawing() #cancelDrawing()}.</li>
161
 *    <li>Applying a <i>zoom in</i> operation centered at mouse position (without a <code>ToolListener</code>): {@link #zoomIn() #zoomIn()}.</li>
162
 *    <li>Applying a <i>zoom out</i> operation centered at mouse position (without a <code>ToolListener</code>): {@link #zoomOut() #zoomOut()}.</li>
163
 *   </ul>
164
 * </p>
165
 * 
166
 * @see CancelDraw
167
 * @see Drawer2
168
 * @see MapContextListener
169
 * @see MapToolListener
170
 * 
171
 * @author Fernando Gonz?lez Cort?s
172
 */
173
public class MapControl extends JComponent implements ComponentListener, CommandListener {
174
        /**
175
         * <p>One of the possible status of <code>MapControl</code>. Determines that all visible information has been
176
         * drawn and its updated.</p> 
177
         */
178
        public static final int ACTUALIZADO = 0;
179

    
180
        /**
181
         * <p>One of the possible status of <code>MapControl</code>. Determines that not all visible information has been
182
         * drawn or isn't updated.</p> 
183
         */
184
        public static final int DESACTUALIZADO = 1;
185
        
186
        /**
187
         * <p>One of the possible status of <code>MapControl</code>. Determines that only the graphical layer must
188
         * be drawn / updated.</p> 
189
         */
190
        public static final int ONLY_GRAPHICS = 2;
191

    
192
        /**
193
         * <p>Determines the number of frames.</p>
194
         *
195
         * <p>Number of updates per second that the timer will invoke repaint this component.</p>
196
         */
197
    private static int drawFrameRate = 3;
198

    
199
    /**
200
     * <p>Determines if the drawer can update this <code>MapControl</code> instance when the timer launches an event.</p>
201
     */
202
    private static boolean drawAnimationEnabled = true;
203

    
204
    // public static final int FAST_PAINT = 3;
205
        //private static Logger logger = Logger.getLogger(MapControl.class.getName());
206

    
207
    /**
208
     * <p>Inner model with the layers, event support for drawing them, and the <code>ViewPort</code>
209
     *  with information to adapt to the bounds available in <i>image coordinates</i>.</p>
210
     *  
211
     * @see #getMapContext()
212
     * @see #setMapContext(MapContext) 
213
     */
214
    private MapContext mapContext = null;
215

    
216
    //private boolean drawerAlive = false;
217

    
218

    
219
        /**
220
         * <p>All registered <code>Behavior</code> that can define a way to work with this <code>MapControl</code>.</p>
221
         * 
222
         * <p>Only one of them can be active at a given moment.</p>
223
         * 
224
         * @see #addMapTool(String, Behavior)
225
         * @see #addMapTool(String, Behavior[])
226
         * @see #getMapTool(String)
227
         * @see #getMapToolsKeySet() 
228
         * @see #getNamesMapTools()
229
         */
230
    protected HashMap namesMapTools = new HashMap();
231

    
232
        /**
233
         * <p>Active {@link Behavior Behavior} that will generate events according a criterion, and then, with a {@link ToolListener ToolListener}
234
         *  associated, will simulate to user that works with this component as a particular tool.</p>
235
         *  
236
         * @see #getCurrentMapTool()
237
         * @see #getCurrentTool()
238
         * @see #setTool(String) 
239
         */
240
    protected Behavior currentMapTool = null;
241

    
242
        /**
243
         * <p>Determines which's the current drawn status of this component:
244
         * <ul>
245
         *  <li><b>OUTDATED</b>: all visible information has been drawn or isn't updated.</li>
246
         *  <li><b>UTDATED</b>: all visible information has been drawn and its updated.</li>
247
         *  <li><b>ONLY_GRAPHICS</b>: only the graphical layer must be drawn / updated.</li>
248
         * </ul>
249
         * </p>
250
         * 
251
         * <p>The <code>MapControl</code> drawing process will consider the value of this parameter to decide which elements will
252
         *  be updated or drawn.</p>
253
         */
254
    private int status = DESACTUALIZADO;
255

    
256
        /**
257
         * <p>Image with a buffer to accelerate the draw the changes of the graphical items in this component.</p>
258
         * 
259
         * <p>Firstly, information will be drawn in the buffer, and, when is outright drawn, that information will be displayed.
260
         * Meanwhile, the previous image can be kept showed.</p>
261
         * 
262
         * @see BufferedImage
263
         * 
264
         * @see #getImage()
265
         */
266
    private BufferedImage image = null;
267

    
268
        /**
269
         * <p>Name of the tool used currently to interact with this component.</p>
270
         * 
271
         * @see #getCurrentTool()
272
         * @see #setTool(String) 
273
         */
274
    protected String currentTool;
275

    
276
        /**
277
         * <p>Object to store the flag that notifies a drawing thread task and <code>MapContext</code>'s layers,
278
         * that must be canceled or can continue with the process.</p>
279
         * 
280
         * @see #cancelDrawing()
281
         */
282
    private CancelDraw canceldraw;
283

    
284
    //private boolean isCancelled = true;
285

    
286
        /**
287
         * <p>Fires an action events after a specified delay.</p>
288
         * 
289
         * <p><code>MapControl</code> will use the timer to update its visible graphical information during
290
         *  a drawing process, or allowing to cancel that process.</p>
291
         * 
292
         * <p>This is very useful to pretend faster interactivity to user when <code>MapControl</code> has
293
         *  lots of layers, and / or layers with heavy graphical elements, that need a long time to finish
294
         *  drawing all its data.</p> 
295
         */
296
    private Timer timer;
297

    
298
        /**
299
         * <p>Reference to the {@link ViewPort ViewPort} of the {@link MapContext MapContext} of this component.</p>
300
         * 
301
         * <p>The view port once is created an instance of <code>MapControl</code>,
302
         *  is obtained from the <i>EPSG:23030</i> projection, that's the default projection for this component.</p>
303
         *  
304
         * <p>After, the view port will change adapting itself according the current projection and the extent.</p>
305
         * 
306
         * @see #getViewPort()
307
         * 
308
         * @see ViewPort
309
         */
310
    protected ViewPort vp;
311

    
312
    //private Drawer drawer;
313

    
314
        /**
315
         * <p>Manager of all <code>MapControl</code> painting requests.</p>
316
         */
317
    private Drawer2 drawer2;
318

    
319
    // private boolean firstDraw = true;
320

    
321
        /**
322
         * <p>Listener of all kind of mouse events produced in this component.</p>
323
         * 
324
         * <p>Delegates each mouse event to the current map tool.</p>
325
         * 
326
         * @see #addMapTool(String, Behavior)
327
         * @see #addMapTool(String, Behavior[])
328
         * @see #getMapTool(String)
329
         * @see #getMapToolsKeySet()
330
         * @see #getNamesMapTools()
331
         * @see #setTool(String)
332
         */
333
    protected MapToolListener mapToolListener = new MapToolListener();
334

    
335
        /**
336
         * <p>Listener of all events produced in a this component's <code>MapContext</code>
337
         * object during an atomic period of time.</p>
338
         */
339
    private MapContextListener mapContextListener = new MapContextListener();
340

    
341
        /**
342
         * <p>Group of <code>ExceptionListener</code> that, in whatever moment could be notified a Throwable Java error or exception.</p>
343
         * 
344
         * @see #addExceptionListener(ExceptionListener)
345
         * @see #removeExceptionListener(ExceptionListener)
346
         */
347
    private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
348

    
349
    /**
350
     * <p>Name of the previous tool used.</p>
351
     */
352
        protected String prevTool;
353

    
354
        /**
355
     * We need this to avoid not wanted refresh. REMEMBER TO SET TO TRUE!!
356
     */
357
    // private boolean paintEnabled = false;
358

    
359
        /**
360
         * <p>Creates a new <code>MapControl</code> instance with the following characteristics:
361
         * <ul>
362
         *  <li><i>Name</i>: MapControl .</li>
363
         *  <li>Disables the double buffer of <code>JComponent</code> .</li>
364
         *  <li>Sets opaque <i>(see {@link JComponent#setOpaque(boolean)} )</i>. </li>
365
         *  <li>Sets its status to <code>OUTDATED</code> .</li>
366
         *  <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>
367
         *  <li>Creates a new {@link MapContext MapContext} with a new {@link ViewPort ViewPort} in the projection <i>"EPSG:23030"</i> .</li>
368
         *  <li>Creates a new {@link CommandListener CommandListener} for edition operations.</li>
369
         *  <li>Creates a new {@link MapToolListener MapToolListener}, and associates it as a listener of whatever kind of mouse events produced in this component.</li>
370
         *  <li>Creates a new {@link Drawer2 Drawer2} for managing the painting requests.</li>
371
         *  <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
372
         *   <code>drawAnimationEnabled</code>.</li>
373
         * </ul>
374
         * </p>
375
         */
376
        public MapControl() {
377
                this.setName("MapControl");
378
                setDoubleBuffered(false);
379
                setOpaque(true);
380
                status = DESACTUALIZADO;
381

    
382
                //Clase usada para cancelar el dibujado
383
                canceldraw = new CancelDraw();
384

    
385
                //Modelo de datos y ventana del mismo
386
                // TODO: Cuando creamos un mapControl, deber?amos asignar
387
                // la projecci?n por defecto con la que vayamos a trabajar.
388
                // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
389
                vp = new ViewPort(CRSFactory.getCRS("EPSG:23030"));
390
                setMapContext(new MapContext(vp));
391

    
392
                //eventos
393
                this.addComponentListener(this);
394
                this.addMouseListener(mapToolListener);
395
                this.addMouseMotionListener(mapToolListener);
396
                this.addMouseWheelListener(mapToolListener);
397

    
398
        this.drawer2 = new Drawer2();
399
                //Timer para mostrar el redibujado mientras se dibuja
400
                timer = new Timer(1000/drawFrameRate,
401
                                new ActionListener() {
402
                                        public void actionPerformed(ActionEvent e) {
403

    
404
                                                if (drawAnimationEnabled) {
405
                                                        MapControl.this.repaint();
406
                                                }
407
                                        }
408
                                });
409
        }
410

    
411
        /**
412
         * <p>Sets a <code>MapContext</code> to this component.</p>
413
         * 
414
         * <p>The <code>MapContext</code> has the <i>model</i>, and most of the <i>view</i>,
415
         * and <i>control</i> logic of the layers of this component, including a {@link ViewPort ViewPort} to adapt the
416
         * information to the projection, and to display it in the available area.</p>
417
         * 
418
         * <p>If <code>model</code> hadn't a <code>ViewPort</code>, assigns the current one to it, otherwise, use its <code>ViewPort</code>.</p>
419
         * 
420
         * <p>After assigning the <code>MapContext</code> and <code>ViewPort</code>, sets the same {@link MapContextListener MapContextListener}
421
         *  that was using, and changes the <i>status</i> to <code>OUTDATED</code>.</p>
422
         * 
423
         * @param model this component's <code>MapContext</code>, that includes the <code>ViewPort</code>.
424
         * 
425
         * @see MapContext
426
         * 
427
         * @see #getMapContext()
428
         */
429
        public void setMapContext(MapContext model) {
430
                if (mapContext != null) {
431
                        mapContext.removeAtomicEventListener(mapContextListener);
432
                }
433

    
434
                mapContext = model;
435

    
436
                if (mapContext.getViewPort() == null) {
437
                        mapContext.setViewPort(vp);
438
                } else {
439
                        vp = mapContext.getViewPort();
440

    
441
                        // vp.setImageSize(new Dimension(getWidth(), getHeight()));
442
                        //System.err.println("Viewport en setMapContext:" + vp);
443
                }
444

    
445
                mapContext.addAtomicEventListener(mapContextListener);
446

    
447
                status = DESACTUALIZADO;
448
        }
449

    
450
        /**
451
         * <p>Gets this component's {@link MapContext MapContext} projection.</p>
452
         *
453
         * @return this component's {@link MapContext MapContext} projection
454
         * 
455
         * @see MapContext#getProjection()
456
         * @see MapControl#setProjection(IProjection)
457
         */
458
        public IProjection getProjection() {
459
                return getMapContext().getProjection();
460
        }
461

    
462
        /**
463
         * <p>Sets the projection to this component's {@link MapContext MapContext}.</p>
464
         *
465
         * @param proj the kind of projection to this component's {@link MapContext MapContext}
466
         * 
467
         * @see MapContext#setProjection(IProjection)
468
         * @see MapControl#getProjection()
469
         */
470
        public void setProjection(IProjection proj) {
471
                getMapContext().setProjection(proj);
472
        }
473

    
474
        /**
475
         * <p>Gets this component's <code>MapContext</code>, with the <i>model</i>, and most of the <i>view</i>,
476
         * and <i>control</i> logic of the layers of this component, including a {@link ViewPort ViewPort} to adapt the
477
         * information to the projection, and display it in the available area.</p>
478
         * 
479
         * @return this component's <code>MapContext</code>, that includes the <code>ViewPort</code> used to project the
480
         * graphical information, and display it in the available area
481
         * 
482
         * @see MapContext
483
         * 
484
         * @see MapControl#setMapContext(MapContext)
485
         */
486
        public MapContext getMapContext() {
487
                return mapContext;
488
        }
489

    
490
        /**
491
         * <p>Registers a new behavior to this component.</p>
492
         * 
493
         * <p>According the nature of the {@link Behavior Behavior}, different events will be generated. Those
494
         *  events can be caught by a particular {@link ToolListener ToolListener}, allowing user to interact with this
495
         *  <code>MapControl</code> object as a <i>tool</i>.</p>
496
         *
497
         * @param name name to identify the behavior to add
498
         * @param tool the behavior to add
499
         * 
500
         * @see #addMapTool(String, Behavior[])
501
         * @see #getNamesMapTools()
502
         * @see #getMapToolsKeySet()
503
         * @see #hasTool(String)
504
         */
505
        public void addMapTool(String name, Behavior tool) {
506
                namesMapTools.put(name, tool);
507
                tool.setMapControl(this);
508
        }
509

    
510
        /**
511
         * <p>Registers a new behavior to this component as a {@link CompoundBehavior CompoundBehavior} made up of <code>tools</code>.</p>
512
         * 
513
         * <p>According the nature of the behaviors registered, different events will be generated. Those
514
         *  events can be caught by a particular {@link ToolListener ToolListener}, allowing user to interact with this
515
         *  <code>MapControl</code> object as a <i>tool</i>.</p>
516
         *
517
         * @param name name to identify the compound behavior to add
518
         * @param tools the compound behavior to add
519
         * 
520
         * @see #addMapTool(String, Behavior)
521
         * @see #getNamesMapTools()
522
         * @see #getMapToolsKeySet()
523
         * @see #hasTool(String)
524
         */
525
        public void addMapTool(String name, Behavior[] tools){
526
                CompoundBehavior tool = new CompoundBehavior(tools);
527
                addMapTool(name, tool);
528
        }
529

    
530
        /**
531
         * <p>Gets the <code>Behavior</code> registered in this component, identified
532
         *  by <code>name</code>.</p>
533
         *
534
         * @param name name of a registered behavior
535
         * 
536
         * @return tool the registered behavior in this component as <code>name</code>, or <code>null</code> if
537
         *  no one has that identifier
538
         *  
539
         * @see #addMapTool(String, Behavior)
540
         * @see #addMapTool(String, Behavior[])
541
         * @see #hasTool(String)
542
         */
543
        public Behavior getMapTool(String name) {
544
                return (Behavior)namesMapTools.get(name);
545
        }
546

    
547
        /**
548
         * <p>Returns a set view of the keys that identified the tools
549
         *  registered.</p>
550
         *
551
         * @return a set view of the keys that identified the tools registered
552
         * 
553
         * @see HashMap#keySet()
554
         * 
555
         * @see #getNamesMapTools()
556
          * @see #addMapTool(String, Behavior)
557
          * @see #addMapTool(String, Behavior[])
558
         */
559
        public Set getMapToolsKeySet() {
560
                return namesMapTools.keySet();
561
        }
562

    
563
        /**
564
         * <p>Returns <code>true</code> if this component contains a tool identified by <code>toolName</code>.</p>
565
         * 
566
         * @param toolName identifier of the tool
567
         * 
568
         * @return <code>true</code> if this component contains a tool identified by <code>toolName</code>; otherwise <code>false</code>
569
         * 
570
         * @see #addMapTool(String, Behavior)
571
         * @see #addMapTool(String, Behavior[])
572
         */
573
        public boolean hasTool(String toolName) {
574
                return namesMapTools.containsKey(toolName);
575
        }
576

    
577
        /**
578
         * <p>Sets as current active <code>Behavior</code> associated to this component, that one which
579
         *  is registered and identified by <code>toolName</code>.</p>
580
         * 
581
         * <p>Changing the current active behavior for this <code>MapControl</code>, implies also updating the
582
         *  previous <i>behavior</i> tool, and the current cursor.</p>
583
         * 
584
         * @param toolName name of a registered behavior
585
         * 
586
         * @see #getCurrentMapTool()
587
         * @see #getCurrentTool()
588
         */
589
        public void setTool(String toolName) {
590
                prevTool=getCurrentTool();
591
                Behavior mapTool = (Behavior) namesMapTools.get(toolName);
592
                currentMapTool = mapTool;
593
                currentTool = toolName;
594
                this.setCursor(mapTool.getCursor());
595
        }
596

    
597
        /**
598
         * <p>Gets as current active <code>Behavior</code> associated to this component, that one which
599
         *  is registered and identified by <code>toolName</code>.</p>
600
         * 
601
         * <p>Changing the current active behavior for this <code>MapControl</code>, implies also updating the
602
         *  previous <i>behavior</i> tool, and the current cursor.</p>
603
         * 
604
         * @param toolName name of a registered behavior
605
         * 
606
         * @see #getCurrentTool()
607
         * @see #setTool(String)
608
         */
609
        public Behavior getCurrentMapTool(){
610
                return currentMapTool;
611
        }
612

    
613
        /**
614
         * <p>Returns the name of the current selected tool on this MapControl</p>
615
         *
616
         * @return the name of the current's behavior tool associated to this component
617
         * 
618
         * @see #getCurrentMapTool()
619
         * @see #setTool(String)
620
         */
621
        public String getCurrentTool() {
622
                return currentTool;
623
        }
624

    
625
        /**
626
         * <p>Determines that current drawing process of <code>MapControl</code>'s <code>MapContext</code>'s data must be canceled.</p>
627
         * 
628
         * <p>It has no effects if now isn't drawing that graphical information.</p>
629
         *  
630
         * <p>At last resort, the particular implementation of each layer in this <code>MapControl</code>'s <code>MapContrext</code>
631
     *   will be that one which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</p>
632
         */
633
        public void cancelDrawing() {
634
                /* if (drawer != null) {
635
                        if (!drawer.isAlive()) {
636
                                return;
637
                        }
638
                }
639
                */
640
                canceldraw.setCanceled(true);
641

    
642
                /* while (!isCancelled) {
643
                        if (!drawer.isAlive()) {
644
                            // Si hemos llegado aqu? con un thread vivo, seguramente
645
                            // no estamos actualizados.
646

647
                                break;
648
                        }
649

650
                }
651
                canceldraw.setCancel(false);
652
                isCancelled = false;
653
        drawerAlive = false; */
654
        }
655

    
656
        /**
657
         * <p>Creates a {@link BufferedImage BufferedImage} image if there was no buffered image, or if
658
         *  its viewport's image height or width is different from this component's size. Once has created
659
         *  a double-buffer, fills it with the vieport's background color, or with <i>white</i> if it had no background color.</p>
660
         * 
661
         * <p>If no double-buffered existed, creates a {@link BufferedImage BufferedImage} with the size of this component,
662
         * and as an image with 8-bit RGBA color components packed into integer pixels. That image has a <code>DirectColorModel</code> with alpha.
663
         * The color data in that image is considered not to be premultiplied with alpha.</p> 
664
         * 
665
         * <p>Once has created and filled the new inner <code>MapControl</code>'s double-buffer, changes the status to
666
         * <code>OUTDATED</code>.</p>
667
         * 
668
         * @return <code>true</code> if has created and filled a new double-buffer for this <code>MapControl</code> instance; otherwise <code>false</code>
669
         */
670
    private boolean adaptToImageSize()
671
    {
672
        if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight()))
673
        {
674
            image = new BufferedImage(this.getWidth(), this.getHeight(),
675
                    BufferedImage.TYPE_INT_ARGB);
676
            // ESTILO MAC
677
//                image = GraphicsEnvironment.getLocalGraphicsEnvironment()
678
//                                .getDefaultScreenDevice().getDefaultConfiguration()
679
//                                .createCompatibleImage(this.getWidth(), this.getHeight());
680
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
681
            getMapContext().getViewPort().refreshExtent();
682

    
683

    
684
            Graphics gTemp = image.createGraphics();
685
            Color theBackColor = vp.getBackColor();
686
            if (theBackColor == null)
687
                gTemp.setColor(Color.WHITE);
688
            else
689
                gTemp.setColor(theBackColor);
690

    
691
            gTemp.fillRect(0,0,getWidth(), getHeight());
692
            gTemp.dispose();
693
            status = DESACTUALIZADO;
694
            // g.drawImage(image,0,0,null);
695
            return true;
696
        }
697
        return false;
698
    }
699

    
700
        /**
701
         * <p>Paints the graphical information of this component using a double buffer.</p>
702
         * 
703
         * <p>If the double buffer wasn't created, creates a new one.</p>
704
         * 
705
         * <p>Paints the component according the following algorithm:
706
         * <br>
707
         *  &nbsp If <i>status</i> is <i>UPDATED</i>:<br>
708
         *  &nbsp &nbsp If there is no <i>double buffer</i>:<br>
709
         *  &nbsp &nbsp &nbsp If there is a <i>behavior</i> for managing the <code>MapControl</code> instance, delegates
710
         *   the drawing process to that behavior, calling: <code><i>behavior_instance</i>.paintComponent(g)</code> &nbsp .<br>
711
         *  &nbsp &nbsp &nbsp Else, repaints the current graphical information quickly calling: <code>g.drawImage(image,0,0,null)</code> &nbsp .<br>
712
         *  &nbsp 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
713
         *   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
714
         *   (invoking <code>repaint()</code> that comprises invoke this method) the view every delay of 360 ms. during the the process drawing.</p>
715
         * 
716
           * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
717
           * @see Drawer2
718
         */
719
        protected void paintComponent(Graphics g) {
720
        adaptToImageSize();
721
        /* if (status == FAST_PAINT) {
722
            System.out.println("FAST_PAINT");
723
            g.drawImage(image,0,0,null);
724
            status = ACTUALIZADO;
725
            return;
726
        } */
727
        // System.out.println("PINTANDO MAPCONTROL" + this);
728
                if (status == ACTUALIZADO) {
729
                        // LWS logger.debug("Dibujando la imagen obtenida");
730

    
731
                        /*
732
                         * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
733
                         * en dicho behaviour
734
                         */
735
            if (image != null)
736
            {
737
                if (currentMapTool != null)
738
                    currentMapTool.paintComponent(g);
739
                else
740
                    g.drawImage(image,0,0,null);
741

    
742
                                // System.out.println("Pinto ACTUALIZADO");
743
                        }
744
                } else if ((status == DESACTUALIZADO)
745
                || (status == ONLY_GRAPHICS)) {
746
                        // LWS System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a");
747
                        /* if (isOpaque())
748
                        {
749
                            if (image==null)
750
                            {
751
                                g.setColor(vp.getBackColor());
752
                                g.fillRect(0,0,getWidth(), getHeight());
753
                            }
754
                            // else g.drawImage(image,0,0,null);
755
                        } */
756
            // cancelDrawing();
757
                        //Se crea la imagen con el color de fonde deseado
758
                        /* if (image == null)
759
                        {
760
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
761
                                                BufferedImage.TYPE_INT_ARGB);
762
                                vp.setImageSize(new Dimension(getWidth(), getHeight()));
763
                                Graphics gTemp = image.createGraphics();
764
                            Color theBackColor = vp.getBackColor();
765
                            if (theBackColor == null)
766
                                gTemp.setColor(Color.WHITE);
767
                            else
768
                                gTemp.setColor(theBackColor);
769

770
                                gTemp.fillRect(0,0,getWidth(), getHeight());
771
                                gTemp.dispose();
772
                                // g.drawImage(image,0,0,null);
773
                                System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
774
                        } */
775
            // else
776
            // {
777

    
778

    
779
            // if (image != null)
780
            //  {
781
                g.drawImage(image,0,0,null);
782

    
783
                drawer2.put(new PaintingRequest());
784
                timer.start();
785
            /* }
786
            else
787
                return; */
788
            // }
789

    
790
            /* if (drawerAlive == false)
791
            {
792
                drawer = new Drawer(image, canceldraw);
793
                drawer.start();
794
                        //Se lanza el tread de dibujado
795
            } */
796

    
797
                        // status = ACTUALIZADO;
798
                }
799
        }
800

    
801
        /**
802
         * <p>Gets the {@link BufferedImage BufferedImage} used to accelerate the draw of new ''frames'' with changes, 
803
         * or new graphical items in this component.</p>
804
         * 
805
         * @return double buffered image used by this component to accelerate the draw of its graphical information, or
806
         * <code>null</code> if isn't already created
807
         * 
808
         * @see BufferedImage 
809
         */
810
        public BufferedImage getImage() {
811
                return image;
812
        }
813

    
814
        /**
815
         * <p>Forces repaint all visible graphical information in this component.</p>
816
         * 
817
         * <p>If <code>doClear == true</code>, before repainting, clears all graphics before repainting.</p>
818
         *
819
         * @param doClear <code>true</code> if needs clearing graphics drawn on the map
820
         * 
821
         * @see #cancelDrawing()
822
         * @see FLayers#setDirty(boolean)
823
         */
824
        public void drawMap(boolean doClear) {
825
                cancelDrawing();
826
                //System.out.println("drawMap con doClear=" + doClear);
827
        status = DESACTUALIZADO;
828
        getMapContext().getLayers().setDirty(true);
829
                if (doClear)
830
        {
831
//            // image = null; // Se usa para el PAN
832
            if (image != null)
833
            {
834
                Graphics2D g = image.createGraphics();
835
                g.setColor(Color.BLACK);
836
////                Color theBackColor = vp.getBackColor();
837
////                if (theBackColor == null)
838
////                    g.setColor(Color.WHITE);
839
////                else
840
////                    g.setColor(theBackColor);
841
//                g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight());
842
                g.dispose();
843
            }
844
        }
845
                repaint();
846
        }
847

    
848
        /**
849
         * <p>Cancels any current drawing process, changing the status to <code>OUTDATED</code>, and forcing
850
         * repaint only the layers dirty.</p>
851
         * 
852
         * @see #cancelDrawing()
853
         */
854
        public void rePaintDirtyLayers()
855
        {
856
                cancelDrawing();
857
        status = DESACTUALIZADO;
858
        repaint();
859
        }
860

    
861
        /**
862
         * <p>Cancels any current drawing process, changing the status to <code>ONLY_GRAPHICS</code>, and forcing
863
         * repaint only the graphical layer of the <code>MapContext</code>.</p>
864
         */
865
    public void drawGraphics() {
866
        status = ONLY_GRAPHICS;
867
        repaint();
868
    }
869

    
870
        /**
871
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
872
         */
873
        public void componentHidden(ComponentEvent e) {
874
        }
875

    
876
        /**
877
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
878
         */
879
        public void componentMoved(ComponentEvent e) {
880
        }
881

    
882
        /**
883
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
884
         */
885
        public void componentResized(ComponentEvent e) {
886
                /* image = new BufferedImage(this.getWidth(), this.getHeight(),
887
                                BufferedImage.TYPE_INT_ARGB);
888
                Graphics gTemp = image.createGraphics();
889
                gTemp.setColor(vp.getBackColor());
890
                gTemp.fillRect(0,0,getWidth(), getHeight());
891
        System.out.println("MapControl resized");
892
            // image = null;
893
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
894
                getMapContext().getViewPort().setScale(); */
895
                // drawMap(true);
896
        }
897

    
898
        /**
899
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
900
         */
901
        public void componentShown(ComponentEvent e) {
902
        }
903

    
904
        /**
905
         * @see ExceptionHandlingSupport#addExceptionListener(ExceptionListener)
906
         */
907
        public void addExceptionListener(ExceptionListener o) {
908
                exceptionHandlingSupport.addExceptionListener(o);
909
        }
910

    
911
        /**
912
         * @see ExceptionHandlingSupport#removeExceptionListener(ExceptionListener)
913
         */
914
        public boolean removeExceptionListener(ExceptionListener o) {
915
                return exceptionHandlingSupport.removeExceptionListener(o);
916
        }
917

    
918
        /**
919
         * @see ExceptionHandlingSupport#throwException(Throwable)
920
         */
921
        protected void throwException(Throwable t) {
922
                exceptionHandlingSupport.throwException(t);
923
        }
924

    
925
        /**
926
         * <p>Represents each <code>MapControl</code>'s data painting request.</p>
927
         * 
928
         * <p>The request will be attended by a <code>Drawer2</code>, which will hold it since the <code>Drawer2</code>'s worker
929
         *  takes it, or arrives a new painting request, which will replace it.</p> 
930
         */
931
    private class PaintingRequest
932
    {
933
            /**
934
             * <p>Creates a new <code>PaintingRequest</p> instance.</p>
935
             */
936
        public PaintingRequest()
937
        {
938
        }
939

    
940
        /**
941
         * <p><code>MapControl</code> paint process:</p>
942
         * 
943
         * <p>
944
         *  <ul>
945
         *   <li><i>1.- </i>Cancels all previous <code>MapControl</code>'s drawing processes.</li>
946
         *   <li><i>2.- </i>If <i>status</i> was OUTDATED:
947
         *    <ul>
948
         *     <li><i>2.1.- </i>Fills the background color with viewport's background color, or <i>white</i> if it was undefined.</li>
949
         *     <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>
950
         *     <li><i>2.3.- </i>If <code>canceldraw.isCanceled()</code>
951
         *      <ul>
952
         *       <li><i>2.3.1.- </i>Sets <i>status</i> to OUTDATED.</li>
953
         *       <li><i>2.3.2.- </i>Sets <i>dirty</i> all layers stored in <i>MapContext</i>.</li>
954
         *      </ul>
955
         *     </li>
956
         *     <li><i>2.4.- </i>Else, sets <i>status</i> to UPDATED.</li>
957
         *    </ul>
958
         *   </li>
959
         *   <li><i>3.- </i>Else, if <i>status</i> was ONLY_GRAPHICS:
960
         *    <ul>
961
         *     <li><i>3.1.- </i>Sets <i>status</i> to UPDATED.</li>
962
         *     <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>
963
         *    </ul>
964
         *   </li>
965
         *   <li><i>4.- </i>Stops the <i>timer</i>.</li>
966
         *   <li><i>5.- </i>Repaints this component invoking: <code>repaint();</code></li>
967
         *  </ul>
968
         * </p>
969
         * 
970
         * @see #cancelDrawing()
971
         * @see MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)
972
         * @see MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)
973
         * 
974
         * @see ViewPort
975
         */
976
        public void paint()
977
        {
978
            try
979
            {
980
                    canceldraw.setCanceled(false);
981
                /* if (image == null)
982
                {
983
                    image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
984
                            BufferedImage.TYPE_INT_ARGB);
985
                    Graphics gTemp = image.createGraphics();
986
                    Color theBackColor = vp.getBackColor();
987
                    if (theBackColor == null)
988
                        gTemp.setColor(Color.WHITE);
989
                    else
990
                        gTemp.setColor(theBackColor);
991

992
                    gTemp.fillRect(0,0,getWidth(), getHeight());
993
                    gTemp.dispose();
994
                    // g.drawImage(image,0,0,null);
995
                    System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
996
                } */
997
                Graphics2D g = image.createGraphics();
998

    
999
                ViewPort viewPort = mapContext.getViewPort();
1000

    
1001
                if (status == DESACTUALIZADO)
1002
                {
1003
                        Graphics2D gTemp = image.createGraphics();
1004
                    Color theBackColor = viewPort.getBackColor();
1005
                    if (theBackColor == null)
1006
                        gTemp.setColor(Color.WHITE);
1007
                    else
1008
                        gTemp.setColor(theBackColor);
1009
                    gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
1010
                    // ESTILO MAC
1011
//                  BufferedImage imgMac = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
1012
//                          BufferedImage.TYPE_INT_ARGB);
1013
          //
1014
//                  mapContext.draw(imgMac, g, canceldraw, mapContext.getScaleView());
1015
//                  g.drawImage(imgMac, 0, 0, null);
1016
                  // FIN ESTILO MAC
1017
                  // SIN MAC:
1018

    
1019
                  mapContext.draw(image, g, canceldraw, mapContext.getScaleView());
1020
                  if (!canceldraw.isCanceled()){
1021
                          status=ACTUALIZADO;
1022
                 }else{
1023
                          status=DESACTUALIZADO;
1024
                          getMapContext().getLayers().setDirty(true);
1025
                  }
1026

    
1027
                }
1028
                else if (status == ONLY_GRAPHICS)
1029
                {
1030
                    status = ACTUALIZADO;
1031
                    mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView());
1032

    
1033
                }
1034

    
1035

    
1036
                // status = FAST_PAINT;
1037
              //  drawerAlive = false;
1038
                timer.stop();
1039
                repaint();
1040

    
1041

    
1042
            } catch (Throwable e) {
1043
                timer.stop();
1044
              //  isCancelled = true;
1045
                e.printStackTrace();
1046
                throwException(e);
1047
            } finally {
1048
            }
1049
        }
1050
    }
1051

    
1052
    /**
1053
     * <p>An instance of <code>Drawer2</code> could manage all <code>MapControl</code> painting requests.</p>
1054
     * 
1055
     * <p>Based on the <i>WorkerThread</i> software pattern, creates a worker thread that will attend sequentially
1056
     *  the current waiting painting request, after finishing the previous (that could be by a cancel action).</p>
1057
     * 
1058
     * <p>All new {@link PaintingRequest PaintingRequest} generated will be stored as <i>waiting requests</i> since the worker
1059
     * attends it.</p>
1060
     * 
1061
     * <p>If a worker finished and there was no <i>painting request</i>, the worker would be set to wait until any
1062
     *  <i>painting request</i> would be put.</p>
1063
     * 
1064
     * @author fjp
1065
     */
1066
    public class Drawer2
1067
    {
1068
        // Una mini cola de 2. No acumulamos peticiones de dibujado
1069
        // dibujamos solo lo ?ltimo que nos han pedido.
1070
            
1071

    
1072
            /**
1073
             * <p>Painting request that's been attended by the <code>Drawer2</code>'s worker.</p>
1074
             * 
1075
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1076
             * @see #take()
1077
             */
1078
            private PaintingRequest paintingRequest;
1079

    
1080
            /**
1081
             * <p>Painting request waiting to be attended by the <code>Drawer2</code>'s worker.</p>
1082
             * 
1083
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1084
             * @see #take()
1085
             */
1086
            private PaintingRequest waitingRequest;
1087

    
1088
            /**
1089
             * <p>Determines that the <code>Drawer2</code>'s worker is busy attending a painting request.</p> 
1090
              * 
1091
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1092
             * @see #take()
1093
             */
1094
        private boolean waiting;
1095

    
1096
            /**
1097
             * <p>Notifies the <code>Drawer2</code>'s worker to finish or continue with its process.</p>
1098
             * 
1099
             * @see #setShutdown(boolean)
1100
             */
1101
        private boolean shutdown;
1102

    
1103
        /**
1104
         * <p>Sets this <code>Drawer2</code>'s worker to finish or continue with its process.</p>
1105
         * 
1106
         * @param isShutdown a boolean value
1107
         */
1108
        public void setShutdown(boolean isShutdown)
1109
        {
1110
            shutdown = isShutdown;
1111
        }
1112

    
1113
        /**
1114
         * <p>Creates a new drawer for managing all data painting requests in <code>MapControl</code>.</p>
1115
         * 
1116
         * <p>Includes the following steps:
1117
         *  <ul>
1118
         *   <li>By default, there is no <i>current painting request</i>.</li>
1119
         *   <li>By default, there is no <i>waiting painting request</i>.</li>
1120
         *   <li>By default, the worker thread is waiting no <i>painting request</i>.</li>
1121
         *   <li>By default, the worker thread is running.</li>
1122
         *   <li>Creates and starts a worker thread for attending the <i>painting requests</i>.</li>
1123
         *  </ul>
1124
         * </p>
1125
         */
1126
        public Drawer2()
1127
        {
1128
            paintingRequest = null;
1129
            waitingRequest = null;
1130
            waiting = false;
1131
            shutdown = false;
1132
            new Thread(new Worker()).start();
1133
        }
1134

    
1135
        /**
1136
         * <p>Sets a <code>PaintingRequest</code> to be attended by the worker thread of this object. If 
1137
         *  this one was waiting, wakes up.</p>
1138
         * 
1139
         * <p>All waiting threads will be notified synchronized.</p>
1140
         * 
1141
         * @param newPaintRequest
1142
         * 
1143
         * @see #take()
1144
         */
1145
        public void put(PaintingRequest newPaintRequest)
1146
        {
1147
            waitingRequest = newPaintRequest;
1148
            if (waiting)
1149
            {
1150
                synchronized (this) {
1151
                    notifyAll();
1152
                }
1153
            }
1154
        }
1155

    
1156
        /**
1157
         * <p>Used by this object's worker, returns the current waiting drawing request, causing current thread
1158
         *  to wait until another thread invokes {@link #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest) #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)},
1159
         *  if there was no waiting request.</p>
1160
         * 
1161
         * <p>All threads will access synchronized to the waiting request.</p>
1162
         * 
1163
         * @return <code>PaintingRequest</code> that was waiting to be attended
1164
         * 
1165
         * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1166
         */
1167
        public PaintingRequest take()
1168
        {
1169
            if (waitingRequest == null)
1170
            {
1171
                synchronized (this) {
1172
                    waiting = true;
1173
                    try {
1174
                        wait();
1175
                    }
1176
                    catch (InterruptedException ie)
1177
                    {
1178
                        waiting = false;
1179
                    }
1180
                }
1181
            }
1182
            paintingRequest = waitingRequest;
1183
            waitingRequest = null;
1184
            return paintingRequest;
1185
        }
1186

    
1187
        /**
1188
         * <p>Thread for attending painting requests.</p>
1189
         * 
1190
         * <p>If there was no double buffer, sets the status to <code>OUTDATED</code> and finishes, otherwise
1191
         *  takes the painting request (it's probably that would wait some time), cancel the previous drawing
1192
         *  process, and starts processing the request.</p>
1193
         * 
1194
         * @see Thread
1195
         */
1196
        private class Worker implements Runnable
1197
        {
1198
                /*
1199
                 * (non-Javadoc)
1200
                 * @see java.lang.Runnable#run()
1201
                 */
1202
                public void run()
1203
            {
1204
                while (!shutdown)
1205
                {
1206
                    PaintingRequest p = take();
1207
                    //System.out.println("Pintando");
1208
                    if (image != null){
1209
                            cancelDrawing();
1210
                        p.paint();
1211
                    } else{
1212
                        status = DESACTUALIZADO;
1213
                    }
1214
                }
1215
            }
1216
        }
1217
    }
1218

    
1219
        /**
1220
         * <p><code>Drawer</code> is implemented for drawing the layers of a <code>MapControl</code>'s <i>MapContext</i> instance
1221
         *  as a <i>thread</i> of execution.</p>
1222
         *  
1223
         * <p>Draws <code>MapControl</code> according its <i>status</i>:
1224
         *  <ul>
1225
         *   <li><code><i>ONLY_GRAPHICS</i></code>: refreshes only the graphical layer, changing the status to <code><i>UPDATED</i></code>, via
1226
         *    {@linkplain MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double) MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)}.</li>
1227
         *   <li><code><i>OUTDATED</i></code>: refreshes all layers, changing the status to <code><i>UPDATED</i></code>, via
1228
         *    {@linkplain MapContext#draw(BufferedImage, Graphics2D, Cancellable, double) MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)}.</li>
1229
         *  <ul>
1230
         * </p>
1231
         * 
1232
         * <p>This drawing process is accelerated by a <code>BufferedImage</code>, and can be canceled.</p>
1233
         * 
1234
         * <p>Once the drawing process has finished, the timer stops and this component gets repainted.</p> 
1235
         *
1236
         * @deprecated
1237
         * @author Vicente Caballero Navarro
1238
         */
1239
        public class Drawer extends Thread {
1240
                //private Graphics g;
1241
                
1242
                /**
1243
                 * <p>Image with a buffer to accelerate the draw the changes of the graphical items in this component.</p>
1244
                 * 
1245
                 * <p>Firstly, information will be drawn in the buffer, and, when is outright drawn, that information will be displayed.
1246
                 * Meanwhile, the previous image can be kept showed.</p>
1247
                 * 
1248
                 * @see BufferedImage
1249
                 */
1250
                private BufferedImage image = null;
1251

    
1252
                /**
1253
                 * <p>Object to store the flag that notifies the drawing must be canceled or can continue with the process.</p>
1254
                 * 
1255
                 * <p>At last resort, the particular implementation of each layer in a <code>MapControl</code>'s <code>MapContrext</code>
1256
         *   will be which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</p>
1257
                 */
1258
                private CancelDraw cancel;
1259
                //private boolean threadCancel = false;
1260

    
1261
                /**
1262
                 * <p>Creates a new <code>Drawer</code> instance.</p>
1263
                 */
1264
                public Drawer(BufferedImage image, CancelDraw cancel)
1265
        {
1266
                        this.image = image;
1267
                        this.cancel = cancel;
1268
         //   drawerAlive = true;
1269
                }
1270

    
1271
                /**
1272
                 * @see java.lang.Runnable#run()
1273
                 * @see MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)
1274
                 * @see MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)
1275
                 */
1276
                public void run() {
1277
                        try {
1278
                                // synchronized (Drawer.class) {
1279
                                    Graphics2D g = image.createGraphics();
1280

    
1281
                                    ViewPort viewPort = mapContext.getViewPort();
1282
                    if (status == DESACTUALIZADO)
1283
                    {
1284
                                        Color theBackColor = viewPort.getBackColor();
1285
                                        if (theBackColor == null)
1286
                                            g.setColor(Color.WHITE);
1287
                                        else
1288
                                            g.setColor(theBackColor);
1289
                                            g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
1290
                        status = ACTUALIZADO;
1291
                        mapContext.draw(image, g, cancel,mapContext.getScaleView());
1292
                    }
1293
                    else if (status == ONLY_GRAPHICS)
1294
                    {
1295
                        status = ACTUALIZADO;
1296
                        mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView());
1297
                    }
1298

    
1299
                                        timer.stop();
1300
                    // status = FAST_PAINT;
1301
                  //  drawerAlive = false;
1302
                                        repaint();
1303

    
1304

    
1305

    
1306
                                // }
1307
                        } catch (Throwable e) {
1308
                            timer.stop();
1309
                                //isCancelled = true;
1310
                e.printStackTrace();
1311
                                throwException(e);
1312
                        } finally {
1313
                        }
1314
                }
1315
        }
1316

    
1317
        /**
1318
         * <p>An instance of <code>CancelDraw</code> will be shared by all this <code>MapControl</code>'s <code>MapContext</code> layers,
1319
         *  allowing receive a notification that, when they're been drawn, to be cancelled.</p>
1320
         *
1321
         * @see Cancellable
1322
         *
1323
         * @author Fernando Gonz?lez Cort?s
1324
         */
1325
        public class CancelDraw implements Cancellable {
1326
                /**
1327
                 * <p>Determines if the drawing task must be canceled or not.</p>
1328
                 * 
1329
                 * @see #isCanceled()
1330
                 * @see #setCanceled(boolean)
1331
                 */
1332
                private boolean cancel = false;
1333

    
1334
                /**
1335
                 * Creates a new <code>CancelDraw</code> object.
1336
                 */
1337
                public CancelDraw() {
1338
                }
1339

    
1340
                /*
1341
                 * (non-Javadoc)
1342
                 * @see com.iver.utiles.swing.threads.Cancellable#setCanceled(boolean)
1343
                 */
1344
                public void setCanceled(boolean b) {
1345
                        cancel = b;
1346
                }
1347

    
1348
                /*
1349
                 * (non-Javadoc)
1350
                 * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
1351
                 */
1352
                public boolean isCanceled() {
1353
                        return cancel;
1354
                }
1355
        }
1356

    
1357
        /**
1358
         * <p>Listens all kind of mouse events produced in {@link MapControl MapControl}, and invokes its current
1359
         *  map tool <i>({@link MapControl#getCurrentMapTool() MapControl#getCurrentMapTool()}</i> to simulate a behavior.</p>
1360
         *  
1361
         * <p>Mouse wheel moved events produce a <i>zoom in</i> operation if wheel rotation is negative, or a <i>zoom out</i>
1362
         *  if its positive. Both will be centered in the position of the mouse, but, meanwhile <i>zoom in</i> operation
1363
         *  applies a factor of 0.9, <i>zoom out</i> operation applies a factor of 1.2</p>
1364
         *  
1365
         * <p>Mouse wheel moved events can be produced as much frequently, that between each one, the drawing process could
1366
         *  hadn't finished. This is the reason that, in this situation, cancels always the previous drawing process before
1367
         *  applying a <i>zoom</i> operation, and ignores all new mouse positions that are produced before 1 second.</p>
1368
         *
1369
         * @author Fernando Gonz?lez Cort?s
1370
         */
1371
        public class MapToolListener implements MouseListener, MouseWheelListener,
1372
                MouseMotionListener {
1373

    
1374
                /**
1375
                 * <p>Used to avoid mouse wheel move events closed.</p>
1376
                 * 
1377
                 * <p>If a mouse wheel move event is produced 
1378
                 */
1379
                long t1;
1380

    
1381
                /**
1382
                 * <p>Position of the mouse, in map coordinates.</p>
1383
                 * 
1384
                 * <p>This point coordinates will be used as center of the <i>zoom</i> operation.</p>
1385
                 */
1386
                Point2D pReal;
1387

    
1388
                /**
1389
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
1390
                 * @see Behavior#mouseClicked(MouseEvent)
1391
                 */
1392
                public void mouseClicked(MouseEvent e) {
1393
                        try {
1394
                                if (currentMapTool != null)
1395
                                    currentMapTool.mouseClicked(e);
1396
                        } catch (BehaviorException t) {
1397
                                throwException(t);
1398
                        }
1399
                }
1400

    
1401
                /**
1402
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
1403
                 * @see Behavior#mouseEntered(MouseEvent)
1404
                 */
1405
                public void mouseEntered(MouseEvent e) {
1406
                        try {
1407
                                if (currentMapTool != null)
1408
                                        currentMapTool.mouseEntered(e);
1409
                        } catch (BehaviorException t) {
1410
                                throwException(t);
1411
                        }
1412
                }
1413

    
1414
                /**
1415
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
1416
                 * @see Behavior#mouseExited(MouseEvent)
1417
                 */
1418
                public void mouseExited(MouseEvent e) {
1419
                        try {
1420
                                if (currentMapTool != null)
1421
                                    currentMapTool.mouseExited(e);
1422
                        } catch (BehaviorException t) {
1423
                                throwException(t);
1424
                        }
1425
                }
1426

    
1427
                /**
1428
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
1429
                 * @see Behavior#mousePressed(MouseEvent)
1430
                 */
1431
                public void mousePressed(MouseEvent e) {
1432
                        try {
1433
                                if (currentMapTool != null)
1434
                                        currentMapTool.mousePressed(e);
1435
                        } catch (BehaviorException t) {
1436
                                throwException(t);
1437
                        }
1438
                }
1439

    
1440
                /**
1441
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
1442
                 * @see Behavior#mouseReleased(MouseEvent)
1443
                 */
1444
                public void mouseReleased(MouseEvent e) {
1445
                        try {
1446
                                if (currentMapTool != null)
1447
                                        currentMapTool.mouseReleased(e);
1448
                        } catch (BehaviorException t) {
1449
                                throwException(t);
1450
                        }
1451
                }
1452

    
1453
                /**
1454
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
1455
                 * @see Behavior#mouseWheelMoved(MouseWheelEvent)
1456
                 */
1457
                public void mouseWheelMoved(MouseWheelEvent e) {
1458
                        try {
1459
                                if (currentMapTool == null)
1460
                                        return;
1461

    
1462
                                currentMapTool.mouseWheelMoved(e);
1463

    
1464
                                // Si el tool actual no ha consumido el evento
1465
                                // entendemos que quiere el comportamiento por defecto.
1466
                                if (!e.isConsumed())
1467
                                {
1468
                                        // Para usar el primer punto sobre el que queremos centrar
1469
                                        // el mapa, dejamos pasar un segundo para considerar el siguiente
1470
                                        // punto como v?lido.
1471
                                        if (t1 == 0)
1472
                                        {
1473
                                                t1= System.currentTimeMillis();
1474
                                                pReal = vp.toMapPoint(e.getPoint());
1475
                                        }
1476
                                        else
1477
                                        {
1478
                                                long t2 = System.currentTimeMillis();
1479
                                                if ((t2-t1) > 1000)
1480
                                                        t1=0;
1481
                                        }
1482
                                        cancelDrawing();
1483
                                        ViewPort vp = getViewPort();
1484

    
1485

    
1486
                                        /* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
1487
                                                        vp.getAdjustedExtent().getCenterY()); */
1488
                                        int amount = e.getWheelRotation();
1489
                                        double nuevoX;
1490
                                        double nuevoY;
1491
                                        double factor;
1492

    
1493
                                        if (amount < 0) // nos acercamos
1494
                                        {
1495
                                                factor = 0.9;
1496
                                        } else // nos alejamos
1497
                                        {
1498
                                                factor = 1.2;
1499
                                        }
1500
                                        Rectangle2D.Double r = new Rectangle2D.Double();
1501
                                        if (vp.getExtent() != null) {
1502
                                                nuevoX = pReal.getX()
1503
                                                                - ((vp.getExtent().getWidth() * factor) / 2.0);
1504
                                                nuevoY = pReal.getY()
1505
                                                                - ((vp.getExtent().getHeight() * factor) / 2.0);
1506
                                                r.x = nuevoX;
1507
                                                r.y = nuevoY;
1508
                                                r.width = vp.getExtent().getWidth() * factor;
1509
                                                r.height = vp.getExtent().getHeight() * factor;
1510

    
1511
                                                vp.setExtent(r);
1512
                                        }
1513

    
1514

    
1515
                                }
1516
                        } catch (BehaviorException t) {
1517
                                throwException(t);
1518
                        }
1519
                }
1520

    
1521
                /**
1522
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
1523
                 * @see Behavior#mouseDragged(MouseEvent)
1524
                 */
1525
                public void mouseDragged(MouseEvent e) {
1526
                        try {
1527
                                if (currentMapTool != null)
1528
                                        currentMapTool.mouseDragged(e);
1529
                        } catch (BehaviorException t) {
1530
                                throwException(t);
1531
                        }
1532
                }
1533

    
1534
                /**
1535
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
1536
                 * @see Behavior#mouseMoved(MouseEvent)
1537
                 */
1538
                public void mouseMoved(MouseEvent e) {
1539
                        try {
1540
                                if (currentMapTool != null)
1541
                                        currentMapTool.mouseMoved(e);
1542
                        } catch (BehaviorException t) {
1543
                                throwException(t);
1544
                        }
1545
                }
1546
        }
1547

    
1548
        /**
1549
         * <p<code>MapContextListener</code> listens all events produced in a <code>MapControl</code>'s <code>MapContext</code>
1550
         * object during an atomic period of time, and sets it to dirty, <i>executing <code>drawMap(false)</code>, if any of the
1551
         * following conditions is accomplished</i>:
1552
         * <ul>
1553
         *  <li>Any of the <code>LayerEvent</code> in the <code>AtomicEvent</code> parameter notifies a <i>visibility change</i>.</li>
1554
         *  <li>There is at least one <code>ColorEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1555
         *  <li>There is at least one <code>ExtentEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1556
         *  <li>Any of the <code>LayerCollectionEvent</code> in the <code>AtomicEvent</code> parameter notifies that a driver's layer has reloaded it successfully.</li>
1557
         *  <li>There is at least one <code>LegendEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1558
         *  <li>There is at least one <code>SelectionEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1559
         * </ul>
1560
         * </p>
1561
         *
1562
         * @author Fernando Gonz?lez Cort?s
1563
         */
1564
        public class MapContextListener implements AtomicEventListener {
1565
                /**
1566
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
1567
                 */
1568
                public void atomicEvent(AtomicEvent e) {
1569
                        boolean redraw = false;
1570
                        LayerEvent[] layerEvents = e.getLayerEvents();
1571

    
1572
                        for (int i = 0; i < layerEvents.length; i++) {
1573
                                if (layerEvents[i].getProperty().equals("visible")) {
1574
                                        redraw = true;
1575
                                }
1576
                        }
1577

    
1578
                        if (e.getColorEvents().length > 0) {
1579
                                redraw = true;
1580
                        }
1581

    
1582
                        if (e.getExtentEvents().length > 0) {
1583
                                redraw = true;
1584
                        }
1585

    
1586
                        if (e.getProjectionEvents().length > 0) {
1587
                                //redraw = true;
1588
                        }
1589

    
1590

    
1591
                        LayerCollectionEvent[] aux = e.getLayerCollectionEvents();
1592
                        if (aux.length > 0) {
1593
                                for (int i=0; i < aux.length; i++)
1594
                                {
1595
                                        if (aux[i].getAffectedLayer().getFLayerStatus().isDriverLoaded())
1596
                                        {
1597
                                                redraw = true;
1598
                                                break;
1599
                                        }
1600
                                }
1601

    
1602
                        }
1603

    
1604
                        if (e.getLegendEvents().length > 0) {
1605
                                redraw = true;
1606
                        }
1607

    
1608
                        if (e.getSelectionEvents().length > 0) {
1609
                                redraw = true;
1610
                        }
1611

    
1612
                        if (redraw) {
1613
                //System.out.println("MapContextListener redraw");
1614
                                MapControl.this.drawMap(false);
1615
                        }
1616
                }
1617
        }
1618

    
1619
        /**
1620
         * <p>Gets the <code>ViewPort</code> of this component's {@link MapContext MapContext} .</p>
1621
         * 
1622
         * @see MapContext#getViewPort()
1623
         */
1624
        public ViewPort getViewPort() {
1625
                return vp;
1626
        }
1627

    
1628
        /**
1629
         * <p>Returns all registered <code>Behavior</code> that can define a way to work with this <code>MapControl</code>.</p>
1630
         * 
1631
         * @return registered <code>Behavior</code> to this <code>MapControl</code>
1632
         * 
1633
         * @see #addMapTool(String, Behavior)
1634
         * @see #addMapTool(String, Behavior[])
1635
         * @see #getMapToolsKeySet()
1636
         * @see #hasTool(String)
1637
         */
1638
        public HashMap getNamesMapTools() {
1639
                return namesMapTools;
1640
        }
1641

    
1642
        /*
1643
         * (non-Javadoc)
1644
         * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRepaint()
1645
         */
1646
        public void commandRepaint() {
1647
                drawMap(false);
1648
        }
1649

    
1650
        /*
1651
         * (non-Javadoc)
1652
         * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRefresh()
1653
         */
1654
        public void commandRefresh() {
1655
                // TODO Auto-generated method stub
1656
        }
1657

    
1658
        /**
1659
         * <p>Equivalent operation to <i>undo</i>.</p>
1660
         * 
1661
         * <p>Exchanges the previous tool with the current one.</p>
1662
         * 
1663
         * @see #addMapTool(String, Behavior)
1664
         * @see #addMapTool(String, Behavior[])
1665
         * @see #setTool(String)
1666
         */
1667
        public void setPrevTool() {
1668
                setTool(prevTool);
1669
        }
1670

    
1671
        /**
1672
         * <p>Executes a <i>zoom in</i> operation centered at the center of the extent.</p>
1673
         * 
1674
         * <p>This implementation is designed for being invoked outside a <code>Behavior</code>, for example
1675
         *  by an action of pressing a button; and simulates that the event has been produced by 
1676
         *  releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
1677
         *  <code>MapControl</code> object that's responsible for the <i>zoom in</i> operation.</p>
1678
         *  
1679
         * @see #zoomOut()
1680
         */
1681
        public void zoomIn() {
1682
                getMapContext().clearAllCachingImageDrawnLayers();
1683
                Behavior mapTool = (Behavior) namesMapTools.get("zoomIn");
1684
                ViewPort vp=getViewPort();
1685
                Rectangle2D r=getViewPort().getAdjustedExtent();
1686
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1687
                MouseEvent e=new MouseEvent((Component)this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1);
1688
                try {
1689
                        mapTool.mousePressed(e);
1690
                        mapTool.mouseReleased(e);
1691
                } catch (BehaviorException t) {
1692
                        throwException(t);
1693
                }
1694
        }
1695
        
1696
        /**
1697
         * <p>Executes a <i>zoom out</i> operation centered at the center of the extent.</p>
1698
         * 
1699
         * <p>This implementation is thought for being invoked outside a <code>Behavior</code>, for example
1700
         *  by an action of pressing a button, and simulates that the event has been produced by 
1701
         *  releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
1702
         *  <code>MapControl</code> object that's responsible for the <i>zoom out</i> operation.</p>
1703
         * 
1704
         * @see #zoomIn()
1705
         */
1706
        public void zoomOut() {
1707
                getMapContext().clearAllCachingImageDrawnLayers();
1708
                Behavior mapTool = (Behavior) namesMapTools.get("zoomOut");
1709
                ViewPort vp=getViewPort();
1710
                Rectangle2D r=getViewPort().getAdjustedExtent();
1711
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1712
                MouseEvent e=new MouseEvent((Component)this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1);
1713
                try {
1714
                        mapTool.mousePressed(e);
1715
                        mapTool.mouseReleased(e);
1716
                } catch (BehaviorException t) {
1717
                        throwException(t);
1718
                }
1719
        }
1720

    
1721
        /**
1722
         * <p>Returns the listener used to catch all mouse events produced in this <code>MapControl</code> instance
1723
         *  and that redirects the calls to the current map tool.</p> 
1724
         * 
1725
         * @return the map tool listener used
1726
         */
1727
        public MapToolListener getMapToolListener() {
1728
                return mapToolListener;
1729
        }
1730

    
1731
//         mapTool can be null, for instance, in 3D's navigation tools
1732
        /**
1733
         * <p>Sets <code>mapTool</code> as this <code>MapControl</code>'s current map tool.
1734
         * 
1735
         * @param mapTool a map tool, or <code>null</code> to disable the interaction with the user
1736
         * 
1737
         * @see #getCurrentMapTool()
1738
         * @see #getCurrentTool()
1739
         * @see #setTool(String)
1740
         * @see #setPrevTool()
1741
         * @see #addMapTool(String, Behavior)
1742
         * @see #addMapTool(String, Behavior[])
1743
         */
1744
        public void setCurrentMapTool(Behavior mapTool ){
1745
                currentMapTool = mapTool;
1746
        }
1747

    
1748
        /**
1749
         * <p>Sets the delay to the timer that refreshes this <code>MapControl</code> instance.</p>
1750
         * 
1751
         * <p><code>Delay (in ms) = 1000 / getDrawFrameRate()</code></p>
1752
         * 
1753
         * @see #getDrawFrameRate()
1754
         * @see #setDrawFrameRate(int)
1755
         */
1756
        public void applyFrameRate() {
1757
                if (getDrawFrameRate()>0) {
1758
                        timer.setDelay(1000/getDrawFrameRate());
1759
                }
1760
        }
1761

    
1762
        /**
1763
         * <p>Returns the draw frame rate.</p>
1764
         * 
1765
         * <p>Draw frame rate is the number of repaints of this <code>MapControl</code> instance that timer invokes per second.</p>
1766
         * 
1767
         * @return number of repaints of this <code>MapControl</code> instance that timer invokes per second
1768
         * 
1769
         * @see #applyFrameRate()
1770
         * @see #setDrawFrameRate(int)
1771
         */
1772
        public static int getDrawFrameRate() {
1773
                return drawFrameRate;
1774
        }
1775

    
1776
        /**
1777
         * <p>Sets the draw frame rate.</p>
1778
         * 
1779
         * <p>Draw frame rate is the number of repaints of this <code>MapControl</code> instance that timer invokes per second.</p>
1780
         * 
1781
         * @param drawFrameRate number of repaints of this <code>MapControl</code> instance that timer invokes per second
1782
         * 
1783
         * @see #applyFrameRate()
1784
         * @see #getDrawFrameRate()
1785
         */
1786
        public static void setDrawFrameRate(int drawFrameRate) {
1787
                MapControl.drawFrameRate = drawFrameRate;
1788
        }
1789

    
1790
        /**
1791
         * <p>Determines if its enabled the repaint that invokes the timer according to {@link #getDrawFrameRate() #getDrawFrameRate()}.</p>
1792
         * 
1793
         * @return <code>true</code> if its enabled; otherwise <code>false</code>
1794
         */
1795
        public static boolean isDrawAnimationEnabled() {
1796
                return drawAnimationEnabled;
1797
        }
1798

    
1799
        /**
1800
         * <p>Sets if its enabled the repaint that invokes the timer according to {@link #getDrawFrameRate() #getDrawFrameRate()}.</p>
1801
         * 
1802
         * @param drawAnimationEnabled <code>true</code> to enable the mode; otherwise <code>false</code>
1803
         */
1804
        public static void setDrawAnimationEnabled(boolean drawAnimationEnabled) {
1805
                MapControl.drawAnimationEnabled = drawAnimationEnabled;
1806
        }
1807

    
1808
        /**
1809
         * <p>Gets the shared object that determines if a drawing process must be cancelled or can continue.</p>
1810
         * 
1811
         * @return the shared object that determines if a drawing process must be cancelled or can continue
1812
         */
1813
        public CancelDraw getCanceldraw() {
1814
                return canceldraw;
1815
        }
1816
}