Statistics
| Revision:

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

History | View | Annotate | Download (66.4 KB)

1 1223 fernando
/* 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 1282 fjp
import java.awt.Color;
44 6266 fjp
import java.awt.Component;
45 1223 fernando
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 5874 fjp
import java.awt.geom.Point2D;
58
import java.awt.geom.Rectangle2D;
59 1223 fernando
import java.awt.image.BufferedImage;
60
import java.util.HashMap;
61 6777 jmvivo
import java.util.Set;
62 1223 fernando
63
import javax.swing.JComponent;
64
import javax.swing.Timer;
65
66
import org.cresques.cts.IProjection;
67
68 6164 luisw2
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
69 4120 caballero
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener;
70 14573 vcaballero
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
71 1223 fernando
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 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
78 1223 fernando
79
80
/**
81 20098 jmvivo
 * <p>A component that includes a {@link MapContext MapContext} with support for use it as a particular {@link Behavior Behavior}.</p>
82 1223 fernando
 *
83 20098 jmvivo
 * <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 20397 vcaballero
 *
89 20098 jmvivo
 * <p><b>Drawing process:</b></p>
90 20397 vcaballero
 *
91 20098 jmvivo
 * <p>Uses a double buffer for the drawing process of <code>MapContext</code>'s information.</p>
92 20397 vcaballero
 *
93 20098 jmvivo
 * <p>If the double buffer wasn't created, creates a new one.</p>
94 20397 vcaballero
 *
95 20098 jmvivo
 * <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 20397 vcaballero
 *   draw the layers: <code>mapContext.draw(image, g, cancel,mapContext.getScaleView());</code>
106 20098 jmvivo
 * <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 20397 vcaballero
 *  </ul>
116 20098 jmvivo
 * </p>
117 20397 vcaballero
 *
118 20098 jmvivo
 * <p><b>Tools:</b></p>
119 20397 vcaballero
 *
120 20098 jmvivo
 * <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 20358 vcaballero
 *    <li>Get the combined tool: {@link #getCombinedTool() #getCombinedTool()}.</li>
143
 *    <li>Set a combined tool: {@link #setCombinedTool(Behavior) #setCombinedTool(Behavior)}.</li>
144
 *    <li>Remove the combined tool: {@link #removeCombinedTool() #removeCombinedTool()}.</li>
145 20098 jmvivo
 *   </ul>
146
 * </p>
147
 *
148
 * <p><b>Exception listener:</b></p>
149 20397 vcaballero
 *
150
 * <p> Adding an <code>ExceptionListener</code>, can get notification about any exception produced:
151 20098 jmvivo
 *  <ul>
152
 *   <li>Attending a <i>painting request</i>.</li>
153
 *   <li>Working with the active tool.</li>
154
 *   <li>Applying a <i>zoom in</i> or <i>zoom out</i> operation.</li>
155
 *  </ul>
156
 * </p>
157 20397 vcaballero
 *
158 20098 jmvivo
 * <p><b>Other:</b></p>
159 20397 vcaballero
 *
160 20098 jmvivo
 * <p>Other useful capabilities of <code>MapControl</code>:
161
 *   <ul>
162
 *    <li>Cancel the current drawing process (notifying it also to the inner
163
 *     <code>MapContext</code> instance and its layers): {@link #cancelDrawing() #cancelDrawing()}.</li>
164
 *    <li>Applying a <i>zoom in</i> operation centered at mouse position (without a <code>ToolListener</code>): {@link #zoomIn() #zoomIn()}.</li>
165
 *    <li>Applying a <i>zoom out</i> operation centered at mouse position (without a <code>ToolListener</code>): {@link #zoomOut() #zoomOut()}.</li>
166
 *   </ul>
167
 * </p>
168 20397 vcaballero
 *
169 20098 jmvivo
 * @see CancelDraw
170
 * @see Drawer2
171
 * @see MapContextListener
172
 * @see MapToolListener
173 20397 vcaballero
 *
174 1223 fernando
 * @author Fernando Gonz?lez Cort?s
175 20358 vcaballero
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
176 1223 fernando
 */
