Statistics
| Revision:

root / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapControl.java @ 20857

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

647
                                break;
648 1243 fjp
                        }
649 3719 caballero

650 1223 fernando
                }
651
                canceldraw.setCancel(false);
652
                isCancelled = false;
653 3368 fjp
        drawerAlive = false; */
654 1223 fernando
        }
655 3719 caballero
656 20100 jmvivo
        /**
657
         * <p>Creates a {@link BufferedImage BufferedImage} image if there was no buffered image, or if
658
         *  its viewport's image height or width is different from this component's size. Once has created
659
         *  a double-buffer, fills it with the vieport's background color, or with <i>white</i> if it had no background color.</p>
660
         *
661
         * <p>If no double-buffered existed, creates a {@link BufferedImage BufferedImage} with the size of this component,
662
         * and as an image with 8-bit RGBA color components packed into integer pixels. That image has a <code>DirectColorModel</code> with alpha.
663
         * The color data in that image is considered not to be premultiplied with alpha.</p>
664
         *
665
         * <p>Once has created and filled the new inner <code>MapControl</code>'s double-buffer, changes the status to
666
         * <code>OUTDATED</code>.</p>
667
         *
668
         * @return <code>true</code> if has created and filled a new double-buffer for this <code>MapControl</code> instance; otherwise <code>false</code>
669
         */
670 3375 fjp
    private boolean adaptToImageSize()
671
    {
672
        if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight()))
673
        {
674
            image = new BufferedImage(this.getWidth(), this.getHeight(),
675
                    BufferedImage.TYPE_INT_ARGB);
676 6212 fjp
            // ESTILO MAC
677
//                image = GraphicsEnvironment.getLocalGraphicsEnvironment()
678
//                                .getDefaultScreenDevice().getDefaultConfiguration()
679
//                                .createCompatibleImage(this.getWidth(), this.getHeight());
680 3375 fjp
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
681 6630 caballero
            getMapContext().getViewPort().refreshExtent();
682 3719 caballero
683
684 3375 fjp
            Graphics gTemp = image.createGraphics();
685
            Color theBackColor = vp.getBackColor();
686
            if (theBackColor == null)
687
                gTemp.setColor(Color.WHITE);
688
            else
689
                gTemp.setColor(theBackColor);
690 3719 caballero
691 3375 fjp
            gTemp.fillRect(0,0,getWidth(), getHeight());
692
            gTemp.dispose();
693
            status = DESACTUALIZADO;
694
            // g.drawImage(image,0,0,null);
695
            return true;
696
        }
697
        return false;
698
    }
699 1223 fernando
700
        /**
701 20100 jmvivo
         * <p>Paints the graphical information of this component using a double buffer.</p>
702
         *
703
         * <p>If the double buffer wasn't created, creates a new one.</p>
704
         *
705
         * <p>Paints the component according the following algorithm:
706
         * <br>
707
         *  &nbsp If <i>status</i> is <i>UPDATED</i>:<br>
708
         *  &nbsp &nbsp If there is no <i>double buffer</i>:<br>
709
         *  &nbsp &nbsp &nbsp If there is a <i>behavior</i> for managing the <code>MapControl</code> instance, delegates
710
         *   the drawing process to that behavior, calling: <code><i>behavior_instance</i>.paintComponent(g)</code> &nbsp .<br>
711
         *  &nbsp &nbsp &nbsp Else, repaints the current graphical information quickly calling: <code>g.drawImage(image,0,0,null)</code> &nbsp .<br>
712
         *  &nbsp Else, (<i>status</i> is <i>OUTDATED</i>, or <i>ONLY_GRAPHICS</i>): executes a quickly repaint of the previous information calling <code>g.drawImage(image,0,0,null)</code>, and creates
713
         *   a <i>painting request</i> to delegate the heavy drawing process to the {@link Drawer2 Drawer2}'s worker thread, according the <i>SingleWorketThread</i> pattern, starting a timer to update
714
         *   (invoking <code>repaint()</code> that comprises invoke this method) the view every delay of 360 ms. during the the process drawing.</p>
715
         *
716
           * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
717
           * @see Drawer2
718 1223 fernando
         */
719
        protected void paintComponent(Graphics g) {
720 3375 fjp
        adaptToImageSize();
721 11034 caballero
              if (status == ACTUALIZADO) {
722 1223 fernando
                        /*
723
                         * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
724
                         * en dicho behaviour
725
                         */
726 3372 fjp
            if (image != null)
727
            {
728
                if (currentMapTool != null)
729
                    currentMapTool.paintComponent(g);
730
                else
731
                    g.drawImage(image,0,0,null);
732 1223 fernando
                        }
733 3719 caballero
                } else if ((status == DESACTUALIZADO)
734 2946 fjp
                || (status == ONLY_GRAPHICS)) {
735 3719 caballero
736 3331 fjp
                g.drawImage(image,0,0,null);
737 3719 caballero
738 3372 fjp
                drawer2.put(new PaintingRequest());
739
                timer.start();
740 11034 caballero
       }
741 1223 fernando
        }
742
743
        /**
744 20100 jmvivo
         * <p>Gets the {@link BufferedImage BufferedImage} used to accelerate the draw of new ''frames'' with changes,
745
         * or new graphical items in this component.</p>
746
         *
747
         * @return double buffered image used by this component to accelerate the draw of its graphical information, or
748
         * <code>null</code> if isn't already created
749
         *
750
         * @see BufferedImage
751 1223 fernando
         */
752
        public BufferedImage getImage() {
753
                return image;
754
        }
