/[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/org/geotools/gui/swing/JMapPane.java revision 320 by mojays, Wed Aug 26 13:49:19 2009 UTC branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java revision 524 by alfonx, Wed Nov 18 09:56:47 2009 UTC
# Line 13  Line 13 
13   *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *    Lesser General Public License for more details.   *    Lesser General Public License for more details.
15   */   */
16  package org.geotools.gui.swing;  package gtmig.org.geotools.swing;
17    
18  /**  /**
19   * <b>Xulu:<br>   * This class is 50% copied from GeoTools 2.6.x JMapPane. GeoTools is LGPL, SCHMITZM also.<br/>
20   *    Code taken from gt-2.4.5 to make some changes (marked with {@code xulu}),   * In addition to
  *    which can not be realized in a subclass:</b>  
  *    <ul>  
  *       <li>{@link #getMapArea()} declared as {@code final}<li>  
  *       <li>some variables declared as {@code protected}</li>  
  *       <li>minimal/maximal zoom scale</li>  
  *       <li>zoom in and zoom out via mouse click realized by setMapArea(..)</li>  
  *    </ul>  
  * <br><br>  
  * A simple map container that is a JPanel with a map in. provides simple  
  * pan,zoom, highlight and selection The mappane stores an image of the map  
  * (drawn from the context) and an image of the slected feature(s) to speed up  
  * rendering of the highlights. Thus the whole map is only redrawn when the bbox  
  * changes, selection is only redrawn when the selected feature changes.  
  *  
  *  
  * @author Ian Turton  
  *  
21   */   */
22    
23    import java.awt.AlphaComposite;
24  import java.awt.Color;  import java.awt.Color;
25  import java.awt.Cursor;  import java.awt.Cursor;
26    import java.awt.Font;
27  import java.awt.Graphics;  import java.awt.Graphics;
28  import java.awt.Graphics2D;  import java.awt.Graphics2D;
29  import java.awt.LayoutManager;  import java.awt.Image;
30    import java.awt.Point;
31  import java.awt.Rectangle;  import java.awt.Rectangle;
32    import java.awt.RenderingHints;
33    import java.awt.event.ActionEvent;
34    import java.awt.event.ActionListener;
35    import java.awt.event.ComponentAdapter;
36    import java.awt.event.ComponentEvent;
37  import java.awt.event.InputEvent;  import java.awt.event.InputEvent;
38  import java.awt.event.MouseEvent;  import java.awt.event.MouseEvent;
39  import java.awt.event.MouseListener;  import java.awt.event.MouseListener;
40  import java.awt.event.MouseMotionListener;  import java.awt.geom.AffineTransform;
41    import java.awt.geom.NoninvertibleTransformException;
42    import java.awt.geom.Point2D;
43  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
44  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
45  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
46  import java.io.IOException;  import java.io.IOException;
 import java.util.Date;  
47  import java.util.HashMap;  import java.util.HashMap;
48  import java.util.Map;  import java.util.Map;
49    import java.util.Vector;
50    
51    import javax.swing.JFrame;
52  import javax.swing.JPanel;  import javax.swing.JPanel;
53    import javax.swing.Timer;
54    
55  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
56  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
57  import org.geotools.filter.IllegalFilterException;  import org.geotools.geometry.jts.JTS;
58    import org.geotools.geometry.jts.ReferencedEnvelope;
59  import org.geotools.map.DefaultMapContext;  import org.geotools.map.DefaultMapContext;
60  import org.geotools.map.MapContext;  import org.geotools.map.MapContext;
61  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
62    import org.geotools.map.event.MapLayerEvent;
63  import org.geotools.map.event.MapLayerListEvent;  import org.geotools.map.event.MapLayerListEvent;
64  import org.geotools.map.event.MapLayerListListener;  import org.geotools.map.event.MapLayerListListener;
65  import org.geotools.referencing.crs.DefaultGeographicCRS;  import org.geotools.map.event.MapLayerListener;
66    import org.geotools.referencing.CRS;
67  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
68    import org.geotools.renderer.label.LabelCacheImpl;
69  import org.geotools.renderer.lite.LabelCache;  import org.geotools.renderer.lite.LabelCache;
70  import org.geotools.renderer.lite.LabelCacheDefault;  import org.geotools.swing.JMapPane;
71  import org.geotools.renderer.lite.StreamingRenderer;  import org.geotools.swing.event.MapMouseEvent;
72  import org.geotools.renderer.shape.TransitionShapefileRenderer;  import org.geotools.swing.event.MapPaneEvent;
73  import org.geotools.styling.Graphic;  import org.geotools.swing.event.MapPaneListener;
74  import org.geotools.styling.LineSymbolizer;  import org.opengis.feature.simple.SimpleFeature;
75  import org.geotools.styling.Mark;  import org.opengis.feature.simple.SimpleFeatureType;
76  import org.geotools.styling.PointSymbolizer;  import org.opengis.referencing.FactoryException;
 import org.geotools.styling.PolygonSymbolizer;  
 import org.geotools.styling.Style;  
 import org.geotools.styling.StyleBuilder;  
 import org.geotools.styling.StyleFactory;  
 import org.opengis.filter.Filter;  
 import org.opengis.filter.FilterFactory2;  
77  import org.opengis.referencing.crs.CoordinateReferenceSystem;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
78    import org.opengis.referencing.operation.MathTransform;
79    import org.opengis.referencing.operation.TransformException;
80    
81    import schmitzm.geotools.GTUtil;
82    import schmitzm.geotools.JTSUtil;
83    import schmitzm.geotools.gui.SelectableXMapPane;
84    import schmitzm.geotools.map.event.JMapPaneListener;
85    import schmitzm.geotools.map.event.MapLayerAdapter;
86  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
87    import skrueger.geotools.RenderingExecutor;
88    
 import com.sun.jini.jeri.internal.runtime.SelectionManager;  
89  import com.vividsolutions.jts.geom.Coordinate;  import com.vividsolutions.jts.geom.Coordinate;
90  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
91  import com.vividsolutions.jts.geom.Geometry;  import com.vividsolutions.jts.geom.Geometry;
 import com.vividsolutions.jts.geom.GeometryFactory;  
92    
93  public class JMapPane extends JPanel implements MouseListener,  public class XMapPane extends JPanel implements PropertyChangeListener {
94                  MouseMotionListener, HighlightChangeListener, SelectionChangeListener,          private static Logger LOGGER = Logger.getLogger(XMapPane.class);
                 PropertyChangeListener, MapLayerListListener {  
         private static Logger LOGGER = Logger.getLogger(JMapPane.class.getName());  
   
         private static final long serialVersionUID = -8647971481359690499L;  
   
         public static final int Reset = 0;  
   
         public static final int ZoomIn = 1;  
   
         public static final int ZoomOut = 2;  
   
         public static final int Pan = 3;  
   
         public static final int Select = 4;  
   
         private static final int POLYGON = 0;  
   
         private static final int LINE = 1;  
   
         private static final int POINT = 2;  
95    
96          /**          /**
97           * what renders the map           * Sets whether a layer is regarded or ignored on {@link #SELECT_TOP},
98             * {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP} actions.
99             *
100             * @param layer
101             *            a layer
102             * @param selectable
103             *            if {@code false} the layer is ignored during the upper
104             *            mentioned actions. If <code>null</code>, the default (true)
105             *            will be used.
106           */           */
107          GTRenderer renderer;          public void setMapLayerSelectable(MapLayer layer, Boolean selectable) {
108                    if (selectable == null)
109          private GTRenderer highlightRenderer, selectionRenderer;                          mapLayerSelectable.remove(layer);
110                    else
111                            mapLayerSelectable.put(layer, selectable);
112            }
113    
114          /**          /**
115           * the map context to render           * Holds a flag for each layer, whether it is regarded or ignored on
116             * {@link #SELECT_TOP}, {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP}
117             * actions.
118           */           */
119          MapContext context;          final protected HashMap<MapLayer, Boolean> mapLayerSelectable = new HashMap<MapLayer, Boolean>();
   
         private MapContext selectionContext;  
120    
121          /**          /**
122           * the area of the map to draw           * Returns whether a layer is regarded or ignored on {@link #SELECT_TOP},
123             * {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP} actions. Returns
124             * <code>true</code> if the selectability has not been defined.
125             *
126             * @param layer
127             *            a layer
128           */           */
129          // xulu.sc          public boolean isMapLayerSelectable(MapLayer layer) {
130          // Envelope mapArea;                  Boolean selectable = mapLayerSelectable.get(layer);
131          protected Envelope mapArea;                  return selectable == null ? true : selectable;
132          // xulu.ec          }
133    
134          /**          /**
135           * the size of the pane last time we drew           * Flag fuer Modus "Kartenausschnitt bewegen". Nicht fuer Window-Auswahl
136             * moeglich!
137             *
138             * @see #setState(int)
139           */           */
140          // xulu.sc          public static final int PAN = 1;
         // private Rectangle oldRect = null;  
         protected Rectangle oldRect = null;  
         // xulu.ec  
   
141          /**          /**
142           * the last map area drawn.           * Flag fuer Modus "Heran zoomen".
143             *
144             * @see #setState(int)
145             * @see #setState(int)
146           */           */
147          // xulu.sc          public static final int ZOOM_IN = 2;
         // private Envelope oldMapArea = null;  
         protected Envelope oldMapArea = null;  
         // xulu.ec  
   
148          /**          /**
149           * the base image of the map           * Flag fuer Modus "Heraus zoomen". Nicht fuer Window-Auswahl moeglich!
150             *
151             * @see #setState(int)
152           */           */
153          protected BufferedImage baseImage, panningImage;          public static final int ZOOM_OUT = 3;
         // SK: private BufferedImage baseImage, panningImage;  
   
154          /**          /**
155           * image of selection           * Flag fuer Modus
156             * "SimpleFeature-Auswahl auf dem obersten (sichtbaren) Layer".
157             *
158             * @see #setState(int)
159             * @see #setState(int)
160           */           */
161          private BufferedImage selectImage;          public static final int SELECT_TOP = 4;
   
162          /**          /**
163           * style for selected items           * Flag fuer Modus "SimpleFeature-Auswahl auf allen (sichtbaren) Layern".
164             *
165             * @see #setState(int)
166             * @see #setState(int)
167           */           */
168          private Style selectionStyle;          public static final int SELECT_ALL = 103;
   
169          /**          /**
170           * layer that selection works on           * Flag fuer Modus
171             * "Auswahl nur eines Features, das erste sichtbare von Oben".
172             *
173             * @see #setState(int)
174             * @see #setState(int)
175           */           */
176          private MapLayer selectionLayer;          public static final int SELECT_ONE_FROM_TOP = 104;
177    
178          /**          /**
179           * layer that highlight works on           * While threads are working, we call {@link XMapPane#updateFinalImage()}
180             * regularly and repaint(). This {@link Timer} is stopped when all renderers
181             * have finished.
182           */           */
183          private MapLayer highlightLayer;          final private Timer repainterTimer;
184    
185          /**          /**
186           * the object which manages highlighting           * While the {@link #bgExecuter} and {@link #localExecuter} are rendering,
187             * the {@link #repainterTimer} is regularly updating the {@link #finalImage}
188             * with previews.
189           */           */
190          private HighlightManager highlightManager;          public static final long REPEATING_REPAINT_DELAY = 500;
191            
192          /**          /**
193           * is highlighting on or off           * The initial delay in milliseconds until the {@link #finalImage} is updated the first time.  
194           */           */
195          private boolean highlight = true;          public static final long INITIAL_REPAINT_DELAY = 1000;
196    
197            protected RenderingExecutor bgExecuter;
198    
199          /**          /**
200           * a factory for filters           * the map context to render
201           */           */
202          FilterFactory2 ff;          MapContext localContext;
   
203          /**          /**
204           * a factory for geometries           * the map context to render
205           */           */
206          GeometryFactory gf = new GeometryFactory(); // FactoryFinder.getGeometryFactory(null);          MapContext bgContext;
207    
208          /**          /**
209           * the collections of features to be selected or highlighted           * the area of the map to draw
210           */           */
211          FeatureCollection selection;          protected Envelope mapArea = null;
212            /**
213             * We store the old mapArea for a moment to use it for the
214             * "quick scaled preview" in case of ZoomOut
215             **/
216            protected Envelope oldMapArea = null;
217    
218          /**          /**
219           * the collections of features to be selected or highlighted           * the size of the pane last time we drew
220           */           */
221          FeatureCollection highlightFeature;          protected Rectangle oldRect = null;
   
         private int state = ZoomIn;  
222    
223          /**          /**
224           * how far to zoom in or out           * The Renderer for the Background uses this Image. When set to null, please
225             * dispose the {@link Graphics2D}
226           */           */
227          private double zoomFactor = 2.0;          protected BufferedImage bgImage;
228    
229          Style lineHighlightStyle;          /**
230             * The Renderer for the LocalLayers uses this Image. When set to null,
231             * please dispose this {@link Graphics2D}
232             */
233            volatile protected BufferedImage localImage;
234    
235          Style pointHighlightStyle;          /**
236             * compass and icon are rendered into this image
237             */
238            // protected BufferedImage gadgetsImage;
239    
240          Style polygonHighlightStyle;          /**
241             * The default state is ZOOM_IN, hence by default the
242             * {@link #zoomMapPaneMouseListener} is also enabled.
243             **/
244            private int state = ZOOM_IN;
245    
246          Style polygonSelectionStyle;          /**
247             * Konvertiert die Maus-Koordinaten (relativ zum <code>JMapPane</code>) in
248             * Karten-Koordinaten.
249             *
250             * @param e
251             *            Maus-Ereignis
252             */
253            public static Point2D getMapCoordinatesFromEvent(MouseEvent e) {
254                    // aktuelle Geo-Position aus GeoMouseEvent ermitteln
255                    if (e != null && e instanceof MapMouseEvent)
256                            try {
257                                    return ((MapMouseEvent) e).getMapPosition().toPoint2D();
258                            } catch (Exception err) {
259                                    LOGGER
260                                                    .error(
261                                                                    "return ((GeoMouseEvent) e).getMapCoordinate(null).toPoint2D();",
262                                                                    err);
263                            }
264    
265          Style pointSelectionStyle;                  // aktuelle Geo-Position ueber Transformation des JMapPane berechnen
266                    if (e != null && e.getSource() instanceof XMapPane) {
267    
268          Style lineSelectionStyle;                          final XMapPane xMapPane = (XMapPane) e.getSource();
269    
270          boolean changed = true;                          if (!xMapPane.isWellDefined())
271                                    return null;
272    
273          LabelCache labelCache = new LabelCacheDefault();                          AffineTransform at = xMapPane.getScreenToWorld();
274                            if (at != null)
275                                    return at.transform(e.getPoint(), null);
276                            return null;
277                    }
278                    throw new IllegalArgumentException(
279                                    "MouseEvent has to be of instance MapMouseEvent or come from an XMapPane");
280            }
281    
282          // xulu.sc          /**
283          // private boolean reset = false;           * A flag indicating that the shown image is invalid and needs to be
284          protected boolean reset = false;           * re-rendered.
285          // xulu.ec           */
286            protected boolean mapImageInvalid = true;
287    
288          int startX;          protected LabelCache labelCache = new LabelCacheImpl();
289    
290          int startY;          /**
291             * If not <code>null</code>, the {@link XMapPane} will not allow to zoom/pan
292             * out of that area
293             **/
294            private Envelope maxExtend = null;
295    
296          private boolean clickable;          private Double maxZoomScale = Double.MIN_VALUE;
297            private Double minZoomScale = Double.MAX_VALUE;
298    
299          int lastX;          /**
300             * This color is used as the default background color when painting a map.
301             */
302            private Color mapBackgroundColor = Color.WHITE;
303    
304          int lastY;          /**
305             * This {@link MouseListener} is managing all zoom related tasks
306             */
307            public final ZoomXMapPaneMouseListener zoomMapPaneMouseListener = new ZoomXMapPaneMouseListener(
308                            this);
309    
310          private SelectionManager selectionManager;          /**
311          // xulu.sn           * A flag indicating, that the image size has changed and the buffered
312          private Double maxZoomScale = Double.MIN_VALUE;           * images are not big enough any more
313          private Double minZoomScale = Double.MAX_VALUE;           **/
314          // xulu.en          protected boolean paneResized = false;
315    
         // sk.sn  
316          /**          /**
317           * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses           * The flag {@link #requestStartRendering} can be set to true by events.
318           * Flag wird benutzt um nur einmal den passenden Cursor nur einmal zu           * This {@link Timer} checks the flag regularly and starts one renderer
319           * setzen.           * thread.
320           */           */
321          private boolean panning_started = false;          final private Timer startRenderThreadsTimer;
322    
323          // sk.en          private Map<Object, Object> rendererHints;
324    
325          public JMapPane() {          public Map<Object, Object> getRendererHints() {
326                  this(null, true, null, null);                  return rendererHints;
327          }          }
328    
329          /**          /**
330           * create a basic JMapPane           * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot gemacht
331             * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt.
332           *           *
333           * @param render           * @author <a href="mailto:[email protected]">Stefan Alfons
334           *            - how to draw the map           *         Kr&uuml;ger</a>
          * @param context  
          *            - the map context to display  
335           */           */
336          public JMapPane(final GTRenderer render, final MapContext context) {          @Override
337                  this(null, true, render, context);          public void print(Graphics g) {
338                    Color orig = getBackground();
339                    setBackground(Color.WHITE);
340    
341                    // wrap in try/finally so that we always restore the state
342                    try {
343                            super.print(g);
344                    } finally {
345                            setBackground(orig);
346                    }
347          }          }
348    
349          /**          /**
350           * full constructor extending JPanel           * full constructor extending JPanel
351           *           *
352             * @param rendererHints
353             *
354           * @param layout           * @param layout
355           *            - layout (probably shouldn't be set)           *            - layout (probably shouldn't be set)
356           * @param isDoubleBuffered           * @param isDoubleBuffered
357           *            - a Swing thing I don't really understand           *            - a Swing thing I don't really understand
358           * @param render           * @param render
359           *            - what to draw the map with           *            - what to draw the map with
360           * @param context           * @param localContext
361           *            - what to draw           *            - {@link MapContext} of layer to render.
362           */           */
363          public JMapPane(final LayoutManager layout, final boolean isDoubleBuffered,          public XMapPane(final MapContext localContext,
364                          final GTRenderer render, final MapContext context) {                          Map<Object, Object> rendererHints) {
365                  super(layout, isDoubleBuffered);                  super(true);
366    
367                  ff = (FilterFactory2) org.geotools.factory.CommonFactoryFinder                  setRendererHints(rendererHints);
                                 .getFilterFactory(null);  
                 setRenderer(render);  
368    
369                  setContext(context);                  setOpaque(true);
370    
371                  this.addMouseListener(this);                  setLocalContext(localContext);
                 this.addMouseMotionListener(this);  
                 setHighlightManager(new HighlightManager(highlightLayer));  
                 setSelectionManager(new SelectionManager(selectionLayer));  
                 lineHighlightStyle = setupStyle(LINE, Color.red);  
372    
373                  pointHighlightStyle = setupStyle(POINT, Color.red);                  /**
374                     * Adding the #zoomMapPaneMouseListener
375                     */
376                    this.addMouseListener(zoomMapPaneMouseListener);
377                    this.addMouseMotionListener(zoomMapPaneMouseListener);
378                    this.addMouseWheelListener(zoomMapPaneMouseListener);
379    
380                  polygonHighlightStyle = setupStyle(POLYGON, Color.red);                  /*
381                     * We use a Timer object to avoid rendering delays and flickering when
382                     * the user is drag-resizing the parent container of this map pane.
383                     *
384                     * Using a ComponentListener doesn't work because, unlike a JFrame, the
385                     * pane receives a stream of events during drag-resizing.
386                     */
387                    resizingPaintDelay = DEFAULT_RESIZING_PAINT_DELAY;
388                    resizeTimer = new Timer(resizingPaintDelay, new ActionListener() {
389    
390                  polygonSelectionStyle = setupStyle(POLYGON, Color.cyan);                          public void actionPerformed(ActionEvent e) {
391                                    paneResized = true;
392    
393                  pointSelectionStyle = setupStyle(POINT, Color.cyan);                                  if (!isWellDefined())
394                                            return;
395    
396                  lineSelectionStyle = setupStyle(LINE, Color.cyan);                                  Rectangle bounds = getVisibleRect();
                 setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));  
         }  
397    
398          /**                                  Envelope geoMapArea = tranformWindowToGeo(bounds.x, bounds.y,
399           * get the renderer                                                  bounds.x + bounds.width, bounds.y + bounds.height);
          */  
         public GTRenderer getRenderer() {  
                 return renderer;  
         }  
400    
401          public void setRenderer(final GTRenderer renderer) {                                  setMapArea(bestAllowedMapArea(geoMapArea));
402                  Map hints = new HashMap();                          }
403                                    });
404                  this.renderer = renderer;                  resizeTimer.setRepeats(false);
405                                    this.addComponentListener(new ComponentAdapter() {
406                  if (renderer instanceof StreamingRenderer || renderer instanceof TransitionShapefileRenderer) {  
407                          hints = renderer.getRendererHints();                          @Override
408                          if (hints == null) {                          public void componentResized(ComponentEvent e) {
409                                  hints = new HashMap();                                  if (bgExecuter != null)
410                          }                                          bgExecuter.cancelTask();
411                          if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {                                  if (localExecuter != null)
412                                  labelCache = (LabelCache) hints                                          localExecuter.cancelTask();
413                                                  .get(StreamingRenderer.LABEL_CACHE_KEY);                                  resizeTimer.restart();
                         } else {  
                                 hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);  
414                          }                          }
415    
416                          hints.put("memoryPreloadingEnabled", Boolean.TRUE);                  });
                           
                         renderer.setRendererHints(hints);  
                 }  
417    
418  //              this.highlightRenderer = new StreamingRenderer();                  repainterTimer = new Timer((int) REPEATING_REPAINT_DELAY,
419  //              this.selectionRenderer = new StreamingRenderer();                                  new ActionListener() {
420    
421  //              highlightRenderer.setRendererHints(hints);                                          @Override
422  //              selectionRenderer.setRendererHints(hints);                                          public void actionPerformed(ActionEvent e) {
423                                                                    updateFinalImage();
424  //              renderer.setRendererHints(hints);                                                  XMapPane.this.repaint();
425                                            }
426                                    });
427                    repainterTimer.setInitialDelay(300);
428                    repainterTimer.setRepeats(true);
429    
430                    startRenderThreadsTimer = new Timer(100, new ActionListener() {
431    
432                            @Override
433                            public void actionPerformed(ActionEvent e) {
434                                    synchronized (requestStartRendering) {
435                                            if (requestStartRendering && isWellDefined()) {
436                                                    
437                                                    if (localExecuter.isRunning()){
438                                                            localExecuter.cancelTask();
439                                                    } else {
440                                                            requestStartRendering = false;
441                                                            startRendering();
442                                                    }
443                                            }
444                                    }
445                            }
446                    });
447                    startRenderThreadsTimer.start();
448    
                 if (this.context != null) {  
                         this.renderer.setContext(this.context);  
                 }  