177 20098 jmvivo
public class MapControl extends JComponent implements ComponentListener, CommandListener {
178
        /**
179
         * <p>One of the possible status of <code>MapControl</code>. Determines that all visible information has been
180 20397 vcaballero
         * drawn and its updated.</p>
181 20098 jmvivo
         */
182 1223 fernando
        public static final int ACTUALIZADO = 0;
183
184 20098 jmvivo
        /**
185
         * <p>One of the possible status of <code>MapControl</code>. Determines that not all visible information has been
186 20397 vcaballero
         * drawn or isn't updated.</p>
187 20098 jmvivo
         */
188 1223 fernando
        public static final int DESACTUALIZADO = 1;
189 20397 vcaballero
190 20098 jmvivo
        /**
191
         * <p>One of the possible status of <code>MapControl</code>. Determines that only the graphical layer must
192 20397 vcaballero
         * be drawn / updated.</p>
193 20098 jmvivo
         */
194
        public static final int ONLY_GRAPHICS = 2;
195
196
        /**
197
         * <p>Determines the number of frames.</p>
198
         *
199
         * <p>Number of updates per second that the timer will invoke repaint this component.</p>
200
         */
201 13733 jaume
    private static int drawFrameRate = 3;
202 20098 jmvivo
203
    /**
204
     * <p>Determines if the drawer can update this <code>MapControl</code> instance when the timer launches an event.</p>
205
     */
206 13733 jaume
    private static boolean drawAnimationEnabled = true;
207
208 3346 fjp
    // public static final int FAST_PAINT = 3;
209 2531 caballero
        //private static Logger logger = Logger.getLogger(MapControl.class.getName());
210 20098 jmvivo
211
    /**
212
     * <p>Inner model with the layers, event support for drawing them, and the <code>ViewPort</code>
213
     *  with information to adapt to the bounds available in <i>image coordinates</i>.</p>
214 20397 vcaballero
     *
215 20098 jmvivo
     * @see #getMapContext()
216 20397 vcaballero
     * @see #setMapContext(MapContext)
217 20098 jmvivo
     */
218
    private MapContext mapContext = null;
219
220 3719 caballero
    //private boolean drawerAlive = false;
221 20098 jmvivo
222
223
        /**
224
         * <p>All registered <code>Behavior</code> that can define a way to work with this <code>MapControl</code>.</p>
225 20397 vcaballero
         *
226 20098 jmvivo
         * <p>Only one of them can be active at a given moment.</p>
227 20397 vcaballero
         *
228 20098 jmvivo
         * @see #addMapTool(String, Behavior)
229
         * @see #addMapTool(String, Behavior[])
230
         * @see #getMapTool(String)
231 20397 vcaballero
         * @see #getMapToolsKeySet()
232 20098 jmvivo
         * @see #getNamesMapTools()
233
         */
234
    protected HashMap namesMapTools = new HashMap();
235
236
        /**
237
         * <p>Active {@link Behavior Behavior} that will generate events according a criterion, and then, with a {@link ToolListener ToolListener}
238
         *  associated, will simulate to user that works with this component as a particular tool.</p>
239 20397 vcaballero
         *
240 20098 jmvivo
         * @see #getCurrentMapTool()
241
         * @see #getCurrentTool()
242 20397 vcaballero
         * @see #setTool(String)
243 20098 jmvivo
         */
244
    protected Behavior currentMapTool = null;
245
246
        /**
247
         * <p>Determines which's the current drawn status of this component:
248
         * <ul>
249
         *  <li><b>OUTDATED</b>: all visible information has been drawn or isn't updated.</li>
250
         *  <li><b>UTDATED</b>: all visible information has been drawn and its updated.</li>
251
         *  <li><b>ONLY_GRAPHICS</b>: only the graphical layer must be drawn / updated.</li>
252
         * </ul>
253
         * </p>
254 20397 vcaballero
         *
255 20098 jmvivo
         * <p>The <code>MapControl</code> drawing process will consider the value of this parameter to decide which elements will
256
         *  be updated or drawn.</p>
257
         */
258
    private int status = DESACTUALIZADO;
259
260
        /**
261
         * <p>Image with a buffer to accelerate the draw the changes of the graphical items in this component.</p>
262 20397 vcaballero
         *
263 20098 jmvivo
         * <p>Firstly, information will be drawn in the buffer, and, when is outright drawn, that information will be displayed.
264
         * Meanwhile, the previous image can be kept showed.</p>
265 20397 vcaballero
         *
266 20098 jmvivo
         * @see BufferedImage
267 20397 vcaballero
         *
268 20098 jmvivo
         * @see #getImage()
269
         */
270
    private BufferedImage image = null;
271
272
        /**
273
         * <p>Name of the tool used currently to interact with this component.</p>
274 20397 vcaballero
         *
275 20098 jmvivo
         * @see #getCurrentTool()
276 20397 vcaballero
         * @see #setTool(String)
277 20098 jmvivo
         */
278
    protected String currentTool;
279
280
        /**
281
         * <p>Object to store the flag that notifies a drawing thread task and <code>MapContext</code>'s layers,
282
         * that must be canceled or can continue with the process.</p>
283 20397 vcaballero
         *
284 20098 jmvivo
         * @see #cancelDrawing()
285
         */
286
    private CancelDraw canceldraw;
287
288
    //private boolean isCancelled = true;
289
290
        /**
291
         * <p>Fires an action events after a specified delay.</p>
292 20397 vcaballero
         *
293 20098 jmvivo
         * <p><code>MapControl</code> will use the timer to update its visible graphical information during
294
         *  a drawing process, or allowing to cancel that process.</p>
295 20397 vcaballero
         *
296 20098 jmvivo
         * <p>This is very useful to pretend faster interactivity to user when <code>MapControl</code> has
297
         *  lots of layers, and / or layers with heavy graphical elements, that need a long time to finish
298 20397 vcaballero
         *  drawing all its data.</p>
299 20098 jmvivo
         */
300
    private Timer timer;
301
302
        /**
303
         * <p>Reference to the {@link ViewPort ViewPort} of the {@link MapContext MapContext} of this component.</p>
304 20397 vcaballero
         *
305 20098 jmvivo
         * <p>The view port once is created an instance of <code>MapControl</code>,
306
         *  is obtained from the <i>EPSG:23030</i> projection, that's the default projection for this component.</p>
307 20397 vcaballero
         *
308 20098 jmvivo
         * <p>After, the view port will change adapting itself according the current projection and the extent.</p>
309 20397 vcaballero
         *
310 20098 jmvivo
         * @see #getViewPort()
311 20397 vcaballero
         *
312 20098 jmvivo
         * @see ViewPort
313
         */
314
    protected ViewPort vp;
315
316
    //private Drawer drawer;
317
318
        /**
319
         * <p>Manager of all <code>MapControl</code> painting requests.</p>
320
         */
321 3368 fjp
    private Drawer2 drawer2;
322 20098 jmvivo
323 3372 fjp
    // private boolean firstDraw = true;
324 4619 caballero
325 20098 jmvivo
        /**
326
         * <p>Listener of all kind of mouse events produced in this component.</p>
327 20397 vcaballero
         *
328 20098 jmvivo
         * <p>Delegates each mouse event to the current map tool.</p>
329 20397 vcaballero
         *
330 20098 jmvivo
         * @see #addMapTool(String, Behavior)
331
         * @see #addMapTool(String, Behavior[])
332
         * @see #getMapTool(String)
333
         * @see #getMapToolsKeySet()
334
         * @see #getNamesMapTools()
335
         * @see #setTool(String)
336
         */
337
    protected MapToolListener mapToolListener = new MapToolListener();
338
339
        /**
340
         * <p>Listener of all events produced in a this component's <code>MapContext</code>
341
         * object during an atomic period of time.</p>
342
         */
343
    private MapContextListener mapContextListener = new MapContextListener();
344
345
        /**
346
         * <p>Group of <code>ExceptionListener</code> that, in whatever moment could be notified a Throwable Java error or exception.</p>
347 20397 vcaballero
         *
348 20098 jmvivo
         * @see #addExceptionListener(ExceptionListener)
349
         * @see #removeExceptionListener(ExceptionListener)
350
         */
351
    private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
352
353
    /**
354
     * <p>Name of the previous tool used.</p>
355
     */
356 12208 jcampos
        protected String prevTool;
357 4888 caballero
358 3719 caballero
        /**
359 20358 vcaballero
         * <p>Tool that will be used combined with the current tool of this <code>MapControl</code>.</p>
360
         */
361
        private Behavior combinedTool = null;
362
363
        /**
364 3331 fjp
     * We need this to avoid not wanted refresh. REMEMBER TO SET TO TRUE!!
365
     */
366
    // private boolean paintEnabled = false;
367 1223 fernando
368
        /**
369 20098 jmvivo
         * <p>Creates a new <code>MapControl</code> instance with the following characteristics:
370
         * <ul>
371
         *  <li><i>Name</i>: MapControl .</li>
372
         *  <li>Disables the double buffer of <code>JComponent</code> .</li>
373
         *  <li>Sets opaque <i>(see {@link JComponent#setOpaque(boolean)} )</i>. </li>
374
         *  <li>Sets its status to <code>OUTDATED</code> .</li>
375
         *  <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>
376
         *  <li>Creates a new {@link MapContext MapContext} with a new {@link ViewPort ViewPort} in the projection <i>"EPSG:23030"</i> .</li>
377
         *  <li>Creates a new {@link CommandListener CommandListener} for edition operations.</li>
378
         *  <li>Creates a new {@link MapToolListener MapToolListener}, and associates it as a listener of whatever kind of mouse events produced in this component.</li>
379
         *  <li>Creates a new {@link Drawer2 Drawer2} for managing the painting requests.</li>
380
         *  <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
381
         *   <code>drawAnimationEnabled</code>.</li>
382
         * </ul>
383
         * </p>
384 1223 fernando
         */
385
        public MapControl() {
386 5787 jaume
                this.setName("MapControl");
387 1223 fernando
                setDoubleBuffered(false);
388
                setOpaque(true);
389
                status = DESACTUALIZADO;
390
391
                //Clase usada para cancelar el dibujado
392
                canceldraw = new CancelDraw();
393
394
                //Modelo de datos y ventana del mismo
395
                // TODO: Cuando creamos un mapControl, deber?amos asignar
396
                // la projecci?n por defecto con la que vayamos a trabajar.
397
                // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
398 6164 luisw2
                vp = new ViewPort(CRSFactory.getCRS("EPSG:23030"));
399 6878 cesar
                setMapContext(new MapContext(vp));
400 3719 caballero
401 1223 fernando
                //eventos
402
                this.addComponentListener(this);
403
                this.addMouseListener(mapToolListener);
404
                this.addMouseMotionListener(mapToolListener);
405
                this.addMouseWheelListener(mapToolListener);
406
407 3368 fjp
        this.drawer2 = new Drawer2();
408 1223 fernando
                //Timer para mostrar el redibujado mientras se dibuja
409 13733 jaume
                timer = new Timer(1000/drawFrameRate,
410 1223 fernando
                                new ActionListener() {
411
                                        public void actionPerformed(ActionEvent e) {
412 14410 vcaballero
413 13733 jaume
                                                if (drawAnimationEnabled) {
414
                                                        MapControl.this.repaint();
415
                                                }
416 1223 fernando
                                        }
417
                                });
418
        }
419
420
        /**
421 20098 jmvivo
         * <p>Sets a <code>MapContext</code> to this component.</p>
422 20397 vcaballero
         *
423 20098 jmvivo
         * <p>The <code>MapContext</code> has the <i>model</i>, and most of the <i>view</i>,
424
         * and <i>control</i> logic of the layers of this component, including a {@link ViewPort ViewPort} to adapt the
425
         * information to the projection, and to display it in the available area.</p>
426 20397 vcaballero
         *
427 20098 jmvivo
         * <p>If <code>model</code> hadn't a <code>ViewPort</code>, assigns the current one to it, otherwise, use its <code>ViewPort</code>.</p>
428 20397 vcaballero
         *
429 20098 jmvivo
         * <p>After assigning the <code>MapContext</code> and <code>ViewPort</code>, sets the same {@link MapContextListener MapContextListener}
430
         *  that was using, and changes the <i>status</i> to <code>OUTDATED</code>.</p>
431 20397 vcaballero
         *
432 20098 jmvivo
         * @param model this component's <code>MapContext</code>, that includes the <code>ViewPort</code>.
433 20397 vcaballero
         *
434 20098 jmvivo
         * @see MapContext
435 20397 vcaballero
         *
436 20098 jmvivo
         * @see #getMapContext()
437 1223 fernando
         */
438 6878 cesar
        public void setMapContext(MapContext model) {
439 1223 fernando
                if (mapContext != null) {
440
                        mapContext.removeAtomicEventListener(mapContextListener);
441
                }
442
443
                mapContext = model;
444
445
                if (mapContext.getViewPort() == null) {
446
                        mapContext.setViewPort(vp);
447
                } else {
448
                        vp = mapContext.getViewPort();
449
450
                        // vp.setImageSize(new Dimension(getWidth(), getHeight()));
451
                        //System.err.println("Viewport en setMapContext:" + vp);
452
                }
453
454
                mapContext.addAtomicEventListener(mapContextListener);
455
456
                status = DESACTUALIZADO;
457
        }
458
459
        /**
460 20098 jmvivo
         * <p>Gets this component's {@link MapContext MapContext} projection.</p>
461 1223 fernando
         *
462 20098 jmvivo
         * @return this component's {@link MapContext MapContext} projection
463 20397 vcaballero
         *
464 20098 jmvivo
         * @see MapContext#getProjection()
465
         * @see MapControl#setProjection(IProjection)
466 1223 fernando
         */
467
        public IProjection getProjection() {
468
                return getMapContext().getProjection();
469
        }
470
471
        /**
472 20098 jmvivo
         * <p>Sets the projection to this component's {@link MapContext MapContext}.</p>
473 1223 fernando
         *
474 20098 jmvivo
         * @param proj the kind of projection to this component's {@link MapContext MapContext}
475 20397 vcaballero
         *
476 20098 jmvivo
         * @see MapContext#setProjection(IProjection)
477
         * @see MapControl#getProjection()
478 1223 fernando
         */
479
        public void setProjection(IProjection proj) {
480
                getMapContext().setProjection(proj);
481
        }
482
483
        /**
484 20098 jmvivo
         * <p>Gets this component's <code>MapContext</code>, with the <i>model</i>, and most of the <i>view</i>,
485
         * and <i>control</i> logic of the layers of this component, including a {@link ViewPort ViewPort} to adapt the
486
         * information to the projection, and display it in the available area.</p>
487 20397 vcaballero
         *
488 20098 jmvivo
         * @return this component's <code>MapContext</code>, that includes the <code>ViewPort</code> used to project the
489
         * graphical information, and display it in the available area
490 20397 vcaballero
         *
491 20098 jmvivo
         * @see MapContext
492 20397 vcaballero
         *
493 20098 jmvivo
         * @see MapControl#setMapContext(MapContext)
494 1223 fernando
         */
495 6878 cesar
        public MapContext getMapContext() {
496 1223 fernando
                return mapContext;
497
        }
498
499
        /**
500 20098 jmvivo
         * <p>Registers a new behavior to this component.</p>
501 20397 vcaballero
         *
502 20098 jmvivo
         * <p>According the nature of the {@link Behavior Behavior}, different events will be generated. Those
503
         *  events can be caught by a particular {@link ToolListener ToolListener}, allowing user to interact with this
504
         *  <code>MapControl</code> object as a <i>tool</i>.</p>
505 1223 fernando
         *
506 20098 jmvivo
         * @param name name to identify the behavior to add
507
         * @param tool the behavior to add
508 20397 vcaballero
         *
509 20098 jmvivo
         * @see #addMapTool(String, Behavior[])
510
         * @see #getNamesMapTools()
511
         * @see #getMapToolsKeySet()
512
         * @see #hasTool(String)
513 1223 fernando
         */
514
        public void addMapTool(String name, Behavior tool) {
515
                namesMapTools.put(name, tool);
516
                tool.setMapControl(this);
517
        }
518
519 20098 jmvivo
        /**
520
         * <p>Registers a new behavior to this component as a {@link CompoundBehavior CompoundBehavior} made up of <code>tools</code>.</p>
521 20397 vcaballero
         *
522 20098 jmvivo
         * <p>According the nature of the behaviors registered, different events will be generated. Those
523
         *  events can be caught by a particular {@link ToolListener ToolListener}, allowing user to interact with this
524
         *  <code>MapControl</code> object as a <i>tool</i>.</p>
525
         *
526
         * @param name name to identify the compound behavior to add
527
         * @param tools the compound behavior to add
528 20397 vcaballero
         *
529 20098 jmvivo
         * @see #addMapTool(String, Behavior)
530
         * @see #getNamesMapTools()
531
         * @see #getMapToolsKeySet()
532
         * @see #hasTool(String)
533
         */
534 1223 fernando
        public void addMapTool(String name, Behavior[] tools){
535
                CompoundBehavior tool = new CompoundBehavior(tools);
536
                addMapTool(name, tool);
537
        }
538 10837 caballero
539 6777 jmvivo
        /**
540 20098 jmvivo
         * <p>Gets the <code>Behavior</code> registered in this component, identified
541
         *  by <code>name</code>.</p>
542 6777 jmvivo
         *
543 20098 jmvivo
         * @param name name of a registered behavior
544 20397 vcaballero
         *
545 20098 jmvivo
         * @return tool the registered behavior in this component as <code>name</code>, or <code>null</code> if
546
         *  no one has that identifier
547 20397 vcaballero
         *
548 20098 jmvivo
         * @see #addMapTool(String, Behavior)
549
         * @see #addMapTool(String, Behavior[])
550
         * @see #hasTool(String)
551 6777 jmvivo
         */
552
        public Behavior getMapTool(String name) {
553
                return (Behavior)namesMapTools.get(name);
554
        }
555 3719 caballero
556 1223 fernando
        /**
557 20098 jmvivo
         * <p>Returns a set view of the keys that identified the tools
558
         *  registered.</p>
559 10837 caballero
         *
560 20098 jmvivo
         * @return a set view of the keys that identified the tools registered
561 20397 vcaballero
         *
562 20098 jmvivo
         * @see HashMap#keySet()
563 20397 vcaballero
         *
564 20098 jmvivo
         * @see #getNamesMapTools()
565
          * @see #addMapTool(String, Behavior)
566
          * @see #addMapTool(String, Behavior[])
567 6777 jmvivo
         */
568
        public Set getMapToolsKeySet() {
569
                return namesMapTools.keySet();
570
        }
571
572
        /**
573 20098 jmvivo
         * <p>Returns <code>true</code> if this component contains a tool identified by <code>toolName</code>.</p>
574 20397 vcaballero
         *
575 20098 jmvivo
         * @param toolName identifier of the tool
576 20397 vcaballero
         *
577 20098 jmvivo
         * @return <code>true</code> if this component contains a tool identified by <code>toolName</code>; otherwise <code>false</code>
578 20397 vcaballero
         *
579 20098 jmvivo
         * @see #addMapTool(String, Behavior)
580
         * @see #addMapTool(String, Behavior[])
581 6378 jaume
         */
582
        public boolean hasTool(String toolName) {
583
                return namesMapTools.containsKey(toolName);
584
        }
585 20098 jmvivo
586 6378 jaume
        /**
587 20098 jmvivo
         * <p>Sets as current active <code>Behavior</code> associated to this component, that one which
588
         *  is registered and identified by <code>toolName</code>.</p>
589 20397 vcaballero
         *
590 20098 jmvivo
         * <p>Changing the current active behavior for this <code>MapControl</code>, implies also updating the
591
         *  previous <i>behavior</i> tool, and the current cursor.</p>
592 20397 vcaballero
         *
593 20098 jmvivo
         * @param toolName name of a registered behavior
594 20397 vcaballero
         *
595 20098 jmvivo
         * @see #getCurrentMapTool()
596
         * @see #getCurrentTool()
597 1223 fernando
         */
598
        public void setTool(String toolName) {
599 6378 jaume
                prevTool=getCurrentTool();
600 1223 fernando
                Behavior mapTool = (Behavior) namesMapTools.get(toolName);
601
                currentMapTool = mapTool;
602
                currentTool = toolName;
603 20358 vcaballero
604
                if (combinedTool != null) {
605
                        if (mapTool instanceof CompoundBehavior) {
606 22657 vcaballero
                                ((CompoundBehavior)mapTool).addMapBehavior(combinedTool, true);
607 20358 vcaballero
                        }
608
                        else {
609 22657 vcaballero
                                currentMapTool = new CompoundBehavior(new Behavior[] {currentMapTool});
610
                                ((CompoundBehavior)currentMapTool).addMapBehavior(combinedTool, true);
611 20358 vcaballero
                        }
612
                }
613
614 1223 fernando
                this.setCursor(mapTool.getCursor());
615
        }
616 20098 jmvivo
617
        /**
618
         * <p>Gets as current active <code>Behavior</code> associated to this component, that one which
619
         *  is registered and identified by <code>toolName</code>.</p>
620 20397 vcaballero
         *
621 20098 jmvivo
         * <p>Changing the current active behavior for this <code>MapControl</code>, implies also updating the
622
         *  previous <i>behavior</i> tool, and the current cursor.</p>
623 20397 vcaballero
         *
624 20098 jmvivo
         * @param toolName name of a registered behavior
625 20397 vcaballero
         *
626 20098 jmvivo
         * @see #getCurrentTool()
627
         * @see #setTool(String)
628
         */
629 3719 caballero
        public Behavior getCurrentMapTool(){
630
                return currentMapTool;
631
        }
632 20098 jmvivo
633 1223 fernando
        /**
634 20098 jmvivo
         * <p>Returns the name of the current selected tool on this MapControl</p>
635 1223 fernando
         *
636 20098 jmvivo
         * @return the name of the current's behavior tool associated to this component
637 20397 vcaballero
         *
638 20098 jmvivo
         * @see #getCurrentMapTool()
639
         * @see #setTool(String)
640 1223 fernando
         */
641 6378 jaume
        public String getCurrentTool() {
642 1223 fernando
                return currentTool;
643
        }
644
645
        /**
646 20098 jmvivo
         * <p>Determines that current drawing process of <code>MapControl</code>'s <code>MapContext</code>'s data must be canceled.</p>
647 20397 vcaballero
         *
648 20098 jmvivo
         * <p>It has no effects if now isn't drawing that graphical information.</p>
649 20397 vcaballero
         *
650 20098 jmvivo
         * <p>At last resort, the particular implementation of each layer in this <code>MapControl</code>'s <code>MapContrext</code>
651
     *   will be that one which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</p>
652 1223 fernando
         */
653
        public void cancelDrawing() {
654 3368 fjp
                /* if (drawer != null) {
655 1223 fernando
                        if (!drawer.isAlive()) {
656
                                return;
657
                        }
658
                }
659 3368 fjp
                */
660 4832 fjp
                canceldraw.setCanceled(true);
661 1223 fernando
662 3368 fjp
                /* while (!isCancelled) {
663 1243 fjp
                        if (!drawer.isAlive()) {
664 1327 fjp
                            // Si hemos llegado aqu? con un thread vivo, seguramente
665
                            // no estamos actualizados.
666

667
                                break;
668 1243 fjp
                        }
669 3719 caballero

670 1223 fernando
                }
671
                canceldraw.setCancel(false);
672
                isCancelled = false;
673 3368 fjp
        drawerAlive = false; */
674 1223 fernando
        }
675 3719 caballero
676 20098 jmvivo
        /**
677
         * <p>Creates a {@link BufferedImage BufferedImage} image if there was no buffered image, or if
678
         *  its viewport's image height or width is different from this component's size. Once has created
679
         *  a double-buffer, fills it with the vieport's background color, or with <i>white</i> if it had no background color.</p>
680 20397 vcaballero
         *
681 20098 jmvivo
         * <p>If no double-buffered existed, creates a {@link BufferedImage BufferedImage} with the size of this component,
682
         * and as an image with 8-bit RGBA color components packed into integer pixels. That image has a <code>DirectColorModel</code> with alpha.
683 20397 vcaballero
         * The color data in that image is considered not to be premultiplied with alpha.</p>
684
         *
685 20098 jmvivo
         * <p>Once has created and filled the new inner <code>MapControl</code>'s double-buffer, changes the status to
686
         * <code>OUTDATED</code>.</p>
687 20397 vcaballero
         *
688 20098 jmvivo
         * @return <code>true</code> if has created and filled a new double-buffer for this <code>MapControl</code> instance; otherwise <code>false</code>
689
         */
690 3375 fjp
    private boolean adaptToImageSize()
691
    {
692
        if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight()))
