Statistics
| Revision:

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

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

670
                                break;
671 1243 fjp
                        }
672 3719 caballero

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

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

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