449          }          }
450    
451          public MapContext getContext() {          // TODO doku
452                  return context;          public XMapPane() {
453                    this(null, null);
454          }          }
455    
456          public void setContext(final MapContext context) {          /**
457                  if (this.context != null) {           * Sets the mapArea to best possibly present the given features. If only one
458                          this.context.removeMapLayerListListener(this);           * single point is given, the window is moved over the point.
459                  }           *
460             * Note: The method does not call {@link #repaint()} on the
461             * {@link SelectableXMapPane} .
462             *
463             * @param features
464             *            if <code>null</code> or size==0, the function doesn nothing.
465             */
466            public void zoomTo(
467                            FeatureCollection<SimpleFeatureType, SimpleFeature> features) {
468    
469                  this.context = context;                  CoordinateReferenceSystem mapCRS = getContext()
470                                    .getCoordinateReferenceSystem();
471                    CoordinateReferenceSystem fCRS = features.getSchema()
472                                    .getGeometryDescriptor().getCoordinateReferenceSystem();
473    
474                    double width = mapArea.getWidth();
475                    double height = mapArea.getHeight();
476                    double ratio = height / width;
477    
478                  if (context != null) {                  if (features == null || features.size() == 0) {
479                          this.context.addMapLayerListListener(this);                          // feature count == 0 Zoom to the full extend
480                  }                          return;
481                    } else if (features.size() == 1) {
482    
483                  if (renderer != null) {                          // feature count == 1 Just move the window to the point and zoom 'a
484                          renderer.setContext(this.context);                          // bit'
485                  }                          SimpleFeature singleFeature = (SimpleFeature) features.iterator()
486          }                                          .next();
487    
488                            if (((Geometry) singleFeature.getDefaultGeometry())
489                                            .getCoordinates().length > 1) {
490                                    // System.out.println("Zoomed to only pne poylgon");
491                                    // Poly
492                                    // TODO max width vs. height
493                                    width = features.getBounds().getWidth() * 3;
494                                    height = ratio * width;
495                            } else {
496                                    // System.out.println("Zoomed in a bit becasue only one point");
497                                    // width *= .9;
498                                    // height *= .9;
499                            }
500    
501          public Envelope getMapArea() {                          Coordinate centre = features.getBounds().centre();
502                  return mapArea;                          if (!mapCRS.equals(fCRS)) {
503          }                                  // only to calculations if the CRS differ
504                                    try {
505                                            MathTransform fToMap;
506                                            fToMap = CRS.findMathTransform(fCRS, mapCRS);
507                                            // centre is transformed to the mapCRS
508                                            centre = JTS.transform(centre, null, fToMap);
509                                    } catch (FactoryException e) {
510                                            LOGGER.error("Looking for a Math transform", e);
511                                    } catch (TransformException e) {
512                                            LOGGER.error("Looking for a Math transform", e);
513                                    }
514                            }
515    
516          public void setMapArea(final Envelope mapArea) {                          Coordinate newLeftBottom = new Coordinate(centre.x - width / 2.,
517                  this.mapArea = mapArea;                                          centre.y - height / 2.);
518          }                          Coordinate newTopRight = new Coordinate(centre.x + width / 2.,
519                                            centre.y + height / 2.);
520    
521          public int getState() {                          Envelope newMapArea = new Envelope(newLeftBottom, newTopRight);
                 return state;  
         }  
522    
523          public void setState(final int state) {                          setMapArea(newMapArea);
                 this.state = state;  
524    
525                  // System.out.println("State: " + state);                  } else {
526          }                          ReferencedEnvelope fBounds = features.getBounds();
527    
528          public double getZoomFactor() {                          Envelope bounds;
529                  return zoomFactor;                          if (!mapCRS.equals(fCRS)) {
530          }                                  bounds = JTSUtil.transformEnvelope(fBounds, fCRS, mapCRS);
531                            } else {
532                                    bounds = fBounds;
533                            }
534                            // BB umrechnen von Layer-CRS in Map-CRS
535    
536          public void setZoomFactor(final double zoomFactor) {                          // Expand a bit
537                  this.zoomFactor = zoomFactor;                          bounds.expandBy(bounds.getWidth() / 6., bounds.getHeight() / 6.);
538    
539                            setMapArea(bounds);
540                    }
541          }          }
542    
543          public MapLayer getSelectionLayer() {          private void setRendererHints(Map<Object, Object> rendererHints) {
544                  return selectionLayer;                  this.rendererHints = rendererHints;
545          }          }
546    
547          public void setSelectionLayer(final MapLayer selectionLayer) {          /**
548                  this.selectionLayer = selectionLayer;           * Return <code>true</code> if a CRS and a {@link #mapArea} are set and the
549                  if (selectionManager != null) {           * {@link XMapPane} is visible and has bounds set.
550                          selectionManager.setSelectionLayer(selectionLayer);           */
551            public boolean isWellDefined() {
552    
553                    try {
554    
555                            if (getContext() == null)
556                                    return false;
557                            if (getContext().getLayerCount() <= 0)
558                                    return false;
559                            if (getMapArea() == null)
560                                    return false;
561                            if (getBounds().getWidth() == 0)
562                                    return false;
563                            if (getBounds().getHeight() == 0)
564                                    return false;
565                    } catch (Exception e) {
566                            return false;
567                  }                  }
568                    return true;
569          }          }
570    
571          public boolean isHighlight() {          /**
572                  return highlight;           * Default delay (milliseconds) before the map will be redrawn when resizing
573          }           * the pane. This is to avoid flickering while drag-resizing.
574             */
575            public static final int DEFAULT_RESIZING_PAINT_DELAY = 500; // delay in
576            // milliseconds
577    
578            private Timer resizeTimer;
579            private int resizingPaintDelay;
580            /**
581             * We store the old transform for a moment to use it for the
582             * "quick scaled preview" in case of ZoomIn
583             **/
584            protected AffineTransform oldScreenToWorld = null;
585    
586            /**
587             * Manuell gesetzter statischer Cursor, unabhaengig von der aktuellen
588             * MapPane-Funktion
589             */
590            protected Cursor staticCursor = null;
591    
592          public void setHighlight(final boolean highlight) {          /**
593                  this.highlight = highlight;           * Standardmaessig wird der Cursor automatisch je nach MapPane-Aktion (Zoom,
594             * Auswahl, ...) gesetzt. Mit dieser Methode kann ein statischer Cursor
595             * gesetzt werden, der unabhaengig von der aktuellen MapPanes-Aktion
596             * beibehalten wird. Um diesen statischen Cursor wieder zu entfernen, kann
597             * {@code null} als Parameter uebergeben werden
598             *
599             * @param cursor
600             *            Cursor
601             */
602            public void setStaticCursor(Cursor cursor) {
603                    this.staticCursor = cursor;
604                    if (cursor != null)
605                            super.setCursor(cursor);
606          }          }
607    
608          public MapLayer getHighlightLayer() {          /**
609                  return highlightLayer;           * Liefert den statisch eingestellten Cursor, der unabhaengig von der
610             * eingestellten MapPane-Aktion (Zoom, Auswahl, ...) verwendet wird.
611             *
612             * @return {@code null}, wenn kein statischer Cursor verwendet, sondern der
613             *         Cursor automatisch je nach MapPane-Aktion eingestellt wird.
614             */
615            public Cursor getStaticCursor() {
616                    return this.staticCursor;
617          }          }
618    
619          public void setHighlightLayer(final MapLayer highlightLayer) {          /**
620                  this.highlightLayer = highlightLayer;           * Abhaengig von selState wird der Cursor gesetzt
621             */
622            public void updateCursor() {
623    
624                  if (highlightManager != null) {                  if (bgExecuter != null && bgExecuter.isRunning()
625                          highlightManager.setHighlightLayer(highlightLayer);                                  || localExecuter != null && localExecuter.isRunning()) {
626                            setCursor(WAIT_CURSOR);
627                            return;
628                    } else {
629                            // Allow one last rendering
630                            if (repainterTimer.isRunning()) {
631                                    repainterTimer.stop();
632                                    updateFinalImage();
633                                    repaint();
634                            }
635                  }                  }
         }  
636    
637          public HighlightManager getHighlightManager() {                  // wenn manueller Cursor gesetzt ist, dann diesen verwenden (unabhaengig
638                  return highlightManager;                  // von der aktuellen Aktion
639          }                  if (this.staticCursor != null) {
640                            setCursor(staticCursor);
641                            return;
642                    }
643                    if (getCursor() == SwingUtil.PANNING_CURSOR) {
644                            // This cursor will reset itself
645                            return;
646                    }
647    
648          public void setHighlightManager(final HighlightManager highlightManager) {                  // Je nach Aktion den Cursor umsetzen
649                  this.highlightManager = highlightManager;                  switch (state) {
650                  this.highlightManager.addHighlightChangeListener(this);                  case SELECT_TOP:
651                  this.addMouseMotionListener(this.highlightManager);                  case SELECT_ONE_FROM_TOP:
652                    case SELECT_ALL:
653                            setCursor(SwingUtil.CROSSHAIR_CURSOR);
654                            break;
655                    case ZOOM_IN:
656                            setCursor(SwingUtil.ZOOMIN_CURSOR);
657                            break;
658                    case ZOOM_OUT:
659                            setCursor(SwingUtil.ZOOMOUT_CURSOR);
660                            break;
661                    case PAN:
662                            setCursor(SwingUtil.PAN_CURSOR);
663                            break;
664                    default:
665                            setCursor(normalCursor);
666                            break;
667                    }
668          }          }
669    
670          public Style getLineHighlightStyle() {          //
671                  return lineHighlightStyle;          // /**
672          }          // * Gibt den "normalen" Cursor zurueck. Dieser kann neben dem "pointer"
673            // auch
674            // * ein Sanduhr-Wartecursor sein.
675            // *
676            // * @author <a href="mailto:[email protected]">Stefan Alfons
677            // * Kr&uuml;ger</a>
678            // */
679            // public Cursor getNormalCursor() {
680            // return normalCursor;
681            // }
682            //
683            // /**
684            // * Setzt den "normalen" Cursor. Dieser kann neben dem default "pointer"
685            // z.B.
686            // * auch ein Sanduhr-Wartecursor sein.
687            // *
688            // * @author <a href="mailto:[email protected]">Stefan Alfons
689            // * Kr&uuml;ger</a>
690            // */
691            // public void setNormalCursor(Cursor normalCursor) {
692            // this.normalCursor = normalCursor;
693            // }
694    
695          public void setLineHighlightStyle(final Style lineHighlightStyle) {          /**
696                  this.lineHighlightStyle = lineHighlightStyle;           * Berechnet die Transformation zwischen Fenster- und Karten-Koordinaten
697          }           * neu.
698             */
699            protected void resetTransforms() {
700                    if (getMapArea() == null || getWidth() == 0 || getHeight() == 0)
701                            return;
702    
703          public Style getLineSelectionStyle() {                  // We store the last Transform
704                  return lineSelectionStyle;                  oldScreenToWorld = screenToWorld;
         }  
705    
706          public void setLineSelectionStyle(final Style lineSelectionStyle) {                  this.screenToWorld = new AffineTransform(
707                  this.lineSelectionStyle = lineSelectionStyle;                  // Genauso wie die Fenster-Koordinaten, werden die Longitude-Koordinaten
708          }                                  // nach rechts (Osten) hin groesser
709                                    // --> positive Verschiebung
710                                    getMapArea().getWidth() / getWidth(),
711                                    // keine Verzerrung
712                                    0.0, 0.0,
713                                    // Waehrend die Fenster-Koordinaten nach unten hin groesser
714                                    // werden,
715                                    // werden Latitude-Koordinaten nach Sueden hin keiner
716                                    // --> negative Verschiebung
717                                    -getMapArea().getHeight() / getHeight(),
718                                    // Die Longitude-Koordinaten werden nach Osten hin groesser
719                                    // --> obere linke Ecke des Fensters hat also den Minimalwert
720                                    getMapArea().getMinX(),
721                                    // Die Latitude-Koordinaten werden nach Norden hin groesser
722                                    // --> obere linke Ecke des Fensters hat also den Maximalwert
723                                    getMapArea().getMaxY());
724    
725          public Style getPointHighlightStyle() {                  try {
726                  return pointHighlightStyle;                          this.worldToScreen = screenToWorld.createInverse();
727                    } catch (NoninvertibleTransformException e) {
728                            LOGGER.error(e);
729                    }
730          }          }
731    
732          public void setPointHighlightStyle(final Style pointHighlightStyle) {          /**
733                  this.pointHighlightStyle = pointHighlightStyle;           * Transformation zwischen Fenster-Koordinaten und Karten-Koordinaten
734          }           * (lat/lon)
735             */
736            protected AffineTransform screenToWorld = null;
737    
738          public Style getPointSelectionStyle() {          private AffineTransform worldToScreen;
                 return pointSelectionStyle;  
         }  
739    
740          public void setPointSelectionStyle(final Style pointSelectionStyle) {          /**
741                  this.pointSelectionStyle = pointSelectionStyle;           * Listens to changes of the "local" {@link MapContext} and triggers
742          }           * repaints where needed.
743             */
744            private MapLayerListListener localContextListener = new MapLayerListListener() {
745    
746          public Style getPolygonHighlightStyle() {                  @Override
747                  return polygonHighlightStyle;                  public void layerAdded(final MapLayerListEvent event) {
748          }                          event.getLayer().addMapLayerListener(localMapLayerListener);
749    
750          public void setPolygonHighlightStyle(final Style polygonHighlightStyle) {                          if (localContext.getLayers().length == 1) { // the first one
751                  this.polygonHighlightStyle = polygonHighlightStyle;                                  // if the Area of Interest is unset, the LayerBounds are used
752          }                                  if (!setMapArea(localContext.getAreaOfInterest()))
753                                            repaint();
754    
755          public Style getPolygonSelectionStyle() {                                  return;
756                  return polygonSelectionStyle;                          }
         }  
757    
758          public void setPolygonSelectionStyle(final Style polygonSelectionStyle) {                          // We need to redraw, even in case that the mapArea didn't change
759                  this.polygonSelectionStyle = polygonSelectionStyle;                          // mapImageInvalid = true;
760          }                          // repaint();
761                            requestStartRendering();
762    
763          protected void paintComponent(final Graphics g) {                  }
                 super.paintComponent(g);  
764    
765                  if ((renderer == null) || (mapArea == null)) {                  @Override
766                          return;                  public void layerRemoved(final MapLayerListEvent event) {
767                            if (event.getLayer() != null)
768                                    event.getLayer().removeMapLayerListener(localMapLayerListener);
769                            // mapImageInvalid = true;
770                            // repaint();
771                            requestStartRendering();
772                  }                  }
773    
774                  final Rectangle r = getBounds();                  @Override
775                  final Rectangle dr = new Rectangle(r.width, r.height);                  public void layerChanged(final MapLayerListEvent event) {
776                            // mapImageInvalid = true;
777                            // repaint();
778                            requestStartRendering();
779                    }
780    
781                  if (!r.equals(oldRect) || reset) {                  @Override
782                          if (!r.equals(oldRect) && (mapArea == null)) {                  public void layerMoved(final MapLayerListEvent event) {
783                                  try {                          // mapImageInvalid = true;
784                                          mapArea = context.getLayerBounds();                          // repaint();
785                                  } catch (final IOException e) {                          requestStartRendering();
786                                          // TODO Auto-generated catch block                  }
787                                          e.printStackTrace();          };
                                 }  
                         }  
788    
789                          if (mapArea != null) {          /**
790                                  /* either the viewer size has changed or we've done a reset */           * Listens to changes of the "background" {@link MapContext} and triggers
791                                  changed = true; /* note we need to redraw */           * repaints where needed.
792                                  reset = false; /* forget about the reset */           */
793                                  oldRect = r; /* store what the current size is */          private MapLayerListListener bgContextListener = new MapLayerListListener() {
794    
795                    @Override
796                    public void layerAdded(final MapLayerListEvent event) {
797                            event.getLayer().addMapLayerListener(bgMapLayerListener);
798    
799                                  mapArea = fixAspectRatio(r, mapArea);                          if (localContext.getLayers().length == 0
800                                            && bgContext.getLayers().length == 1) { // the first one and
801                                    // localContext is
802                                    // empty
803                                    if (!setMapArea(localContext.getAreaOfInterest()))
804                                            requestStartRendering();
805                                    return;
806                          }                          }
807    
808                            // We need to redraw, even in case that the mapArea didn't change
809                            // mapImageInvalid = true;
810                            // repaint();
811                            requestStartRendering();
812    
813                  }                  }
814    
815                  if (!mapArea.equals(oldMapArea)) { /* did the map extent change? */                  @Override
816                          changed = true;                  public void layerRemoved(final MapLayerListEvent event) {
817                          oldMapArea = mapArea;                          if (event.getLayer() != null)
818                          // when we tell the context that the bounds have changed WMSLayers                                  event.getLayer().removeMapLayerListener(bgMapLayerListener);
819                          // can refresh them selves                          // mapImageInvalid = true;
820                          context.setAreaOfInterest(mapArea, context                          // repaint();
821                                          .getCoordinateReferenceSystem());                          requestStartRendering();
822                  }                  }
823    
824                  if (changed ) { /* if the map changed then redraw */                  @Override
825                          changed = false;                  public void layerChanged(final MapLayerListEvent event) {
826                          baseImage = new BufferedImage(dr.width, dr.height,                          // mapImageInvalid = true;
827                                          BufferedImage.TYPE_INT_ARGB);                          // repaint();
828                            requestStartRendering();
829                    }
830    
831                          final Graphics2D ig = baseImage.createGraphics();                  @Override
832                          /* System.out.println("rendering"); */                  public void layerMoved(final MapLayerListEvent event) {
833                          if (renderer.getContext() != null)                          // mapImageInvalid = true;
834                                  renderer.setContext(context);                          // repaint();
835                          labelCache.clear(); // work around anoying labelcache bug                          requestStartRendering();
836                    }
837            };
838    
839                          // draw the map          /**
840                          renderer.paint((Graphics2D) ig, dr, mapArea);           * Listens to each layer in the local {@link MapContext} for changes and
841             * triggers repaints.
842             */
843            protected MapLayerListener localMapLayerListener = new MapLayerAdapter() {
844    
845                          // TODO nur machen, wenn panning beginnt                  @Override
846                          panningImage = new BufferedImage(dr.width, dr.height,                  public void layerShown(MapLayerEvent event) {
847                                          BufferedImage.TYPE_INT_RGB);                          // mapImageInvalid = true;
848                            // repaint();
849                            requestStartRendering();
850                    }
851    
852                    @Override
853                    public void layerHidden(MapLayerEvent event) {
854                            // mapImageInvalid = true;
855                            // repaint();
856                            requestStartRendering();
857                  }                  }
858    
859                  ((Graphics2D) g).drawImage(baseImage, 0, 0, this);                  @Override
860                    public void layerChanged(MapLayerEvent event) {
861                            // Change of SLD for example
862                            // mapImageInvalid = true;
863                            // repaint();
864                            requestStartRendering();
865                    }
866            };
867    
868                  if ((selection != null) && (selection.size() > 0)) {          /**
869                          // paint selection           * Listens to each layer in the local {@link MapContext} for changes and
870             * triggers repaints.
871             */
872            protected MapLayerListener bgMapLayerListener = new MapLayerAdapter() {
873    
874                          String type = selectionLayer.getFeatureSource().getSchema()                  @Override
875                                          .getDefaultGeometry().getType().getName();                  public void layerShown(MapLayerEvent event) {
876                          /* String type = selection.getDefaultGeometry().getGeometryType(); */                          // mapImageInvalid = true;
877                          /* System.out.println(type); */                          // repaint();
878                          if (type == null)                          requestStartRendering();
879                                  type = "polygon";                  }
880    
881                          /* String type = "point"; */                  @Override
882                    public void layerHidden(MapLayerEvent event) {
883                            // mapImageInvalid = true;
884                            // repaint();
885                            requestStartRendering();
886                    }
887    
888                          if (type.toLowerCase().endsWith("polygon")) {                  @Override
889                                  selectionStyle = polygonSelectionStyle;                  public void layerChanged(MapLayerEvent event) {
890                          } else if (type.toLowerCase().endsWith("point")) {                          // Change of SLD for example
891                                  selectionStyle = pointSelectionStyle;                          // mapImageInvalid = true;
892                          } else if (type.toLowerCase().endsWith("line")) {                          // repaint();
893                                  selectionStyle = lineSelectionStyle;                          requestStartRendering();
894                          }                  }
895            };
896    
897                          selectionContext = new DefaultMapContext(DefaultGeographicCRS.WGS84);          /**
898             * Liefert eine affine Transformation, um von den Fenster-Koordinaten in die
899             * Karten-Koordinaten (Lat/Lon) umzurechnen.
900             *
901             * @return eine Kopie der aktuellen Transformation; <code>null</code> wenn
902             *         noch keine Karte angezeigt wird
903             */
904            public AffineTransform getScreenToWorld() {
905                    if (screenToWorld == null)
906                            resetTransforms();
907                    // nur Kopie der Transformation zurueckgeben!
908                    if (screenToWorld == null)
909                            return null;
910                    return new AffineTransform(screenToWorld);
911            }
912    
913            public AffineTransform getWorldToScreenTransform() {
914                    if (worldToScreen == null) {
915                            resetTransforms();
916                    }
917                    // nur Kopie der Transformation zurueckgeben!
918                    return new AffineTransform(worldToScreen);
919            }
920    
921                          selectionContext.addLayer(selection, selectionStyle);          public MapContext getContext() {
922                          selectionRenderer.setContext(selectionContext);                  if (localContext == null) {
923                            this.localContext = new DefaultMapContext();
924                            this.localContext.addMapLayerListListener(localContextListener);
925                    }
926                    return localContext;
927            }
928    
929                          selectImage = new BufferedImage(dr.width, dr.height,          public MapContext getBgContext() {
930                                          BufferedImage.TYPE_INT_ARGB);                  return bgContext;
931            }
932    
933                          final Graphics2D ig = selectImage.createGraphics();          /**
934                          /* System.out.println("rendering selection"); */           *
935                          selectionRenderer.paint((Graphics2D) ig, dr, mapArea);           * @param context
936             */
937            public void setLocalContext(final MapContext context) {
938                    // Remove the default listener from the old context
939                    if (this.localContext != null) {
940                            this.localContext.removeMapLayerListListener(localContextListener);
941    
942                            // adding listener to all layers
943                            for (MapLayer mapLayer : localContext.getLayers()) {
944                                    mapLayer.removeMapLayerListener(localMapLayerListener);
945                            }
946                    }
947    
948                    this.localContext = context;
949    
950                          ((Graphics2D) g).drawImage(selectImage, 0, 0, this);                  if (context != null) {
951                            setMapArea(localContext.getAreaOfInterest());
952    
953                            this.localContext.addMapLayerListListener(localContextListener);
954    
955                            // adding listener to all layers
956                            for (MapLayer mapLayer : localContext.getLayers()) {
957                                    mapLayer.addMapLayerListener(localMapLayerListener);
958                            }
959                  }                  }
960    
961                  if (highlight && (highlightFeature != null)                  mapImageInvalid = true;
962                                  && (highlightFeature.size() > 0)) {                  repaint();
963                          /*          }
964                           * String type = selection.getDefaultGeometry().getGeometryType();  
965                           * System.out.println(type); if(type==null) type="polygon";          public void setBgContext(final MapContext context) {
                          */  
                         String type = highlightLayer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getType().getName();  
                         /* String type = selection.getDefaultGeometry().getGeometryType(); */  
                         // System.out.println(type);  
                         if (type == null)  
                                 type = "polygon";  
966    
967                          /* String type = "point"; */                  // Remove the default listener from the old context
968                          Style highlightStyle = null;                  if (this.bgContext != null) {
969                          if (type.toLowerCase().endsWith("polygon")) {                          this.bgContext.removeMapLayerListListener(bgContextListener);
970                                  highlightStyle = polygonHighlightStyle;  
971                          } else if (type.toLowerCase().endsWith("point")) {                          // adding listener to all layers
972                                  highlightStyle = pointHighlightStyle;                          for (MapLayer mapLayer : bgContext.getLayers()) {
973                          } else if (type.toLowerCase().endsWith("line")) {                                  mapLayer.removeMapLayerListener(bgMapLayerListener);
                                 highlightStyle = lineHighlightStyle;  
974                          }                          }
975                    }
976    
977                    this.bgContext = context;
978    
979                          final MapContext highlightContext = new DefaultMapContext(                  if (context != null) {
980                                          DefaultGeographicCRS.WGS84);                          setMapArea(bgContext.getAreaOfInterest());
981    
982                          highlightContext.addLayer(highlightFeature, highlightStyle);                          this.bgContext.addMapLayerListListener(bgContextListener);
                         highlightRenderer.setContext(highlightContext);  
983    
984                          /* System.out.println("rendering highlight"); */                          // adding listener to all layers
985                          highlightRenderer.paint((Graphics2D) g, dr, mapArea);                          for (MapLayer mapLayer : bgContext.getLayers()) {
986                                    mapLayer.addMapLayerListener(bgMapLayerListener);
987                            }
988                  }                  }
989                    mapImageInvalid = true;
990                    repaint();
991          }          }
992    
993          private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea) {          /**
994             * Returns a copy of the mapArea
995             *
996             * @return
997             */
998            public Envelope getMapArea() {
999                    if (mapArea == null) {
1000                            final Rectangle paneBounds = getBounds();
1001    
1002                  final double mapWidth = mapArea.getWidth(); /* get the extent of the map */                          try {
1003                  final double mapHeight = mapArea.getHeight();                                  mapArea = localContext.getLayerBounds();
1004                  final double scaleX = r.getWidth() / mapArea.getWidth(); /*                          } catch (final IOException e) {
1005                                                                                                                           * calculate the new                                  LOGGER.warn("context.getLayerBounds()", e);
1006                                                                                                                           * scale                          }
                                                                                                                          */  
1007    
1008                  final double scaleY = r.getHeight() / mapArea.getHeight();                          if (mapArea != null) {
1009                  double scale = 1.0; // stupid compiler!                                  /* either the viewer size has changed or we've done a reset */
1010                                    mapImageInvalid = true; /* note we need to redraw */
1011                                    oldRect = paneBounds; /* store what the current size is */
1012                                    mapArea = bestAllowedMapArea(mapArea);
1013                            }
1014                    }
1015    
1016                  if (scaleX < scaleY) { /* pick the smaller scale */                  if (mapArea == null)
1017                          scale = scaleX;                          return null;
1018                  } else {  
1019                          scale = scaleY;                  return new Envelope(mapArea);
1020            }
1021    
1022            /**
1023             * @param newMapArea
1024             * @return <code>true</code> if the mapArea has been changed and a repaint
1025             *         has been triggered.
1026             */
1027            public boolean setMapArea(final Envelope newMapArea) {
1028    
1029                    if (newMapArea == null
1030                                    || bestAllowedMapArea(newMapArea).equals(mapArea)) {
1031                            // No change.. no need to repaint
1032                            return false;
1033                  }                  }
1034    
1035                  /* calculate the difference in width and height of the new extent */                  // Testing, whether NaN or Infinity are used in the newMapArea
1036                  final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);                  if (newMapArea.isNull() || Double.isInfinite(newMapArea.getMaxX())
1037                  final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);                                  || Double.isInfinite(newMapArea.getMaxY())
1038                                    || Double.isInfinite(newMapArea.getMinX())
1039                                    || Double.isInfinite(newMapArea.getMinY())) {
1040                            // No change.. ugly new values
1041                            LOGGER.warn("setMapArea has been called with newArea = "
1042                                            + newMapArea);
1043                            return false;
1044                    }
1045    
1046                  /*                  // Testing, whether the difference if just minimal
1047                   * System.out.println("delta x " + deltaX);                  if (mapArea != null) {
1048                   * System.out.println("delta y " + deltaY);                          Envelope candNew = bestAllowedMapArea(newMapArea);
1049                   */                          double tolX = mapArea.getWidth() / 1000.;
1050                            double tolY = mapArea.getHeight() / 1000.;
1051                            if ((candNew.getMinX() - tolX < mapArea.getMinX())
1052                                            && (mapArea.getMinX() < candNew.getMinX() + tolX)
1053                                            && (candNew.getMaxX() - tolX < mapArea.getMaxX())
1054                                            && (mapArea.getMaxX() < candNew.getMaxX() + tolX)
1055    
1056                  /* create the new extent */                                          && (candNew.getMinY() - tolY < mapArea.getMinY())
1057                  final Coordinate ll = new Coordinate(mapArea.getMinX() - (deltaX / 2.0),                                          && (mapArea.getMinY() < candNew.getMinY() + tolY)
1058                                  mapArea.getMinY() - (deltaY / 2.0));                                          && (candNew.getMaxY() - tolY < mapArea.getMaxY())
1059                  final Coordinate ur = new Coordinate(mapArea.getMaxX() + (deltaX / 2.0),                                          && (mapArea.getMaxY() < candNew.getMaxY() + tolY)
                                 mapArea.getMaxY() + (deltaY / 2.0));  
1060    
1061                  return new Envelope(ll, ur);                          ) {
1062          }                                  // The two mapAreas only differ my 1/1000th.. ignore
1063    
1064          public void doSelection(final double x, final double y, final MapLayer layer) {                                  return false;
1065                            }
1066                    }
1067    
1068                  final Geometry geometry = gf.createPoint(new Coordinate(x, y));                  oldMapArea = mapArea;
1069    
1070                  // org.opengis.geometry.Geometry geometry = new Point();                  this.mapArea = bestAllowedMapArea(newMapArea);
1071    
1072                  findFeature(geometry, layer);                  if (localContext != null) {
1073                            localContext.setAreaOfInterest(mapArea, localContext
1074                                            .getCoordinateReferenceSystem());
1075                    }
1076                    if (bgContext != null) {
1077                            bgContext.setAreaOfInterest(mapArea, localContext
1078                                            .getCoordinateReferenceSystem());
1079                    }
1080                    resetTransforms();
1081                    mapImageInvalid = true;
1082                    mapAreaChanged = true;
1083                    repaint();
1084    
1085    //              LOGGER.debug("New maparea = " + mapArea);
1086                    return true;
1087            }
1088    
1089            public int getState() {
1090                    return state;
1091          }          }
1092    
1093          /**          /**
1094           * @param geometry           * Enables/Disables the ZOOM Mouse Listener. Upates the Cursor and stops the
1095           *            - a geometry to construct the filter with           * repaint Timer if
1096           * @param i           *
1097           *            - the index of the layer to search           * @param state
          * @throws IndexOutOfBoundsException  
1098           */           */
1099          private void findFeature(final Geometry geometry, final MapLayer layer)          public void setState(final int state) {
1100                          throws IndexOutOfBoundsException {                  this.state = state;
                 org.opengis.filter.spatial.BinarySpatialOperator f = null;  
1101    
1102                  if ((context == null) || (layer == null)) {                  zoomMapPaneMouseListener.setEnabled((state == ZOOM_IN
1103                          return;                                  || state == ZOOM_OUT || state == PAN));
                 }  
1104    
1105                  try {                  // Je nach Aktion den Cursor umsetzen
1106                          String name = layer.getFeatureSource().getSchema()                  updateCursor();
1107                                          .getDefaultGeometry().getLocalName();          }
1108    
1109                          if (name == "") {          /** Cursor wenn kein Mausbutton gedrueckt wird. default oder SwingUtil.PAN **/
1110                                  name = "the_geom";          protected static Cursor normalCursor = Cursor
1111                          }                          .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
1112    
1113                          try {          public static final Cursor WAIT_CURSOR = Cursor
1114                                  f = ff.contains(ff.property(name), ff.literal(geometry));                          .getPredefinedCursor(Cursor.WAIT_CURSOR);
                                 if (selectionManager != null) {  
 //                                      System.out.println("selection changed");  
                                         selectionManager.selectionChanged(this, f);  
1115    
1116                                  }          public static final int NONE = -123;
                         } catch (final IllegalFilterException e) {  
                                 // TODO Auto-generated catch block  
                                 e.printStackTrace();  
                         }  
1117    
1118                          /*          private RenderingExecutor localExecuter = new RenderingExecutor(this, 150l);
                          * // f.addLeftGeometry(ff.property(name)); //  
                          * System.out.println("looking with " + f); FeatureCollection fc =  
                          * layer.getFeatureSource().getFeatures(f);  
                          *  
                          *  
                          *  
                          * if (fcol == null) { fcol = fc;  
                          *  
                          * // here we should set the defaultgeom type } else {  
                          * fcol.addAll(fc); }  
                          */  
1119    
1120                          /*          private BufferedImage finalImage;
                          * GeometryAttributeType gat =  
                          * layer.getFeatureSource().getSchema().getDefaultGeometry();  
                          * fcol.setDefaultGeometry((Geometry)gat.createDefaultValue());  
                          */  
1121    
1122                          /*          /**
1123                           * Iterator fi = fc.iterator(); while (fi.hasNext()) { SimpleFeature feat           * A flag set it {@link #setMapArea(Envelope)} to indicated the
1124                           * = (SimpleFeature) fi.next(); System.out.println("selected " +           * {@link #paintComponent(Graphics)} method, that the image on-screen is
1125                           * feat.getAttribute("STATE_NAME")); }           * associated with {@link #oldMapArea} and may hence be scaled for a quick
1126                           */           * preview.<br/>
1127                  } catch (final IllegalFilterException e) {           * The flag is reset
1128                          // TODO Auto-generated catch block           **/
1129                          e.printStackTrace();          private boolean mapAreaChanged = false;
                 }  
                 return;  
         }  
   
         public void mouseClicked(final MouseEvent e) {  
                 if (mapArea == null) return;  
                 // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"  
                 // h:"+mapArea.getHeight());  
                 final Rectangle bounds = this.getBounds();  
                 final double x = (double) (e.getX());  
                 final double y = (double) (e.getY());  
                 final double width = mapArea.getWidth();  
                 final double height = mapArea.getHeight();  
                 // xulu.sc  
                 // double width2 = mapArea.getWidth() / 2.0;  
                 // double height2 = mapArea.getHeight() / 2.0;  
                 final double width2 = width / 2.0;  
                 final double height2 = height / 2.0;  
                 // xulu.ec  
                 final double mapX = ((x * width) / (double) bounds.width) + mapArea.getMinX();  
                 final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)  
                                 + mapArea.getMinY();  
1130    
1131                  /*          private JFrame finalImageFrame;
                  * System.out.println(""+x+"->"+mapX);  
                  * System.out.println(""+y+"->"+mapY);  
                  */  
1132    
1133                  /*          private volatile Boolean requestStartRendering = false;
1134                   * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());          private BufferedImage preFinalImage;
                  * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());  
                  */  
                 double zlevel = 1.0;  
1135    
1136                  switch (state) {          protected void paintComponent(final Graphics g) {
1137                  case Pan:                  // Maybe update the cursor
1138                          zlevel = 1.0;                  updateCursor();
1139                          // xulu.sc SK: return here.. a mouselistener is managing the PANNING  
1140                          // break;                  if (!acceptsRepaintCalls)
1141                          return;                          return;
                         // xulu.ec  
                 case ZoomIn:  
                         zlevel = zoomFactor;  
1142    
1143                          break;                  boolean paintedSomething = false;
1144    
1145                  case ZoomOut:                  if (mapImageInvalid) { /* if the map changed then redraw */
                         zlevel = 1.0 / zoomFactor;  
1146    
1147                          break;                          mapImageInvalid = false; // Reset for next round
1148    
1149                  case Select:                          // If the new mapArea and the oldMapArea intersect, we can draw some
1150                          doSelection(mapX, mapY, selectionLayer);                          // quick scaled preview to make the user feel that something is
1151                            // happening.
1152                            if (mapAreaChanged && oldMapArea != null
1153                                            && getMapArea().intersects(oldMapArea)
1154                                            & !getMapArea().equals(oldMapArea)) {
1155    
1156                                    mapAreaChanged = false;
1157    
1158                                    if (getMapArea().covers(oldMapArea)) {
1159                                            setQuickPreviewHint(ZOOM_OUT);
1160                                            paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1161                                    } else if (oldMapArea.covers(getMapArea())) {
1162                                            setQuickPreviewHint(ZOOM_IN);
1163                                            paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1164                                    }
1165    
1166                          return;                          }
1167    
1168                            if (paneResized) {
1169                                    paneResized = false;
1170                                    preFinalImage = null;
1171                                    finalImage = null;
1172                                    localImage = null;
1173                                    bgImage = null;
1174                                    // gadgetsImage = null;
1175                            }
1176    
1177                            // Start the Threads and Timers to render the image
1178                            requestStartRendering();
1179    
                 default:  
                         return;  
1180                  }                  }
1181    
1182                  final Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY                  if (!paintedSomething) {
                                 - (height2 / zlevel));  
                 final Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY  
                                 + (height2 / zlevel));  
                 // xulu.sc SK: Check for min/max scale  
                 // mapArea = new Envelope(ll, ur);  
                 final Envelope newMapArea = new Envelope(ll, ur);  
                 setMapArea(bestAllowedMapArea(newMapArea));  
                 // xulu.ec  
1183    
1184                  // sk.ec                          // TODO Should just paint the getFinalImage(). Update should be
1185                            // called by timer every 300ms, and the repaint() until all threads
1186                            // are done.
1187                            g.drawImage(getFinalImage(), 0, 0, this);
1188    
1189                            paintedSomething = true;
1190                    }
1191    
                 // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"  
                 // h:"+mapArea.getHeight());  
                 repaint();  
1192          }          }
1193    
1194          public void mouseEntered(final MouseEvent e) {          /**
1195             * Cancels all running renderers and sets the flag to start new ones. <br/>
1196             *
1197             * @see #startRenderThreadsTimer
1198             */
1199            private void requestStartRendering() {
1200                    if (bgExecuter != null)
1201                            bgExecuter.cancelTask();
1202                    if (localExecuter != null)
1203                            localExecuter.cancelTask();
1204                    requestStartRendering = true;
1205                    
1206          }          }
1207    
1208          public void mouseExited(final MouseEvent e) {          /**
1209             * Transformiert einen Fenster-Koordinaten-Bereich in Geo-Koordinaten.
1210             *
1211             * @param ox
1212             *            X-Koordinate der VON-Position
1213             * @param oy
1214             *            Y-Koordinate der VON-Position
1215             * @param px
1216             *            X-Koordinate der BIS-Position
1217             * @param py
1218             *            Y-Koordinate der BIS-Position
1219             */
1220            public Envelope tranformWindowToGeo(int ox, int oy, int px, int py) {
1221                    AffineTransform at = getScreenToWorld();
1222                    Point2D geoO = at.transform(new Point2D.Double(ox, oy), null);
1223                    Point2D geoP = at.transform(new Point2D.Double(px, py), null);
1224                    return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY());
1225          }          }
1226    
1227          public void mousePressed(final MouseEvent e) {          /**
1228                  startX = e.getX();           * Transformiert einen Geo-Koordinaten-Bereich in Fenster-Koordinaten.
1229                  startY = e.getY();           *
1230                  lastX = 0;           * @param ox
1231                  lastY = 0;           *            X-Koordinate der VON-Position
1232             * @param oy
1233             *            Y-Koordinate der VON-Position
1234             * @param px
1235             *            X-Koordinate der BIS-Position
1236             * @param py
1237             *            Y-Koordinate der BIS-Position
1238             * @param winToGeotransform
1239             *            Eine Window to Geo transform. If <code>null</code>,
1240             *            {@link #getScreenToWorld()} is used.
1241             */
1242            public Envelope tranformGeoToWindow(double ox, double oy, double px,
1243                            double py, AffineTransform winToGeotransform) {
1244                    AffineTransform at = winToGeotransform == null ? getScreenToWorld()
1245                                    : winToGeotransform;
1246                    Point2D geoO;
1247                    try {
1248                            geoO = at.inverseTransform(new Point2D.Double(ox, oy), null);
1249                            Point2D geoP = at
1250                                            .inverseTransform(new Point2D.Double(px, py), null);
1251                            return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP
1252                                            .getY());
1253                    } catch (NoninvertibleTransformException e) {
1254                            LOGGER.error(e);
1255                            return new Envelope(ox, oy, px, py);
1256                    }
1257          }          }
1258    
1259          public void mouseReleased(final MouseEvent e) {          /**
1260                  final int endX = e.getX();           * Diretly paints scaled preview into the {@link SelectableXMapPane}. Used
1261                  final int endY = e.getY();           * to give the user something to look at while we are rendering. Method
1262             * should be called after {@link #setMapArea(Envelope)} has been set to the
1263             * new mapArea and transform has been reset.<br/>
1264             * This method does nothing if the {@link #lastRenderingDuration} is smaller
1265             * then a trashhold.
1266             *
1267             * @param g
1268             *            Graphics2D to paint the preview into
1269             *
1270             * @param state
1271             *            Max be {@link #ZOOM_IN} or {@link #ZOOM_OUT}
1272             */
1273            protected boolean drawScaledPreviewImage_Zoom(Graphics2D graphics) {
1274    
1275                  processDrag(startX, startY, endX, endY, e);                  if (quickPreviewHint == 0)
1276                  lastX = 0;                          return false;
                 lastY = 0;  
1277    
1278                  /**                  graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
1279                   * Es wird nicht (mehr) gepannt!                                  RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
1280                   */                  graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1281                  panning_started = false;                                  RenderingHints.VALUE_ANTIALIAS_OFF);
1282          }                  graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
1283                                    RenderingHints.VALUE_RENDER_SPEED);
1284    
1285          public void mouseDragged(final MouseEvent e) {                  if (oldMapArea == null)
1286                  final Graphics graphics = this.getGraphics();                          return false;
                 final int x = e.getX();  
                 final int y = e.getY();  
   
                 if ((state == JMapPane.Pan)  
                                 || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {  
                         /**  
                          * SK: Der Cursor wird auf PANNING gesetzt.  
                          */  
                         if (panning_started == false) {  
                                 panning_started = true;  
                                 setCursor(SwingUtil.PANNING_CURSOR);  
                         }  
1287    
1288                          // move the image with the mouse                  Rectangle visibleArea = getVisibleRect();
                         if ((lastX > 0) && (lastY > 0)) {  
                                 final int dx = lastX - startX;  
                                 final int dy = lastY - startY;  
                                 // System.out.println("translate "+dx+","+dy);  
                                 final Graphics2D g2 = panningImage.createGraphics();  
                                 g2.setBackground(new Color(240, 240, 240)); // TODO richtige  
                                                                                                                         // farbe? am besten  
                                                                                                                         // vom L&F die  
                                                                                                                         // hintergrundfarbe  
                                                                                                                         // auslesen...  
                                   
                                 g2.clearRect(0, 0, this.getWidth(), this.getHeight());  
                                 g2.drawImage(baseImage, dx, dy, this);  
                                 graphics.drawImage(panningImage, 0, 0, this);  
                         }  
   
                         lastX = x;  
                         lastY = y;  
                 } else  
   
                 if ((state == JMapPane.ZoomIn) || (state == JMapPane.ZoomOut)) {  
   
                         graphics.setXORMode(Color.WHITE);  
   
                         if ((lastX > 0) && (lastY > 0)) {  
                                 drawRectangle(graphics);  
                         }  
   
                         // draw new box  
                         lastX = x;  
                         lastY = y;  
                         drawRectangle(graphics);  
                 } else if (state == JMapPane.Select && selectionLayer != null) {  
   
                         // construct a new bbox filter  
                         final Rectangle bounds = this.getBounds();  
   
                         final double mapWidth = mapArea.getWidth();  
                         final double mapHeight = mapArea.getHeight();  
   
                         final double x1 = ((this.startX * mapWidth) / (double) bounds.width)  
                                         + mapArea.getMinX();  
                         final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) / (double) bounds.height)  
                                         + mapArea.getMinY();  
                         final double x2 = ((x * mapWidth) / (double) bounds.width)  
                                         + mapArea.getMinX();  
                         final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double) bounds.height)  
                                         + mapArea.getMinY();  
                         final double left = Math.min(x1, x2);  
                         final double right = Math.max(x1, x2);  
                         final double bottom = Math.min(y1, y2);  
                         final double top = Math.max(y1, y2);  
   
                         String name = selectionLayer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getName();  
   
                         if (name == "") {  
                                 name = "the_geom";  
                         }  
                         final Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,  
                                         getContext().getCoordinateReferenceSystem().toString());  
                         if (selectionManager != null) {  
                                 selectionManager.selectionChanged(this, bb);  
                         }  