693
        {
694
            image = new BufferedImage(this.getWidth(), this.getHeight(),
695
                    BufferedImage.TYPE_INT_ARGB);
696 6212 fjp
            // ESTILO MAC
697
//                image = GraphicsEnvironment.getLocalGraphicsEnvironment()
698
//                                .getDefaultScreenDevice().getDefaultConfiguration()
699
//                                .createCompatibleImage(this.getWidth(), this.getHeight());
700 3375 fjp
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
701 6630 caballero
            getMapContext().getViewPort().refreshExtent();
702 3719 caballero
703
704 3375 fjp
            Graphics gTemp = image.createGraphics();
705
            Color theBackColor = vp.getBackColor();
706
            if (theBackColor == null)
707
                gTemp.setColor(Color.WHITE);
708
            else
709
                gTemp.setColor(theBackColor);
710 3719 caballero
711 3375 fjp
            gTemp.fillRect(0,0,getWidth(), getHeight());
712
            gTemp.dispose();
713
            status = DESACTUALIZADO;
714
            // g.drawImage(image,0,0,null);
715
            return true;
716
        }
717
        return false;
718
    }
719 1223 fernando
720 20098 jmvivo
        /**
721
         * <p>Paints the graphical information of this component using a double buffer.</p>
722 20397 vcaballero
         *
723 20098 jmvivo
         * <p>If the double buffer wasn't created, creates a new one.</p>
724 20397 vcaballero
         *
725 20098 jmvivo
         * <p>Paints the component according the following algorithm:
726
         * <br>
727
         *  &nbsp If <i>status</i> is <i>UPDATED</i>:<br>
728
         *  &nbsp &nbsp If there is no <i>double buffer</i>:<br>
729
         *  &nbsp &nbsp &nbsp If there is a <i>behavior</i> for managing the <code>MapControl</code> instance, delegates
730
         *   the drawing process to that behavior, calling: <code><i>behavior_instance</i>.paintComponent(g)</code> &nbsp .<br>
731
         *  &nbsp &nbsp &nbsp Else, repaints the current graphical information quickly calling: <code>g.drawImage(image,0,0,null)</code> &nbsp .<br>
732
         *  &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
733
         *   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
734
         *   (invoking <code>repaint()</code> that comprises invoke this method) the view every delay of 360 ms. during the the process drawing.</p>
735 20397 vcaballero
         *
736 20098 jmvivo
           * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
737
           * @see Drawer2
738 1223 fernando
         */
