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

Legend:
Removed from v.516  
changed lines
  Added in v.533

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26