755
756
        /**
757 20100 jmvivo
         * <p>Forces repaint all visible graphical information in this component.</p>
758
         *
759
         * <p>If <code>doClear == true</code>, before repainting, clears the background color, with the
760
         *  inner viewport's background color.</p>
761
         *
762
         * @param doClear <code>true</code> if needs clearing the background color before drawing the map
763
         *
764
         * @see #cancelDrawing()
765
         * @see FLayers#setDirty(boolean)
766 1223 fernando
         */
767
        public void drawMap(boolean doClear) {
768 3979 caballero
                cancelDrawing();
769 11034 caballero
                status = DESACTUALIZADO;
770 5923 fjp
        getMapContext().getLayers().setDirty(true);
771 3331 fjp
                if (doClear)
772
        {
773 3372 fjp
            // image = null; // Se usa para el PAN
774
            if (image != null)
775
            {
776
                Graphics2D g = image.createGraphics();
777
                Color theBackColor = vp.getBackColor();
778
                if (theBackColor == null)
779
                    g.setColor(Color.WHITE);
780
                else
781
                    g.setColor(theBackColor);
782
                g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight());
783
                g.dispose();
784 3719 caballero
            }
785 3331 fjp
        }
786 1223 fernando
                repaint();
787
        }
788 6306 caballero
789 20100 jmvivo
        /**
790
         * <p>Cancels any current drawing process, changing the status to <code>OUTDATED</code>, and forcing
791
         * repaint only the layers dirty.</p>
792
         *
793
         * @see #cancelDrawing()
794
         */
795 5923 fjp
        public void rePaintDirtyLayers()
796
        {
797
                cancelDrawing();
798
        status = DESACTUALIZADO;
799
        repaint();
800
        }
801 6306 caballero
802 20100 jmvivo
        /**
803
         * <p>Cancels any current drawing process, changing the status to <code>ONLY_GRAPHICS</code>, and forcing
804
         * repaint only the graphical layer of the <code>MapContext</code>.</p>
805
         */
806 2901 fjp
    public void drawGraphics() {
807
        status = ONLY_GRAPHICS;
808
        repaint();
809
    }
810 1223 fernando
811
        /**
812
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
813
         */
814
        public void componentHidden(ComponentEvent e) {
815
        }
816
817
        /**
818
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
819
         */
820
        public void componentMoved(ComponentEvent e) {
821
        }
822
823
        /**
824
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
825
         */
826
        public void componentResized(ComponentEvent e) {
827 3375 fjp
                /* image = new BufferedImage(this.getWidth(), this.getHeight(),
828 3719 caballero
                                BufferedImage.TYPE_INT_ARGB);
829 1223 fernando
                Graphics gTemp = image.createGraphics();
830
                gTemp.setColor(vp.getBackColor());
831 3372 fjp
                gTemp.fillRect(0,0,getWidth(), getHeight());
832
        System.out.println("MapControl resized");
833
            // image = null;
834 1223 fernando
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
835 3375 fjp
                getMapContext().getViewPort().setScale(); */
836 3372 fjp
                // drawMap(true);
837 1223 fernando
        }
838
839
        /**
840
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
841
         */
842
        public void componentShown(ComponentEvent e) {
843
        }
844
845
        /**
846 20100 jmvivo
         * @see ExceptionHandlingSupport#addExceptionListener(ExceptionListener)
847 1223 fernando
         */
848
        public void addExceptionListener(ExceptionListener o) {
849
                exceptionHandlingSupport.addExceptionListener(o);
850
        }
851
852
        /**
853 20100 jmvivo
         * @see ExceptionHandlingSupport#removeExceptionListener(ExceptionListener)
854 1223 fernando
         */
855
        public boolean removeExceptionListener(ExceptionListener o) {
856
                return exceptionHandlingSupport.removeExceptionListener(o);
857
        }
858
859
        /**
860 20100 jmvivo
         * @see ExceptionHandlingSupport#throwException(Throwable)
861 1223 fernando
         */
862 10520 ldiaz
        protected void throwException(Throwable t) {
863 1223 fernando
                exceptionHandlingSupport.throwException(t);
864
        }
865
866 20100 jmvivo
        /**
867
         * <p>Represents each <code>MapControl</code>'s data painting request.</p>
868
         *
869
         * <p>The request will be attended by a <code>Drawer2</code>, which will hold it since the <code>Drawer2</code>'s worker
870
         *  takes it, or arrives a new painting request, which will replace it.</p>
871
         */
872 3368 fjp
    private class PaintingRequest