739
        protected void paintComponent(Graphics g) {
740 3375 fjp
        adaptToImageSize();
741 3346 fjp
        /* if (status == FAST_PAINT) {
742 3331 fjp
            System.out.println("FAST_PAINT");
743
            g.drawImage(image,0,0,null);
744
            status = ACTUALIZADO;
745
            return;
746 3346 fjp
        } */
747 4568 fjp
        // System.out.println("PINTANDO MAPCONTROL" + this);
748 1223 fernando
                if (status == ACTUALIZADO) {
749 1680 luisw
                        // LWS logger.debug("Dibujando la imagen obtenida");
750 1223 fernando
751
                        /*
752
                         * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
753
                         * en dicho behaviour
754
                         */
755 3372 fjp
            if (image != null)
756
            {
757
                if (currentMapTool != null)
758
                    currentMapTool.paintComponent(g);
759
                else
760
                    g.drawImage(image,0,0,null);
761 3719 caballero
762 3372 fjp
                                // System.out.println("Pinto ACTUALIZADO");
763 1223 fernando
                        }
764 3719 caballero
                } else if ((status == DESACTUALIZADO)
765 2946 fjp
                || (status == ONLY_GRAPHICS)) {
766 1680 luisw
                        // LWS System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a");
767 1223 fernando
                        /* if (isOpaque())
768
                        {
769
                            if (image==null)
770
                            {
771
                                g.setColor(vp.getBackColor());
772 3719 caballero
                                g.fillRect(0,0,getWidth(), getHeight());
773
                            }
774 1223 fernando
                            // else g.drawImage(image,0,0,null);
775
                        } */
776 3368 fjp
            // cancelDrawing();
777 1223 fernando
                        //Se crea la imagen con el color de fonde deseado
778 3375 fjp
                        /* if (image == null)
779 1223 fernando
                        {
780
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
781
                                                BufferedImage.TYPE_INT_ARGB);
782
                                vp.setImageSize(new Dimension(getWidth(), getHeight()));
783
                                Graphics gTemp = image.createGraphics();
784 1303 fjp
                            Color theBackColor = vp.getBackColor();
785
                            if (theBackColor == null)
786
                                gTemp.setColor(Color.WHITE);
787
                            else
788
                                gTemp.setColor(theBackColor);
789 3719 caballero

790 1223 fernando
                                gTemp.fillRect(0,0,getWidth(), getHeight());
791
                                gTemp.dispose();
792
                                // g.drawImage(image,0,0,null);
793 3174 ldiaz
                                System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
794 3375 fjp
                        } */
795 3331 fjp
            // else
796
            // {
797 3719 caballero
798
799 3372 fjp
            // if (image != null)
800
            //  {
801 3331 fjp
                g.drawImage(image,0,0,null);
802 3719 caballero
803 3372 fjp
                drawer2.put(new PaintingRequest());
804
                timer.start();
805
            /* }
806
            else
807
                return; */
808 3331 fjp
            // }
809 3719 caballero
810 3368 fjp
            /* if (drawerAlive == false)
811 3331 fjp
            {
812
                drawer = new Drawer(image, canceldraw);
813
                drawer.start();
814 3719 caballero
                        //Se lanza el tread de dibujado
815 3368 fjp
            } */
816 3719 caballero
817 2946 fjp
                        // status = ACTUALIZADO;
818 1223 fernando
                }
819
        }
820
821
        /**
822 20397 vcaballero
         * <p>Gets the {@link BufferedImage BufferedImage} used to accelerate the draw of new ''frames'' with changes,
823 20098 jmvivo
         * or new graphical items in this component.</p>
824 20397 vcaballero
         *
825 20098 jmvivo
         * @return double buffered image used by this component to accelerate the draw of its graphical information, or
826
         * <code>null</code> if isn't already created
827 20397 vcaballero
         *
828
         * @see BufferedImage
829 1223 fernando
         */
830
        public BufferedImage getImage() {
831
                return image;
832
        }
833
834
        /**
835 20098 jmvivo
         * <p>Forces repaint all visible graphical information in this component.</p>
836
         *
837 20397 vcaballero
         * <p>If <code>doClear == true</code>, before repainting, clears the background color, with the
838
         *  inner viewport's background color.</p>
839
         *
840
         * @param doClear <code>true</code> if needs clearing the background color before drawing the map
841
         *
842 20098 jmvivo
         * @see #cancelDrawing()
843
         * @see FLayers#setDirty(boolean)
844 1223 fernando
         */
845
        public void drawMap(boolean doClear) {
846 3979 caballero
                cancelDrawing();
847 12173 nacho
                //System.out.println("drawMap con doClear=" + doClear);
848 3346 fjp
        status = DESACTUALIZADO;
849 24160 jmvivo
//        getMapContext().getLayers().setDirty(true);
850 3331 fjp
                if (doClear)
851
        {
852 20397 vcaballero
            // image = null; // Se usa para el PAN
853 3372 fjp
            if (image != null)
854
            {
855
                Graphics2D g = image.createGraphics();
856 20397 vcaballero
                Color theBackColor = vp.getBackColor();
857
                if (theBackColor == null)
858
                    g.setColor(Color.WHITE);
859
                else
860
                    g.setColor(theBackColor);
861
                g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight());
862 3372 fjp
                g.dispose();
863 3719 caballero
            }
864 3331 fjp
        }
865 1223 fernando
                repaint();
866
        }
867 6306 caballero
868 20397 vcaballero
869 20098 jmvivo
        /**
870
         * <p>Cancels any current drawing process, changing the status to <code>OUTDATED</code>, and forcing
871
         * repaint only the layers dirty.</p>
872 20397 vcaballero
         *
873 20098 jmvivo
         * @see #cancelDrawing()
874
         */
875 5923 fjp
        public void rePaintDirtyLayers()
876
        {
877
                cancelDrawing();
878
        status = DESACTUALIZADO;
879
        repaint();
880
        }
881 6306 caballero
882 20098 jmvivo
        /**
883
         * <p>Cancels any current drawing process, changing the status to <code>ONLY_GRAPHICS</code>, and forcing
884
         * repaint only the graphical layer of the <code>MapContext</code>.</p>
885
         */
886 2901 fjp
    public void drawGraphics() {
887
        status = ONLY_GRAPHICS;
888
        repaint();
889
    }
890 1223 fernando
891
        /**
892
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
893
         */
894
        public void componentHidden(ComponentEvent e) {
895
        }
896
897
        /**
898
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
899
         */
900
        public void componentMoved(ComponentEvent e) {
901
        }
902
903
        /**
904
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
905
         */
906
        public void componentResized(ComponentEvent e) {
907 3375 fjp
                /* image = new BufferedImage(this.getWidth(), this.getHeight(),
908 3719 caballero
                                BufferedImage.TYPE_INT_ARGB);
909 1223 fernando
                Graphics gTemp = image.createGraphics();
910
                gTemp.setColor(vp.getBackColor());
911 3372 fjp
                gTemp.fillRect(0,0,getWidth(), getHeight());
912
        System.out.println("MapControl resized");
913
            // image = null;
914 1223 fernando
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
915 3375 fjp
                getMapContext().getViewPort().setScale(); */
916 3372 fjp
                // drawMap(true);
917 1223 fernando
        }
918
919
        /**
920
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
921
         */
922
        public void componentShown(ComponentEvent e) {
923
        }
924
925
        /**
926 20098 jmvivo
         * @see ExceptionHandlingSupport#addExceptionListener(ExceptionListener)
927 1223 fernando
         */
928
        public void addExceptionListener(ExceptionListener o) {
929
                exceptionHandlingSupport.addExceptionListener(o);
930
        }
931
932
        /**
933 20098 jmvivo
         * @see ExceptionHandlingSupport#removeExceptionListener(ExceptionListener)
934 1223 fernando
         */
935
        public boolean removeExceptionListener(ExceptionListener o) {
936
                return exceptionHandlingSupport.removeExceptionListener(o);
937
        }
938
939
        /**
940 20098 jmvivo
         * @see ExceptionHandlingSupport#throwException(Throwable)
941 1223 fernando
         */
942 9079 ldiaz
        protected void throwException(Throwable t) {
943 1223 fernando
                exceptionHandlingSupport.throwException(t);
944
        }
945
946 20098 jmvivo
        /**
947
         * <p>Represents each <code>MapControl</code>'s data painting request.</p>
948 20397 vcaballero
         *
949 20098 jmvivo
         * <p>The request will be attended by a <code>Drawer2</code>, which will hold it since the <code>Drawer2</code>'s worker
950 20397 vcaballero
         *  takes it, or arrives a new painting request, which will replace it.</p>
951 20098 jmvivo
         */
952 3368 fjp
    private class PaintingRequest
