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

Legend:
Removed from v.512  
changed lines
  Added in v.531

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26