873
    {
874 20100 jmvivo
            /**
875
             * <p>Creates a new <code>PaintingRequest</p> instance.</p>
876
             */
877 3372 fjp
        public PaintingRequest()
878 3368 fjp
        {
879
        }
880 3719 caballero
881 20100 jmvivo
        /**
882
         * <p><code>MapControl</code> paint process:</p>
883
         *
884
         * <p>
885
         *  <ul>
886
         *   <li><i>1.- </i>Cancels all previous <code>MapControl</code>'s drawing processes.</li>
887
         *   <li><i>2.- </i>If <i>status</i> was OUTDATED:
888
         *    <ul>
889
         *     <li><i>2.1.- </i>Fills the background color with viewport's background color, or <i>white</i> if it was undefined.</li>
890
         *     <li><i>2.2.- </i>Sets <i>status</i> to UPDATED.</li>
891
         *     <li><i>2.3.- </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>
892
         *    </ul>
893
         *   </li>
894
         *   <li><i>3.- </i>Else, if <i>status</i> was ONLY_GRAPHICS:
895
         *    <ul>
896
         *     <li><i>3.1.- </i>Sets <i>status</i> to UPDATED.</li>
897
         *     <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>
898
         *    </ul>
899
         *   </li>
900
         *   <li><i>4.- </i>Stops the <i>timer</i>.</li>
901
         *   <li><i>5.- </i>Repaints this component invoking: <code>repaint();</code></li>
902
         *  </ul>
903
         * </p>
904
         *
905
         * @see #cancelDrawing()
906
         * @see MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)
907
         * @see MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)
908
         *
909
         * @see ViewPort
910
         */
911 3368 fjp
        public void paint()
912
        {
913 3719 caballero
            try
914
            {
915 4832 fjp
                    canceldraw.setCanceled(false);
916 3372 fjp
                /* if (image == null)
917
                {
918
                    image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
919
                            BufferedImage.TYPE_INT_ARGB);
920
                    Graphics gTemp = image.createGraphics();
921
                    Color theBackColor = vp.getBackColor();
922
                    if (theBackColor == null)
923
                        gTemp.setColor(Color.WHITE);
924
                    else
925
                        gTemp.setColor(theBackColor);
926 3719 caballero

927 3372 fjp
                    gTemp.fillRect(0,0,getWidth(), getHeight());
928
                    gTemp.dispose();
929
                    // g.drawImage(image,0,0,null);
930
                    System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
931
                } */
932 3368 fjp
                Graphics2D g = image.createGraphics();
933 3719 caballero
934 3368 fjp
                ViewPort viewPort = mapContext.getViewPort();
935 3719 caballero
936 3368 fjp
                if (status == DESACTUALIZADO)
937
                {
938 5874 fjp
                        Graphics2D gTemp = image.createGraphics();
939 3368 fjp
                    Color theBackColor = viewPort.getBackColor();
940
                    if (theBackColor == null)
941 5874 fjp
                        gTemp.setColor(Color.WHITE);
942 3368 fjp
                    else
943 5874 fjp
                        gTemp.setColor(theBackColor);
944
                    gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
945 3368 fjp
                    status = ACTUALIZADO;
946 6261 fjp
                    // ESTILO MAC
947
//                    BufferedImage imgMac = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
948
//                            BufferedImage.TYPE_INT_ARGB);
949 6306 caballero
//
950 6261 fjp
//                    mapContext.draw(imgMac, g, canceldraw, mapContext.getScaleView());
951
//                    g.drawImage(imgMac, 0, 0, null);
952
                    // FIN ESTILO MAC
953
                    // SIN MAC:
954 3372 fjp
                    mapContext.draw(image, g, canceldraw, mapContext.getScaleView());
955 3719 caballero
956 3368 fjp
                }
957
                else if (status == ONLY_GRAPHICS)
958
                {
959 3719 caballero
                    status = ACTUALIZADO;
960 3372 fjp
                    mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView());
961 3719 caballero
962 3368 fjp
                }
963 3719 caballero
964
965 3368 fjp
                // status = FAST_PAINT;
966 3719 caballero
              //  drawerAlive = false;
967 3372 fjp
                timer.stop();
968 3368 fjp
                repaint();
969 3719 caballero
970
971 3368 fjp
            } catch (Throwable e) {
972
                timer.stop();
973 3719 caballero
              //  isCancelled = true;
974 3368 fjp
                e.printStackTrace();
975
                throwException(e);
976
            } finally {
977
            }
978 3719 caballero
979 3368 fjp
        }
980 3719 caballero
981 3368 fjp
    }
982 3719 caballero
983 20100 jmvivo
984 3368 fjp
    /**
985 20100 jmvivo
     * <p>An instance of <code>Drawer2</code> could manage all <code>MapControl</code> painting requests.</p>
986
     *
987
     * <p>Based on the <i>WorkerThread</i> software pattern, creates a worker thread that will attend sequentially
988
     *  the current waiting painting request, after finishing the previous (that could be by a cancel action).</p>
989
     *
990
     * <p>All new {@link PaintingRequest PaintingRequest} generated will be stored as <i>waiting requests</i> since the worker
991
     * attends it.</p>
992
     *
993
     * <p>If a worker finished and there was no <i>painting request</i>, the worker would be set to wait until any
994
     *  <i>painting request</i> would be put.</p>
995
     *
996 3368 fjp
     * @author fjp
997
     */
998
    public class Drawer2
