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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26