953
    {
954 20098 jmvivo
            /**
955
             * <p>Creates a new <code>PaintingRequest</p> instance.</p>
956
             */
957 3372 fjp
        public PaintingRequest()
958 3368 fjp
        {
959
        }
960 3719 caballero
961 20098 jmvivo
        /**
962
         * <p><code>MapControl</code> paint process:</p>
963 20397 vcaballero
         *
964 20098 jmvivo
         * <p>
965
         *  <ul>
966
         *   <li><i>1.- </i>Cancels all previous <code>MapControl</code>'s drawing processes.</li>
967
         *   <li><i>2.- </i>If <i>status</i> was OUTDATED:
968
         *    <ul>
969
         *     <li><i>2.1.- </i>Fills the background color with viewport's background color, or <i>white</i> if it was undefined.</li>
970
         *     <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>
971
         *     <li><i>2.3.- </i>If <code>canceldraw.isCanceled()</code>
972
         *      <ul>
973
         *       <li><i>2.3.1.- </i>Sets <i>status</i> to OUTDATED.</li>
974
         *       <li><i>2.3.2.- </i>Sets <i>dirty</i> all layers stored in <i>MapContext</i>.</li>
975
         *      </ul>
976
         *     </li>
977
         *     <li><i>2.4.- </i>Else, sets <i>status</i> to UPDATED.</li>
978
         *    </ul>
979
         *   </li>
980
         *   <li><i>3.- </i>Else, if <i>status</i> was ONLY_GRAPHICS:
981
         *    <ul>
982
         *     <li><i>3.1.- </i>Sets <i>status</i> to UPDATED.</li>
983
         *     <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>
984
         *    </ul>
985
         *   </li>
986
         *   <li><i>4.- </i>Stops the <i>timer</i>.</li>
987
         *   <li><i>5.- </i>Repaints this component invoking: <code>repaint();</code></li>
988
         *  </ul>
989
         * </p>
990 20397 vcaballero
         *
991 20098 jmvivo
         * @see #cancelDrawing()
992
         * @see MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)
993
         * @see MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)
994 20397 vcaballero
         *
995 20098 jmvivo
         * @see ViewPort
996
         */
997 3368 fjp
        public void paint()
998
        {
999 3719 caballero
            try
1000
            {
1001 4832 fjp
                    canceldraw.setCanceled(false);
1002 3372 fjp
                /* if (image == null)
1003
                {
1004
                    image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
1005
                            BufferedImage.TYPE_INT_ARGB);
1006
                    Graphics gTemp = image.createGraphics();
1007
                    Color theBackColor = vp.getBackColor();
1008
                    if (theBackColor == null)
1009
                        gTemp.setColor(Color.WHITE);
1010
                    else
1011
                        gTemp.setColor(theBackColor);
1012 3719 caballero

1013 3372 fjp
                    gTemp.fillRect(0,0,getWidth(), getHeight());
1014
                    gTemp.dispose();
1015
                    // g.drawImage(image,0,0,null);
1016
                    System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
1017
                } */
1018 3368 fjp
                Graphics2D g = image.createGraphics();
1019 3719 caballero
1020 3368 fjp
                ViewPort viewPort = mapContext.getViewPort();
1021 3719 caballero
1022 3368 fjp
                if (status == DESACTUALIZADO)
1023
                {
1024 5874 fjp
                        Graphics2D gTemp = image.createGraphics();
1025 3368 fjp
                    Color theBackColor = viewPort.getBackColor();
1026
                    if (theBackColor == null)
1027 5874 fjp
                        gTemp.setColor(Color.WHITE);
1028 3368 fjp
                    else
1029 5874 fjp
                        gTemp.setColor(theBackColor);
1030
                    gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
1031 6261 fjp
                    // ESTILO MAC
1032 14410 vcaballero
//                  BufferedImage imgMac = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
1033
//                          BufferedImage.TYPE_INT_ARGB);
1034
          //
1035
//                  mapContext.draw(imgMac, g, canceldraw, mapContext.getScaleView());
1036
//                  g.drawImage(imgMac, 0, 0, null);
1037
                  // FIN ESTILO MAC
1038
                  // SIN MAC:
1039 3719 caballero
1040 14410 vcaballero
                  mapContext.draw(image, g, canceldraw, mapContext.getScaleView());
1041
                  if (!canceldraw.isCanceled()){
1042
                          status=ACTUALIZADO;
1043
                 }else{
1044
                          status=DESACTUALIZADO;
1045 24160 jmvivo
//                          getMapContext().getLayers().setDirty(true);
1046 14410 vcaballero
                  }
1047
1048 3368 fjp
                }
1049
                else if (status == ONLY_GRAPHICS)
1050
                {
1051 3719 caballero
                    status = ACTUALIZADO;
1052 3372 fjp
                    mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView());
1053 3719 caballero
1054 3368 fjp
                }
1055 3719 caballero
1056
1057 3368 fjp
                // status = FAST_PAINT;
1058 3719 caballero
              //  drawerAlive = false;
1059 3372 fjp
                timer.stop();
1060 3368 fjp
                repaint();
1061 3719 caballero
1062
1063 3368 fjp
            } catch (Throwable e) {
1064
                timer.stop();
1065 3719 caballero
              //  isCancelled = true;
1066 3368 fjp
                e.printStackTrace();
1067
                throwException(e);
1068
            } finally {
1069
            }
1070
        }
1071 20098 jmvivo
    }
1072 3719 caballero
1073 20098 jmvivo
    /**
1074
     * <p>An instance of <code>Drawer2</code> could manage all <code>MapControl</code> painting requests.</p>
1075 20397 vcaballero
     *
1076 20098 jmvivo
     * <p>Based on the <i>WorkerThread</i> software pattern, creates a worker thread that will attend sequentially
1077
     *  the current waiting painting request, after finishing the previous (that could be by a cancel action).</p>
1078 20397 vcaballero
     *
1079 20098 jmvivo
     * <p>All new {@link PaintingRequest PaintingRequest} generated will be stored as <i>waiting requests</i> since the worker
1080
     * attends it.</p>
1081 20397 vcaballero
     *
1082 20098 jmvivo
     * <p>If a worker finished and there was no <i>painting request</i>, the worker would be set to wait until any
1083
     *  <i>painting request</i> would be put.</p>
1084 20397 vcaballero
     *
1085 3368 fjp
     * @author fjp
1086
     */
1087
    public class Drawer2
1088
    {
1089
        // Una mini cola de 2. No acumulamos peticiones de dibujado
1090
        // dibujamos solo lo ?ltimo que nos han pedido.
1091 3719 caballero
1092 20397 vcaballero
1093 20098 jmvivo
            /**
1094
             * <p>Painting request that's been attended by the <code>Drawer2</code>'s worker.</p>
1095 20397 vcaballero
             *
1096 20098 jmvivo
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1097
             * @see #take()
1098
             */
1099
            private PaintingRequest paintingRequest;
1100
1101
            /**
1102
             * <p>Painting request waiting to be attended by the <code>Drawer2</code>'s worker.</p>
1103 20397 vcaballero
             *
1104 20098 jmvivo
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1105
             * @see #take()
1106
             */
1107
            private PaintingRequest waitingRequest;
1108
1109
            /**
1110 20397 vcaballero
             * <p>Determines that the <code>Drawer2</code>'s worker is busy attending a painting request.</p>
1111
              *
1112 20098 jmvivo
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1113
             * @see #take()
1114
             */
1115 3368 fjp
        private boolean waiting;
1116 20098 jmvivo
1117
            /**
1118
             * <p>Notifies the <code>Drawer2</code>'s worker to finish or continue with its process.</p>
1119 20397 vcaballero
             *
1120 20098 jmvivo
             * @see #setShutdown(boolean)
1121
             */
1122 3368 fjp
        private boolean shutdown;
1123 3719 caballero
1124 20098 jmvivo
        /**
1125
         * <p>Sets this <code>Drawer2</code>'s worker to finish or continue with its process.</p>
1126 20397 vcaballero
         *
1127 20098 jmvivo
         * @param isShutdown a boolean value
1128
         */
1129 3368 fjp
        public void setShutdown(boolean isShutdown)
1130
        {
1131
            shutdown = isShutdown;
1132
        }
1133 3719 caballero
1134 20098 jmvivo
        /**
1135
         * <p>Creates a new drawer for managing all data painting requests in <code>MapControl</code>.</p>
1136 20397 vcaballero
         *
1137 20098 jmvivo
         * <p>Includes the following steps:
1138
         *  <ul>
1139
         *   <li>By default, there is no <i>current painting request</i>.</li>
1140
         *   <li>By default, there is no <i>waiting painting request</i>.</li>
1141
         *   <li>By default, the worker thread is waiting no <i>painting request</i>.</li>
1142
         *   <li>By default, the worker thread is running.</li>
1143
         *   <li>Creates and starts a worker thread for attending the <i>painting requests</i>.</li>
1144
         *  </ul>
1145
         * </p>
1146
         */
1147 3719 caballero
        public Drawer2()
1148 3368 fjp
        {
1149
            paintingRequest = null;
1150
            waitingRequest = null;
1151
            waiting = false;
1152
            shutdown = false;
1153
            new Thread(new Worker()).start();
1154
        }
1155 3719 caballero
1156 20098 jmvivo
        /**
1157 20397 vcaballero
         * <p>Sets a <code>PaintingRequest</code> to be attended by the worker thread of this object. If
1158 20098 jmvivo
         *  this one was waiting, wakes up.</p>
1159 20397 vcaballero
         *
1160 20098 jmvivo
         * <p>All waiting threads will be notified synchronized.</p>
1161 20397 vcaballero
         *
1162 20098 jmvivo
         * @param newPaintRequest
1163 20397 vcaballero
         *
1164 20098 jmvivo
         * @see #take()
1165
         */
1166 3368 fjp
        public void put(PaintingRequest newPaintRequest)
1167
        {
1168
            waitingRequest = newPaintRequest;
1169
            if (waiting)
1170
            {
1171
                synchronized (this) {
1172
                    notifyAll();
1173
                }
1174
            }
1175
        }
1176 3719 caballero
1177 20098 jmvivo
        /**
1178
         * <p>Used by this object's worker, returns the current waiting drawing request, causing current thread
1179
         *  to wait until another thread invokes {@link #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest) #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)},
1180
         *  if there was no waiting request.</p>
1181 20397 vcaballero
         *
1182 20098 jmvivo
         * <p>All threads will access synchronized to the waiting request.</p>
1183 20397 vcaballero
         *
1184 20098 jmvivo
         * @return <code>PaintingRequest</code> that was waiting to be attended
1185 20397 vcaballero
         *
1186 20098 jmvivo
         * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1187
         */
1188 3368 fjp
        public PaintingRequest take()
1189
        {
1190
            if (waitingRequest == null)
1191
            {
1192
                synchronized (this) {
1193
                    waiting = true;
1194
                    try {
1195
                        wait();
1196
                    }
1197
                    catch (InterruptedException ie)
1198
                    {
1199
                        waiting = false;
1200
                    }
1201
                }
1202
            }
1203
            paintingRequest = waitingRequest;
1204
            waitingRequest = null;
1205
            return paintingRequest;
1206
        }
1207 3719 caballero
1208 20098 jmvivo
        /**
1209
         * <p>Thread for attending painting requests.</p>
1210 20397 vcaballero
         *
1211 20098 jmvivo
         * <p>If there was no double buffer, sets the status to <code>OUTDATED</code> and finishes, otherwise
1212
         *  takes the painting request (it's probably that would wait some time), cancel the previous drawing
1213
         *  process, and starts processing the request.</p>
1214 20397 vcaballero
         *
1215 20098 jmvivo
         * @see Thread
1216
         */
1217 3368 fjp
        private class Worker implements Runnable
1218
        {
1219 20098 jmvivo
                /*
1220
                 * (non-Javadoc)
1221
                 * @see java.lang.Runnable#run()
1222
                 */
1223
                public void run()
1224 3368 fjp
            {
1225
                while (!shutdown)
1226
                {
1227
                    PaintingRequest p = take();
1228 12173 nacho
                    //System.out.println("Pintando");
1229 4120 caballero
                    if (image != null){
1230
                            cancelDrawing();
1231 3372 fjp
                        p.paint();
1232 14410 vcaballero
                    } else{
1233 3372 fjp
                        status = DESACTUALIZADO;
1234 14410 vcaballero
                    }
1235 3368 fjp
                }
1236
            }
1237
        }
1238
    }