999
    {
1000
        // Una mini cola de 2. No acumulamos peticiones de dibujado
1001
        // dibujamos solo lo ?ltimo que nos han pedido.
1002 3719 caballero
1003 20100 jmvivo
            /**
1004
             * <p>Painting request that's been attended by the <code>Drawer2</code>'s worker.</p>
1005
             *
1006
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1007
             * @see #take()
1008
             */
1009
            private PaintingRequest paintingRequest;
1010
1011
            /**
1012
             * <p>Painting request waiting to be attended by the <code>Drawer2</code>'s worker.</p>
1013
             *
1014
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1015
             * @see #take()
1016
             */
1017
            private PaintingRequest waitingRequest;
1018
1019
            /**
1020
             * <p>Determines that the <code>Drawer2</code>'s worker is busy attending a painting request.</p>
1021
              *
1022
             * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1023
             * @see #take()
1024
             */
1025 3368 fjp
        private boolean waiting;
1026 20100 jmvivo
1027
            /**
1028
             * <p>Notifies the <code>Drawer2</code>'s worker to finish or continue with its process.</p>
1029
             *
1030
             * @see #setShutdown(boolean)
1031
             */
1032 3368 fjp
        private boolean shutdown;
1033 3719 caballero
1034 20100 jmvivo
        /**
1035
         * <p>Sets this <code>Drawer2</code>'s worker to finish or continue with its process.</p>
1036
         *
1037
         * @param isShutdown a boolean value
1038
         */
1039 3368 fjp
        public void setShutdown(boolean isShutdown)
1040
        {
1041
            shutdown = isShutdown;
1042
        }
1043 3719 caballero
1044 20100 jmvivo
        /**
1045
         * <p>Creates a new drawer for managing all data painting requests in <code>MapControl</code>.</p>
1046
         *
1047
         * <p>Includes the following steps:
1048
         *  <ul>
1049
         *   <li>By default, there is no <i>current painting request</i>.</li>
1050
         *   <li>By default, there is no <i>waiting painting request</i>.</li>
1051
         *   <li>By default, the worker thread is waiting no <i>painting request</i>.</li>
1052
         *   <li>By default, the worker thread is running.</li>
1053
         *   <li>Creates and starts a worker thread for attending the <i>painting requests</i>.</li>
1054
         *  </ul>
1055
         * </p>
1056
         */
1057 3719 caballero
        public Drawer2()
1058 3368 fjp
        {
1059
            paintingRequest = null;
1060
            waitingRequest = null;
1061
            waiting = false;
1062
            shutdown = false;
1063
            new Thread(new Worker()).start();
1064
        }
1065 3719 caballero
1066 20100 jmvivo
        /**
1067
         * <p>Sets a <code>PaintingRequest</code> to be attended by the worker thread of this object. If
1068
         *  this one was waiting, wakes up.</p>
1069
         *
1070
         * <p>All waiting threads will be notified synchronized.</p>
1071
         *
1072
         * @param newPaintRequest
1073
         *
1074
         * @see #take()
1075
         */
1076 3368 fjp
        public void put(PaintingRequest newPaintRequest)
1077
        {
1078
            waitingRequest = newPaintRequest;
1079
            if (waiting)
1080
            {
1081
                synchronized (this) {
1082
                    notifyAll();
1083
                }
1084
            }
1085
        }
1086 3719 caballero
1087 20100 jmvivo
        /**
1088
         * <p>Used by this object's worker, returns the current waiting drawing request, causing current thread
1089
         *  to wait until another thread invokes {@link #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest) #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)},
1090
         *  if there was no waiting request.</p>
1091
         *
1092
         * <p>All threads will access synchronized to the waiting request.</p>
1093
         *
1094
         * @return <code>PaintingRequest</code> that was waiting to be attended
1095
         *
1096
         * @see #put(com.iver.cit.gvsig.fmap.MapControl.PaintingRequest)
1097
         */
1098 3368 fjp
        public PaintingRequest take()
1099
        {
1100
            if (waitingRequest == null)
1101
            {
1102
                synchronized (this) {
1103
                    waiting = true;
1104
                    try {
1105
                        wait();
1106
                    }
1107
                    catch (InterruptedException ie)
1108
                    {
1109
                        waiting = false;
1110
                    }
1111
                }
1112
            }
1113
            paintingRequest = waitingRequest;
1114
            waitingRequest = null;
1115
            return paintingRequest;
1116
        }
1117 3719 caballero
1118 20100 jmvivo
        /**
1119
         * <p>Thread for attending painting requests.</p>
1120
         *
1121
         * <p>If there was no double buffer, sets the status to <code>OUTDATED</code> and finishes, otherwise
1122
         *  takes the painting request (it's probably that would wait some time), cancel the previous drawing
1123
         *  process, and starts processing the request.</p>
1124
         *
1125
         * @see Thread
1126
         */
1127 3368 fjp
        private class Worker implements Runnable
1128
        {
1129 20100 jmvivo
                /*
1130
                 * (non-Javadoc)
1131
                 * @see java.lang.Runnable#run()
1132
                 */
1133
                public void run()
1134 3368 fjp
            {
1135
                while (!shutdown)
1136
                {
1137
                    PaintingRequest p = take();
1138
                    System.out.println("Pintando");
1139 4120 caballero
                    if (image != null){
1140
                            cancelDrawing();
1141 3372 fjp
                        p.paint();
1142 4120 caballero
                    } else
1143 3372 fjp
                        status = DESACTUALIZADO;
1144 3368 fjp
                }
1145
            }
1146
        }
1147
    }
1148 3719 caballero
1149 1223 fernando
        /**
1150 20100 jmvivo
         * <p><code>Drawer</code> is implemented for drawing the layers of a <code>MapControl</code>'s <i>MapContext</i> instance
1151
         *  as a <i>thread</i> of execution.</p>
1152
         *
1153
         * <p>Draws <code>MapControl</code> according its <i>status</i>:
1154
         *  <ul>
1155
         *   <li><code><i>ONLY_GRAPHICS</i></code>: refreshes only the graphical layer, changing the status to <code><i>UPDATED</i></code>, via
1156
         *    {@linkplain MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double) MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)}.</li>
1157
         *   <li><code><i>OUTDATED</i></code>: refreshes all layers, changing the status to <code><i>UPDATED</i></code>, via
1158
         *    {@linkplain MapContext#draw(BufferedImage, Graphics2D, Cancellable, double) MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)}.</li>
1159
         *  <ul>
1160
         * </p>
1161
         *
1162
         * <p>This drawing process is accelerated by a <code>BufferedImage</code>, and can be canceled.</p>
1163
         *
1164
         * <p>Once the drawing process has finished, the timer stops and this component gets repainted.</p>
1165 1223 fernando
         *
1166 20100 jmvivo
         * @deprecated
1167 1223 fernando
         * @author Vicente Caballero Navarro
1168
         */
