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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26