1239 3719 caballero
1240 1223 fernando
        /**
1241 20098 jmvivo
         * <p><code>Drawer</code> is implemented for drawing the layers of a <code>MapControl</code>'s <i>MapContext</i> instance
1242
         *  as a <i>thread</i> of execution.</p>
1243 20397 vcaballero
         *
1244 20098 jmvivo
         * <p>Draws <code>MapControl</code> according its <i>status</i>:
1245
         *  <ul>
1246
         *   <li><code><i>ONLY_GRAPHICS</i></code>: refreshes only the graphical layer, changing the status to <code><i>UPDATED</i></code>, via
1247
         *    {@linkplain MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double) MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)}.</li>
1248
         *   <li><code><i>OUTDATED</i></code>: refreshes all layers, changing the status to <code><i>UPDATED</i></code>, via
1249
         *    {@linkplain MapContext#draw(BufferedImage, Graphics2D, Cancellable, double) MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)}.</li>
1250
         *  <ul>
1251
         * </p>
1252 20397 vcaballero
         *
1253 20098 jmvivo
         * <p>This drawing process is accelerated by a <code>BufferedImage</code>, and can be canceled.</p>
1254 1223 fernando
         *
1255 20397 vcaballero
         * <p>Once the drawing process has finished, the timer stops and this component gets repainted.</p>
1256
         *
1257 20098 jmvivo
         * @deprecated
1258 1223 fernando
         * @author Vicente Caballero Navarro
1259
         */
1260
        public class Drawer extends Thread {
1261 2531 caballero
                //private Graphics g;
1262 20397 vcaballero
1263 20098 jmvivo
                /**
1264
                 * <p>Image with a buffer to accelerate the draw the changes of the graphical items in this component.</p>
1265 20397 vcaballero
                 *
1266 20098 jmvivo
                 * <p>Firstly, information will be drawn in the buffer, and, when is outright drawn, that information will be displayed.
1267
                 * Meanwhile, the previous image can be kept showed.</p>
1268 20397 vcaballero
                 *
1269 20098 jmvivo
                 * @see BufferedImage
1270
                 */
1271 3331 fjp
                private BufferedImage image = null;
1272 20098 jmvivo
1273
                /**
1274
                 * <p>Object to store the flag that notifies the drawing must be canceled or can continue with the process.</p>
1275 20397 vcaballero
                 *
1276 20098 jmvivo
                 * <p>At last resort, the particular implementation of each layer in a <code>MapControl</code>'s <code>MapContrext</code>
1277
         *   will be which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</p>
1278
                 */
1279 1223 fernando
                private CancelDraw cancel;
1280 2531 caballero
                //private boolean threadCancel = false;
1281 1223 fernando
1282
                /**
1283 20098 jmvivo
                 * <p>Creates a new <code>Drawer</code> instance.</p>
1284 1223 fernando
                 */
1285 3331 fjp
                public Drawer(BufferedImage image, CancelDraw cancel)
1286
        {
1287 1223 fernando
                        this.image = image;
1288
                        this.cancel = cancel;
1289 3719 caballero
         //   drawerAlive = true;
1290 1223 fernando
                }
1291
1292
                /**
1293
                 * @see java.lang.Runnable#run()
1294 20098 jmvivo
                 * @see MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)
1295
                 * @see MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)
1296 1223 fernando
                 */
1297
                public void run() {
1298
                        try {
1299 2857 jaume
                                // synchronized (Drawer.class) {
1300 1282 fjp
                                    Graphics2D g = image.createGraphics();
1301 3719 caballero
1302 1282 fjp
                                    ViewPort viewPort = mapContext.getViewPort();
1303 2946 fjp
                    if (status == DESACTUALIZADO)
1304
                    {
1305
                                        Color theBackColor = viewPort.getBackColor();
1306
                                        if (theBackColor == null)
1307
                                            g.setColor(Color.WHITE);
1308
                                        else
1309
                                            g.setColor(theBackColor);
1310
                                            g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
1311
                        status = ACTUALIZADO;
1312
                        mapContext.draw(image, g, cancel,mapContext.getScaleView());
1313
                    }
1314
                    else if (status == ONLY_GRAPHICS)
1315
                    {
1316 2951 fjp
                        status = ACTUALIZADO;
1317 2946 fjp
                        mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView());
1318
                    }
1319 3719 caballero
1320 1327 fjp
                                        timer.stop();
1321 3346 fjp
                    // status = FAST_PAINT;
1322 3719 caballero
                  //  drawerAlive = false;
1323 1223 fernando
                                        repaint();
1324 3719 caballero
1325
1326
1327 2857 jaume
                                // }
1328 1223 fernando
                        } catch (Throwable e) {
1329 1266 fernando
                            timer.stop();
1330 3719 caballero
                                //isCancelled = true;
1331 2943 fjp
                e.printStackTrace();
1332 1223 fernando
                                throwException(e);
1333
                        } finally {
1334
                        }
1335
                }
1336
        }
1337
1338
        /**
1339 20098 jmvivo
         * <p>An instance of <code>CancelDraw</code> will be shared by all this <code>MapControl</code>'s <code>MapContext</code> layers,
1340
         *  allowing receive a notification that, when they're been drawn, to be cancelled.</p>
1341 1223 fernando
         *
1342 20098 jmvivo
         * @see Cancellable
1343
         *
1344 1223 fernando
         * @author Fernando Gonz?lez Cort?s
1345
         */
1346
        public class CancelDraw implements Cancellable {
1347 20098 jmvivo
                /**
1348
                 * <p>Determines if the drawing task must be canceled or not.</p>
1349 20397 vcaballero
                 *
1350 20098 jmvivo
                 * @see #isCanceled()
1351
                 * @see #setCanceled(boolean)
1352
                 */
1353 1223 fernando
                private boolean cancel = false;
1354
1355
                /**
1356 20098 jmvivo
                 * Creates a new <code>CancelDraw</code> object.
1357 1223 fernando
                 */
1358
                public CancelDraw() {
1359
                }
1360
1361 20098 jmvivo
                /*
1362
                 * (non-Javadoc)
1363
                 * @see com.iver.utiles.swing.threads.Cancellable#setCanceled(boolean)
1364 1223 fernando
                 */
1365 4832 fjp
                public void setCanceled(boolean b) {
1366 1223 fernando
                        cancel = b;
1367
                }
1368
1369 20098 jmvivo
                /*
1370
                 * (non-Javadoc)
1371 5317 fjp
                 * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
1372 1223 fernando
                 */
1373
                public boolean isCanceled() {
1374
                        return cancel;
1375
                }
1376
        }
1377
1378
        /**
1379 20098 jmvivo
         * <p>Listens all kind of mouse events produced in {@link MapControl MapControl}, and invokes its current
1380
         *  map tool <i>({@link MapControl#getCurrentMapTool() MapControl#getCurrentMapTool()}</i> to simulate a behavior.</p>
1381 20397 vcaballero
         *
1382 20098 jmvivo
         * <p>Mouse wheel moved events produce a <i>zoom in</i> operation if wheel rotation is negative, or a <i>zoom out</i>
1383
         *  if its positive. Both will be centered in the position of the mouse, but, meanwhile <i>zoom in</i> operation
1384
         *  applies a factor of 0.9, <i>zoom out</i> operation applies a factor of 1.2</p>
1385 20397 vcaballero
         *
1386 20098 jmvivo
         * <p>Mouse wheel moved events can be produced as much frequently, that between each one, the drawing process could
1387
         *  hadn't finished. This is the reason that, in this situation, cancels always the previous drawing process before
1388
         *  applying a <i>zoom</i> operation, and ignores all new mouse positions that are produced before 1 second.</p>
1389 1223 fernando
         *
1390
         * @author Fernando Gonz?lez Cort?s
1391
         */
1392
        public class MapToolListener implements MouseListener, MouseWheelListener,