1169
        public class Drawer extends Thread {
1170 2531 caballero
                //private Graphics g;
1171 20100 jmvivo
1172
                /**
1173
                 * <p>Image with a buffer to accelerate the draw the changes of the graphical items in this component.</p>
1174
                 *
1175
                 * <p>Firstly, information will be drawn in the buffer, and, when is outright drawn, that information will be displayed.
1176
                 * Meanwhile, the previous image can be kept showed.</p>
1177
                 *
1178
                 * @see BufferedImage
1179
                 */
1180 3331 fjp
                private BufferedImage image = null;
1181 20100 jmvivo
1182
                /**
1183
                 * <p>Object to store the flag that notifies the drawing must be canceled or can continue with the process.</p>
1184
                 *
1185
                 * <p>At last resort, the particular implementation of each layer in a <code>MapControl</code>'s <code>MapContrext</code>
1186
         *   will be which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.</p>
1187
                 */
1188 1223 fernando
                private CancelDraw cancel;
1189 2531 caballero
                //private boolean threadCancel = false;
1190 1223 fernando
1191 20100 jmvivo
1192 1223 fernando
                /**
1193 20100 jmvivo
                 * <p>Creates a new <code>Drawer</code> instance.</p>
1194 1223 fernando
                 */
1195 3331 fjp
                public Drawer(BufferedImage image, CancelDraw cancel)
1196
        {
1197 1223 fernando
                        this.image = image;
1198
                        this.cancel = cancel;
1199 3719 caballero
         //   drawerAlive = true;
1200 1223 fernando
                }
1201
1202
                /**
1203
                 * @see java.lang.Runnable#run()
1204 20100 jmvivo
                 * @see MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)
1205
                 * @see MapContext#drawGraphics(BufferedImage, Graphics2D, Cancellable, double)
1206 1223 fernando
                 */
1207
                public void run() {
1208
                        try {
1209 2857 jaume
                                // synchronized (Drawer.class) {
1210 1282 fjp
                                    Graphics2D g = image.createGraphics();
1211 3719 caballero
1212 1282 fjp
                                    ViewPort viewPort = mapContext.getViewPort();
1213 2946 fjp
                    if (status == DESACTUALIZADO)
1214
                    {
1215
                                        Color theBackColor = viewPort.getBackColor();
1216
                                        if (theBackColor == null)
1217
                                            g.setColor(Color.WHITE);
1218
                                        else
1219
                                            g.setColor(theBackColor);
1220
                                            g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
1221
                        status = ACTUALIZADO;
1222
                        mapContext.draw(image, g, cancel,mapContext.getScaleView());
1223
                    }
1224
                    else if (status == ONLY_GRAPHICS)
1225
                    {
1226 2951 fjp
                        status = ACTUALIZADO;
1227 2946 fjp
                        mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView());
1228
                    }
1229 3719 caballero
1230 1327 fjp
                                        timer.stop();
1231 3346 fjp
                    // status = FAST_PAINT;
1232 3719 caballero
                  //  drawerAlive = false;
1233 1223 fernando
                                        repaint();
1234 3719 caballero
1235
1236
1237 2857 jaume
                                // }
1238 1223 fernando
                        } catch (Throwable e) {
1239 1266 fernando
                            timer.stop();
1240 3719 caballero
                                //isCancelled = true;
1241 2943 fjp
                e.printStackTrace();
1242 1223 fernando
                                throwException(e);
1243
                        } finally {
1244
                        }
1245
                }
1246
        }
1247
1248
        /**
1249 20100 jmvivo
         * <p>An instance of <code>CancelDraw</code> will be shared by all this <code>MapControl</code>'s <code>MapContext</code> layers,
1250
         *  allowing receive a notification that, when they're been drawn, to be cancelled.</p>
1251 1223 fernando
         *
1252 20100 jmvivo
         * @see Cancellable
1253
         *
1254 1223 fernando
         * @author Fernando Gonz?lez Cort?s
1255
         */
1256
        public class CancelDraw implements Cancellable {
1257 20100 jmvivo
                /**
1258
                 * <p>Determines if the drawing task must be canceled or not.</p>
1259
                 *
1260
                 * @see #isCanceled()
1261
                 * @see #setCanceled(boolean)
1262
                 */
1263 1223 fernando
                private boolean cancel = false;
1264
1265
                /**
1266 20100 jmvivo
                 * Creates a new <code>CancelDraw</code> object.
1267 1223 fernando
                 */
1268
                public CancelDraw() {
1269
                }
1270
1271 20100 jmvivo
                /*
1272
                 * (non-Javadoc)
1273
                 * @see com.iver.utiles.swing.threads.Cancellable#setCanceled(boolean)
1274 1223 fernando
                 */
1275 4832 fjp
                public void setCanceled(boolean b) {
1276 1223 fernando
                        cancel = b;
1277
                }
1278
1279 20100 jmvivo
                /*
1280
                 * (non-Javadoc)
1281 5317 fjp
                 * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
1282 1223 fernando
                 */
1283
                public boolean isCanceled() {
1284
                        return cancel;
1285
                }
1286
        }
1287
1288
        /**
1289 20100 jmvivo
         * <p>Listens all kind of mouse events produced in {@link MapControl MapControl}, and invokes its current
1290
         *  map tool <i>({@link MapControl#getCurrentMapTool() MapControl#getCurrentMapTool()}</i> to simulate a behavior.</p>
1291
         *
1292
         * <p>Mouse wheel moved events produce a <i>zoom in</i> operation if wheel rotation is negative, or a <i>zoom out</i>
1293
         *  if its positive. Both will be centered in the position of the mouse, but, meanwhile <i>zoom in</i> operation
1294
         *  applies a factor of 0.9, <i>zoom out</i> operation applies a factor of 1.2</p>
1295
         *
1296
         * <p>Mouse wheel moved events can be produced as much frequently, that between each one, the drawing process could
1297
         *  hadn't finished. This is the reason that, in this situation, cancels always the previous drawing process before
1298
         *  applying a <i>zoom</i> operation, and ignores all new mouse positions that are produced before 1 second.</p>
1299 1223 fernando
         *
1300
         * @author Fernando Gonz?lez Cort?s
1301
         */
