/[schmitzm]/trunk/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.514  
changed lines
  Added in v.543

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26