1393
                MouseMotionListener {
1394 5881 fjp
1395 20098 jmvivo
                /**
1396
                 * <p>Used to avoid mouse wheel move events closed.</p>
1397 20397 vcaballero
                 *
1398
                 * <p>If a mouse wheel move event is produced
1399 20098 jmvivo
                 */
1400 5881 fjp
                long t1;
1401 20098 jmvivo
1402
                /**
1403
                 * <p>Position of the mouse, in map coordinates.</p>
1404 20397 vcaballero
                 *
1405 20098 jmvivo
                 * <p>This point coordinates will be used as center of the <i>zoom</i> operation.</p>
1406
                 */
1407 5881 fjp
                Point2D pReal;
1408 20098 jmvivo
1409 1223 fernando
                /**
1410
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
1411 20098 jmvivo
                 * @see Behavior#mouseClicked(MouseEvent)
1412 1223 fernando
                 */
1413
                public void mouseClicked(MouseEvent e) {
1414
                        try {
1415 12208 jcampos
                                if (currentMapTool != null)
1416
                                    currentMapTool.mouseClicked(e);
1417 1223 fernando
                        } catch (BehaviorException t) {
1418
                                throwException(t);
1419
                        }
1420
                }
1421
1422
                /**
1423
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
1424 20098 jmvivo
                 * @see Behavior#mouseEntered(MouseEvent)
1425 1223 fernando
                 */
1426
                public void mouseEntered(MouseEvent e) {
1427
                        try {
1428 12208 jcampos
                                if (currentMapTool != null)
1429
                                        currentMapTool.mouseEntered(e);
1430 1223 fernando
                        } catch (BehaviorException t) {
1431
                                throwException(t);
1432
                        }
1433
                }
1434
1435
                /**
1436
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
1437 20098 jmvivo
                 * @see Behavior#mouseExited(MouseEvent)
1438 1223 fernando
                 */
1439
                public void mouseExited(MouseEvent e) {
1440
                        try {
1441 12208 jcampos
                                if (currentMapTool != null)
1442
                                    currentMapTool.mouseExited(e);
1443 1223 fernando
                        } catch (BehaviorException t) {
1444
                                throwException(t);
1445
                        }
1446
                }
1447
1448
                /**
1449
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
1450 20098 jmvivo
                 * @see Behavior#mousePressed(MouseEvent)
1451 1223 fernando
                 */
1452
                public void mousePressed(MouseEvent e) {
1453
                        try {
1454 12208 jcampos
                                if (currentMapTool != null)
1455
                                        currentMapTool.mousePressed(e);
1456 1223 fernando
                        } catch (BehaviorException t) {
1457
                                throwException(t);
1458
                        }
1459
                }
1460
1461
                /**
1462
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
1463 20098 jmvivo
                 * @see Behavior#mouseReleased(MouseEvent)
1464 1223 fernando
                 */
1465
                public void mouseReleased(MouseEvent e) {
1466
                        try {
1467 12208 jcampos
                                if (currentMapTool != null)
1468
                                        currentMapTool.mouseReleased(e);
1469 1223 fernando
                        } catch (BehaviorException t) {
1470
                                throwException(t);
1471
                        }
1472
                }
1473
1474
                /**
1475
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
1476 20098 jmvivo
                 * @see Behavior#mouseWheelMoved(MouseWheelEvent)
1477 1223 fernando
                 */
1478
                public void mouseWheelMoved(MouseWheelEvent e) {
1479
                        try {
1480 12208 jcampos
                                if (currentMapTool == null)
1481
                                        return;
1482 14410 vcaballero
1483 1223 fernando
                                currentMapTool.mouseWheelMoved(e);
1484 6306 caballero
1485 5874 fjp
                                // Si el tool actual no ha consumido el evento
1486
                                // entendemos que quiere el comportamiento por defecto.
1487
                                if (!e.isConsumed())
1488
                                {
1489 5881 fjp
                                        // Para usar el primer punto sobre el que queremos centrar
1490
                                        // el mapa, dejamos pasar un segundo para considerar el siguiente
1491
                                        // punto como v?lido.
1492
                                        if (t1 == 0)
1493
                                        {
1494
                                                t1= System.currentTimeMillis();
1495
                                                pReal = vp.toMapPoint(e.getPoint());
1496
                                        }
1497
                                        else
1498
                                        {
1499
                                                long t2 = System.currentTimeMillis();
1500
                                                if ((t2-t1) > 1000)
1501
                                                        t1=0;
1502
                                        }
1503 5874 fjp
                                        cancelDrawing();
1504
                                        ViewPort vp = getViewPort();
1505
1506 6306 caballero
1507 5874 fjp
                                        /* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
1508
                                                        vp.getAdjustedExtent().getCenterY()); */
1509
                                        int amount = e.getWheelRotation();
1510
                                        double nuevoX;
1511
                                        double nuevoY;
1512
                                        double factor;
1513
1514 6306 caballero
                                        if (amount < 0) // nos acercamos
1515 5874 fjp
                                        {
1516
                                                factor = 0.9;
1517
                                        } else // nos alejamos
1518
                                        {
1519
                                                factor = 1.2;
1520
                                        }
1521
                                        Rectangle2D.Double r = new Rectangle2D.Double();
1522
                                        if (vp.getExtent() != null) {
1523
                                                nuevoX = pReal.getX()
1524
                                                                - ((vp.getExtent().getWidth() * factor) / 2.0);
1525
                                                nuevoY = pReal.getY()
1526
                                                                - ((vp.getExtent().getHeight() * factor) / 2.0);
1527
                                                r.x = nuevoX;
1528
                                                r.y = nuevoY;
1529
                                                r.width = vp.getExtent().getWidth() * factor;
1530
                                                r.height = vp.getExtent().getHeight() * factor;
1531
1532
                                                vp.setExtent(r);
1533
                                        }
1534 6306 caballero
1535
1536 5874 fjp
                                }
1537 1223 fernando
                        } catch (BehaviorException t) {
1538
                                throwException(t);
1539
                        }
1540
                }
1541
1542
                /**
1543
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
1544 20098 jmvivo
                 * @see Behavior#mouseDragged(MouseEvent)
1545 1223 fernando
                 */
1546
                public void mouseDragged(MouseEvent e) {
1547
                        try {
1548 12208 jcampos
                                if (currentMapTool != null)
1549
                                        currentMapTool.mouseDragged(e);
1550 1223 fernando
                        } catch (BehaviorException t) {
1551
                                throwException(t);
1552
                        }
1553
                }
1554
1555
                /**
1556
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
1557 20098 jmvivo
                 * @see Behavior#mouseMoved(MouseEvent)
1558 1223 fernando
                 */
1559
                public void mouseMoved(MouseEvent e) {
1560
                        try {
1561 12208 jcampos
                                if (currentMapTool != null)
1562
                                        currentMapTool.mouseMoved(e);
1563 1223 fernando
                        } catch (BehaviorException t) {
1564
                                throwException(t);
1565
                        }
1566
                }
1567
        }
1568
1569
        /**
1570 20098 jmvivo
         * <p<code>MapContextListener</code> listens all events produced in a <code>MapControl</code>'s <code>MapContext</code>
1571
         * object during an atomic period of time, and sets it to dirty, <i>executing <code>drawMap(false)</code>, if any of the
1572
         * following conditions is accomplished</i>:
1573
         * <ul>
1574
         *  <li>Any of the <code>LayerEvent</code> in the <code>AtomicEvent</code> parameter notifies a <i>visibility change</i>.</li>
1575
         *  <li>There is at least one <code>ColorEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1576
         *  <li>There is at least one <code>ExtentEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1577
         *  <li>Any of the <code>LayerCollectionEvent</code> in the <code>AtomicEvent</code> parameter notifies that a driver's layer has reloaded it successfully.</li>
1578
         *  <li>There is at least one <code>LegendEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1579
         *  <li>There is at least one <code>SelectionEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1580
         * </ul>
1581
         * </p>
1582 1223 fernando
         *
1583
         * @author Fernando Gonz?lez Cort?s
1584
         */
1585
        public class MapContextListener implements AtomicEventListener {
1586
                /**
1587
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
1588
                 */
1589
                public void atomicEvent(AtomicEvent e) {
1590
                        boolean redraw = false;
1591
                        LayerEvent[] layerEvents = e.getLayerEvents();
1592
1593
                        for (int i = 0; i < layerEvents.length; i++) {
1594
                                if (layerEvents[i].getProperty().equals("visible")) {
1595
                                        redraw = true;
1596
                                }
1597
                        }
1598
1599
                        if (e.getColorEvents().length > 0) {
1600
                                redraw = true;
1601
                        }
1602
1603
                        if (e.getExtentEvents().length > 0) {
1604
                                redraw = true;
1605
                        }
1606 6306 caballero
1607 5941 jmvivo
                        if (e.getProjectionEvents().length > 0) {
1608
                                //redraw = true;
1609
                        }
1610 1223 fernando
1611 6306 caballero
1612 14573 vcaballero
                        LayerCollectionEvent[] aux = e.getLayerCollectionEvents();
1613
                        if (aux.length > 0) {
1614
                                for (int i=0; i < aux.length; i++)
1615
                                {
1616
                                        if (aux[i].getAffectedLayer().getFLayerStatus().isDriverLoaded())
1617
                                        {
1618
                                                redraw = true;
1619
                                                break;
1620
                                        }
1621
                                }
1622
1623 1223 fernando
                        }
1624
1625
                        if (e.getLegendEvents().length > 0) {
1626
                                redraw = true;
1627
                        }
1628
1629
                        if (e.getSelectionEvents().length > 0) {
1630
                                redraw = true;
1631
                        }
1632
1633
                        if (redraw) {
1634 12173 nacho
                //System.out.println("MapContextListener redraw");
1635 3422 fjp
                                MapControl.this.drawMap(false);
1636 1223 fernando
                        }
1637
                }
1638
        }
1639 20098 jmvivo
1640
        /**
1641
         * <p>Gets the <code>ViewPort</code> of this component's {@link MapContext MapContext} .</p>
1642 20397 vcaballero
         *
1643 20098 jmvivo
         * @see MapContext#getViewPort()
1644
         */
1645 1223 fernando
        public ViewPort getViewPort() {
1646
                return vp;
1647
        }
1648 3719 caballero
1649 2877 caballero
        /**
1650 20098 jmvivo
         * <p>Returns all registered <code>Behavior</code> that can define a way to work with this <code>MapControl</code>.</p>
1651 20397 vcaballero
         *
1652 20098 jmvivo
         * @return registered <code>Behavior</code> to this <code>MapControl</code>
1653 20397 vcaballero
         *
1654 20098 jmvivo
         * @see #addMapTool(String, Behavior)
1655
         * @see #addMapTool(String, Behavior[])
1656
         * @see #getMapToolsKeySet()
1657
         * @see #hasTool(String)
1658 2877 caballero
         */
1659 2876 caballero
        public HashMap getNamesMapTools() {
1660
                return namesMapTools;
1661
        }
1662 4120 caballero
1663 20098 jmvivo
        /*
1664
         * (non-Javadoc)
1665
         * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRepaint()
1666
         */
1667 4142 caballero
        public void commandRepaint() {
1668 4127 caballero
                drawMap(false);
1669 4120 caballero
        }
1670 4127 caballero
1671 20098 jmvivo
        /*
1672
         * (non-Javadoc)
1673
         * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRefresh()
1674
         */
1675 4127 caballero
        public void commandRefresh() {
1676
                // TODO Auto-generated method stub
1677
        }
1678 4888 caballero
1679 20098 jmvivo
        /**
1680
         * <p>Equivalent operation to <i>undo</i>.</p>
1681 20397 vcaballero
         *
1682 20098 jmvivo
         * <p>Exchanges the previous tool with the current one.</p>
1683 20397 vcaballero
         *
1684 20098 jmvivo
         * @see #addMapTool(String, Behavior)
1685
         * @see #addMapTool(String, Behavior[])
1686
         * @see #setTool(String)
1687
         */
1688 4888 caballero
        public void setPrevTool() {
1689
                setTool(prevTool);
1690
        }
1691 6306 caballero
1692 20098 jmvivo
        /**
1693
         * <p>Executes a <i>zoom in</i> operation centered at the center of the extent.</p>
1694 20397 vcaballero
         *
1695 20098 jmvivo
         * <p>This implementation is designed for being invoked outside a <code>Behavior</code>, for example
1696 20397 vcaballero
         *  by an action of pressing a button; and simulates that the event has been produced by
1697 20098 jmvivo
         *  releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
1698
         *  <code>MapControl</code> object that's responsible for the <i>zoom in</i> operation.</p>
1699 20397 vcaballero
         *
1700 20098 jmvivo
         * @see #zoomOut()
1701
         */
1702 10837 caballero
        public void zoomIn() {
1703 24160 jmvivo
//                getMapContext().clearAllCachingImageDrawnLayers();
1704 6264 fjp
                Behavior mapTool = (Behavior) namesMapTools.get("zoomIn");
1705
                ViewPort vp=getViewPort();
1706
                Rectangle2D r=getViewPort().getAdjustedExtent();
1707
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1708
                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);
1709
                try {
1710
                        mapTool.mousePressed(e);
1711
                        mapTool.mouseReleased(e);
1712
                } catch (BehaviorException t) {
1713
                        throwException(t);
1714
                }
1715
        }