1302
        public class MapToolListener implements MouseListener, MouseWheelListener,
1303
                MouseMotionListener {
1304 5881 fjp
1305 20100 jmvivo
                /**
1306
                 * <p>Used to avoid mouse wheel move events closed.</p>
1307
                 *
1308
                 * <p>If a mouse wheel move event is produced
1309
                 */
1310 5881 fjp
                long t1;
1311 20100 jmvivo
1312
                /**
1313
                 * <p>Position of the mouse, in map coordinates.</p>
1314
                 *
1315
                 * <p>This point coordinates will be used as center of the <i>zoom</i> operation.</p>
1316
                 */
1317 5881 fjp
                Point2D pReal;
1318 20100 jmvivo
1319 1223 fernando
                /**
1320
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
1321 20100 jmvivo
                 * @see Behavior#mouseClicked(MouseEvent)
1322 1223 fernando
                 */
1323
                public void mouseClicked(MouseEvent e) {
1324
                        try {
1325 20357 vcaballero
                                if (currentMapTool != null)
1326
                                    currentMapTool.mouseClicked(e);
1327 1223 fernando
                        } catch (BehaviorException t) {
1328
                                throwException(t);
1329
                        }
1330
                }
1331
1332
                /**
1333
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
1334 20100 jmvivo
                 * @see Behavior#mouseEntered(MouseEvent)
1335 1223 fernando
                 */
1336
                public void mouseEntered(MouseEvent e) {
1337
                        try {
1338 20357 vcaballero
                                if (currentMapTool != null)
1339
                                        currentMapTool.mouseEntered(e);
1340 1223 fernando
                        } catch (BehaviorException t) {
1341
                                throwException(t);
1342
                        }
1343
                }
1344
1345
                /**
1346
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
1347 20100 jmvivo
                 * @see Behavior#mouseExited(MouseEvent)
1348 1223 fernando
                 */
1349
                public void mouseExited(MouseEvent e) {
1350
                        try {
1351 20357 vcaballero
                                if (currentMapTool != null)
1352
                                        currentMapTool.mouseExited(e);
1353 1223 fernando
                        } catch (BehaviorException t) {
1354
                                throwException(t);
1355
                        }
1356
                }
1357
1358
                /**
1359
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
1360 20100 jmvivo
                 * @see Behavior#mousePressed(MouseEvent)
1361 1223 fernando
                 */
1362
                public void mousePressed(MouseEvent e) {
1363
                        try {
1364 20357 vcaballero
                                if (currentMapTool != null)
1365
                                        currentMapTool.mousePressed(e);
1366 1223 fernando
                        } catch (BehaviorException t) {
1367
                                throwException(t);
1368
                        }
1369
                }
1370
1371
                /**
1372
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
1373 20100 jmvivo
                 * @see Behavior#mouseReleased(MouseEvent)
1374 1223 fernando
                 */
1375
                public void mouseReleased(MouseEvent e) {
1376
                        try {
1377 20357 vcaballero
                                if (currentMapTool != null)
1378
                                        currentMapTool.mouseReleased(e);
1379 1223 fernando
                        } catch (BehaviorException t) {
1380
                                throwException(t);
1381
                        }
1382
                }
1383
1384
                /**
1385
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
1386 20100 jmvivo
                 * @see Behavior#mouseWheelMoved(MouseWheelEvent)
1387 1223 fernando
                 */
1388
                public void mouseWheelMoved(MouseWheelEvent e) {
1389
                        try {
1390 20357 vcaballero
                                if (currentMapTool == null)
1391
                                        return;
1392
1393 1223 fernando
                                currentMapTool.mouseWheelMoved(e);
1394 6306 caballero
1395 5874 fjp
                                // Si el tool actual no ha consumido el evento
1396
                                // entendemos que quiere el comportamiento por defecto.
1397
                                if (!e.isConsumed())
1398
                                {
1399 5881 fjp
                                        // Para usar el primer punto sobre el que queremos centrar
1400
                                        // el mapa, dejamos pasar un segundo para considerar el siguiente
1401
                                        // punto como v?lido.
1402
                                        if (t1 == 0)
1403
                                        {
1404
                                                t1= System.currentTimeMillis();
1405
                                                pReal = vp.toMapPoint(e.getPoint());
1406
                                        }
1407
                                        else
1408
                                        {
1409
                                                long t2 = System.currentTimeMillis();
1410
                                                if ((t2-t1) > 1000)
1411
                                                        t1=0;
1412
                                        }
1413 5874 fjp
                                        cancelDrawing();
1414
                                        ViewPort vp = getViewPort();
1415
1416 6306 caballero
1417 5874 fjp
                                        /* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
1418
                                                        vp.getAdjustedExtent().getCenterY()); */
1419
                                        int amount = e.getWheelRotation();
1420
                                        double nuevoX;
1421
                                        double nuevoY;
1422
                                        double factor;
1423
1424 6306 caballero
                                        if (amount < 0) // nos acercamos
1425 5874 fjp
                                        {
1426
                                                factor = 0.9;
1427
                                        } else // nos alejamos
1428
                                        {
1429
                                                factor = 1.2;
1430
                                        }
1431
                                        Rectangle2D.Double r = new Rectangle2D.Double();
1432
                                        if (vp.getExtent() != null) {
1433
                                                nuevoX = pReal.getX()
1434
                                                                - ((vp.getExtent().getWidth() * factor) / 2.0);
1435
                                                nuevoY = pReal.getY()
1436
                                                                - ((vp.getExtent().getHeight() * factor) / 2.0);
1437
                                                r.x = nuevoX;
1438
                                                r.y = nuevoY;
1439
                                                r.width = vp.getExtent().getWidth() * factor;
1440
                                                r.height = vp.getExtent().getHeight() * factor;
1441
1442
                                                vp.setExtent(r);
1443
                                        }
1444 6306 caballero
1445
1446 5874 fjp
                                }
1447 1223 fernando
                        } catch (BehaviorException t) {
1448
                                throwException(t);
1449
                        }
1450
                }
1451
1452
                /**
1453
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
1454 20100 jmvivo
                 * @see Behavior#mouseDragged(MouseEvent)
1455 1223 fernando
                 */
1456
                public void mouseDragged(MouseEvent e) {
1457
                        try {
1458 20357 vcaballero
                                if (currentMapTool != null)
1459
                                        currentMapTool.mouseDragged(e);
1460 1223 fernando
                        } catch (BehaviorException t) {
1461
                                throwException(t);
1462
                        }
1463
                }
1464
1465
                /**
1466
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
1467 20100 jmvivo
                 * @see Behavior#mouseMoved(MouseEvent)
1468 1223 fernando
                 */
1469
                public void mouseMoved(MouseEvent e) {
1470
                        try {
1471 20357 vcaballero
                                if (currentMapTool != null)
1472
                                        currentMapTool.mouseMoved(e);
1473 1223 fernando
                        } catch (BehaviorException t) {
1474
                                throwException(t);
1475
                        }
1476
                }
1477
        }