1289    
1290                          graphics.setXORMode(Color.green);                  // Calculate the oldMapArea in the current WindowCoordinates:
1291                    Envelope oldMapWindow = tranformGeoToWindow(oldMapArea.getMinX(),
1292                                    oldMapArea.getMinY(), oldMapArea.getMaxX(), oldMapArea
1293                                                    .getMaxY(), null);
1294    
1295                          /*                  int xx1 = (int) Math.round(oldMapWindow.getMinX());
1296                           * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }                  int yy1 = (int) Math.round(oldMapWindow.getMinY());
1297                           */                  int xx2 = (int) Math.round(oldMapWindow.getMaxX());
1298                    int yy2 = (int) Math.round(oldMapWindow.getMaxY());
1299    
1300                          // draw new box                  graphics.drawImage(getPreFinalImage(), xx1, yy1, xx2, yy2,
1301                          lastX = x;                                  (int) visibleArea.getMinX(), (int) visibleArea.getMinY(),
1302                          lastY = y;                                  (int) visibleArea.getMaxX(), (int) visibleArea.getMaxY(),
1303                          drawRectangle(graphics);                                  getMapBackgroundColor(), null);
                 }  
1304    
1305          }                  Rectangle painedArea = new Rectangle(xx1, yy1, xx2 - xx1, yy2 - yy1);
1306    
1307          // sk.cs                  SwingUtil.clearAround(graphics, painedArea, visibleArea);
         // private void processDrag(int x1, int y1, int x2, int y2) {  
         // sk.ce  
         protected void processDrag(final int x1, final int y1, final int x2,  
                         final int y2, final MouseEvent e) {  
1308    
1309                  /****                  addGadgets(graphics);
                  * If no layer is availabe we dont want a NullPointerException  
                  */  
                 if (mapArea == null)  
                         return;  
1310    
1311                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "                  quickPreviewHint = 0;
                 // + x2 + "," + y2);  
                 if ((x1 == x2) && (y1 == y2)) {  
                         if (isClickable()) {  
                                 mouseClicked(new MouseEvent(this, 0, new Date().getTime(), 0,  
                                                 x1, y1, y2, false));  
                         }  
1312    
1313                          return;                  graphics.dispose();
1314                  }                  
1315                    // Something has been drawn
1316                    return true;
1317            }
1318    
1319            final static Font waitFont = new Font("Arial", Font.BOLD, 30);
1320    
1321                  final Rectangle bounds = this.getBounds();          /**
1322             * Paints some optinal stuff into the given {@link Graphics2D}. Usually
1323             * called as the last paint on the mapImage.
1324             */
1325            private void addGadgets(Graphics2D graphics) {
1326    
1327                  final double mapWidth = mapArea.getWidth();                  if (mapImage != null)
1328                  final double mapHeight = mapArea.getHeight();                          graphics.drawImage(mapImage, getBounds().width
1329                                            - mapImage.getWidth() - 10, getBounds().height
1330                                            - mapImage.getHeight() - 10, this);
1331    
1332                  final double startX = ((x1 * mapWidth) / (double) bounds.width)                  // If still rendering, paint a gray shadow or so...
1333                                  + mapArea.getMinX();                  if (bgExecuter != null && bgExecuter.isRunning()
1334                  final double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)                                  || localExecuter != null && localExecuter.isRunning()) {
1335                                  + mapArea.getMinY();                          graphics.setColor(Color.BLACK);
                 final double endX = ((x2 * mapWidth) / (double) bounds.width)  
                                 + mapArea.getMinX();  
                 final double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)  
                                 + mapArea.getMinY();  
   
                 if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {  
                         // move the image with the mouse  
                         // calculate X offsets from start point to the end Point  
                         final double deltaX1 = endX - startX;  
   
                         // System.out.println("deltaX " + deltaX1);  
                         // new edges  
                         final double left = mapArea.getMinX() - deltaX1;  
                         final double right = mapArea.getMaxX() - deltaX1;  
   
                         // now for Y  
                         final double deltaY1 = endY - startY;  
   
                         // System.out.println("deltaY " + deltaY1);  
                         final double bottom = mapArea.getMinY() - deltaY1;  
                         final double top = mapArea.getMaxY() - deltaY1;  
                         final Coordinate ll = new Coordinate(left, bottom);  
                         final Coordinate ur = new Coordinate(right, top);  
                         // xulu.sc  
                         // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));  
                         setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur)));  
                         // xulu.ec  
                 } else if (state == JMapPane.ZoomIn) {  
   
                         // Zu kleine Flächen sollen nicht gezoomt werden.  
                         // sk.bc  
                         if ((Math.abs(x1 - x2) * Math.abs(y2 - y1)) < 150)  
                                 return;  
                         // sk.ec  
1336    
1337                          drawRectangle(this.getGraphics());                          graphics.setFont(waitFont);
1338                          // make the dragged rectangle (in map coords) the new BBOX                          graphics.drawString("Wait...", 40, 70); //i8n
1339                          final double left = Math.min(startX, endX);                          
1340                          final double right = Math.max(startX, endX);                          graphics.setColor(getMapBackgroundColor());
                         final double bottom = Math.min(startY, endY);  
                         final double top = Math.max(startY, endY);  
                         final Coordinate ll = new Coordinate(left, bottom);  
                         final Coordinate ur = new Coordinate(right, top);  
                         // xulu.sc  
   
                         // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));  
                         setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));  
   
                         // sk.sc  
 //                      {  
 //                      // SK tries to paint a preview of the zoom ;-9 aha.... well  
 //                      Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();  
 //                      graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
 //                                      RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);  
 //                      graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
 //                                      RenderingHints.VALUE_ANTIALIAS_OFF);  
 //                      graphics.setRenderingHint(RenderingHints.KEY_RENDERING,  
 //                                      RenderingHints.VALUE_RENDER_SPEED);  
 //                      graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),  
 //                                      JMapPane.this.getHeight(), x1, y1, x2, y2, null);  
 //                      }  
                         // xulu.ec  
                 } else if (state == JMapPane.ZoomOut) {  
                         drawRectangle(this.getGraphics());  
   
                         // make the dragged rectangle in screen coords the new map size?  
                         final double left = Math.min(startX, endX);  
                         final double right = Math.max(startX, endX);  
                         final double bottom = Math.min(startY, endY);  
                         final double top = Math.max(startY, endY);  
                         final double nWidth = (mapWidth * mapWidth) / (right - left);  
                         final double nHeight = (mapHeight * mapHeight) / (top - bottom);  
                         final double deltaX1 = left - mapArea.getMinX();  
                         final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;  
                         final double deltaY1 = bottom - mapArea.getMinY();  
                         final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;  
                         final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,  
                                         mapArea.getMinY() - nDeltaY1);  
                         final double deltaX2 = mapArea.getMaxX() - right;  
                         final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;  
                         final double deltaY2 = mapArea.getMaxY() - top;  
                         final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;  
                         final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,  
                                         mapArea.getMaxY() + nDeltaY2);  
                         // xulu.sc  
                         // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));  
                         setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));  
   
                         // xulu.ec  
                 } else if (state == JMapPane.Select && selectionLayer != null) {  
                         final double left = Math.min(startX, endX);  
                         final double right = Math.max(startX, endX);  
                         final double bottom = Math.min(startY, endY);  
                         final double top = Math.max(startY, endY);  
   
                         String name = selectionLayer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getLocalName();  
   
                         if (name == "") {  
                                 name = "the_geom";  
                         }  
                         final Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,  
                                         getContext().getCoordinateReferenceSystem().toString());  
                         // System.out.println(bb.toString());  
                         if (selectionManager != null) {  
                                 selectionManager.selectionChanged(this, bb);  
                         }  
                         /*  
                          * FeatureCollection fc; selection = null; try { fc =  
                          * selectionLayer.getFeatureSource().getFeatures(bb); selection =  
                          * fc; } catch (IOException e) { e.printStackTrace(); }  
                          */  
