/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 581 - (hide annotations)
Thu Nov 26 09:18:18 2009 UTC (15 years, 3 months ago) by alfonx
File size: 72230 byte(s)
Fixed a bug that lead to a blink when zooming out with the right mouse button

1 alfonx 531 package skrueger.geotools;
2 mojays 2
3     import java.awt.Color;
4     import java.awt.Cursor;
5 alfonx 509 import java.awt.Font;
6 mojays 2 import java.awt.Graphics;
7     import java.awt.Graphics2D;
8 alfonx 509 import java.awt.Image;
9     import java.awt.Point;
10 mojays 2 import java.awt.Rectangle;
11 alfonx 505 import java.awt.RenderingHints;
12 alfonx 509 import java.awt.event.ActionEvent;
13     import java.awt.event.ActionListener;
14     import java.awt.event.ComponentAdapter;
15     import java.awt.event.ComponentEvent;
16 mojays 2 import java.awt.event.InputEvent;
17     import java.awt.event.MouseEvent;
18     import java.awt.event.MouseListener;
19 alfonx 505 import java.awt.geom.AffineTransform;
20 alfonx 509 import java.awt.geom.NoninvertibleTransformException;
21     import java.awt.geom.Point2D;
22 mojays 2 import java.awt.image.BufferedImage;
23     import java.io.IOException;
24 alfonx 533 import java.util.ArrayList;
25 mojays 2 import java.util.HashMap;
26     import java.util.Map;
27 alfonx 509 import java.util.Vector;
28 mojays 2
29 alfonx 509 import javax.swing.Timer;
30 mojays 561 import javax.swing.border.Border;
31 mojays 2
32     import org.apache.log4j.Logger;
33 alfonx 509 import org.geotools.feature.FeatureCollection;
34     import org.geotools.geometry.jts.JTS;
35     import org.geotools.geometry.jts.ReferencedEnvelope;
36     import org.geotools.map.DefaultMapContext;
37 mojays 2 import org.geotools.map.MapContext;
38 alfonx 509 import org.geotools.map.MapLayer;
39     import org.geotools.map.event.MapLayerEvent;
40 mojays 2 import org.geotools.map.event.MapLayerListEvent;
41     import org.geotools.map.event.MapLayerListListener;
42 alfonx 509 import org.geotools.map.event.MapLayerListener;
43     import org.geotools.referencing.CRS;
44 mojays 2 import org.geotools.renderer.GTRenderer;
45 alfonx 336 import org.geotools.renderer.label.LabelCacheImpl;
46 mojays 2 import org.geotools.renderer.lite.LabelCache;
47 alfonx 539 import org.geotools.renderer.lite.RendererUtilities;
48 alfonx 529 import org.geotools.renderer.lite.StreamingRenderer;
49 alfonx 509 import org.geotools.swing.JMapPane;
50     import org.geotools.swing.event.MapMouseEvent;
51     import org.geotools.swing.event.MapPaneEvent;
52     import org.geotools.swing.event.MapPaneListener;
53     import org.opengis.feature.simple.SimpleFeature;
54     import org.opengis.feature.simple.SimpleFeatureType;
55     import org.opengis.referencing.FactoryException;
56 mojays 2 import org.opengis.referencing.crs.CoordinateReferenceSystem;
57 alfonx 509 import org.opengis.referencing.operation.MathTransform;
58     import org.opengis.referencing.operation.TransformException;
59 mojays 2
60 alfonx 509 import schmitzm.geotools.GTUtil;
61 mojays 436 import schmitzm.geotools.JTSUtil;
62 alfonx 509 import schmitzm.geotools.gui.SelectableXMapPane;
63 alfonx 533 import schmitzm.geotools.io.GeoImportUtil;
64 alfonx 509 import schmitzm.geotools.map.event.JMapPaneListener;
65     import schmitzm.geotools.map.event.MapLayerAdapter;
66 alfonx 529 import schmitzm.lang.LangUtil;
67 alfonx 530 import schmitzm.swing.JPanel;
68 mojays 2 import schmitzm.swing.SwingUtil;
69    
70     import com.vividsolutions.jts.geom.Coordinate;
71     import com.vividsolutions.jts.geom.Envelope;
72 alfonx 509 import com.vividsolutions.jts.geom.Geometry;
73 mojays 2
74 alfonx 530 /**
75     * The {@link XMapPane} class uses a Geotools {@link GTRenderer} to paint up to
76     * two {@link MapContext}s: a "local" {@link MapContext} and a "background"
77     * {@link MapContext}. The idea is, that rendering a background layer made up of
78     * e.g. OSM data, may take much longer than rendering local data.<br>
79     * Every {@link MapContext} is rendered on a {@link Thread} of it's own.
80     * Starting/ cancelling these threads is done by the {@link RenderingExecutor}.<br>
81     * <br>
82     * While the renderers are rending the map, a <br>
83     * The {@link XMapPane} is based on schmitzm {@link JPanel}, so
84     * {@link #print(Graphics)} will automatically set the background of components
85     * to pure white.
86     *
87     * The XMapPane has a {@link MouseListener} that manages zooming.<br>
88     * A logo/icon to float in the lower left corner may be set with
89     * {@link #setMapImage(BufferedImage)}<br>
90     *
91     * @see SelectableXMapPane - an extension of {@link XMapPane} that supports
92     * selecting features.
93     *
94     * @author stefan
95     *
96     */
97 alfonx 529 public class XMapPane extends JPanel {
98 alfonx 530
99 alfonx 580 // private static final int IMAGETYPE = BufferedImage.TYPE_INT_RGB;
100     // private static final int IMAGETYPE_withAlpha =
101     // BufferedImage.TYPE_INT_ARGB;
102     private static final int IMAGETYPE = BufferedImage.TYPE_3BYTE_BGR;
103     private static final int IMAGETYPE_withAlpha = BufferedImage.TYPE_4BYTE_ABGR;
104 alfonx 530
105 alfonx 555 private final static Logger LOGGER = Logger.getLogger(XMapPane.class);
106 alfonx 153
107 alfonx 530 private boolean acceptsRepaintCalls = true;
108    
109 alfonx 509 /**
110 alfonx 530 * Main {@link MapContext} that holds all layers that are rendered into the
111     * {@link #localImage} by the {@link #localRenderer}
112 alfonx 509 */
113 alfonx 530 MapContext localContext;
114 mojays 2
115 alfonx 509 /**
116 alfonx 530 * {@link MapContext} holding the background layers. Use it for layers that
117     * CAN take very long for rendering, like layer from the Internet: WMS, WFS,
118     * OSM...<br>
119     * <code>null</code> by default.
120     *
121     * @see #setBgContext(MapContext)
122     * @see #getBgContext()
123 alfonx 509 */
124 alfonx 530 MapContext bgContext;
125 mojays 2
126 alfonx 509 /**
127 alfonx 530 * While threads are working, calls {@link XMapPane#updateFinalImage()}
128     * regularly and {@link #repaint()}. This {@link Timer} is stopped when all
129     * renderers have finished.
130 alfonx 509 *
131 alfonx 544 * @see INITIAL_REPAINT_DELAYAL
132 alfonx 530 * @see #REPEATING_REPAINT_DELAY
133 alfonx 509 */
134 alfonx 530 final private Timer repaintTimer;
135 mojays 2
136 alfonx 509 /**
137 alfonx 530 * The initial delay in milliseconds until the {@link #finalImage} is
138     * updated the first time.
139 alfonx 509 */
140 alfonx 555 public static final int INITIAL_REPAINT_DELAY = 900;
141 alfonx 530
142 alfonx 509 /**
143 alfonx 530 * While the {@link #bgExecuter} and {@link #localExecuter} are rendering,
144     * the {@link #repaintTimer} is regularly updating the {@link #finalImage}
145     * with previews.
146 alfonx 509 */
147 alfonx 560 public static final int REPEATING_REPAINT_DELAY = 500;
148 alfonx 530
149 alfonx 509 /**
150 alfonx 530 * Default delay (milliseconds) before the map will be redrawn when resizing
151     * the pane. This is to avoid flickering while drag-resizing.
152 alfonx 544 *
153     * @see #resizeTimer
154 alfonx 509 */
155 alfonx 539 public static final int DEFAULT_RESIZING_PAINT_DELAY = 600;
156 alfonx 530
157 alfonx 555 /**
158     * This not-repeating {@link Timer} is re-started whenever the component is
159     * resized. That means => only if the component is not resizing for
160     * {@link #DEFAULT_RESIZING_PAINT_DELAY} milliseconds, does the
161     * {@link XMapPane} react.
162     */
163 alfonx 544 private final Timer resizeTimer;
164    
165 alfonx 555 /**
166     * Flag for no-tool.
167     */
168 alfonx 530 public static final int NONE = -123;
169    
170 alfonx 509 /**
171 alfonx 530 * Flag fuer Modus "Kartenausschnitt bewegen". Nicht fuer Window-Auswahl
172     * moeglich!
173 alfonx 509 *
174     * @see #setState(int)
175     */
176 alfonx 530 public static final int PAN = 1;
177    
178 alfonx 509 /**
179 alfonx 555 * Flag fuer Modus "Heran zoomen".
180     *
181     * @see #setState(int)
182     * @see #setState(int)
183     */
184     public static final int ZOOM_IN = 2;
185    
186     /**
187     * Flag fuer Modus "Heraus zoomen". Nicht fuer Window-Auswahl moeglich!
188     *
189     * @see #setState(int)
190     */
191     public static final int ZOOM_OUT = 3;
192    
193     /**
194 alfonx 509 * Flag fuer Modus "SimpleFeature-Auswahl auf allen (sichtbaren) Layern".
195     *
196     * @see #setState(int)
197     * @see #setState(int)
198     */
199     public static final int SELECT_ALL = 103;
200     /**
201     * Flag fuer Modus
202     * "Auswahl nur eines Features, das erste sichtbare von Oben".
203     *
204     * @see #setState(int)
205     * @see #setState(int)
206     */
207     public static final int SELECT_ONE_FROM_TOP = 104;
208 alfonx 524 /**
209 alfonx 530 * Flag fuer Modus
210     * "SimpleFeature-Auswahl auf dem obersten (sichtbaren) Layer".
211     *
212     * @see #setState(int)
213     * @see #setState(int)
214 alfonx 524 */
215 alfonx 530 public static final int SELECT_TOP = 4;
216 alfonx 524
217 alfonx 544 /**
218     * {@link Font} used to paint the wait messages into the map
219     *
220     * @see #addGadgets(Graphics2D, boolean)
221     */
222 alfonx 580 final static Font waitFont = new Font("Arial", Font.BOLD, 28);
223 alfonx 544
224     /**
225     * {@link Font} used to paint error messages into the map
226     *
227     * @see #addGadgets(Graphics2D, boolean)
228     */
229 alfonx 533 final static Font errorFont = new Font("Arial", Font.BOLD, 13);
230 alfonx 524
231 alfonx 144 /**
232 alfonx 580 * If last average last two renderings took more than that many ms, show the
233     * user a scaled preview
234     **/
235 alfonx 581 private static final long PRESCALE_MINTIME = 230;
236 alfonx 580
237     /**
238 alfonx 544 * The wait message painted into the map while rendering is going on on
239     * another thread.
240     *
241     * @see #addGadgets(Graphics2D, boolean)
242     */
243     final String waitMsg = SwingUtil.R("WaitMess");
244    
245     /**
246 alfonx 509 * Konvertiert die Maus-Koordinaten (relativ zum <code>JMapPane</code>) in
247     * Karten-Koordinaten.
248     *
249     * @param e
250     * Maus-Ereignis
251 alfonx 144 */
252 alfonx 530 public static Point2D getMapCoordinatesFromEvent(final MouseEvent e) {
253 alfonx 509 // aktuelle Geo-Position aus GeoMouseEvent ermitteln
254     if (e != null && e instanceof MapMouseEvent)
255     try {
256     return ((MapMouseEvent) e).getMapPosition().toPoint2D();
257 alfonx 530 } catch (final Exception err) {
258 alfonx 509 LOGGER
259     .error(
260     "return ((GeoMouseEvent) e).getMapCoordinate(null).toPoint2D();",
261     err);
262     }
263 mojays 2
264 alfonx 509 // aktuelle Geo-Position ueber Transformation des JMapPane berechnen
265     if (e != null && e.getSource() instanceof XMapPane) {
266 alfonx 513
267 alfonx 509 final XMapPane xMapPane = (XMapPane) e.getSource();
268 alfonx 513
269     if (!xMapPane.isWellDefined())
270     return null;
271    
272 alfonx 530 final AffineTransform at = xMapPane.getScreenToWorld();
273 alfonx 509 if (at != null)
274     return at.transform(e.getPoint(), null);
275     return null;
276     }
277     throw new IllegalArgumentException(
278     "MouseEvent has to be of instance MapMouseEvent or come from an XMapPane");
279     }
280 mojays 2
281 alfonx 509 /**
282 alfonx 530 * Listens to changes of the "background" {@link MapContext} and triggers
283     * repaints where needed.
284     */
285     private final MapLayerListListener bgContextListener = new MapLayerListListener() {
286    
287     @Override
288     public void layerAdded(final MapLayerListEvent event) {
289 alfonx 581 final MapLayer layer = event.getLayer();
290 alfonx 533 layer.addMapLayerListener(bgMapLayerListener);
291 alfonx 530 requestStartRendering();
292    
293     }
294    
295     @Override
296     public void layerChanged(final MapLayerListEvent event) {
297     requestStartRendering();
298     }
299    
300     @Override
301     public void layerMoved(final MapLayerListEvent event) {
302     requestStartRendering();
303     }
304    
305     @Override
306     public void layerRemoved(final MapLayerListEvent event) {
307     if (event.getLayer() != null)
308     event.getLayer().removeMapLayerListener(bgMapLayerListener);
309     requestStartRendering();
310     }
311     };
312    
313     /**
314 alfonx 543 * This {@link RenderingExecutor} manages the creation and cancellation of
315     * up to one {@link Thread} for rendering the {@link #localContext}.
316 alfonx 530 */
317 alfonx 543 private final RenderingExecutor localExecuter = new RenderingExecutor(this);
318 alfonx 530
319 alfonx 543 /**
320     * This {@link RenderingExecutor} manages the creation and cancellation of
321     * up to one {@link Thread} for rendering the {@link #bgContext}.
322     */
323 alfonx 530 protected RenderingExecutor bgExecuter;
324    
325     /**
326 alfonx 543 * The {@link #localRenderer} for the {@link #localContext} uses this
327     * {@link Image}.
328 alfonx 530 */
329     private BufferedImage localImage;
330    
331     /**
332 alfonx 543 * The {@link #bgRenderer} for the {@link #bgContext} uses this
333     * {@link Image}.
334 alfonx 530 */
335     private BufferedImage bgImage;
336 alfonx 544
337 alfonx 543 /**
338     * This {@link Image} is a merge of the {@link #bgImage},
339     * {@link #localImage} and {@link #addGadgets(Graphics2D, boolean)}. It is
340     * updated with {@link #updateFinalImage()} and used for painting in
341     * {@link #paintComponent(Graphics)}
342     */
343     private BufferedImage finalImage;
344 alfonx 530
345     /**
346     * Optionally a transparent image to paint over the map in the lower right
347     * corner.
348     *
349     * @see #addGadgets(Graphics2D)
350     * @see #setMapImage(BufferedImage)
351     **/
352     private BufferedImage mapImage = null;
353    
354     /**
355     * Listens to each layer in the local {@link MapContext} for changes and
356     * triggers repaints.
357     */
358     protected MapLayerListener bgMapLayerListener = new MapLayerAdapter() {
359    
360     @Override
361     public void layerChanged(final MapLayerEvent event) {
362     requestStartRendering();
363     }
364    
365     @Override
366     public void layerHidden(final MapLayerEvent event) {
367     requestStartRendering();
368     }
369    
370     @Override
371     public void layerShown(final MapLayerEvent event) {
372     requestStartRendering();
373     }
374     };
375    
376     /**
377     * A flag indicating if dispose() was already called. If true, then further
378     * use of this {@link SelectableXMapPane} is undefined.
379     */
380     private boolean disposed = false;
381    
382     /**
383     * While dragging, the {@link #updateFinalImage()} method is translating the
384     * cached images while setting it together.
385     **/
386     Point imageOrigin = new Point(0, 0);
387     /**
388     * For every rendering thread started,
389     * {@link GTUtil#createGTRenderer(MapContext)} is called to create a new
390     * renderer. These Java2D rendering hints are passed to the
391     * {@link GTRenderer}. The java2dHints are the same for all renderers (bg
392     * and local).
393     */
394     private RenderingHints java2dHints;
395    
396     protected LabelCache labelCache = new LabelCacheImpl();
397    
398     /**
399     * Listens to changes of the "local" {@link MapContext} and triggers
400     * repaints where needed.
401     */
402     private final MapLayerListListener localContextListener = new MapLayerListListener() {
403    
404     @Override
405     public void layerAdded(final MapLayerListEvent event) {
406     event.getLayer().addMapLayerListener(localMapLayerListener);
407    
408 alfonx 560 getLocalRenderer().setContext(getMapContext());
409 alfonx 530 requestStartRendering();
410    
411     }
412    
413     @Override
414     public void layerChanged(final MapLayerListEvent event) {
415 alfonx 578 // localRenderer = GTUtil.createGTRenderer();
416 alfonx 560 getLocalRenderer().setContext(getMapContext());
417 alfonx 530 requestStartRendering();
418     }
419    
420     @Override
421     public void layerMoved(final MapLayerListEvent event) {
422 alfonx 560 getLocalRenderer().setContext(getMapContext());
423 alfonx 530 requestStartRendering();
424     }
425    
426     @Override
427     public void layerRemoved(final MapLayerListEvent event) {
428     if (event.getLayer() != null)
429     event.getLayer().removeMapLayerListener(localMapLayerListener);
430 alfonx 560 getLocalRenderer().setContext(getMapContext());
431 alfonx 530 requestStartRendering();
432     }
433     };
434    
435     /**
436     * Listens to each layer in the local {@link MapContext} for changes and
437     * triggers repaints.
438     */
439     protected MapLayerListener localMapLayerListener = new MapLayerAdapter() {
440    
441     @Override
442     public void layerChanged(final MapLayerEvent event) {
443 alfonx 560 getLocalRenderer().setContext(getMapContext()); // betters for SLD
444 alfonx 555 // changes?!
445 alfonx 530 requestStartRendering();
446     }
447    
448     @Override
449     public void layerHidden(final MapLayerEvent event) {
450     requestStartRendering();
451     }
452    
453     @Override
454     public void layerShown(final MapLayerEvent event) {
455     requestStartRendering();
456     }
457     };
458    
459 alfonx 560 final private GTRenderer localRenderer = GTUtil.createGTRenderer();
460 alfonx 533
461 alfonx 530 private final GTRenderer bgRenderer = GTUtil.createGTRenderer();
462    
463     /**
464     * the area of the map to draw
465     */
466     protected Envelope mapArea = null;
467    
468     /**
469     * A flag set it {@link #setMapArea(Envelope)} to indicated the
470     * {@link #paintComponent(Graphics)} method, that the image on-screen is
471     * associated with {@link #oldMapArea} and may hence be scaled for a quick
472     * preview.<br>
473     * The flag is reset
474     **/
475     private boolean mapAreaChanged = false;
476    
477     /**
478     * This color is used as the default background color when painting a map.
479     */
480 alfonx 558 private Color mapBackgroundColor = null;
481 alfonx 530
482     /**
483 alfonx 509 * A flag indicating that the shown image is invalid and needs to be
484     * re-rendered.
485     */
486     protected boolean mapImageInvalid = true;
487 mojays 2
488 alfonx 530 /**
489     * Holds a flag for each layer, whether it is regarded or ignored on
490     * {@link #SELECT_TOP}, {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP}
491     * actions.
492     */
493     final protected HashMap<MapLayer, Boolean> mapLayerSelectable = new HashMap<MapLayer, Boolean>();
494 mojays 2
495 alfonx 414 /**
496 alfonx 530 * List of listeners of this {@link XMapPane}
497     */
498     protected Vector<JMapPaneListener> mapPaneListeners = new Vector<JMapPaneListener>();
499     /**
500 alfonx 509 * If not <code>null</code>, the {@link XMapPane} will not allow to zoom/pan
501 alfonx 414 * out of that area
502     **/
503     private Envelope maxExtend = null;
504 alfonx 530 private Double maxZoomScale = Double.MIN_VALUE;
505 alfonx 414
506 alfonx 509 private Double minZoomScale = Double.MAX_VALUE;
507 alfonx 414
508 alfonx 509 /**
509 alfonx 530 * We store the old mapArea for a moment to use it for the
510     * "quick scaled preview" in case of ZoomOut
511     **/
512     protected Envelope oldMapArea = null;
513 mojays 2
514 alfonx 509 /**
515 alfonx 530 * We store the old transform for a moment to use it for the
516     * "quick scaled preview" in case of ZoomIn
517     **/
518     protected AffineTransform oldScreenToWorld = null;
519 mojays 2
520 alfonx 509 /**
521     * A flag indicating, that the image size has changed and the buffered
522     * images are not big enough any more
523     **/
524 alfonx 581 protected boolean paneResized = true;
525 mojays 2
526 alfonx 530 private BufferedImage preFinalImage;
527    
528 alfonx 555 // ** if 0, no quick preview will be shown **/
529 alfonx 581 // private int quickPreviewHint = 0;
530 alfonx 530
531 alfonx 533 private Map<Object, Object> rendererHints = GTUtil
532 alfonx 560 .getDefaultGTRendererHints(getLocalRenderer());
533 alfonx 530
534 alfonx 544 /**
535     * If set to <code>true</code>, the {@link #startRenderThreadsTimer} will
536     * start rendering {@link Thread}s
537     **/
538     private volatile Boolean requestStartRendering = false;
539 alfonx 530
540 alfonx 144 /**
541 alfonx 530 * Transformation zwischen Fenster-Koordinaten und Karten-Koordinaten
542     * (lat/lon)
543     */
544     protected AffineTransform screenToWorld = null;
545    
546     /**
547 alfonx 544 * The flag {@link #requestStartRendering} can be set to true by events.
548 alfonx 509 * This {@link Timer} checks the flag regularly and starts one renderer
549     * thread.
550 alfonx 505 */
551 alfonx 509 final private Timer startRenderThreadsTimer;
552 alfonx 505
553 alfonx 530 /**
554     * The default state is ZOOM_IN, hence by default the
555     * {@link #zoomMapPaneMouseListener} is also enabled.
556     **/
557     private int state = ZOOM_IN;
558 alfonx 509
559 alfonx 530 /**
560     * Manuell gesetzter statischer Cursor, unabhaengig von der aktuellen
561     * MapPane-Funktion
562     */
563     protected Cursor staticCursor = null;
564 mojays 2
565 alfonx 530 private AffineTransform worldToScreen;
566    
567 alfonx 144 /**
568 alfonx 530 * This {@link MouseListener} is managing all zoom related tasks
569 alfonx 144 */
570 alfonx 530 public final ZoomXMapPaneMouseListener zoomMapPaneMouseListener = new ZoomXMapPaneMouseListener(
571     this);
572 alfonx 509
573 alfonx 533 /** Is set if a renderer has an error **/
574     protected ArrayList<Exception> renderingErrors = new ArrayList<Exception>();
575    
576 alfonx 555 /**
577     * If <code>true</code>, then rendering exceptions are rendererd into the
578     * map pane
579     **/
580     private boolean showExceptions = false;
581 alfonx 544
582 alfonx 530 public XMapPane() {
583     this(null, null);
584 alfonx 144 }
585 mojays 2
586 alfonx 144 /**
587     * full constructor extending JPanel
588     *
589 alfonx 509 * @param rendererHints
590 alfonx 543 * may be <code>null</code>. Otherwise a {@link Map<Object,
591     * Object>} of {@link RenderingHints} to override the default
592     * from {@link GTUtil#getDefaultGTRendererHints(GTRenderer)}
593 alfonx 509 *
594     * @param localContext
595 alfonx 543 * The main {@link MapContext} to use. If <code>null</code>, an
596     * empty {@link DefaultMapContext} will be created.
597 alfonx 144 */
598 alfonx 529 public XMapPane(final MapContext localContext_,
599 alfonx 530 final Map<Object, Object> rendererHints) {
600 alfonx 509 super(true);
601 mojays 2
602 alfonx 509 setRendererHints(rendererHints);
603 mojays 2
604 alfonx 509 setOpaque(true);
605 mojays 2
606 alfonx 529 if (localContext_ != null)
607     setLocalContext(localContext_);
608    
609 alfonx 509 /**
610     * Adding the #zoomMapPaneMouseListener
611     */
612     this.addMouseListener(zoomMapPaneMouseListener);
613     this.addMouseMotionListener(zoomMapPaneMouseListener);
614     this.addMouseWheelListener(zoomMapPaneMouseListener);
615    
616     /*
617     * We use a Timer object to avoid rendering delays and flickering when
618     * the user is drag-resizing the parent container of this map pane.
619     *
620     * Using a ComponentListener doesn't work because, unlike a JFrame, the
621     * pane receives a stream of events during drag-resizing.
622     */
623 alfonx 543 resizeTimer = new Timer(DEFAULT_RESIZING_PAINT_DELAY,
624     new ActionListener() {
625 alfonx 509
626 alfonx 543 public void actionPerformed(final ActionEvent e) {
627     if (!isWellDefined())
628     return;
629 alfonx 509
630 alfonx 579 // LOGGER.debug("resizeTimer performed");
631 alfonx 555
632 alfonx 580 // final Rectangle bounds = getVisibleRect();
633 alfonx 544 //
634     // System.out.println("\n\ntimer performs with bounds = "
635     // + bounds);
636 alfonx 509
637 alfonx 580 // final Envelope geoMapArea = tranformWindowToGeo(
638     // bounds.x, bounds.y, bounds.x + bounds.width,
639     // bounds.y + bounds.height);
640 alfonx 509
641 alfonx 579 paneResized = true;
642     if (setMapArea(getMapArea())) {
643 alfonx 578 }
644 alfonx 543 }
645     });
646 alfonx 509 resizeTimer.setRepeats(false);
647    
648 alfonx 544 this.addComponentListener(new ComponentAdapter() {
649    
650 alfonx 555 private Rectangle oldVisibleRect;
651    
652 alfonx 544 @Override
653     public void componentResized(final ComponentEvent e) {
654    
655 alfonx 556 // Seems to be called twice with the same size..
656 alfonx 555 if (oldVisibleRect != null
657 alfonx 556 && oldVisibleRect.equals(getVisibleRect())) {
658 alfonx 579 // LOGGER.debug("skipping resize.");
659 alfonx 555 return;
660     }
661 alfonx 544
662 alfonx 579 // LOGGER.debug("resized: " + getVisibleRect());
663 alfonx 544 resizeTimer.restart();
664 alfonx 555 oldVisibleRect = getVisibleRect();
665 alfonx 544 }
666    
667     });
668    
669 alfonx 530 /*
670     * Setting up the repaintTimer. Not started automatically.
671     */
672 alfonx 544 repaintTimer = new Timer(REPEATING_REPAINT_DELAY, new ActionListener() {
673 alfonx 509
674 alfonx 544 @Override
675     public void actionPerformed(final ActionEvent e) {
676 alfonx 555 if ((!localExecuter.isRunning())
677 alfonx 544 && (bgExecuter != null && !bgExecuter.isRunning())) {
678     repaintTimer.stop();
679     } else {
680     updateFinalImage();
681     XMapPane.this.repaint(100);
682 alfonx 555
683 alfonx 544 }
684     }
685     });
686 alfonx 555
687 alfonx 530 repaintTimer.setInitialDelay(INITIAL_REPAINT_DELAY);
688     repaintTimer.setRepeats(true);
689 alfonx 509
690 alfonx 530 /*
691     * Setting up the startRenderThreadsTimer. This Timer starts
692     * automatically.
693     */
694 alfonx 524 startRenderThreadsTimer = new Timer(100, new ActionListener() {
695    
696 alfonx 509 @Override
697 alfonx 530 public void actionPerformed(final ActionEvent e) {
698 alfonx 544 synchronized (requestStartRendering) {
699     if (requestStartRendering && isWellDefined()) {
700 alfonx 529
701     if (localExecuter.isRunning()) {
702 alfonx 524 localExecuter.cancelTask();
703     } else {
704 alfonx 544 // Stupidly, but we have to recheck the
705     setMapArea(getMapArea());
706     requestStartRendering = false;
707 alfonx 524 startRendering();
708     }
709     }
710 alfonx 509 }
711     }
712     });
713     startRenderThreadsTimer.start();
714    
715 alfonx 144 }
716 mojays 2
717 alfonx 530 /**
718     * Fuegt der Map einen Listener hinzu.
719     *
720     * @param l
721     * neuer Listener
722     */
723     public void addMapPaneListener(final JMapPaneListener l) {
724     mapPaneListeners.add(l);
725 alfonx 509 }
726    
727 alfonx 144 /**
728 alfonx 530 * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
729     * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
730     * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br>
731     * Since 21. April 09: Before thecalculation starts, the aspect ratio is
732     * corrected. This change implies, that setMapArea() will most of the time
733     * not allow setting to a wrong aspectRatio.
734 alfonx 509 *
735 alfonx 530 * @author <a href="mailto:[email protected]">Stefan Alfons
736     * Kr&uuml;ger</a>
737 alfonx 144 */
738 alfonx 544 public ReferencedEnvelope bestAllowedMapArea(ReferencedEnvelope env) {
739    
740 alfonx 530 if (getWidth() == 0)
741     return env;
742 alfonx 544
743 alfonx 530 if (env == null)
744     return null;
745 alfonx 509
746 alfonx 530 Envelope newArea = null;
747 alfonx 509
748 alfonx 530 /**
749     * Correct the aspect Ratio before we check the rest. Otherwise we might
750     * easily fail. We allow to grow here, because we don't check against
751     * the maxExtend
752     */
753     final Rectangle curPaintArea = getVisibleRect();
754 alfonx 509
755 alfonx 530 env = JTSUtil.fixAspectRatio(curPaintArea, env, true);
756 alfonx 509
757 alfonx 530 final double scale = env.getWidth() / getWidth();
758     final double centerX = env.getMinX() + env.getWidth() / 2.;
759     final double centerY = env.getMinY() + env.getHeight() / 2.;
760     double newWidth2 = 0;
761     double newHeight2 = 0;
762     if (scale < getMaxZoomScale()) {
763     // ****************************************************************************
764     // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
765     // ****************************************************************************
766     newWidth2 = getMaxZoomScale() * getWidth() / 2.;
767     newHeight2 = getMaxZoomScale() * getHeight() / 2.;
768     } else if (scale > getMinZoomScale()) {
769     // ****************************************************************************
770     // Wir zoomen weiter raus als erlaubt => Anpassen des envelope
771     // ****************************************************************************
772     newWidth2 = getMinZoomScale() * getWidth() / 2.;
773     newHeight2 = getMinZoomScale() * getHeight() / 2.;
774     } else {
775     // ****************************************************************************
776     // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
777     // ****************************************************************************
778     newArea = env;
779     }
780 alfonx 509
781 alfonx 530 if (newArea == null) {
782    
783     final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
784     - newHeight2);
785     final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
786     + newHeight2);
787    
788     newArea = new Envelope(ll, ur);
789     }
790    
791     final Envelope maxAllowedExtend = getMaxExtend();
792 alfonx 555
793 alfonx 530 while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
794     && newArea != null && !newArea.isNull()
795     && !Double.isNaN(newArea.getMinX())
796     && !Double.isNaN(newArea.getMaxX())
797     && !Double.isNaN(newArea.getMinY())
798     && !Double.isNaN(newArea.getMaxY())) {
799     /*
800     * If a maxExtend is set, we have to honour that...
801     */
802    
803     // Exceeds top? Move down and maybe cut
804     if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
805     final double divY = newArea.getMaxY()
806     - maxAllowedExtend.getMaxY();
807 alfonx 579 // LOGGER.debug("Moving area down by " + divY);
808 alfonx 530
809     newArea = new Envelope(new Coordinate(newArea.getMinX(),
810     newArea.getMinY() - divY), new Coordinate(newArea
811     .getMaxX(), newArea.getMaxY() - divY));
812    
813     if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
814     // LOGGER.debug("Now it exeeds the bottom border.. cut!");
815     // And cut the bottom if it moved out of the area
816     newArea = new Envelope(new Coordinate(newArea.getMinX(),
817     maxAllowedExtend.getMinY()), new Coordinate(newArea
818     .getMaxX(), newArea.getMaxY()));
819    
820     // LOGGER.debug("and fix aspect ratio");
821    
822 alfonx 544 newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
823     new ReferencedEnvelope(newArea, env
824     .getCoordinateReferenceSystem()), false);
825 alfonx 530 }
826 alfonx 509 }
827    
828 alfonx 530 // Exceeds bottom? Move up and maybe cut
829     if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
830     final double divY = newArea.getMinY()
831     - maxAllowedExtend.getMinY();
832     // LOGGER.debug("Moving area up by " + divY);
833    
834     newArea = new Envelope(new Coordinate(newArea.getMinX(),
835     newArea.getMinY() - divY), new Coordinate(newArea
836     .getMaxX(), newArea.getMaxY() - divY));
837    
838     if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
839     // LOGGER.debug("Now it exeeds the top border.. cut!");
840     // And cut the bottom if it moved out of the area
841     newArea = new Envelope(new Coordinate(newArea.getMinX(),
842     newArea.getMinY()), new Coordinate(newArea
843     .getMaxX(), maxAllowedExtend.getMaxY()));
844    
845     // LOGGER.debug("and fix aspect ratio");
846    
847 alfonx 544 newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
848     new ReferencedEnvelope(newArea, env
849     .getCoordinateReferenceSystem()), false);
850 alfonx 509 }
851     }
852    
853 alfonx 530 // Exceeds to the right? move and maybe cut
854     if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
855 alfonx 509
856 alfonx 530 // Move left..
857     final double divX = newArea.getMaxX()
858     - maxAllowedExtend.getMaxX();
859     // LOGGER.debug("Moving area left by " + divX);
860 alfonx 509
861 alfonx 530 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
862     newArea.getMinY()), new Coordinate(newArea.getMaxX()
863     - divX, newArea.getMaxY()));
864 alfonx 509
865 alfonx 530 if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
866     // LOGGER.debug("Now it exeeds the left border.. cut!");
867     // And cut the left if it moved out of the area
868     newArea = new Envelope(new Coordinate(maxAllowedExtend
869     .getMinX(), newArea.getMinY()), new Coordinate(
870     newArea.getMaxX(), newArea.getMaxY()));
871 alfonx 509
872 alfonx 530 // LOGGER.debug("and fix aspect ratio");
873    
874 alfonx 544 newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
875     new ReferencedEnvelope(newArea, env
876     .getCoordinateReferenceSystem()), false);
877 alfonx 530 }
878 alfonx 509 }
879    
880 alfonx 530 // Exceeds to the left? move and maybe cut
881     if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
882 alfonx 509
883 alfonx 530 // Move right..
884     final double divX = newArea.getMinX()
885     - maxAllowedExtend.getMinX();
886     // LOGGER.debug("Moving area right by " + divX);
887    
888     newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
889     newArea.getMinY()), new Coordinate(newArea.getMaxX()
890     - divX, newArea.getMaxY()));
891    
892     if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
893     // LOGGER.debug("Now it exeeds the right border.. cut!");
894     // And cut the left if it moved out of the area
895     newArea = new Envelope(new Coordinate(newArea.getMinX(),
896     newArea.getMinY()), new Coordinate(maxAllowedExtend
897     .getMaxX(), newArea.getMaxY()));
898    
899     // LOGGER.debug("and fix aspect ratio");
900    
901 alfonx 544 newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
902     new ReferencedEnvelope(newArea, env
903     .getCoordinateReferenceSystem()), false);
904 alfonx 530 }
905     }
906 alfonx 509 }
907 mojays 2
908 alfonx 544 return new ReferencedEnvelope(newArea, env
909     .getCoordinateReferenceSystem());
910 alfonx 509 }
911 alfonx 414
912 alfonx 509 /**
913 alfonx 530 * Should be called when the {@link JMapPane} is not needed no more to help
914     * the GarbageCollector
915     *
916     * Removes all {@link JMapPaneListener}s that are registered
917     *
918     * @author <a href="mailto:[email protected]">Stefan Alfons
919     * Kr&uuml;ger</a>
920 alfonx 509 */
921 alfonx 530 public void dispose() {
922     if (isDisposed())
923     return;
924 alfonx 513
925 alfonx 530 setPainting(false);
926 alfonx 513
927 alfonx 530 resizeTimer.stop();
928     startRenderThreadsTimer.stop();
929    
930     disposed = true;
931    
932     if (bgExecuter != null) {
933     bgExecuter.cancelTask();
934     bgExecuter.dispose();
935 alfonx 504 }
936 alfonx 509
937 alfonx 555 if (localExecuter.isRunning()) {
938 alfonx 530 int i = 0;
939     localExecuter.cancelTask();
940     while (i++ < 10 && localExecuter.isRunning()) {
941     try {
942 alfonx 543 Thread.sleep(200);
943 alfonx 530 } catch (final InterruptedException e) {
944 alfonx 543 LOGGER
945     .warn(
946     "while XMapPane we are waiting for the localExcutor to stop",
947     e);
948 alfonx 530 }
949     }
950     if (localExecuter.isRunning()) {
951 alfonx 543 LOGGER
952     .warn("localExecutor Thread still running after 2s! Continuing anyways...");
953 alfonx 530 }
954     localExecuter.dispose();
955     }
956 alfonx 539
957 alfonx 530 disposeImages();
958 alfonx 509
959 alfonx 543 // Remove all mapPaneListeners that have registered with us
960 alfonx 530 mapPaneListeners.clear();
961 alfonx 509
962 alfonx 530 removeMouseMotionListener(zoomMapPaneMouseListener);
963     removeMouseListener(zoomMapPaneMouseListener);
964 alfonx 509
965 alfonx 530 if (localContext != null)
966 alfonx 551 getMapContext().clearLayerList();
967 alfonx 530 if (bgContext != null)
968     getBgContext().clearLayerList();
969    
970     removeAll();
971 alfonx 509 }
972    
973     /**
974 alfonx 530 * Draws a rectangle in XOR mode from the origin at {@link #startPos} to the
975     * given point. All in screen coordinates.
976 alfonx 509 */
977 alfonx 530 protected void drawRectangle(final Graphics graphics, final Point startPos,
978     final Point e) {
979 alfonx 509
980 alfonx 530 if (!isWellDefined())
981 alfonx 509 return;
982 alfonx 307
983 alfonx 530 // undraw last box/draw new box
984     final int left = Math.min(startPos.x, e.x);
985     final int right = Math.max(startPos.x, e.x);
986     final int top = Math.max(startPos.y, e.y);
987     final int bottom = Math.min(startPos.y, e.y);
988     final int width = right - left;
989     final int height = top - bottom;
990    
991     if (width == 0 && height == 0)
992 alfonx 509 return;
993 alfonx 414
994 alfonx 530 graphics.setXORMode(Color.WHITE);
995     graphics.drawRect(left, bottom, width, height);
996 alfonx 144 }
997 mojays 2
998 alfonx 509 /**
999 alfonx 530 * Diretly paints scaled preview into the {@link SelectableXMapPane}. Used
1000     * to give the user something to look at while we are rendering. Method
1001     * should be called after {@link #setMapArea(Envelope)} has been set to the
1002     * new mapArea and transform has been reset.<br>
1003     *
1004     * @param g
1005     * Graphics2D to paint the preview into
1006 alfonx 509 */
1007 alfonx 530 protected boolean drawScaledPreviewImage_Zoom(final Graphics2D graphics) {
1008 alfonx 509
1009 alfonx 543 // if (1 == 1)return false;
1010 alfonx 581 // if (quickPreviewHint == 0)
1011     // return false;
1012 alfonx 543
1013 alfonx 530 if (oldMapArea == null)
1014     return false;
1015 alfonx 581
1016     if (getPreFinalImage() == null)
1017     return false;
1018 mojays 2
1019 alfonx 530 final Rectangle visibleArea = getVisibleRect();
1020 alfonx 509
1021 alfonx 530 // Calculate the oldMapArea in the current WindowCoordinates:
1022     final Envelope oldMapWindow = tranformGeoToWindow(oldMapArea.getMinX(),
1023     oldMapArea.getMinY(), oldMapArea.getMaxX(), oldMapArea
1024 alfonx 539 .getMaxY());
1025 alfonx 509
1026 alfonx 530 final int xx1 = (int) Math.round(oldMapWindow.getMinX());
1027     final int yy1 = (int) Math.round(oldMapWindow.getMinY());
1028     final int xx2 = (int) Math.round(oldMapWindow.getMaxX());
1029     final int yy2 = (int) Math.round(oldMapWindow.getMaxY());
1030 alfonx 509
1031 alfonx 581 graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
1032     RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
1033     graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1034     RenderingHints.VALUE_ANTIALIAS_OFF);
1035     graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
1036     RenderingHints.VALUE_RENDER_SPEED);
1037    
1038 alfonx 530 graphics.drawImage(getPreFinalImage(), xx1, yy1, xx2, yy2,
1039     (int) visibleArea.getMinX(), (int) visibleArea.getMinY(),
1040     (int) visibleArea.getMaxX(), (int) visibleArea.getMaxY(),
1041     getMapBackgroundColor(), null);
1042 alfonx 509
1043 alfonx 530 final Rectangle painedArea = new Rectangle(xx1, yy1, xx2 - xx1, yy2
1044     - yy1);
1045 alfonx 529
1046 alfonx 580 SwingUtil.clearAround(graphics, painedArea, visibleArea,
1047     getMapBackgroundColor());
1048 alfonx 529
1049 alfonx 533 addGadgets(graphics, true);
1050 alfonx 509
1051 alfonx 581 // quickPreviewHint = 0;
1052 alfonx 509
1053 alfonx 533 repaintTimer.restart();
1054    
1055 alfonx 530 // Something has been drawn
1056     return true;
1057     }
1058 mojays 2
1059 alfonx 530 public MapContext getBgContext() {
1060     return bgContext;
1061     }
1062 mojays 2
1063 alfonx 530 /**
1064     * Lazyly initializes a {@link BufferedImage} for the background renderer.
1065     */
1066     private Image getBgImage() {
1067 alfonx 579 if (bgImage == null) {
1068     bgImage = new BufferedImage(getVisibleRect().width,
1069     getVisibleRect().height, IMAGETYPE);
1070     SwingUtil.clearImage(finalImage, getMapBackgroundColor());
1071     }
1072 alfonx 530
1073     return bgImage;
1074     }
1075    
1076 alfonx 551 public MapContext getMapContext() {
1077 alfonx 530 if (localContext == null) {
1078     setLocalContext(new DefaultMapContext());
1079 alfonx 144 }
1080 alfonx 530 return localContext;
1081     }
1082 mojays 2
1083 alfonx 530 private BufferedImage getFinalImage() {
1084     //
1085     if (finalImage == null) {
1086     // Rectangle curPaintArea = getVisibleRect();
1087 alfonx 540 finalImage = new BufferedImage(getVisibleRect().width,
1088     getVisibleRect().height, IMAGETYPE);
1089 alfonx 579 SwingUtil.clearImage(finalImage, getMapBackgroundColor());
1090 alfonx 530
1091 alfonx 581 // requestStartRendering();
1092 alfonx 144 }
1093 alfonx 530 return finalImage;
1094     }
1095 alfonx 509
1096 alfonx 530 public RenderingHints getJava2dHints() {
1097     return java2dHints;
1098     }
1099    
1100 alfonx 509 /**
1101 alfonx 530 * Lazyly initializes a {@link BufferedImage} for the background renderer.
1102 alfonx 509 */
1103 alfonx 530 private BufferedImage getLocalImage() {
1104 alfonx 509
1105 alfonx 530 if (localImage == null) {
1106 alfonx 540 localImage = new BufferedImage(getVisibleRect().width,
1107     getVisibleRect().height, IMAGETYPE_withAlpha);
1108 alfonx 579 SwingUtil.clearImage(localImage, getMapBackgroundColor());
1109 alfonx 530 }
1110 alfonx 509
1111 alfonx 530 return localImage;
1112     }
1113    
1114     /**
1115     * Returns a copy of the mapArea
1116     *
1117     * @return
1118     */
1119 alfonx 533 public ReferencedEnvelope getMapArea() {
1120 alfonx 530 if (mapArea == null) {
1121     ReferencedEnvelope mapArea_ = null;
1122     try {
1123     mapArea_ = localContext.getLayerBounds();
1124     } catch (final IOException e) {
1125 alfonx 544 LOGGER.warn("localContext.getLayerBounds()", e);
1126 alfonx 509 }
1127    
1128 alfonx 544 if (mapArea_ == null && bgContext != null) {
1129     try {
1130     mapArea_ = bgContext.getLayerBounds();
1131     } catch (final IOException e) {
1132     LOGGER.warn("bgContext.getLayerBounds()", e);
1133     }
1134     }
1135    
1136 alfonx 530 if (mapArea_ != null) {
1137     mapArea = bestAllowedMapArea(mapArea_);
1138 alfonx 544 requestStartRendering();
1139 alfonx 530 }
1140 alfonx 509 }
1141    
1142 alfonx 530 if (mapArea == null)
1143     return null;
1144 alfonx 509
1145 alfonx 539 // TODO is needed at all, this should go to setMapArea maybe
1146 alfonx 533 if (localContext.getCoordinateReferenceSystem() == null)
1147     try {
1148     localContext.setCoordinateReferenceSystem(GeoImportUtil
1149     .getDefaultCRS());
1150 alfonx 581 } catch (final Exception e) {
1151 alfonx 539 throw new RuntimeException("setting context CRS:", e);
1152 alfonx 533 }
1153    
1154     return new ReferencedEnvelope(mapArea, localContext
1155     .getCoordinateReferenceSystem());
1156 alfonx 530 }
1157 alfonx 509
1158 alfonx 530 /**
1159 alfonx 557 * Returns the background {@link Color} of the map pane. If not set, the
1160     * methods looks for a parent component and will use its background color.
1161     * If no parent component is available, WHITE is returned.
1162 alfonx 530 **/
1163     public Color getMapBackgroundColor() {
1164 alfonx 560 if (mapBackgroundColor == null) {
1165     if (getParent() != null)
1166     return getParent().getBackground();
1167     else
1168     return Color.WHITE;
1169 alfonx 557 }
1170 alfonx 530 return mapBackgroundColor;
1171     }
1172 alfonx 509
1173     /**
1174 alfonx 530 * Get the BufferedImage to use as a flaoting icon in the lower right
1175     * corner.
1176     *
1177     * @return <code>null</code> if the feature is deactivated.
1178 alfonx 509 */
1179 alfonx 530 public BufferedImage getMapImage() {
1180     return mapImage;
1181     }
1182 alfonx 509
1183 alfonx 530 /**
1184     * Returns the evelope of the viewable area. The JMapPane will never show
1185     * anything outside of this extend. If this has been set to
1186     * <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return
1187     * quickly the context's bounds. It it takes to long to determine the
1188     * context bounds, <code>null</code> is returned.
1189     *
1190     * @param maxExtend
1191     * <code>null</code> to not have this restriction.
1192     */
1193 alfonx 509
1194 alfonx 530 public Envelope getMaxExtend() {
1195     if (maxExtend == null) {
1196 alfonx 578
1197     // The next command may take long time!
1198 alfonx 555 // long start = System.currentTimeMillis();
1199 alfonx 578 final ReferencedEnvelope layerBounds = GTUtil
1200     .getVisibleLayoutBounds(localContext);
1201 alfonx 555 //
1202     // LOGGER.info(
1203     // (System.currentTimeMillis()-start)+"m to get maxExtend");
1204     //
1205 alfonx 578 if (layerBounds == null) {
1206     // // TODO Last fallback could be the CRS valid area
1207     return null;
1208     }
1209    
1210 alfonx 579 // Vergrößerung um 10% nochmal rausgenommen
1211     // // // Kartenbereich um 10% vergroessern
1212     // return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil
1213     // .expandEnvelope(layerBounds, 0.1), true);
1214    
1215 alfonx 578 return JTSUtil.fixAspectRatio(getVisibleRect(), layerBounds, true);
1216 alfonx 509 }
1217 alfonx 530 return maxExtend;
1218     }
1219 alfonx 509
1220     /**
1221 alfonx 530 * Retuns the maximum allowed zoom scale. This is the smaller number value
1222     * of the two. Defaults to {@link Double}.MIN_VALUE
1223     *
1224     * @author <a href="mailto:[email protected]">Stefan Alfons
1225     * Kr&uuml;ger</a>
1226 alfonx 509 */
1227 alfonx 530 public Double getMaxZoomScale() {
1228     return maxZoomScale;
1229     }
1230 alfonx 509
1231 alfonx 530 /**
1232     * Retuns the minimum allowed zoom scale. This is the bigger number value of
1233     * the two. Defaults to {@link Double}.MAX_VALUE
1234     *
1235     * @author <a href="mailto:[email protected]">Stefan Alfons
1236     * Kr&uuml;ger</a>
1237     */
1238     public Double getMinZoomScale() {
1239     return minZoomScale;
1240     }
1241 alfonx 509
1242 alfonx 530 private Image getPreFinalImage() {
1243     return preFinalImage;
1244     }
1245 alfonx 509
1246 alfonx 530 public Map<Object, Object> getRendererHints() {
1247 alfonx 533 // Clear label cache
1248     labelCache.clear();
1249     rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
1250    
1251 alfonx 530 return rendererHints;
1252     }
1253 alfonx 509
1254     /**
1255     * Liefert eine affine Transformation, um von den Fenster-Koordinaten in die
1256     * Karten-Koordinaten (Lat/Lon) umzurechnen.
1257     *
1258     * @return eine Kopie der aktuellen Transformation; <code>null</code> wenn
1259     * noch keine Karte angezeigt wird
1260     */
1261     public AffineTransform getScreenToWorld() {
1262     if (screenToWorld == null)
1263     resetTransforms();
1264     // nur Kopie der Transformation zurueckgeben!
1265     if (screenToWorld == null)
1266     return null;
1267     return new AffineTransform(screenToWorld);
1268 alfonx 144 }
1269 mojays 2
1270 alfonx 530 public int getState() {
1271     return state;
1272     }
1273    
1274     /**
1275     * Liefert den statisch eingestellten Cursor, der unabhaengig von der
1276     * eingestellten MapPane-Aktion (Zoom, Auswahl, ...) verwendet wird.
1277     *
1278     * @return {@code null}, wenn kein statischer Cursor verwendet, sondern der
1279     * Cursor automatisch je nach MapPane-Aktion eingestellt wird.
1280     */
1281     public Cursor getStaticCursor() {
1282     return this.staticCursor;
1283     }
1284    
1285 alfonx 509 public AffineTransform getWorldToScreenTransform() {
1286     if (worldToScreen == null) {
1287     resetTransforms();
1288     }
1289     // nur Kopie der Transformation zurueckgeben!
1290     return new AffineTransform(worldToScreen);
1291 alfonx 144 }
1292 mojays 2
1293 alfonx 530 /**
1294     * A flag indicating if dispose() has already been called. If true, then
1295     * further use of this {@link SelectableXMapPane} is undefined.
1296     */
1297     private boolean isDisposed() {
1298     return disposed;
1299     }
1300    
1301     /**
1302     * Returns whether a layer is regarded or ignored on {@link #SELECT_TOP},
1303     * {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP} actions. Returns
1304     * <code>true</code> if the selectability has not been defined.
1305     *
1306     * @param layer
1307     * a layer
1308     */
1309     public boolean isMapLayerSelectable(final MapLayer layer) {
1310     final Boolean selectable = mapLayerSelectable.get(layer);
1311     return selectable == null ? true : selectable;
1312     }
1313    
1314     /**
1315     * Return <code>true</code> if a CRS and a {@link #mapArea} are set and the
1316     * {@link XMapPane} is visible and has bounds set.
1317     */
1318     public boolean isWellDefined() {
1319     try {
1320 alfonx 551 if (getMapContext() == null)
1321 alfonx 530 return false;
1322 alfonx 551 if (getMapContext().getLayerCount() <= 0)
1323 alfonx 530 return false;
1324 alfonx 544 if (getVisibleRect().getWidth() == 0)
1325 alfonx 530 return false;
1326 alfonx 544 if (getVisibleRect().getHeight() == 0)
1327 alfonx 530 return false;
1328 alfonx 544 // if (getMapArea() == null)
1329     // return false;
1330 alfonx 530 } catch (final Exception e) {
1331     return false;
1332 alfonx 509 }
1333 alfonx 530 return true;
1334 alfonx 144 }
1335 mojays 2
1336 alfonx 555 /**
1337     * Called from the listeners while the mouse is dragging, this method either
1338     * paints a translated (moved/panned) version of the image, or a rectangle.
1339     *
1340     * @param startPos
1341     * in screen coordinates
1342     * @param lastPos
1343     * in screen coordinates
1344     * @param event
1345     * the {@link MouseEvent} to read the mouse buttons from
1346     */
1347 alfonx 530 public void mouseDragged(final Point startPos, final Point lastPos,
1348     final MouseEvent event) {
1349    
1350     if ((getState() == XMapPane.PAN)
1351     || ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
1352    
1353 alfonx 555 // Panning needs a panning coursor
1354 alfonx 530 if (getCursor() != SwingUtil.PANNING_CURSOR) {
1355     setCursor(SwingUtil.PANNING_CURSOR);
1356    
1357 alfonx 555 // While panning, we deactivate the rendering. So the tasks are
1358     // ready to start when the panning is finished.
1359     if (bgExecuter != null && bgExecuter.isRunning())
1360 alfonx 530 bgExecuter.cancelTask();
1361 alfonx 555 if (localExecuter.isRunning())
1362 alfonx 530 localExecuter.cancelTask();
1363     }
1364    
1365     if (lastPos.x > 0 && lastPos.y > 0) {
1366     final int dx = event.getX() - lastPos.x;
1367     final int dy = event.getY() - lastPos.y;
1368    
1369     // TODO Stop dragging when the drag would not be valid...
1370     // boolean dragValid = true;
1371     // // check if this panning results in a valid mapArea
1372     // {
1373     // Rectangle winBounds = xMapPane.getBounds();
1374     // winBounds.translate(xMapPane.imageOrigin.x,
1375     // -xMapPane.imageOrigin.y);
1376     // Envelope newMapAreaBefore = xMapPane.tranformWindowToGeo(
1377     // winBounds.x, winBounds.y, winBounds.x
1378     // + winBounds.width, winBounds.y
1379     // + winBounds.height);
1380     //
1381     //
1382     // winBounds = xMapPane.getBounds();
1383     // Point testIng = new Point(xMapPane.imageOrigin);
1384     // testIng.translate(dx, dy);
1385     // winBounds.translate(testIng.x, -testIng.y);
1386     // Envelope newMapAreaAfter = xMapPane.tranformWindowToGeo(
1387     // winBounds.x, winBounds.y, winBounds.x
1388     // + winBounds.width, winBounds.y
1389     // + winBounds.height);
1390     //
1391     // // If the last drag doesn't change the MapArea anymore cancel
1392     // it.
1393     // if (xMapPane.bestAllowedMapArea(newMapAreaAfter).equals(
1394     // xMapPane.bestAllowedMapArea(newMapAreaBefore))){
1395     // dragValid = false;
1396     // return;
1397     // }
1398     // }
1399    
1400     imageOrigin.translate(dx, dy);
1401     updateFinalImage();
1402     repaint();
1403     }
1404    
1405     } else if ((getState() == XMapPane.ZOOM_IN)
1406     || (getState() == XMapPane.ZOOM_OUT)
1407     || (getState() == XMapPane.SELECT_ALL)
1408 alfonx 555 || (getState() == XMapPane.SELECT_TOP)) {
1409    
1410     // Draws a rectangle
1411 alfonx 580 final Graphics2D graphics = (Graphics2D) getGraphics();
1412 alfonx 530 drawRectangle(graphics, startPos, event.getPoint());
1413 alfonx 555 if ((lastPos.x > 0) && (lastPos.y > 0))
1414 alfonx 530 drawRectangle(graphics, startPos, lastPos);
1415     graphics.dispose();
1416     }
1417 alfonx 144 }
1418 mojays 2
1419 alfonx 509 /**
1420 alfonx 530 * Called by the {@link RenderingExecutor} when rendering was cancelled.
1421     */
1422     public void onRenderingCancelled() {
1423 alfonx 579 // LOGGER.debug("Rendering cancelled");
1424 alfonx 530 repaintTimer.stop();
1425     }
1426    
1427     /**
1428     * Called by the {@link RenderingExecutor} when rendering has been
1429     * completed.
1430 alfonx 580 *
1431     * @param l
1432     * long ms the rendering took
1433 alfonx 530 */
1434 alfonx 581 public void onRenderingCompleted(final long l) {
1435     lastRenderingDuration = (lastRenderingDuration + l) / 2;
1436     LOGGER.debug("complete rendering after "+lastRenderingDuration+"ms");
1437 alfonx 555
1438 alfonx 530 repaintTimer.stop();
1439 alfonx 555
1440     // We "forget" about an exception every time we complete a rendering
1441     // thread successfully
1442     if (renderingErrors.size() > 0)
1443     renderingErrors.remove(0);
1444    
1445 alfonx 530 updateFinalImage();
1446     repaint();
1447     }
1448    
1449     /**
1450     * Called by the {@linkplain XMapPane.RenderingTask} when rendering failed.
1451     * Publishes a {@linkplain MapPaneEvent} of type {@code
1452     * MapPaneEvent.Type.RENDERING_STOPPED} to listeners.
1453 alfonx 509 *
1454 alfonx 530 * @param renderingError
1455     * The error that occured during rendering
1456     *
1457     * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1458 alfonx 509 */
1459 alfonx 530 public void onRenderingFailed(final Exception renderingError) {
1460 alfonx 553
1461     // Store the exceptions so we can show it to the user:
1462 alfonx 555 if (!(renderingError instanceof java.lang.IllegalArgumentException && renderingError
1463     .getMessage().equals(
1464 alfonx 553 "Argument \"sourceCRS\" should not be null.")))
1465     this.renderingErrors.add(renderingError);
1466 alfonx 533 if (renderingErrors.size() > 3)
1467     renderingErrors.remove(0);
1468 alfonx 555
1469 alfonx 530 repaintTimer.stop();
1470 alfonx 555
1471 alfonx 530 LOGGER.warn("Rendering failed", renderingError);
1472     updateFinalImage();
1473     repaint();
1474 mojays 2
1475 alfonx 530 }
1476    
1477     @Override
1478     protected void paintComponent(final Graphics g) {
1479 alfonx 544
1480 alfonx 555 // Maybe update the cursor and maybe stop the repainting timer
1481     updateCursor();
1482    
1483 alfonx 530 if (!acceptsRepaintCalls)
1484     return;
1485    
1486 alfonx 555 if (!isWellDefined())
1487     return;
1488 alfonx 581 //
1489     // if (paneResized) {
1490     //// ((Graphics2D) g).setBackground(getMapBackgroundColor());
1491     //// g.clearRect(0, 0, getVisibleRect().width, getVisibleRect().height);
1492     // return;
1493     // }
1494 alfonx 580
1495 alfonx 578 // super.paintComponent(g); // candidate for removal
1496 alfonx 530
1497     boolean paintedSomething = false;
1498    
1499     if (mapImageInvalid) { /* if the map changed then redraw */
1500    
1501     mapImageInvalid = false; // Reset for next round
1502    
1503     // If the new mapArea and the oldMapArea intersect, we can draw some
1504     // quick scaled preview to make the user feel that something is
1505     // happening.
1506 alfonx 580 if (lastRenderingDuration > PRESCALE_MINTIME && mapAreaChanged
1507     && oldMapArea != null
1508 alfonx 530 && getMapArea().intersects(oldMapArea)
1509 alfonx 581 && !getMapArea().equals(oldMapArea) && !paneResized) {
1510 alfonx 530
1511     mapAreaChanged = false;
1512    
1513 alfonx 581 // if (getMapArea().covers(oldMapArea)) {
1514     //// quickPreviewHint = ZOOM_OUT;
1515     // paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1516     // } else if (oldMapArea.covers(getMapArea())) {
1517     // quickPreviewHint = ZOOM_IN;
1518 alfonx 530 paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1519 alfonx 581 // }
1520 alfonx 509 }
1521     }
1522 mojays 2
1523 alfonx 530 if (!paintedSomething) {
1524 mojays 2
1525 alfonx 530 g.drawImage(getFinalImage(), 0, 0, null);
1526 alfonx 529
1527 alfonx 530 paintedSomething = true; // cand. for removal
1528     }
1529 alfonx 529
1530 alfonx 530 }
1531 alfonx 509
1532 alfonx 530 /**
1533     * Heavily works on releasing all resources related to the four
1534     * {@link Image}s used to cache the results of the different renderers.<br>
1535     * In November 2009 i had some memory leaking problems with {@link XMapPane}
1536     * . The resources of the buffered images were never released. It seem to be
1537     * important to call the GC right after flushing the images.<br>
1538     * Hence this method may take a while, because it calls the GC up to four
1539     * times.
1540     */
1541     private void disposeImages() {
1542    
1543     // System.out.println("vorher = "
1544     // + new MbDecimalFormatter().format(LangUtil.gcTotal()));
1545     // bi.flush();
1546     // return bi = null;
1547     // System.out.println("nacher = "
1548     // + new MbDecimalFormatter().format(LangUtil.gcTotal()));
1549     //
1550     // System.out.println("\n");
1551    
1552     if (preFinalImage != null) {
1553     preFinalImage.flush();
1554     preFinalImage = null;
1555     LangUtil.gc();
1556 alfonx 509 }
1557 alfonx 530 if (finalImage != null) {
1558     finalImage.flush();
1559     finalImage = null;
1560     LangUtil.gc();
1561     }
1562     if (localImage != null) {
1563     localImage.flush();
1564     localImage = null;
1565     LangUtil.gc();
1566     }
1567     if (bgImage != null) {
1568     bgImage.flush();
1569     bgImage = null;
1570     LangUtil.gc();
1571     }
1572 alfonx 509
1573 alfonx 144 }
1574 mojays 2
1575 alfonx 530 /**
1576     * Performs a {@value #PAN} action. During panning, the displacement is
1577     * stored in {@link #imageOrigin} object. Calling {@link #performPan()} will
1578     * reset the offset and call {@link #setMapArea(Envelope)}.
1579     */
1580     public void performPan() {
1581    
1582 alfonx 581 final Rectangle winBounds = getVisibleRect();
1583 alfonx 543
1584 alfonx 530 winBounds.translate(-imageOrigin.x, -imageOrigin.y);
1585     final Envelope newMapArea = tranformWindowToGeo(winBounds.x,
1586     winBounds.y, winBounds.x + winBounds.width, winBounds.y
1587     + winBounds.height);
1588    
1589     imageOrigin.x = 0;
1590     imageOrigin.y = 0;
1591    
1592     if (!setMapArea(newMapArea)) {
1593     /**
1594     * If setMapArea returns true, the finalImage is updated anyways.
1595     * This if-case exists to ensure that we repaint a correct image
1596     * even if the new panning area has been denied.
1597     */
1598     updateFinalImage();
1599     repaint();
1600     }
1601    
1602     if (getCursor() == SwingUtil.PANNING_CURSOR)
1603     setCursor(SwingUtil.PAN_CURSOR);
1604     }
1605    
1606     /**
1607     * Entfernt einen Listener von der Map.
1608     *
1609     * @param l
1610     * zu entfernender Listener
1611     */
1612     public void removeMapPaneListener(final JMapPaneListener l) {
1613     mapPaneListeners.remove(l);
1614     }
1615    
1616     /**
1617     * Cancels all running renderers and sets the flag to start new ones. <br>
1618     *
1619     * @see #startRenderThreadsTimer
1620     */
1621     private void requestStartRendering() {
1622     if (bgExecuter != null)
1623     bgExecuter.cancelTask();
1624    
1625 alfonx 555 localExecuter.cancelTask();
1626    
1627 alfonx 544 mapImageInvalid = true;
1628     if (paneResized) {
1629     paneResized = false;
1630     disposeImages();
1631     }
1632     requestStartRendering = true;
1633    
1634 alfonx 530 }
1635    
1636 alfonx 539 /**
1637     * Calculate the affine transforms used to convert between world and pixel
1638     * coordinates. The calculations here are very basic and assume a cartesian
1639     * reference system.
1640     * <p>
1641     * Tne transform is calculated such that {@code envelope} will be centred in
1642     * the display
1643     *
1644     * @param envelope
1645     * the current map extent (world coordinates)
1646     * @param paintArea
1647     * the current map pane extent (screen units)
1648     */
1649     private void resetTransforms() {
1650 alfonx 581 final ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,
1651 alfonx 551 getMapContext().getCoordinateReferenceSystem());
1652 alfonx 540
1653 alfonx 544 // System.out
1654     // .println("paintArea in resetTeansofrms = " + getVisibleRect());
1655     if (!isWellDefined())
1656     return;
1657 alfonx 543
1658 alfonx 539 worldToScreen = RendererUtilities.worldToScreenTransform(refMapEnv,
1659 alfonx 544 getVisibleRect());
1660 alfonx 530
1661 alfonx 539 try {
1662     screenToWorld = worldToScreen.createInverse();
1663 alfonx 530
1664 alfonx 581 } catch (final NoninvertibleTransformException ex) {
1665 alfonx 544 LOGGER
1666     .error("can't invert worldToScreen to get screenToWorld!",
1667     ex);
1668 alfonx 539 }
1669     }
1670 alfonx 536
1671 alfonx 509 public void setBgContext(final MapContext context) {
1672 mojays 2
1673 alfonx 509 // Remove the default listener from the old context
1674     if (this.bgContext != null) {
1675     this.bgContext.removeMapLayerListListener(bgContextListener);
1676    
1677     // adding listener to all layers
1678 alfonx 530 for (final MapLayer mapLayer : bgContext.getLayers()) {
1679 alfonx 509 mapLayer.removeMapLayerListener(bgMapLayerListener);
1680     }
1681 alfonx 144 }
1682 mojays 2
1683 alfonx 509 this.bgContext = context;
1684 mojays 2
1685 alfonx 509 if (context != null) {
1686 alfonx 544 // setMapArea(bgContext.getAreaOfInterest());
1687 alfonx 509
1688     this.bgContext.addMapLayerListListener(bgContextListener);
1689    
1690     // adding listener to all layers
1691 alfonx 530 for (final MapLayer mapLayer : bgContext.getLayers()) {
1692 alfonx 509 mapLayer.addMapLayerListener(bgMapLayerListener);
1693 alfonx 144 }
1694 alfonx 509 }
1695 alfonx 544
1696     requestStartRendering();
1697 alfonx 509 }
1698 mojays 2
1699 alfonx 530 public void setJava2dHints(final RenderingHints java2dHints) {
1700     this.java2dHints = java2dHints;
1701     }
1702    
1703     public void setLocalContext(final MapContext context) {
1704     // Remove the default listener from the old context
1705     if (this.localContext != null) {
1706     this.localContext.removeMapLayerListListener(localContextListener);
1707    
1708     // adding listener to all layers
1709     for (final MapLayer mapLayer : localContext.getLayers()) {
1710     mapLayer.removeMapLayerListener(localMapLayerListener);
1711 alfonx 509 }
1712 alfonx 530 }
1713 alfonx 509
1714 alfonx 530 this.localContext = context;
1715    
1716     if (context != null) {
1717    
1718 alfonx 544 // setMapArea(localContext.getAreaOfInterest());
1719 alfonx 530
1720 alfonx 560 getLocalRenderer().setContext(localContext);
1721 alfonx 530
1722     this.localContext.addMapLayerListListener(localContextListener);
1723    
1724     // adding listener to all layers
1725     for (final MapLayer mapLayer : localContext.getLayers()) {
1726     mapLayer.addMapLayerListener(localMapLayerListener);
1727 alfonx 144 }
1728     }
1729 mojays 2
1730 alfonx 544 requestStartRendering();
1731    
1732 alfonx 509 }
1733 alfonx 578
1734 alfonx 581 public void setBorder(final Border b) {
1735 alfonx 578 super.setBorder(b);
1736 mojays 561 }
1737 alfonx 509
1738 alfonx 560 /**
1739     * Triggers to repaint (fast) and re-render (slow) the JMapPane.
1740     */
1741     public void refresh() {
1742     mapImageInvalid = true;
1743     repaint();
1744     }
1745    
1746     // /**
1747     // * Triggers to use new {@link GTRenderer} and refresh the map. Should be
1748     // * called after {@link Style}s have been changed because GTRenderer is
1749     // * otherwise not working well.
1750     // */
1751     // public void refreshRenderers() {
1752     // localRenderer = GTUtil.createGTRenderer();
1753     // setLocalContext(getMapContext());
1754     // mapImageInvalid = true;
1755     // repaint();
1756     // }
1757    
1758 alfonx 578 /**
1759     * Set the new map area.
1760     *
1761     * @param newMapArea
1762     * @return <code>true</code> if the mapArea has been changed and a repaint
1763     * has been triggered.
1764     */
1765 alfonx 544 public boolean setMapArea(final Envelope newMapArea) {
1766 alfonx 556 if (newMapArea == null)
1767     return false;
1768 alfonx 560 if (getMapContext().getCoordinateReferenceSystem() == null)
1769     return false;
1770 alfonx 551 return setMapArea(new ReferencedEnvelope(newMapArea, getMapContext()
1771 alfonx 544 .getCoordinateReferenceSystem()));
1772     }
1773    
1774 alfonx 509 /**
1775 alfonx 578 * Set the new map area.
1776     *
1777 alfonx 509 * @param newMapArea
1778     * @return <code>true</code> if the mapArea has been changed and a repaint
1779     * has been triggered.
1780     */
1781 alfonx 544 public boolean setMapArea(final ReferencedEnvelope newMapArea) {
1782 alfonx 509
1783     if (newMapArea == null
1784     || bestAllowedMapArea(newMapArea).equals(mapArea)) {
1785     // No change.. no need to repaint
1786     return false;
1787 alfonx 144 }
1788 mojays 2
1789 alfonx 513 // Testing, whether NaN or Infinity are used in the newMapArea
1790     if (newMapArea.isNull() || Double.isInfinite(newMapArea.getMaxX())
1791     || Double.isInfinite(newMapArea.getMaxY())
1792     || Double.isInfinite(newMapArea.getMinX())
1793     || Double.isInfinite(newMapArea.getMinY())) {
1794     // No change.. ugly new values
1795     LOGGER.warn("setMapArea has been called with newArea = "
1796     + newMapArea);
1797     return false;
1798     }
1799    
1800 alfonx 539 final Envelope candNew = bestAllowedMapArea(newMapArea);
1801    
1802 alfonx 513 // Testing, whether the difference if just minimal
1803 alfonx 509 if (mapArea != null) {
1804 alfonx 530 final double tolX = mapArea.getWidth() / 1000.;
1805     final double tolY = mapArea.getHeight() / 1000.;
1806 alfonx 509 if ((candNew.getMinX() - tolX < mapArea.getMinX())
1807     && (mapArea.getMinX() < candNew.getMinX() + tolX)
1808     && (candNew.getMaxX() - tolX < mapArea.getMaxX())
1809     && (mapArea.getMaxX() < candNew.getMaxX() + tolX)
1810 mojays 2
1811 alfonx 509 && (candNew.getMinY() - tolY < mapArea.getMinY())
1812     && (mapArea.getMinY() < candNew.getMinY() + tolY)
1813     && (candNew.getMaxY() - tolY < mapArea.getMaxY())
1814     && (mapArea.getMaxY() < candNew.getMaxY() + tolY)
1815 mojays 2
1816 alfonx 509 ) {
1817     // The two mapAreas only differ my 1/1000th.. ignore
1818 alfonx 505
1819 alfonx 509 return false;
1820     }
1821     }
1822 mojays 2
1823 alfonx 539 // New map are is accepted:
1824 alfonx 509 oldMapArea = mapArea;
1825 alfonx 539 mapArea = candNew;
1826     resetTransforms();
1827 mojays 2
1828 alfonx 509 if (localContext != null) {
1829     localContext.setAreaOfInterest(mapArea, localContext
1830     .getCoordinateReferenceSystem());
1831 alfonx 144 }
1832 alfonx 509 if (bgContext != null) {
1833     bgContext.setAreaOfInterest(mapArea, localContext
1834     .getCoordinateReferenceSystem());
1835     }
1836 alfonx 544
1837 alfonx 509 mapAreaChanged = true;
1838 alfonx 513
1839 alfonx 555 repaint(200); // Do not remove it!
1840 alfonx 544
1841     requestStartRendering();
1842    
1843 alfonx 509 return true;
1844     }
1845 mojays 2
1846 alfonx 509 /**
1847 alfonx 530 * Set the background color of the map.
1848 alfonx 509 *
1849 alfonx 530 * @param if <code>null</code>, white is used.
1850 alfonx 509 */
1851 alfonx 581 public void setMapBackgroundColor(final Color bgColor) {
1852 alfonx 530 this.mapBackgroundColor = bgColor;
1853 alfonx 509 }
1854 mojays 2
1855 alfonx 509 /**
1856 alfonx 530 * Set the BufferedImage to use as a flaoting icon in the lower right corner
1857     *
1858     * @param mapImageIcon
1859     * <code>null</code> is allowed and deactivates this icon.
1860 alfonx 529 */
1861 alfonx 530 public void setMapImage(final BufferedImage mapImage) {
1862     this.mapImage = mapImage;
1863     }
1864 alfonx 529
1865     /**
1866 alfonx 530 * Sets whether a layer is regarded or ignored on {@link #SELECT_TOP},
1867     * {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP} actions.
1868     *
1869     * @param layer
1870     * a layer
1871     * @param selectable
1872     * if {@code false} the layer is ignored during the upper
1873     * mentioned actions. If <code>null</code>, the default (true)
1874     * will be used.
1875 alfonx 529 */
1876 alfonx 530 public void setMapLayerSelectable(final MapLayer layer,
1877     final Boolean selectable) {
1878     if (selectable == null)
1879     mapLayerSelectable.remove(layer);
1880     else
1881     mapLayerSelectable.put(layer, selectable);
1882 alfonx 144 }
1883 mojays 2
1884 alfonx 509 /**
1885 alfonx 530 * Defines an evelope of the viwable area. The JMapPane will never show
1886     * anything outside of this extend.
1887 alfonx 509 *
1888 alfonx 530 * @param maxExtend
1889     * <code>null</code> to not have this restriction.
1890 alfonx 509 */
1891 alfonx 530 public void setMaxExtend(final Envelope maxExtend) {
1892     this.maxExtend = maxExtend;
1893 alfonx 144 }
1894 mojays 2
1895 alfonx 509 /**
1896 alfonx 530 * Set the maximum allowed zoom scale. This is the smaller number value of
1897     * the two. If <code>null</code> is passed, Double.MINVALUE are used which
1898     * mean there is no restriction.
1899 alfonx 509 *
1900 alfonx 530 * @author <a href="mailto:[email protected]">Stefan Alfons
1901     * Kr&uuml;ger</a>
1902 alfonx 509 */
1903 alfonx 530 public void setMaxZoomScale(final Double maxZoomScale) {
1904     this.maxZoomScale = maxZoomScale == null ? Double.MIN_VALUE
1905     : maxZoomScale;
1906 alfonx 144 }
1907 mojays 2
1908 alfonx 530 // /** Stored the time used for the last real rendering in ms. **/
1909 alfonx 580 private long lastRenderingDuration = 1000;
1910 alfonx 530
1911 alfonx 509 /**
1912 alfonx 530 * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1913     * value of the two. If <code>null</code> is passed, Double.MAXVALUE are
1914     * used which mean there is no restriction.
1915 alfonx 509 *
1916 alfonx 530 *
1917     * @author <a href="mailto:[email protected]">Stefan Alfons
1918     * Kr&uuml;ger</a>
1919 alfonx 509 */
1920 alfonx 530 public void setMinZoomScale(final Double minZoomScale) {
1921     this.minZoomScale = minZoomScale == null ? Double.MAX_VALUE
1922     : minZoomScale;
1923 alfonx 144 }
1924 mojays 2
1925 alfonx 509 /**
1926 alfonx 555 * If <code>true</code>, allow the {@link XMapPane} to process #repaint()
1927     * requests. Otherwise the map will not paint anything and not start any
1928     * rendering {@link Thread}s.
1929 alfonx 509 */
1930 alfonx 530 public void setPainting(final boolean b) {
1931     acceptsRepaintCalls = b;
1932 alfonx 555 if (acceptsRepaintCalls == true)
1933     repaint();
1934 alfonx 530 }
1935 mojays 2
1936 alfonx 530 private void setRendererHints(final Map<Object, Object> rendererHints) {
1937 alfonx 533 if (rendererHints != null)
1938     this.rendererHints = rendererHints;
1939 alfonx 144 }
1940 alfonx 513
1941 alfonx 509 /**
1942 alfonx 530 * Enables/Disables the ZOOM Mouse Listener. Upates the Cursor and stops the
1943     * repaint Timer if
1944     *
1945     * @param state
1946 alfonx 509 */
1947 alfonx 530 public void setState(final int state) {
1948     this.state = state;
1949 mojays 2
1950 alfonx 530 zoomMapPaneMouseListener.setEnabled((state == ZOOM_IN
1951     || state == ZOOM_OUT || state == PAN));
1952 alfonx 509
1953 alfonx 530 // Je nach Aktion den Cursor umsetzen
1954     updateCursor();
1955 alfonx 509 }
1956 alfonx 504
1957 alfonx 509 /**
1958 alfonx 530 * Standardmaessig wird der Cursor automatisch je nach MapPane-Aktion (Zoom,
1959     * Auswahl, ...) gesetzt. Mit dieser Methode kann ein statischer Cursor
1960     * gesetzt werden, der unabhaengig von der aktuellen MapPanes-Aktion
1961     * beibehalten wird. Um diesen statischen Cursor wieder zu entfernen, kann
1962     * {@code null} als Parameter uebergeben werden
1963 alfonx 509 *
1964 alfonx 530 * @param cursor
1965     * Cursor
1966 alfonx 509 */
1967 alfonx 530 public void setStaticCursor(final Cursor cursor) {
1968     this.staticCursor = cursor;
1969     if (cursor != null)
1970     super.setCursor(cursor);
1971 alfonx 509 }
1972 alfonx 431
1973 alfonx 509 /**
1974     * Starts rendering on one or two threads
1975     */
1976     private void startRendering() {
1977    
1978 alfonx 544 if (!isWellDefined() || !acceptsRepaintCalls) {
1979 alfonx 555 // if we are not ready to start rendering, try it again the next
1980     // time the timer is chacking the flag.
1981 alfonx 544 requestStartRendering = true;
1982 alfonx 509 return;
1983 alfonx 544 }
1984 alfonx 509
1985 alfonx 529 if (bgExecuter != null) {
1986 alfonx 509 // Stop all renderers
1987     bgExecuter.cancelTask();
1988 alfonx 524 }
1989 alfonx 509
1990 alfonx 555 localExecuter.cancelTask();
1991 alfonx 529
1992 alfonx 530 final Rectangle curPaintArea = getVisibleRect();
1993 mojays 2
1994 alfonx 509 /**
1995     * We have to set new renderer
1996     */
1997    
1998     if (getBgContext() != null) {
1999 alfonx 533 bgRenderer.setJava2DHints(getJava2dHints());
2000     bgRenderer.setRendererHints(getRendererHints());
2001    
2002 alfonx 529 // bgExecuter = new RenderingExecutor();
2003     // LOGGER.debug("starting bg renderer:");
2004     // // /* System.out.println("rendering"); */
2005     // final GTRenderer createGTRenderer = GTUtil.createGTRenderer(
2006     // bgContext, getRendererHints());
2007     // createGTRenderer.setJava2DHints(getJava2dHints());
2008     // bgExecuter.submit(getBgContext().getAreaOfInterest(),
2009     // curPaintArea,
2010     // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);
2011 alfonx 509 }
2012    
2013 alfonx 551 if (getMapContext() != null) {
2014 alfonx 529 // localExecuter = new RenderingExecutor(this, 150l);
2015 alfonx 530 // LOGGER.debug("starting local renderer:");
2016    
2017 alfonx 560 getLocalRenderer().setJava2DHints(getJava2dHints());
2018     getLocalRenderer().setRendererHints(getRendererHints());
2019 alfonx 529
2020 alfonx 544 final boolean submitted = localExecuter.submit(getMapArea(),
2021 alfonx 533 curPaintArea, (Graphics2D) getLocalImage().getGraphics(),
2022 alfonx 560 getLocalRenderer());
2023 alfonx 530 if (submitted)
2024     repaintTimer.restart();
2025     else
2026 alfonx 544 requestStartRendering = true; // Try to start rendering
2027     // again in
2028 alfonx 530 // a moment
2029 alfonx 509 }
2030    
2031 alfonx 514 updateCursor();
2032 alfonx 144 }
2033 mojays 2
2034 alfonx 509 /**
2035 alfonx 530 * Transformiert einen Geo-Koordinaten-Bereich in Fenster-Koordinaten.
2036 alfonx 509 *
2037 alfonx 530 * @param ox
2038     * X-Koordinate der VON-Position
2039     * @param oy
2040     * Y-Koordinate der VON-Position
2041     * @param px
2042     * X-Koordinate der BIS-Position
2043     * @param py
2044     * Y-Koordinate der BIS-Position
2045     * @param winToGeotransform
2046     * Eine Window to Geo transform. If <code>null</code>,
2047     * {@link #getScreenToWorld()} is used.
2048 alfonx 509 */
2049 alfonx 530 public Envelope tranformGeoToWindow(final double ox, final double oy,
2050 alfonx 539 final double px, final double py) {
2051     final AffineTransform at = getWorldToScreenTransform();
2052 alfonx 530 Point2D geoO;
2053 alfonx 539 // try {
2054     geoO = at.transform(new Point2D.Double(ox, oy), null);
2055     final Point2D geoP = at.transform(new Point2D.Double(px, py), null);
2056     return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY());
2057     // } catch (final NoninvertibleTransformException e) {
2058     // LOGGER.error(e);
2059     // return new Envelope(ox, oy, px, py);
2060     // }
2061 alfonx 144 }
2062 mojays 2
2063 alfonx 509 /**
2064 alfonx 530 * Transformiert einen Fenster-Koordinaten-Bereich in Geo-Koordinaten.
2065 alfonx 509 *
2066 alfonx 530 * @param ox
2067     * X-Koordinate der VON-Position
2068     * @param oy
2069     * Y-Koordinate der VON-Position
2070     * @param px
2071     * X-Koordinate der BIS-Position
2072     * @param py
2073     * Y-Koordinate der BIS-Position
2074 alfonx 509 */
2075 alfonx 530 public Envelope tranformWindowToGeo(final int ox, final int oy,
2076     final int px, final int py) {
2077     final AffineTransform at = getScreenToWorld();
2078     final Point2D geoO = at.transform(new Point2D.Double(ox, oy), null);
2079     final Point2D geoP = at.transform(new Point2D.Double(px, py), null);
2080 alfonx 543
2081 alfonx 539 // Mmmmm... don't really understand why its x,x,y,y
2082 alfonx 543 // return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(),
2083     // geoP.getY());
2084     return new Envelope(new Coordinate(geoO.getX(), geoO.getY()),
2085     new Coordinate(geoP.getX(), geoP.getY()));
2086 alfonx 144 }
2087 mojays 2
2088 alfonx 509 /**
2089 alfonx 530 * Will update the cursor. If all rendering is finished also stops the
2090     * {@link #repaintTimer}
2091 alfonx 509 */
2092 alfonx 530 public void updateCursor() {
2093 mojays 2
2094 alfonx 530 // if the renderers have stopped, also stop the timer that is updating
2095     // the final image
2096     if (bgExecuter != null && bgExecuter.isRunning()
2097 alfonx 555 || localExecuter.isRunning()) {
2098     setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
2099 alfonx 530 return;
2100     } else {
2101     // Allow one last rendering
2102     if (repaintTimer.isRunning()) {
2103 alfonx 544 // System.out.println("one last rendering....");
2104 alfonx 530 repaintTimer.stop();
2105     updateFinalImage();
2106     repaint();
2107     }
2108 alfonx 144 }
2109 mojays 2
2110 alfonx 530 // wenn manueller Cursor gesetzt ist, dann diesen verwenden (unabhaengig
2111     // von der aktuellen Aktion
2112     if (this.staticCursor != null) {
2113     setCursor(staticCursor);
2114     return;
2115     }
2116     if (getCursor() == SwingUtil.PANNING_CURSOR) {
2117     // This cursor will reset itself
2118     return;
2119     }
2120 mojays 2
2121 alfonx 530 // Set the cursor depending on what tool is in use...
2122     switch (state) {
2123     case SELECT_TOP:
2124     case SELECT_ONE_FROM_TOP:
2125     case SELECT_ALL:
2126     setCursor(SwingUtil.CROSSHAIR_CURSOR);
2127     break;
2128     case ZOOM_IN:
2129     setCursor(SwingUtil.ZOOMIN_CURSOR);
2130     break;
2131     case ZOOM_OUT:
2132     setCursor(SwingUtil.ZOOMOUT_CURSOR);
2133     break;
2134     case PAN:
2135     setCursor(SwingUtil.PAN_CURSOR);
2136     break;
2137     default:
2138     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
2139     break;
2140     }
2141 alfonx 144 }
2142 mojays 2
2143 alfonx 509 /**
2144 alfonx 530 * The renderers are all rendering into their own {@link Image}s. This
2145     * method combines all images to one {@link #finalImage}. The
2146     * {@link #repaintTimer} is calling this method regularely to update the
2147     * {@link #finalImage} even if the renderers are still working.
2148 alfonx 509 */
2149 alfonx 530 synchronized protected Image updateFinalImage() {
2150 mojays 2
2151 alfonx 530 // Render the two map images first, into the preFinalImage
2152     if (bgExecuter != null) {
2153     final Graphics2D preFinalG = (Graphics2D) getPreFinalImage()
2154     .getGraphics();
2155     preFinalG.setBackground(getMapBackgroundColor());
2156 mojays 2
2157 alfonx 530 preFinalG.drawImage(getBgImage(), 0, 0, getMapBackgroundColor(),
2158     null);
2159 mojays 2
2160 alfonx 530 // // Draw the local layers image
2161     preFinalG.drawImage(getLocalImage(), 0, 0, null);
2162     preFinalG.dispose();
2163 mojays 2
2164 alfonx 76 } else {
2165 alfonx 530 preFinalImage = getLocalImage();
2166 alfonx 76 }
2167 mojays 2
2168 alfonx 530 final Graphics2D finalG = getFinalImage().createGraphics();
2169     finalG.setBackground(getMapBackgroundColor());
2170     finalG.drawImage(getPreFinalImage(), imageOrigin.x, imageOrigin.y,
2171     getMapBackgroundColor(), null);
2172 mojays 2
2173 alfonx 580 // What was it good for?
2174     // final int finalImageHeight = getFinalImage().getHeight();
2175     // final int finalImageWidth = getFinalImage().getWidth();
2176     // final Rectangle painedArea = new Rectangle(imageOrigin.x,
2177     // imageOrigin.y, finalImageWidth, finalImageHeight);
2178     // SwingUtil.clearAround(finalG, painedArea, getVisibleRect(),
2179     // getMapBackgroundColor());
2180 alfonx 414
2181 alfonx 533 addGadgets(finalG, false);
2182 alfonx 414
2183 alfonx 530 finalG.dispose();
2184 alfonx 414
2185 alfonx 530 return finalImage;
2186 alfonx 76 }
2187    
2188 alfonx 144 /**
2189 alfonx 530 * Paints some optional stuff into the given {@link Graphics2D}. Usually
2190     * called as the last layer when {@link #updateFinalImage()}
2191 alfonx 533 *
2192     * @param forceWait
2193     * if <code>true</code>, a Wait-message will be painted even
2194     * though the rendering threads may not yet have started. If
2195     * <code>false</code>, it will only depend on
2196     * {@link #localExecuter.isRunning} and #bgExecuter.isRunning
2197 alfonx 144 */
2198 alfonx 581 private void addGadgets(final Graphics2D graphics, final boolean forceWait) {
2199 mojays 2
2200 alfonx 530 // Paint a logo to the bottom right if available
2201     if (mapImage != null) {
2202 alfonx 581 final Rectangle visibleRect = getVisibleRect();
2203 alfonx 540 graphics.drawImage(mapImage, visibleRect.width
2204     - mapImage.getWidth() - 10, getVisibleRect().height
2205 alfonx 530 - mapImage.getHeight() - 10, null);
2206     }
2207 alfonx 539
2208 alfonx 533 int y = 17;
2209 mojays 2
2210 alfonx 530 // If the rendering process is still running, indicate this is the image
2211 alfonx 533 if (forceWait || bgExecuter != null && bgExecuter.isRunning()
2212 alfonx 555 || localExecuter.isRunning()) {
2213 mojays 2
2214 alfonx 539 y += 8;
2215    
2216 alfonx 530 final Color c = graphics.getColor();
2217     graphics.setFont(waitFont);
2218 alfonx 533
2219     graphics.setColor(getMapBackgroundColor());
2220     graphics.drawString(waitMsg, 5, y);
2221     graphics.setColor(getMapBackgroundColor());
2222 alfonx 539 graphics.drawString(waitMsg, 7, y + 2);
2223 alfonx 530 graphics.setColor(Color.BLACK);
2224 alfonx 539 graphics.drawString(waitMsg, 6, y + 1);
2225 mojays 2
2226 alfonx 530 graphics.setColor(c);
2227 alfonx 539
2228 alfonx 580 y += 21;
2229 alfonx 418 }
2230 alfonx 414
2231 alfonx 555 if (!renderingErrors.isEmpty() && isShowExceptions()) {
2232 alfonx 533
2233     final Color c = graphics.getColor();
2234     graphics.setFont(errorFont);
2235    
2236 alfonx 581 for (final Exception ex : renderingErrors) {
2237 alfonx 533
2238     String errStr = ex.getLocalizedMessage();
2239    
2240 alfonx 544 if (errStr == null)
2241     errStr = ex.getMessage();
2242     if (errStr == null)
2243     errStr = "unknown error: " + ex.getClass().getSimpleName();
2244    
2245 alfonx 560 graphics.setColor(getMapBackgroundColor());
2246 alfonx 533 graphics.drawString(errStr, 5, y);
2247     graphics.setColor(Color.RED);
2248     graphics.drawString(errStr, 6, y + 1);
2249    
2250     y += 19;
2251     }
2252    
2253     graphics.setColor(c);
2254     }
2255    
2256 alfonx 505 }
2257    
2258     /**
2259 alfonx 533 * Sets the {@link #mapArea} to best possibly present the given features. If
2260     * only one single point is given, the window is moved over the point.
2261 alfonx 509 *
2262 alfonx 530 * @param features
2263     * if <code>null</code> or size==0, the function doesn nothing.
2264 alfonx 509 */
2265 alfonx 530 public void zoomTo(
2266     final FeatureCollection<SimpleFeatureType, SimpleFeature> features) {
2267 alfonx 509
2268 alfonx 551 // if (!isWellDefined()) return;
2269    
2270     final CoordinateReferenceSystem mapCRS = getMapContext()
2271 alfonx 530 .getCoordinateReferenceSystem();
2272     final CoordinateReferenceSystem fCRS = features.getSchema()
2273     .getGeometryDescriptor().getCoordinateReferenceSystem();
2274 alfonx 509
2275 alfonx 551 ReferencedEnvelope _mapArea;
2276     if (mapArea == null)
2277     _mapArea = features.getBounds();
2278 alfonx 555 else
2279     _mapArea = getMapArea();
2280 alfonx 551 double width = _mapArea.getWidth();
2281     double height = _mapArea.getHeight();
2282 alfonx 530 final double ratio = height / width;
2283 alfonx 509
2284 alfonx 530 if (features == null || features.size() == 0) {
2285     // feature count == 0 Zoom to the full extend
2286 alfonx 509 return;
2287 alfonx 530 } else if (features.size() == 1) {
2288 alfonx 529
2289 alfonx 530 // feature count == 1 Just move the window to the point and zoom 'a
2290     // bit'
2291     final SimpleFeature singleFeature = features.iterator().next();
2292 alfonx 509
2293 alfonx 530 if (((Geometry) singleFeature.getDefaultGeometry())
2294     .getCoordinates().length > 1) {
2295     // System.out.println("Zoomed to only pne poylgon");
2296     // Poly
2297     // TODO max width vs. height
2298     width = features.getBounds().getWidth() * 3;
2299     height = ratio * width;
2300     } else {
2301     // System.out.println("Zoomed in a bit becasue only one point");
2302     // width *= .9;
2303     // height *= .9;
2304     }
2305 alfonx 509
2306 alfonx 530 Coordinate centre = features.getBounds().centre();
2307     if (!mapCRS.equals(fCRS)) {
2308     // only to calculations if the CRS differ
2309 alfonx 529 try {
2310 alfonx 530 MathTransform fToMap;
2311     fToMap = CRS.findMathTransform(fCRS, mapCRS);
2312     // centre is transformed to the mapCRS
2313     centre = JTS.transform(centre, null, fToMap);
2314     } catch (final FactoryException e) {
2315     LOGGER.error("Looking for a Math transform", e);
2316     } catch (final TransformException e) {
2317     LOGGER.error("Looking for a Math transform", e);
2318 alfonx 529 }
2319     }
2320 alfonx 509
2321 alfonx 530 final Coordinate newLeftBottom = new Coordinate(centre.x - width
2322     / 2., centre.y - height / 2.);
2323     final Coordinate newTopRight = new Coordinate(
2324     centre.x + width / 2., centre.y + height / 2.);
2325 alfonx 509
2326 alfonx 530 final Envelope newMapArea = new Envelope(newLeftBottom, newTopRight);
2327 alfonx 509
2328 alfonx 530 setMapArea(newMapArea);
2329 alfonx 529
2330 alfonx 530 } else {
2331     final ReferencedEnvelope fBounds = features.getBounds();
2332 alfonx 529
2333 alfonx 551 ReferencedEnvelope bounds;
2334 alfonx 530 if (!mapCRS.equals(fCRS)) {
2335 alfonx 551 bounds = JTSUtil.transformEnvelope(fBounds, mapCRS);
2336 alfonx 530 } else {
2337     bounds = fBounds;
2338     }
2339     // BB umrechnen von Layer-CRS in Map-CRS
2340 alfonx 529
2341 alfonx 530 // Expand a bit
2342     bounds.expandBy(bounds.getWidth() / 6., bounds.getHeight() / 6.);
2343 alfonx 509
2344 alfonx 530 setMapArea(bounds);
2345     }
2346 alfonx 509 }
2347    
2348     /**
2349 alfonx 530 * Zooms towards a point.
2350     *
2351     * @param center
2352     * position in window coordinates
2353     * @param zoomFactor
2354     * > 1 for zoom in, < 1 for zoom out. Default is 1.33
2355 alfonx 509 */
2356 alfonx 530 public void zoomTo(final Point center) {
2357     zoomTo(center, null);
2358 alfonx 509 }
2359    
2360     /**
2361     * Zooms towards a point.
2362     *
2363     * @param center
2364     * position in window coordinates
2365     * @param zoomFaktor
2366     * > 1 for zoom in, < 1 for zoom out. Default is 1.33.
2367     */
2368     public void zoomTo(Point center, Double zoomFaktor) {
2369     if (zoomFaktor == null || zoomFaktor == 0.)
2370     zoomFaktor = 2.;
2371    
2372 alfonx 530 final Point2D gcenter = getScreenToWorld().transform(center, null);
2373 alfonx 509 center = null;
2374 alfonx 513
2375     if (Double.isNaN(gcenter.getX()) || Double.isNaN(gcenter.getY())
2376     || Double.isInfinite(gcenter.getX())
2377     || Double.isInfinite(gcenter.getY())
2378    
2379 alfonx 509 ) {
2380     // Not inside valid CRS area! cancel
2381     return;
2382     }
2383    
2384     final Envelope mapArea = getMapArea();
2385 alfonx 513
2386 alfonx 530 final Envelope newMapArea = new Envelope(mapArea);
2387 alfonx 513 newMapArea.expandBy((mapArea.getWidth() * zoomFaktor - mapArea
2388     .getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea
2389     .getHeight()) / 2.);
2390 alfonx 509
2391 alfonx 543 // TODO we actually want that
2392     // // Move the newMapArea above the new center
2393     // newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter
2394     // .getY()
2395     // - mapArea.centre().y);
2396    
2397 alfonx 509 setMapArea(newMapArea);
2398     }
2399    
2400 alfonx 553 /**
2401     * Shall non-fatal rendering exceptions be reported in the mappane or be
2402     * dropped quitely.
2403     */
2404 alfonx 581 public void setShowExceptions(final boolean showExceptions) {
2405 alfonx 553 this.showExceptions = showExceptions;
2406     }
2407    
2408     /**
2409     * Shall exceptions be reported in the mappane?
2410     */
2411     public boolean isShowExceptions() {
2412     return showExceptions;
2413     }
2414    
2415 alfonx 560 public GTRenderer getLocalRenderer() {
2416     return localRenderer;
2417     }
2418    
2419 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26