1478
1479
        /**
1480 20357 vcaballero
         * <p>Gets the tool used in combination with the current tool of this <code>MapControl</code>.</p>
1481
         *
1482
         * @return the tool used in combination with the <code>currentMapTool</code>; <code>null</code> if there is
1483
         *  no combined tool
1484
         */
1485
        public Behavior getCombinedTool() {
1486
                return combinedTool;
1487
        }
1488
1489
        /**
1490
         * <p>Sets a tool to be used in combination with the current tool of this <code>MapControl</code>.</p>
1491
         *
1492
         * @param combinedTool a tool to be used in combination with the current tool of <code>MapControl</code>
1493
         */
1494
        public void setCombinedTool(Behavior combinedTool) {
1495
                this.combinedTool = combinedTool;
1496
1497
                if (currentMapTool == null)
1498
                        return;
1499
1500
                if ((currentMapTool != null) && (currentMapTool instanceof CompoundBehavior)) {
1501
                        ((CompoundBehavior)currentMapTool).addMapBehavior(combinedTool, combinedTool.getListener().cancelDrawing());
1502
                }
1503
                else {
1504
                        currentMapTool = new CompoundBehavior(new Behavior[] {currentMapTool, combinedTool});
1505
                }
1506
1507
        }
1508
1509
        /**
1510
         * <p>Removes the tool used in combination with the current tool of this <code>MapControl</code>.</p>
1511
         */
1512
        public void removeCombinedTool() {
1513
                if ((currentMapTool != null) && (currentMapTool instanceof CompoundBehavior)) {
1514
                                ((CompoundBehavior)currentMapTool).removeMapBehavior(combinedTool);
1515
                }
1516
1517
                combinedTool = null;
1518
        }
1519
1520
        /**
1521 20100 jmvivo
         * <p<code>MapContextListener</code> listens all events produced in a <code>MapControl</code>'s <code>MapContext</code>
1522
         * object during an atomic period of time, and sets it to dirty, <i>executing <code>drawMap(false)</code>, if any of the
1523
         * following conditions is accomplished</i>:
1524
         * <ul>
1525
         *  <li>Any of the <code>LayerEvent</code> in the <code>AtomicEvent</code> parameter notifies a <i>visibility change</i>.</li>
1526
         *  <li>There is at least one <code>ColorEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1527
         *  <li>There is at least one <code>ExtentEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1528
         *  <li>Any of the <code>LayerCollectionEvent</code> in the <code>AtomicEvent</code> parameter notifies that a driver's layer has reloaded it successfully.</li>
1529
         *  <li>There is at least one <code>LegendEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1530
         *  <li>There is at least one <code>SelectionEvent</code> in the <code>AtomicEvent</code> parameter.</li>
1531
         * </ul>
1532
         * </p>
1533 1223 fernando
         *
1534
         * @author Fernando Gonz?lez Cort?s
1535
         */
1536
        public class MapContextListener implements AtomicEventListener {
1537
                /**
1538
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
1539
                 */
1540
                public void atomicEvent(AtomicEvent e) {
1541
                        boolean redraw = false;
1542
                        LayerEvent[] layerEvents = e.getLayerEvents();
1543
1544
                        for (int i = 0; i < layerEvents.length; i++) {
1545
                                if (layerEvents[i].getProperty().equals("visible")) {
1546
                                        redraw = true;
1547
                                }
1548
                        }
1549
1550
                        if (e.getColorEvents().length > 0) {
1551
                                redraw = true;
1552
                        }
1553
1554
                        if (e.getExtentEvents().length > 0) {
1555
                                redraw = true;
1556
                        }
1557 6306 caballero
1558 5941 jmvivo
                        if (e.getProjectionEvents().length > 0) {
1559
                                //redraw = true;
1560
                        }
1561 1223 fernando
1562 6306 caballero
1563 10752 caballero
                        LayerCollectionEvent[] aux = e.getLayerCollectionEvents();
1564 10401 fjp
                        if (aux.length > 0) {
1565
                                for (int i=0; i < aux.length; i++)
1566
                                {
1567
                                        if (aux[i].getAffectedLayer().getFLayerStatus().isDriverLoaded())
1568
                                        {
1569
                                                redraw = true;
1570
                                                break;
1571
                                        }
1572
                                }
1573 10752 caballero
1574 1223 fernando
                        }
1575
1576
                        if (e.getLegendEvents().length > 0) {
1577
                                redraw = true;
1578
                        }
1579
1580
                        if (e.getSelectionEvents().length > 0) {
1581
                                redraw = true;
1582
                        }
1583
1584
                        if (redraw) {
1585 3331 fjp
                System.out.println("MapContextListener redraw");
1586 3422 fjp
                                MapControl.this.drawMap(false);
1587 1223 fernando
                        }
1588
                }
1589
        }