1341                  }                  }
1342    
                 // xulu.so  
                 // setMapArea(mapArea);  
                 // xulu.eo  
                 repaint();  
         }  
   
         private boolean isClickable() {  
                 return clickable;  
1343          }          }
1344    
1345          private org.geotools.styling.Style setupStyle(final int type, final Color color) {          /**
1346                  final StyleFactory sf = org.geotools.factory.CommonFactoryFinder           * Accumulates all three images
1347                                  .getStyleFactory(null);           *
1348                  final StyleBuilder sb = new StyleBuilder();           * @return
1349             */
1350                  org.geotools.styling.Style s = sf.createStyle();          synchronized protected BufferedImage updateFinalImage() {
                 s.setTitle("selection");  
1351    
1352                  // TODO parameterise the color                  final Graphics2D finalG = (Graphics2D) getFinalImage().getGraphics();
1353                  final PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);                  finalG.setBackground(getMapBackgroundColor());
                 ps.setStroke(sb.createStroke(color));  
1354    
1355                  final LineSymbolizer ls = sb.createLineSymbolizer(color);                  // Render the two map images first, into the preFinalImage
1356                  final Graphic h = sb.createGraphic();                  if (bgExecuter != null)
1357                  h.setMarks(new Mark[] { sb.createMark("square", color) });                  {
1358                            final Graphics2D preFinalG = (Graphics2D) getPreFinalImage()
1359                                            .getGraphics();
1360                            preFinalG.setBackground(getMapBackgroundColor());
1361    
1362                  final PointSymbolizer pts = sb.createPointSymbolizer(h);                          preFinalG.drawImage(getBgImage(), 0, 0, getMapBackgroundColor(),
1363                                            null);
1364                            
1365                            // // Draw the local layers image
1366                            preFinalG.drawImage(getLocalImage(), 0, 0, null);
1367                            preFinalG.dispose();
1368                    } else {
1369                            preFinalImage = getLocalImage();
1370                    }
1371    
1372                  // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});                  finalG.drawImage(getPreFinalImage(), imageOrigin.x, imageOrigin.y,
1373                  switch (type) {                                  getMapBackgroundColor(), null);
                 case POLYGON:  
                         s = sb.createStyle(ps);  
1374    
1375                          break;                  final int finalImageHeight = getFinalImage().getHeight(null);
1376                    final int finalImageWidth = getFinalImage().getWidth(null);
1377    
1378                  case POINT:                  Rectangle painedArea = new Rectangle(imageOrigin.x, imageOrigin.y,
1379                          s = sb.createStyle(pts);                                  finalImageWidth, finalImageHeight);
1380                    SwingUtil.clearAround(finalG, painedArea, getVisibleRect());
1381    
1382                          break;                  addGadgets(finalG);
1383    
1384                  case LINE:                  finalG.dispose();
                         s = sb.createStyle(ls);  
                 }  
