/[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 579 - (hide annotations)
Wed Nov 25 19:14:40 2009 UTC (15 years, 3 months ago) by alfonx
File size: 71965 byte(s)
Resizing the XMapPane keeps the mapArea (as it was in 1.2)

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26