1716 20397 vcaballero
1717 20098 jmvivo
        /**
1718
         * <p>Executes a <i>zoom out</i> operation centered at the center of the extent.</p>
1719 20397 vcaballero
         *
1720 20098 jmvivo
         * <p>This implementation is thought for being invoked outside a <code>Behavior</code>, for example
1721 20397 vcaballero
         *  by an action of pressing a button, and simulates that the event has been produced by
1722 20098 jmvivo
         *  releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
1723
         *  <code>MapControl</code> object that's responsible for the <i>zoom out</i> operation.</p>
1724 20397 vcaballero
         *
1725 20098 jmvivo
         * @see #zoomIn()
1726
         */
1727 6264 fjp
        public void zoomOut() {
1728 24160 jmvivo
//                getMapContext().clearAllCachingImageDrawnLayers();
1729 6264 fjp
                Behavior mapTool = (Behavior) namesMapTools.get("zoomOut");
1730
                ViewPort vp=getViewPort();
1731
                Rectangle2D r=getViewPort().getAdjustedExtent();
1732
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1733
                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);
1734
                try {
1735
                        mapTool.mousePressed(e);
1736
                        mapTool.mouseReleased(e);
1737
                } catch (BehaviorException t) {
1738
                        throwException(t);
1739
                }
1740
        }
1741 20098 jmvivo
1742
        /**
1743
         * <p>Returns the listener used to catch all mouse events produced in this <code>MapControl</code> instance
1744 20397 vcaballero
         *  and that redirects the calls to the current map tool.</p>
1745
         *
1746 20098 jmvivo
         * @return the map tool listener used
1747
         */
1748 12208 jcampos
        public MapToolListener getMapToolListener() {
1749
                return mapToolListener;
1750
        }
1751 20098 jmvivo
1752 12208 jcampos
//         mapTool can be null, for instance, in 3D's navigation tools
1753 20098 jmvivo
        /**
1754
         * <p>Sets <code>mapTool</code> as this <code>MapControl</code>'s current map tool.
1755 20397 vcaballero
         *
1756 20098 jmvivo
         * @param mapTool a map tool, or <code>null</code> to disable the interaction with the user
1757 20397 vcaballero
         *
1758 20098 jmvivo
         * @see #getCurrentMapTool()
1759
         * @see #getCurrentTool()
1760
         * @see #setTool(String)
1761
         * @see #setPrevTool()
1762
         * @see #addMapTool(String, Behavior)
1763
         * @see #addMapTool(String, Behavior[])
1764
         */
1765 12208 jcampos
        public void setCurrentMapTool(Behavior mapTool ){
1766
                currentMapTool = mapTool;
1767
        }
1768 14410 vcaballero
1769 20098 jmvivo
        /**
1770
         * <p>Sets the delay to the timer that refreshes this <code>MapControl</code> instance.</p>
1771 20397 vcaballero
         *
1772 20098 jmvivo
         * <p><code>Delay (in ms) = 1000 / getDrawFrameRate()</code></p>
1773 20397 vcaballero
         *
1774 20098 jmvivo
         * @see #getDrawFrameRate()
1775
         * @see #setDrawFrameRate(int)
1776
         */
1777 13733 jaume
        public void applyFrameRate() {
1778
                if (getDrawFrameRate()>0) {
1779
                        timer.setDelay(1000/getDrawFrameRate());
1780
                }
1781
        }
1782
1783 20098 jmvivo
        /**
1784
         * <p>Returns the draw frame rate.</p>
1785 20397 vcaballero
         *
1786 20098 jmvivo
         * <p>Draw frame rate is the number of repaints of this <code>MapControl</code> instance that timer invokes per second.</p>
1787 20397 vcaballero
         *
1788 20098 jmvivo
         * @return number of repaints of this <code>MapControl</code> instance that timer invokes per second
1789 20397 vcaballero
         *
1790 20098 jmvivo
         * @see #applyFrameRate()
1791
         * @see #setDrawFrameRate(int)
1792
         */
1793 13733 jaume
        public static int getDrawFrameRate() {
1794
                return drawFrameRate;
1795
        }
1796
1797 20098 jmvivo
        /**
1798
         * <p>Sets the draw frame rate.</p>
1799 20397 vcaballero
         *
1800 20098 jmvivo
         * <p>Draw frame rate is the number of repaints of this <code>MapControl</code> instance that timer invokes per second.</p>
1801 20397 vcaballero
         *
1802 20098 jmvivo
         * @param drawFrameRate number of repaints of this <code>MapControl</code> instance that timer invokes per second
1803 20397 vcaballero
         *
1804 20098 jmvivo
         * @see #applyFrameRate()
1805
         * @see #getDrawFrameRate()
1806
         */
1807 13733 jaume
        public static void setDrawFrameRate(int drawFrameRate) {
1808
                MapControl.drawFrameRate = drawFrameRate;
1809
        }
1810
1811 20098 jmvivo
        /**
1812
         * <p>Determines if its enabled the repaint that invokes the timer according to {@link #getDrawFrameRate() #getDrawFrameRate()}.</p>
1813 20397 vcaballero
         *
1814 20098 jmvivo
         * @return <code>true</code> if its enabled; otherwise <code>false</code>
1815
         */
1816 13733 jaume
        public static boolean isDrawAnimationEnabled() {
1817
                return drawAnimationEnabled;
1818
        }
1819
1820 20098 jmvivo
        /**
1821
         * <p>Sets if its enabled the repaint that invokes the timer according to {@link #getDrawFrameRate() #getDrawFrameRate()}.</p>
1822 20397 vcaballero
         *
1823 20098 jmvivo
         * @param drawAnimationEnabled <code>true</code> to enable the mode; otherwise <code>false</code>
1824
         */
1825 13733 jaume
        public static void setDrawAnimationEnabled(boolean drawAnimationEnabled) {
1826
                MapControl.drawAnimationEnabled = drawAnimationEnabled;
1827
        }
1828 20098 jmvivo
1829
        /**
1830
         * <p>Gets the shared object that determines if a drawing process must be cancelled or can continue.</p>
1831 20397 vcaballero
         *
1832 20098 jmvivo
         * @return the shared object that determines if a drawing process must be cancelled or can continue
1833
         */
1834 18454 vcaballero
        public CancelDraw getCanceldraw() {
1835
                return canceldraw;
1836
        }
1837 20358 vcaballero
1838
        /**
1839 22657 vcaballero
         * <p>Adds a new tool as combined tool.</p>
1840
         * <p>The new tool will be stored with the previous combined tools, and will be combined with
1841
         *  the current tool.</p>
1842
         * <p>If <code>tool</code> was already stored as a combined tool, doesn't adds it.</p>
1843 24160 jmvivo
         *
1844 22657 vcaballero
         * @param tool a new tool to be used combined with the current tool
1845
         */
1846
        public void addCombinedTool(Behavior tool) {
1847
                if (combinedTool == null) {
1848
                        combinedTool = tool;
1849
                }
1850
                else {
1851
                        if (combinedTool instanceof CompoundBehavior) {
1852
                                if (((CompoundBehavior)combinedTool).containsBehavior(tool))
1853
                                        return;
1854 24160 jmvivo
1855 22657 vcaballero
                                ((CompoundBehavior)combinedTool).addMapBehavior(tool, true);
1856
                        }
1857
                        else {
1858
                                if (combinedTool.equals(tool))
1859
                                        return;
1860 24160 jmvivo
1861 22657 vcaballero
                                combinedTool = new CompoundBehavior(new Behavior[] {combinedTool});
1862
                                ((CompoundBehavior)combinedTool).addMapBehavior(tool, true);
1863
                        }
1864
                }
1865
1866
                if (currentMapTool == null)
1867
                        return;
1868
1869
                if (currentMapTool instanceof CompoundBehavior) {
1870
                        ((CompoundBehavior)currentMapTool).addMapBehavior(tool, true);
1871
                }
1872
                else {
1873
                        currentMapTool = new CompoundBehavior(new Behavior[] {currentMapTool});
1874
                        ((CompoundBehavior)currentMapTool).addMapBehavior(tool, true);
1875
                }
1876
        }
1877 24160 jmvivo
1878 22657 vcaballero
        /**
1879 20358 vcaballero
         * <p>Gets the tool used in combination with the current tool of this <code>MapControl</code>.</p>
1880 24160 jmvivo
         *
1881 20358 vcaballero
         * @return the tool used in combination with the <code>currentMapTool</code>; <code>null</code> if there is
1882
         *  no combined tool
1883
         */
1884
        public Behavior getCombinedTool() {
1885
                return combinedTool;
1886
        }
1887
1888
        /**
1889
         * <p>Sets a tool to be used in combination with the current tool of this <code>MapControl</code>.</p>
1890 24160 jmvivo
         *
1891 20358 vcaballero
         * @param combinedTool a tool to be used in combination with the current tool of <code>MapControl</code>
1892
         */
1893
        public void setCombinedTool(Behavior combinedTool) {
1894
                this.combinedTool = combinedTool;
1895 24160 jmvivo
1896 20358 vcaballero
                if (currentMapTool == null)
1897
                        return;
1898
1899 22657 vcaballero
                if (currentMapTool instanceof CompoundBehavior) {
1900
                        ((CompoundBehavior)currentMapTool).addMapBehavior(combinedTool, true);
1901 20358 vcaballero
                }
1902
                else {
1903 22657 vcaballero
                        currentMapTool = new CompoundBehavior(new Behavior[] {currentMapTool});
1904
                        ((CompoundBehavior)currentMapTool).addMapBehavior(combinedTool, true);
1905 20358 vcaballero
                }
1906
        }
1907
1908
        /**
1909
         * <p>Removes the tool used in combination with the current tool of this <code>MapControl</code>.</p>
1910
         */
1911
        public void removeCombinedTool() {
1912
                if ((currentMapTool != null) && (currentMapTool instanceof CompoundBehavior)) {
1913
                                ((CompoundBehavior)currentMapTool).removeMapBehavior(combinedTool);
1914
                }
1915 24160 jmvivo
1916 20358 vcaballero
                combinedTool = null;
1917
        }
1918 22657 vcaballero
1919
        /**
1920
         * <p>Removes the tool <code>tool</code> used in combination with the current tool of this <code>MapControl</code>.</p>
1921
         */
1922
        public void removeCombinedTool(Behavior tool) {
1923
                if ((currentMapTool != null) && (currentMapTool instanceof CompoundBehavior)) {
1924
                                ((CompoundBehavior)currentMapTool).removeMapBehavior(tool);
1925
                }
1926
1927
                if (combinedTool == null)
1928
                        return;
1929
1930
                if (combinedTool instanceof CompoundBehavior) {
1931
                        ((CompoundBehavior)combinedTool).removeMapBehavior(tool);
1932
                }
1933
                else {
1934
                        combinedTool = null;
1935
                }
1936
        }
1937 1223 fernando
}