1590 20100 jmvivo
1591
        /**
1592
         * <p>Gets the <code>ViewPort</code> of this component's {@link MapContext MapContext} .</p>
1593
         *
1594
         * @see MapContext#getViewPort()
1595
         */
1596 1223 fernando
        public ViewPort getViewPort() {
1597
                return vp;
1598
        }
1599 3719 caballero
1600 2877 caballero
        /**
1601 20100 jmvivo
         * <p>Returns all registered <code>Behavior</code> that can define a way to work with this <code>MapControl</code>.</p>
1602
         *
1603
         * @return registered <code>Behavior</code> to this <code>MapControl</code>
1604
         *
1605
         * @see #addMapTool(String, Behavior)
1606
         * @see #addMapTool(String, Behavior[])
1607
         * @see #getMapToolsKeySet()
1608
         * @see #hasTool(String)
1609 2877 caballero
         */
1610 2876 caballero
        public HashMap getNamesMapTools() {
1611
                return namesMapTools;
1612
        }
1613 4120 caballero
1614 20100 jmvivo
        /*
1615
         * (non-Javadoc)
1616
         * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRepaint()
1617
         */
1618 4142 caballero
        public void commandRepaint() {
1619 4127 caballero
                drawMap(false);
1620 4120 caballero
        }
1621 4127 caballero
1622 20100 jmvivo
        /*
1623
         * (non-Javadoc)
1624
         * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#commandRefresh()
1625
         */
1626 4127 caballero
        public void commandRefresh() {
1627
                // TODO Auto-generated method stub
1628
        }
1629 4888 caballero
1630 20100 jmvivo
        /**
1631
         * <p>Equivalent operation to <i>undo</i>.</p>
1632
         *
1633
         * <p>Exchanges the previous tool with the current one.</p>
1634
         *
1635
         * @see #addMapTool(String, Behavior)
1636
         * @see #addMapTool(String, Behavior[])
1637
         * @see #setTool(String)
1638
         */
1639 4888 caballero
        public void setPrevTool() {
1640
                setTool(prevTool);
1641
        }
1642 6306 caballero
1643 20100 jmvivo
        /**
1644
         * <p>Executes a <i>zoom in</i> operation centered at the center of the extent.</p>
1645
         *
1646
         * <p>This implementation is designed for being invoked outside a <code>Behavior</code>, for example
1647
         *  by an action of pressing a button; and simulates that the event has been produced by
1648
         *  releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
1649
         *  <code>MapControl</code> object that's responsible for the <i>zoom in</i> operation.</p>
1650
         *
1651
         * @see #zoomOut()
1652
         */
1653 10752 caballero
        public void zoomIn() {
1654
                getMapContext().clearAllCachingImageDrawnLayers();
1655 6264 fjp
                Behavior mapTool = (Behavior) namesMapTools.get("zoomIn");
1656
                ViewPort vp=getViewPort();
1657
                Rectangle2D r=getViewPort().getAdjustedExtent();
1658
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1659
                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);
1660
                try {
1661
                        mapTool.mousePressed(e);
1662
                        mapTool.mouseReleased(e);
1663
                } catch (BehaviorException t) {
1664
                        throwException(t);
1665
                }
1666
        }
1667 20100 jmvivo
1668
        /**
1669
         * <p>Executes a <i>zoom out</i> operation centered at the center of the extent.</p>
1670
         *
1671
         * <p>This implementation is thought for being invoked outside a <code>Behavior</code>, for example
1672
         *  by an action of pressing a button, and simulates that the event has been produced by
1673
         *  releasing the <i>button 1</i> of the mouse using the registered <code>Behavior</code> in this
1674
         *  <code>MapControl</code> object that's responsible for the <i>zoom out</i> operation.</p>
1675
         *
1676
         * @see #zoomIn()
1677
         */
1678 6264 fjp
        public void zoomOut() {
1679 10752 caballero
                getMapContext().clearAllCachingImageDrawnLayers();
1680 6264 fjp
                Behavior mapTool = (Behavior) namesMapTools.get("zoomOut");
1681
                ViewPort vp=getViewPort();
1682
                Rectangle2D r=getViewPort().getAdjustedExtent();
1683
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1684
                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);
1685
                try {
1686
                        mapTool.mousePressed(e);
1687
                        mapTool.mouseReleased(e);
1688
                } catch (BehaviorException t) {
1689
                        throwException(t);
1690
                }
1691
        }
1692 20100 jmvivo
1693
        /**
1694
         * <p>Returns the shared <code>CancelDraw</code> object used by this component and its layers drawing processes, that determines
1695
         *  if the drawing process must be cancelled or can continue running.</p>
1696
         *
1697
         * @return shared object that determines if the layers drawing process of this component can continue or must be cancelled
1698
         */
1699 18451 vcaballero
        public CancelDraw getCanceldraw() {
1700
                return canceldraw;
1701
        }
1702 1223 fernando
}