1385    
1386                  return s;                  return finalImage;
1387          }          }
1388    
1389          public void highlightChanged(final HighlightChangedEvent e) {          private Image getFinalImage() {
                 final org.opengis.filter.Filter f = e.getFilter();  
1390    
1391                  try {                  if (finalImage == null) {
1392                          highlightFeature = highlightLayer.getFeatureSource().getFeatures(f);                          finalImage = null;
1393                  } catch (final IOException e1) {                          Rectangle curPaintArea = getVisibleRect();
1394                          // TODO Auto-generated catch block                          finalImage = new BufferedImage(curPaintArea.width,
1395                          e1.printStackTrace();                                          curPaintArea.height, BufferedImage.TYPE_INT_RGB);
                 }  
1396    
1397                  repaint();                          requestStartRendering();
1398                    }
1399                    return finalImage;
1400          }          }
1401    
1402          public void propertyChange(final PropertyChangeEvent evt) {          private Image getPreFinalImage() {
1403                  final String prop = evt.getPropertyName();                  if (preFinalImage == null) {
1404                            preFinalImage = null;
1405                            Rectangle curPaintArea = getVisibleRect();
1406    
1407                  if (prop.equalsIgnoreCase("crs")) {                          preFinalImage = new BufferedImage(curPaintArea.width,
1408                          context.setAreaOfInterest(context.getAreaOfInterest(),                                          curPaintArea.height, BufferedImage.TYPE_INT_RGB);
1409                                          (CoordinateReferenceSystem) evt.getNewValue());  
1410                            requestStartRendering();
1411                  }                  }
1412                    return preFinalImage;
1413          }          }
1414    
1415          public boolean isReset() {          /**
1416                  return reset;           * While dragging, the {@link #updateFinalImage()} method is translating the
1417          }           * cached images while setting it together.
1418             **/
1419            Point imageOrigin = new Point(0, 0);
1420    
1421          public void setReset(final boolean reset) {          /**
1422                  this.reset = reset;           * Starts rendering on one or two threads
1423          }           */
1424            private void startRendering() {
1425    
1426          public void layerAdded(final MapLayerListEvent event) {                  if (!isWellDefined())
1427                  changed = true;                          return;
1428    
1429                  if (context.getLayers().length == 1) { // the first one                  if (bgExecuter != null){
1430                            // Stop all renderers
1431                            bgExecuter.cancelTask();
1432                    }
1433    
1434                          try {                  if (localExecuter != null){
1435                                  // xulu.sc                          localExecuter.cancelTask();
1436                                  // mapArea = context.getLayerBounds();                  }
1437                                  mapArea = context.getAreaOfInterest();          
1438                                  if (mapArea == null)                  Rectangle curPaintArea = getVisibleRect();
1439                                          mapArea = context.getLayerBounds();  
1440                                  // xulu.ec                  labelCache.clear();
1441                          } catch (final IOException e) {  
1442                                  // TODO Auto-generated catch block                  /**
1443                                  e.printStackTrace();                   * We have to set new renderer
1444                          }                   */
1445    
1446                          reset = true;                  if (getBgContext() != null) {
1447                            bgExecuter = new RenderingExecutor(this, 333l);
1448                            LOGGER.debug("starting bg renderer:");
1449                            // /* System.out.println("rendering"); */
1450                            final GTRenderer createGTRenderer = GTUtil.createGTRenderer(
1451                                            bgContext, getRendererHints());
1452                            createGTRenderer.setJava2DHints(getJava2dHints());
1453                            bgExecuter.submit(getBgContext().getAreaOfInterest(), curPaintArea,
1454                                            (Graphics2D) getBgImage().getGraphics(), createGTRenderer);
1455                  }                  }
1456    
1457                  repaint();                  if (getContext() != null) {
1458          }  //                      localExecuter = new RenderingExecutor(this, 150l);
1459                            LOGGER.debug("starting local renderer:");
1460                            final GTRenderer createGTRenderer = GTUtil.createGTRenderer(
1461                                            localContext, getRendererHints());
1462                            createGTRenderer.setJava2DHints(getJava2dHints());
1463                            localExecuter.submit(getContext().getAreaOfInterest(),
1464                                            curPaintArea, (Graphics2D) getLocalImage().getGraphics(),
1465                                            createGTRenderer);
1466                    }
1467    
1468          public void layerRemoved(final MapLayerListEvent event) {                  updateCursor();
                 changed = true;  
                 repaint();  
         }  
1469    
         public void layerChanged(final MapLayerListEvent event) {  
                 changed = true;  
                 // System.out.println("layer changed - repaint");  
                 repaint();  
1470          }          }
1471    
1472          public void layerMoved(final MapLayerListEvent event) {          /**
1473                  changed = true;           * Lazyly initializes a {@link BufferedImage} for the background renderer.
1474                  repaint();           */
1475          }          private BufferedImage getBgImage() {
1476    
1477          protected void drawRectangle(final Graphics graphics) {                  if (bgImage == null) {
1478                  // undraw last box/draw new box                          Rectangle curPaintArea = getVisibleRect();
1479                  final int left = Math.min(startX, lastX);  
1480                  final int right = Math.max(startX, lastX);                          bgImage = new BufferedImage(curPaintArea.width + 1,
1481                  final int top = Math.max(startY, lastY);                                          curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);
1482                  final int bottom = Math.min(startY, lastY);                  }
1483                  final int width = right - left;  
1484                  final int height = top - bottom;                  return bgImage;
                 // System.out.println("drawing rect("+left+","+bottom+","+ width+","+  
                 // height+")");  
                 graphics.drawRect(left, bottom, width, height);  
1485          }          }
1486    
1487            /** An (transparent) image to paint over the map in the lower right corner **/
1488            private BufferedImage mapImage = null;
1489    
1490            private boolean acceptsRepaintCalls = true;
1491    
1492          /**          /**
1493           * if clickable is set to true then a single click on the map pane will zoom           * Get the BufferedImage to use as a flaoting icon in the lower right
1494           * or pan the map.           * corner.
1495           *           *
1496           * @param clickable           * @return <code>null</code> if the feature is deactivated.
1497           */           */
1498          public void setClickable(final boolean clickable) {          public BufferedImage getMapImage() {
1499                  this.clickable = clickable;                  return mapImage;
1500          }          }
1501    
1502          public void mouseMoved(final MouseEvent e) {          /**
1503             * Set the BufferedImage to use as a flaoting icon in the lower right corner
1504             *
1505             * @param mapImageIcon
1506             *            <code>null</code> is allowed and deactivates this icon.
1507             */
1508            public void setMapImage(BufferedImage mapImage) {
1509                    this.mapImage = mapImage;
1510          }          }
1511    
1512          public FeatureCollection getSelection() {          /**
1513                  return selection;           * Lazyly initializes a {@link BufferedImage} for the background renderer.
1514             */
1515            private BufferedImage getLocalImage() {
1516    
1517                    if (localImage == null) {
1518    
1519                            Rectangle curPaintArea = getVisibleRect();
1520    
1521                            localImage = new BufferedImage(curPaintArea.width + 1,
1522                                            curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);
1523                    }
1524    
1525                    return localImage;
1526          }          }
1527    
1528          public void setSelection(final FeatureCollection selection) {  
1529                  this.selection = selection;          /**
1530             * Called by the {@linkplain XMapPane.RenderingTask} when rendering has been
1531             * completed Publishes a {@linkplain MapPaneEvent} of type {@code
1532             * MapPaneEvent.Type.RENDERING_STOPPED} to listeners.
1533             *
1534             * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1535             */
1536            public void onRenderingCompleted() {
1537                    repainterTimer.stop();
1538                    updateFinalImage();
1539                  repaint();                  repaint();
1540          }          }
1541    
1542          /*          /**
1543           * (non-Javadoc)           * Called by the {@linkplain XMapPane.RenderingTask} when rendering was
1544             * cancelled. Publishes a {@linkplain MapPaneEvent} of type {@code
1545             * MapPaneEvent.Type.RENDERING_STOPPED} to listeners.
1546           *           *
1547           * @see           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
          * org.geotools.gui.swing.event.SelectionChangeListener#selectionChanged  
          * (org.geotools.gui.swing.event.SelectionChangedEvent)  
1548           */           */
1549          public void selectionChanged(final SelectionChangedEvent e) {          public void onRenderingCancelled() {
1550                    repainterTimer.stop();
1551                  try {                  LOGGER.debug("Rendering cancelled");
                         selection = selectionLayer.getFeatureSource().getFeatures(  
                                         e.getFilter());  
                         repaint();  
                 } catch (final IOException e1) {  
                         e1.printStackTrace();  
                 }  
1552          }          }
1553    
1554          public SelectionManager getSelectionManager() {          /**
1555                  return selectionManager;           * Called by the {@linkplain XMapPane.RenderingTask} when rendering failed.
1556             * Publishes a {@linkplain MapPaneEvent} of type {@code
1557             * MapPaneEvent.Type.RENDERING_STOPPED} to listeners.
1558             *
1559             * @param renderingError
1560             *            The error that occured during rendering
1561             *
1562             * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1563             */
1564            public void onRenderingFailed(Exception renderingError) {
1565                    repainterTimer.stop();
1566                    LOGGER.warn("Rendering failed", renderingError);
1567                    updateFinalImage();
1568                    repaint();
1569    
1570          }          }
1571    
         public void setSelectionManager(final SelectionManager selectionManager) {  
                 this.selectionManager = selectionManager;  
                 this.selectionManager.addSelectionChangeListener(this);  
1572    
1573            @Override
1574            public void propertyChange(final PropertyChangeEvent evt) {
1575                    final String prop = evt.getPropertyName();
1576    
1577                    if (prop.equalsIgnoreCase("crs")) {
1578                            localContext.setAreaOfInterest(localContext.getAreaOfInterest(),
1579                                            (CoordinateReferenceSystem) evt.getNewValue());
1580                    }
1581          }          }
1582    
         // xulu.sn  
1583          /**          /**
1584           * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste           * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
1585           * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten           * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
# Line 1273  public class JMapPane extends JPanel imp Line 1595  public class JMapPane extends JPanel imp
1595                  if (getWidth() == 0)                  if (getWidth() == 0)
1596                          return env;                          return env;
1597                  if (env == null)                  if (env == null)
1598                          return env;                          return null;
1599    
1600                    Envelope newArea = null;
1601    
1602                  /**                  /**
1603                   * Correct the aspect Ratio before we check the rest. Otherwise we might                   * Correct the aspect Ratio before we check the rest. Otherwise we might
1604                   * easily fail.                   * easily fail. We allow to grow here, because we don't check against
1605                     * the maxExtend
1606                   */                   */
1607                  env = fixAspectRatio(this.getBounds(), env);                  Rectangle curPaintArea = getVisibleRect();
1608    
1609                    env = JTSUtil.fixAspectRatio(curPaintArea, env, true);
1610    
1611                  final double scale = env.getWidth() / getWidth();                  final double scale = env.getWidth() / getWidth();
1612                  final double centerX = env.getMinX() + env.getWidth() / 2.;                  final double centerX = env.getMinX() + env.getWidth() / 2.;
1613                  final double centerY = env.getMinY() + env.getHeight() / 2.;                  final double centerY = env.getMinY() + env.getHeight() / 2.;
1614                  double newWidth2;                  double newWidth2 = 0;
1615                  double newHeight2;                  double newHeight2 = 0;
1616                  if (scale < getMaxZoomScale()) {                  if (scale < getMaxZoomScale()) {
1617                          // ****************************************************************************                          // ****************************************************************************
1618                          // Wir zoomen weiter rein als erlaubt => Anpassen des envelope                          // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
# Line 1302  public class JMapPane extends JPanel imp Line 1629  public class JMapPane extends JPanel imp
1629                          // ****************************************************************************                          // ****************************************************************************
1630                          // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen                          // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
1631                          // ****************************************************************************                          // ****************************************************************************
1632                          return env;                          newArea = env;
1633                    }
1634    
1635                    if (newArea == null) {
1636    
1637                            final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
1638                                            - newHeight2);
1639                            final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
1640                                            + newHeight2);
1641    
1642                            newArea = new Envelope(ll, ur);
1643                  }                  }
1644    
1645                  final Coordinate ll = new Coordinate(centerX - newWidth2, centerY                  Envelope maxAllowedExtend = getMaxExtend();
1646                                  - newHeight2);                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
1647                  final Coordinate ur = new Coordinate(centerX + newWidth2, centerY                                  && newArea != null && !newArea.isNull()
1648                                  + newHeight2);                                  && !Double.isNaN(newArea.getMinX())
1649                                    && !Double.isNaN(newArea.getMaxX())
1650                                    && !Double.isNaN(newArea.getMinY())
1651                                    && !Double.isNaN(newArea.getMaxY())) {
1652                            /*
1653                             * If a maxExtend is set, we have to honour that...
1654                             */
1655    
1656                            // Exceeds top? Move down and maybe cut
1657                            if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
1658                                    double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
1659                                    // LOGGER.debug("Moving area down by " + divY);
1660    
1661                                    newArea = new Envelope(new Coordinate(newArea.getMinX(),
1662                                                    newArea.getMinY() - divY), new Coordinate(newArea
1663                                                    .getMaxX(), newArea.getMaxY() - divY));
1664    
1665                                    if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
1666                                            // LOGGER.debug("Now it exeeds the bottom border.. cut!");
1667                                            // And cut the bottom if it moved out of the area
1668                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
1669                                                            maxAllowedExtend.getMinY()), new Coordinate(newArea
1670                                                            .getMaxX(), newArea.getMaxY()));
1671    
1672                                            // LOGGER.debug("and fix aspect ratio");
1673    
1674                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
1675                                                            false);
1676                                    }
1677                            }
1678    
1679                            // Exceeds bottom? Move up and maybe cut
1680                            if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
1681                                    double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
1682                                    // LOGGER.debug("Moving area up by " + divY);
1683    
1684                                    newArea = new Envelope(new Coordinate(newArea.getMinX(),
1685                                                    newArea.getMinY() - divY), new Coordinate(newArea
1686                                                    .getMaxX(), newArea.getMaxY() - divY));
1687    
1688                                    if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
1689                                            // LOGGER.debug("Now it exeeds the top border.. cut!");
1690                                            // And cut the bottom if it moved out of the area
1691                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
1692                                                            newArea.getMinY()), new Coordinate(newArea
1693                                                            .getMaxX(), maxAllowedExtend.getMaxY()));
1694    
1695                                            // LOGGER.debug("and fix aspect ratio");
1696    
1697                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
1698                                                            false);
1699                                    }
1700                            }
1701    
1702                            // Exceeds to the right? move and maybe cut
1703                            if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1704    
1705                                    // Move left..
1706                                    double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
1707                                    // LOGGER.debug("Moving area left by " + divX);
1708    
1709                                    newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1710                                                    newArea.getMinY()), new Coordinate(newArea.getMaxX()
1711                                                    - divX, newArea.getMaxY()));
1712    
1713                                    if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1714                                            // LOGGER.debug("Now it exeeds the left border.. cut!");
1715                                            // And cut the left if it moved out of the area
1716                                            newArea = new Envelope(new Coordinate(maxAllowedExtend
1717                                                            .getMinX(), newArea.getMinY()), new Coordinate(
1718                                                            newArea.getMaxX(), newArea.getMaxY()));
1719    
1720                  return new Envelope(ll, ur);                                          // LOGGER.debug("and fix aspect ratio");
1721    
1722                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
1723                                                            false);
1724                                    }
1725                            }
1726    
1727                            // Exceeds to the left? move and maybe cut
1728                            if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1729    
1730                                    // Move right..
1731                                    double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
1732                                    // LOGGER.debug("Moving area right by " + divX);
1733    
1734                                    newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1735                                                    newArea.getMinY()), new Coordinate(newArea.getMaxX()
1736                                                    - divX, newArea.getMaxY()));
1737    
1738                                    if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1739                                            // LOGGER.debug("Now it exeeds the right border.. cut!");
1740                                            // And cut the left if it moved out of the area
1741                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
1742                                                            newArea.getMinY()), new Coordinate(maxAllowedExtend
1743                                                            .getMaxX(), newArea.getMaxY()));
1744    
1745                                            // LOGGER.debug("and fix aspect ratio");
1746    
1747                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
1748                                                            false);
1749                                    }
1750                            }
1751                    }
1752    
1753                    return newArea;
1754          }          }
1755    
1756          /**          /**
# Line 1337  public class JMapPane extends JPanel imp Line 1777  public class JMapPane extends JPanel imp
1777    
1778          /**          /**
1779           * Set the maximum allowed zoom scale. This is the smaller number value of           * Set the maximum allowed zoom scale. This is the smaller number value of
1780           * the two.           * the two. If <code>null</code> is passed, Double.MINVALUE are used which
1781             * mean there is no restriction.
1782           *           *
1783           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
1784           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
1785           */           */
1786          public void setMaxZoomScale(final Double maxZoomScale) {          public void setMaxZoomScale(final Double maxZoomScale) {
1787                  // System.out.println("setting max scale to "+maxZoomScale);                  this.maxZoomScale = maxZoomScale == null ? Double.MIN_VALUE
1788                  this.maxZoomScale = maxZoomScale;                                  : maxZoomScale;
1789          }          }
1790    
1791          /**          /**
1792           * Set the minimum (nearest) allowed zoom scale. This is the bigger number           * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1793           * value of the two.           * value of the two. If <code>null</code> is passed, Double.MAXVALUE are
1794             * used which mean there is no restriction.
1795             *
1796           *           *
1797           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
1798           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
1799           */           */
1800          public void setMinZoomScale(final Double minZoomScale) {          public void setMinZoomScale(final Double minZoomScale) {
1801                  this.minZoomScale = minZoomScale;                  this.minZoomScale = minZoomScale == null ? Double.MAX_VALUE
1802                                    : minZoomScale;
1803            }
1804    
1805            /**
1806             * Defines an evelope of the viwable area. The JMapPane will never show
1807             * anything outside of this extend.
1808             *
1809             * @param maxExtend
1810             *            <code>null</code> to not have this restriction.
1811             */
1812            public void setMaxExtend(Envelope maxExtend) {
1813                    this.maxExtend = maxExtend;
1814            }
1815    
1816            /**
1817             * Returns the evelope of the viewable area. The JMapPane will never show
1818             * anything outside of this extend. If this has been set to
1819             * <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return
1820             * quickly the context's bounds. It it takes to long to determine the
1821             * context bounds, <code>null</code> is returned.
1822             *
1823             * @param maxExtend
1824             *            <code>null</code> to not have this restriction.
1825             */
1826    
1827            public Envelope getMaxExtend() {
1828                    if (maxExtend == null) {
1829                            final ReferencedEnvelope layerBounds = GTUtil
1830                                            .getVisibleLayoutBounds(localContext);
1831                            if (layerBounds == null) {
1832                                    // TODO Last fallback could be the CRS valid area
1833                                    return null;
1834                            }
1835    
1836                            // Kartenbereich um 10% vergroessern
1837                            return JTSUtil.fixAspectRatio(this.getBounds(), JTSUtil
1838                                            .expandEnvelope(layerBounds, 0.1), true);
1839                    }
1840                    return maxExtend;
1841            }
1842    
1843            /**
1844             * Set the background color of the map.
1845             *
1846             * @param if <code>null</code>, white is used.
1847             */
1848            public void setMapBackgroundColor(Color bgColor) {
1849                    if (bgColor == null)
1850                            bgColor = Color.WHITE;
1851                    this.mapBackgroundColor = bgColor;
1852            }
1853    
1854            /**
1855             * Returns the background {@link Color} of the map pane. Default is white.
1856             **/
1857            public Color getMapBackgroundColor() {
1858                    return mapBackgroundColor;
1859            }
1860    
1861            /**
1862             *
1863             * @param b
1864             */
1865            public void setPainting(boolean b) {
1866                    acceptsRepaintCalls = b;
1867            }
1868    
1869            /**
1870             * Fuegt der Map einen Listener hinzu.
1871             *
1872             * @param l
1873             *            neuer Listener
1874             */
1875            public void addMapPaneListener(JMapPaneListener l) {
1876                    mapPaneListeners.add(l);
1877            }
1878    
1879            /**
1880             * Liste der angeschlossenen Listener, die auf Aktionen des MapPanes
1881             * lauschen.
1882             */
1883            protected Vector<JMapPaneListener> mapPaneListeners = new Vector<JMapPaneListener>();
1884    
1885            /**
1886             * A flag indicating if dispose() was already called. If true, then further
1887             * use of this {@link SelectableXMapPane} is undefined.
1888             */
1889            private boolean disposed = false;
1890    
1891            /**
1892             * Entfernt einen Listener von der Map.
1893             *
1894             * @param l
1895             *            zu entfernender Listener
1896             */
1897            public void removeMapPaneListener(JMapPaneListener l) {
1898                    mapPaneListeners.remove(l);
1899            }
1900    
1901            /** Stored the time used for the last real rendering in ms. **/
1902            private long lastRenderingDuration = Long.MAX_VALUE;
1903    
1904            // if null, no quick preview will be shown
1905            private int quickPreviewHint = 0;
1906    
1907            /**
1908             * For every rendering thread started,
1909             * {@link GTUtil#createGTRenderer(MapContext)} is called to create a new
1910             * renderer. These Java2D rendering hints are passed to the
1911             * {@link GTRenderer}. The java2dHints are the same for all renderers (bg
1912             * and local).
1913             */
1914            private RenderingHints java2dHints;
1915    
1916            /**
1917             * Returns in milli seconds the time the last rending of the
1918             * {@link SelectableXMapPane} took. #Long.MAX_VALUE if the JMapPane has not
1919             * been rendered yet.
1920             */
1921            public long getLastRenderingDuration() {
1922                    return lastRenderingDuration;
1923            }
1924    
1925            /**
1926             * Should be called when the {@link JMapPane} is not needed no more to help
1927             * the GarbageCollector
1928             *
1929             * Removes all {@link JMapPaneListener}s that are registered
1930             *
1931             * @author <a href="mailto:[email protected]">Stefan Alfons
1932             *         Kr&uuml;ger</a>
1933             */
1934            public void dispose() {
1935                    if (isDisposed())
1936                            return;
1937                    
1938                    
1939                    setPainting(false);
1940    
1941                    disposed = true;
1942    
1943                    if (bgExecuter != null) {
1944                            bgExecuter.cancelTask();
1945                            bgExecuter.dispose();
1946                    }
1947    
1948                    if (localExecuter != null) {
1949                            localExecuter.cancelTask();
1950                            localExecuter.dispose();
1951                    }
1952    
1953                    startRenderThreadsTimer.stop();
1954    
1955                    if (bgImage != null)
1956                            bgImage.flush();
1957                    if (localImage != null)
1958                            localImage.flush();
1959                    if (finalImage != null)
1960                            finalImage.flush();
1961                    if (preFinalImage != null)
1962                            preFinalImage.flush();
1963    
1964                    // Alle mapPaneListener entfernen
1965                    mapPaneListeners.clear();
1966    
1967                    removeMouseMotionListener(zoomMapPaneMouseListener);
1968                    removeMouseListener(zoomMapPaneMouseListener);
1969    
1970                    if (localContext != null)
1971                            getContext().clearLayerList();
1972                    if (bgContext != null)
1973                            getBgContext().clearLayerList();
1974    
1975                    removeAll();
1976            }
1977    
1978            /**
1979             * A flag indicating if dispose() has already been called. If true, then
1980             * further use of this {@link SelectableXMapPane} is undefined.
1981             */
1982            private boolean isDisposed() {
1983                    return disposed;
1984            }
1985    
1986            public void setQuickPreviewHint(int quickPreviewHint) {
1987                    this.quickPreviewHint = quickPreviewHint;
1988    
1989            }
1990    
1991            public void setJava2dHints(RenderingHints java2dHints) {
1992                    this.java2dHints = java2dHints;
1993            }
1994    
1995            public RenderingHints getJava2dHints() {
1996                    return java2dHints;
1997            }
1998    
1999            /**
2000             * Zooms towards a point.
2001             *
2002             * @param center
2003             *            position in window coordinates
2004             * @param zoomFaktor
2005             *            > 1 for zoom in, < 1 for zoom out. Default is 1.33.
2006             */
2007            public void zoomTo(Point center, Double zoomFaktor) {
2008                    if (zoomFaktor == null || zoomFaktor == 0.)
2009                            zoomFaktor = 2.;
2010    
2011                    Point2D gcenter = getScreenToWorld().transform(center, null);
2012                    center = null;
2013    
2014                    if (Double.isNaN(gcenter.getX()) || Double.isNaN(gcenter.getY())
2015                                    || Double.isInfinite(gcenter.getX())
2016                                    || Double.isInfinite(gcenter.getY())
2017    
2018                    ) {
2019                            // Not inside valid CRS area! cancel
2020                            return;
2021                    }
2022    
2023                    final Envelope mapArea = getMapArea();
2024    
2025                    Envelope newMapArea = new Envelope(mapArea);
2026                    newMapArea.expandBy((mapArea.getWidth() * zoomFaktor - mapArea
2027                                    .getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea
2028                                    .getHeight()) / 2.);
2029    
2030                    // Move the newMapArea above the new center
2031                    newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter
2032                                    .getY()
2033                                    - mapArea.centre().y);
2034    
2035                    setMapArea(newMapArea);
2036            }
2037    
2038            /**
2039             * Zooms towards a point.
2040             *
2041             * @param center
2042             *            position in window coordinates
2043             * @param zoomFactor
2044             *            > 1 for zoom in, < 1 for zoom out. Default is 1.33
2045             */
2046            public void zoomTo(Point center) {
2047                    zoomTo(center, null);
2048            }
2049    
2050            public void mouseDragged(Point startPos, Point lastPos, MouseEvent event) {
2051    
2052                    if ((getState() == XMapPane.PAN)
2053                                    || ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
2054    
2055                            if (getCursor() != SwingUtil.PANNING_CURSOR) {
2056                                    setCursor(SwingUtil.PANNING_CURSOR);
2057    
2058                                    // While panning, we deactivate the rendering. So the tasts are
2059                                    // ready to start when the panning os done.
2060                                    if (bgExecuter != null)
2061                                            bgExecuter.cancelTask();
2062                                    if (localExecuter != null)
2063                                            localExecuter.cancelTask();
2064                            }
2065    
2066                            if (lastPos.x > 0 && lastPos.y > 0) {
2067                                    final int dx = event.getX() - lastPos.x;
2068                                    final int dy = event.getY() - lastPos.y;
2069    
2070                                    // TODO Stop dragging when the drag would not be valid...
2071                                    // boolean dragValid = true;
2072                                    // // check if this panning results in a valid mapArea
2073                                    // {
2074                                    // Rectangle winBounds = xMapPane.getBounds();
2075                                    // winBounds.translate(xMapPane.imageOrigin.x,
2076                                    // -xMapPane.imageOrigin.y);
2077                                    // Envelope newMapAreaBefore = xMapPane.tranformWindowToGeo(
2078                                    // winBounds.x, winBounds.y, winBounds.x
2079                                    // + winBounds.width, winBounds.y
2080                                    // + winBounds.height);
2081                                    //                                      
2082                                    //
2083                                    // winBounds = xMapPane.getBounds();
2084                                    // Point testIng = new Point(xMapPane.imageOrigin);
2085                                    // testIng.translate(dx, dy);
2086                                    // winBounds.translate(testIng.x, -testIng.y);
2087                                    // Envelope newMapAreaAfter = xMapPane.tranformWindowToGeo(
2088                                    // winBounds.x, winBounds.y, winBounds.x
2089                                    // + winBounds.width, winBounds.y
2090                                    // + winBounds.height);
2091                                    //
2092                                    // // If the last drag doesn't change the MapArea anymore cancel
2093                                    // it.
2094                                    // if (xMapPane.bestAllowedMapArea(newMapAreaAfter).equals(
2095                                    // xMapPane.bestAllowedMapArea(newMapAreaBefore))){
2096                                    // dragValid = false;
2097                                    // return;
2098                                    // }
2099                                    // }
2100    
2101                                    imageOrigin.translate(dx, dy);
2102                                    updateFinalImage();
2103                                    repaint();
2104                            }
2105    
2106                    } else if ((getState() == XMapPane.ZOOM_IN)
2107                                    || (getState() == XMapPane.ZOOM_OUT)
2108                                    || (getState() == XMapPane.SELECT_ALL)
2109                                    || (getState() == XMapPane.SELECT_TOP)
2110                    // || (getState() == XMapPane.SELECT_ONE_FROM_TOP)
2111                    ) {
2112                            final Graphics graphics = getGraphics();
2113    
2114                            drawRectangle(graphics, startPos, event.getPoint());
2115    
2116                            if ((lastPos.x > 0) && (lastPos.y > 0)) {
2117                                    drawRectangle(graphics, startPos, lastPos);
2118                            }
2119    
2120                    }
2121    
2122            }
2123    
2124            /**
2125             * Draws a rectangle in XOR mode from the origin at {@link #startPos} to the
2126             * given point. All in screen coordinates.
2127             */
2128            protected void drawRectangle(final Graphics graphics, Point startPos,
2129                            Point e) {
2130                    
2131                    if (!isWellDefined()) return;
2132                    
2133                    // undraw last box/draw new box
2134                    final int left = Math.min(startPos.x, e.x);
2135                    final int right = Math.max(startPos.x, e.x);
2136                    final int top = Math.max(startPos.y, e.y);
2137                    final int bottom = Math.min(startPos.y, e.y);
2138                    final int width = right - left;
2139                    final int height = top - bottom;
2140    
2141                    if (width == 0 && height == 0)
2142                            return;
2143    
2144                    graphics.setXORMode(Color.WHITE);
2145                    graphics.drawRect(left, bottom, width, height);
2146            }
2147    
2148            /**
2149             * Finalizes a PAN action
2150             */
2151            public void performPan() {
2152                    Rectangle winBounds = getBounds();
2153                    winBounds.translate(-imageOrigin.x, -imageOrigin.y);
2154                    Envelope newMapArea = tranformWindowToGeo(winBounds.x, winBounds.y,
2155                                    winBounds.x + winBounds.width, winBounds.y + winBounds.height);
2156    
2157                    imageOrigin.x = 0;
2158                    imageOrigin.y = 0;
2159    
2160                    if (!setMapArea(newMapArea)) {
2161                            updateFinalImage();
2162                            repaint();
2163                    }
2164    
2165                    if (getCursor() == SwingUtil.PANNING_CURSOR)
2166                            setCursor(SwingUtil.PAN_CURSOR);
2167            }
2168    
2169            public void onRenderingPending() {
2170    //              LOGGER.debug("Pending rendering updates the preview...");
2171                    updateFinalImage();
2172                    repaint();
2173          }          }
         // xulu.en  
2174    
2175  }  }

Legend:
Removed from v.320  
changed lines
  Added in v.524

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26