/[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

trunk/src/org/geotools/gui/swing/JMapPane.java revision 226 by alfonx, Thu Jul 16 07:37:43 2009 UTC branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java revision 485 by mojays, Tue Oct 20 16:22: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>   * <b>Xulu:<br>
# Line 58  import java.util.Map; Line 58  import java.util.Map;
58  import javax.swing.JPanel;  import javax.swing.JPanel;
59    
60  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
 import org.geotools.feature.FeatureCollection;  
 import org.geotools.filter.IllegalFilterException;  
 import org.geotools.gui.swing.event.HighlightChangeListener;  
 import org.geotools.gui.swing.event.HighlightChangedEvent;  
 import org.geotools.gui.swing.event.SelectionChangeListener;  
 import org.geotools.gui.swing.event.SelectionChangedEvent;  
 import org.geotools.map.DefaultMapContext;  
61  import org.geotools.map.MapContext;  import org.geotools.map.MapContext;
 import org.geotools.map.MapLayer;  
62  import org.geotools.map.event.MapLayerListEvent;  import org.geotools.map.event.MapLayerListEvent;
63  import org.geotools.map.event.MapLayerListListener;  import org.geotools.map.event.MapLayerListListener;
 import org.geotools.referencing.crs.DefaultGeographicCRS;  
64  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
65    import org.geotools.renderer.label.LabelCacheImpl;
66  import org.geotools.renderer.lite.LabelCache;  import org.geotools.renderer.lite.LabelCache;
 import org.geotools.renderer.lite.LabelCacheDefault;  
67  import org.geotools.renderer.lite.StreamingRenderer;  import org.geotools.renderer.lite.StreamingRenderer;
 import org.geotools.renderer.shape.TransitionShapefileRenderer;  
 import org.geotools.styling.Graphic;  
 import org.geotools.styling.LineSymbolizer;  
 import org.geotools.styling.Mark;  
 import org.geotools.styling.PointSymbolizer;  
 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;  
68  import org.opengis.filter.FilterFactory2;  import org.opengis.filter.FilterFactory2;
69  import org.opengis.referencing.crs.CoordinateReferenceSystem;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
70    
71    import schmitzm.geotools.JTSUtil;
72  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
73    
74  import com.vividsolutions.jts.geom.Coordinate;  import com.vividsolutions.jts.geom.Coordinate;
75  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
 import com.vividsolutions.jts.geom.Geometry;  
76  import com.vividsolutions.jts.geom.GeometryFactory;  import com.vividsolutions.jts.geom.GeometryFactory;
77    
78  public class JMapPane extends JPanel implements MouseListener,  public class JMapPane extends JPanel implements MouseListener,
79                  MouseMotionListener, HighlightChangeListener, SelectionChangeListener,                  MouseMotionListener, PropertyChangeListener, MapLayerListListener {
80                  PropertyChangeListener, MapLayerListListener {          private static Logger LOGGER = Logger.getLogger(JMapPane.class);
         private static Logger LOGGER = Logger.getLogger(JMapPane.class.getName());  
81    
82          private static final long serialVersionUID = -8647971481359690499L;          private static final long serialVersionUID = -8647971481359690499L;
83    
# Line 111  public class JMapPane extends JPanel imp Line 91  public class JMapPane extends JPanel imp
91    
92          public static final int Select = 4;          public static final int Select = 4;
93    
         private static final int POLYGON = 0;  
   
         private static final int LINE = 1;  
   
         private static final int POINT = 2;  
   
94          /**          /**
95           * what renders the map           * what renders the map
96           */           */
97          GTRenderer renderer;          GTRenderer renderer;
98    
         private GTRenderer highlightRenderer, selectionRenderer;  
   
99          /**          /**
100           * the map context to render           * the map context to render
101           */           */
102          MapContext context;          MapContext context;
103    
         private MapContext selectionContext;  
   
104          /**          /**
105           * the area of the map to draw           * the area of the map to draw
106           */           */
         // xulu.sc  
         // Envelope mapArea;  
107          protected Envelope mapArea;          protected Envelope mapArea;
         // xulu.ec  
108    
109          /**          /**
110           * the size of the pane last time we drew           * the size of the pane last time we drew
111           */           */
         // xulu.sc  
         // private Rectangle oldRect = null;  
112          protected Rectangle oldRect = null;          protected Rectangle oldRect = null;
         // xulu.ec  
113    
114          /**          /**
115           * the last map area drawn.           * the last map area drawn.
116           */           */
         // xulu.sc  
         // private Envelope oldMapArea = null;  
117          protected Envelope oldMapArea = null;          protected Envelope oldMapArea = null;
         // xulu.ec  
118    
119          /**          /**
120           * the base image of the map           * the base image of the map
121           */           */
122          protected BufferedImage baseImage, panningImage;          protected BufferedImage baseImage, panningImage;
         // SK: private BufferedImage baseImage, panningImage;  
   
         /**  
          * image of selection  
          */  
         private BufferedImage selectImage;  
   
         /**  
          * style for selected items  
          */  
         private Style selectionStyle;  
   
         /**  
          * layer that selection works on  
          */  
         private MapLayer selectionLayer;  
   
         /**  
          * layer that highlight works on  
          */  
         private MapLayer highlightLayer;  
   
         /**  
          * the object which manages highlighting  
          */  
         private HighlightManager highlightManager;  
   
         /**  
          * is highlighting on or off  
          */  
         private boolean highlight = true;  
123    
124          /**          /**
125           * a factory for filters           * a factory for filters
# Line 201  public class JMapPane extends JPanel imp Line 131  public class JMapPane extends JPanel imp
131           */           */
132          GeometryFactory gf = new GeometryFactory(); // FactoryFinder.getGeometryFactory(null);          GeometryFactory gf = new GeometryFactory(); // FactoryFinder.getGeometryFactory(null);
133    
         /**  
          * the collections of features to be selected or highlighted  
          */  
         FeatureCollection selection;  
   
         /**  
          * the collections of features to be selected or highlighted  
          */  
         FeatureCollection highlightFeature;  
   
134          private int state = ZoomIn;          private int state = ZoomIn;
135    
136          /**          /**
# Line 218  public class JMapPane extends JPanel imp Line 138  public class JMapPane extends JPanel imp
138           */           */
139          private double zoomFactor = 2.0;          private double zoomFactor = 2.0;
140    
         Style lineHighlightStyle;  
   
         Style pointHighlightStyle;  
   
         Style polygonHighlightStyle;  
   
         Style polygonSelectionStyle;  
   
         Style pointSelectionStyle;  
   
         Style lineSelectionStyle;  
   
141          boolean changed = true;          boolean changed = true;
142    
143          LabelCache labelCache = new LabelCacheDefault();          LabelCache labelCache = new LabelCacheImpl();
144    
         // xulu.sc  
         // private boolean reset = false;  
145          protected boolean reset = false;          protected boolean reset = false;
         // xulu.ec  
146    
147          int startX;          int startX;
148    
149          int startY;          int startY;
150    
151            /**
152             * If not <code>null</code>, the {@link JMapPane} will not allow to zoom/pan
153             * out of that area
154             **/
155            private Envelope maxExtend = null;
156    
157            // /**
158            // * Is max. 1 or 0 of the 2 axised allowed to extend the maxExtend? If
159            // * <code>true</code> the extends has to be fully inside maxExtend
160            // **/
161            // boolean maxExtendForceMode = true;
162    
163          private boolean clickable;          private boolean clickable;
164    
165          int lastX;          int lastX;
166    
167          int lastY;          int lastY;
168    
         private SelectionManager selectionManager;  
         // xulu.sn  
169          private Double maxZoomScale = Double.MIN_VALUE;          private Double maxZoomScale = Double.MIN_VALUE;
170          private Double minZoomScale = Double.MAX_VALUE;          private Double minZoomScale = Double.MAX_VALUE;
         // xulu.en  
171    
         // sk.sn  
172          /**          /**
173           * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses           * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses
174           * Flag wird benutzt um nur einmal den passenden Cursor nur einmal zu           * Flag wird benutzt um nur einmal den passenden Cursor nur einmal zu
# Line 263  public class JMapPane extends JPanel imp Line 176  public class JMapPane extends JPanel imp
176           */           */
177          private boolean panning_started = false;          private boolean panning_started = false;
178    
         // sk.en  
   
179          public JMapPane() {          public JMapPane() {
180                  this(null, true, null, null);                  this(null, true, null, null);
181          }          }
# Line 277  public class JMapPane extends JPanel imp Line 188  public class JMapPane extends JPanel imp
188           * @param context           * @param context
189           *            - the map context to display           *            - the map context to display
190           */           */
191          public JMapPane(GTRenderer render, MapContext context) {          public JMapPane(final GTRenderer render, final MapContext context) {
192                  this(null, true, render, context);                  this(null, true, render, context);
193          }          }
194    
# Line 293  public class JMapPane extends JPanel imp Line 204  public class JMapPane extends JPanel imp
204           * @param context           * @param context
205           *            - what to draw           *            - what to draw
206           */           */
207          public JMapPane(LayoutManager layout, boolean isDoubleBuffered,          public JMapPane(final LayoutManager layout, final boolean isDoubleBuffered,
208                          GTRenderer render, MapContext context) {                          final GTRenderer render, final MapContext context) {
209                  super(layout, isDoubleBuffered);                  super(layout, isDoubleBuffered);
210    
211                  ff = (FilterFactory2) org.geotools.factory.CommonFactoryFinder                  ff = (FilterFactory2) org.geotools.factory.CommonFactoryFinder
# Line 305  public class JMapPane extends JPanel imp Line 216  public class JMapPane extends JPanel imp
216    
217                  this.addMouseListener(this);                  this.addMouseListener(this);
218                  this.addMouseMotionListener(this);                  this.addMouseMotionListener(this);
                 setHighlightManager(new HighlightManager(highlightLayer));  
                 setSelectionManager(new SelectionManager(selectionLayer));  
                 lineHighlightStyle = setupStyle(LINE, Color.red);  
   
                 pointHighlightStyle = setupStyle(POINT, Color.red);  
   
                 polygonHighlightStyle = setupStyle(POLYGON, Color.red);  
   
                 polygonSelectionStyle = setupStyle(POLYGON, Color.cyan);  
   
                 pointSelectionStyle = setupStyle(POINT, Color.cyan);  
   
                 lineSelectionStyle = setupStyle(LINE, Color.cyan);  
219                  setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));                  setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
220          }          }
221    
# Line 328  public class JMapPane extends JPanel imp Line 226  public class JMapPane extends JPanel imp
226                  return renderer;                  return renderer;
227          }          }
228    
229          public void setRenderer(GTRenderer renderer) {          public void setRenderer(final GTRenderer renderer) {
230                  Map hints = new HashMap();                  Map<Object, Object> hints = new HashMap<Object, Object>();
231                  if (renderer instanceof StreamingRenderer || renderer instanceof TransitionShapefileRenderer) {  
232                    this.renderer = renderer;
233    //MS: Apply hint also for ShapeFileRenderer
234    //              if (renderer instanceof StreamingRenderer) {
235                          hints = renderer.getRendererHints();                          hints = renderer.getRendererHints();
236                          if (hints == null) {                          if (hints == null) {
237                                  hints = new HashMap();                                  hints = new HashMap<Object, Object>();
238                          }                          }
239                          if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {                          if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {
240                                  labelCache = (LabelCache) hints                                  labelCache = (LabelCache) hints
# Line 341  public class JMapPane extends JPanel imp Line 242  public class JMapPane extends JPanel imp
242                          } else {                          } else {
243                                  hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);                                  hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
244                          }                          }
                         renderer.setRendererHints(hints);  
                 }  
245    
246                  this.renderer = renderer;                          hints.put("memoryPreloadingEnabled", Boolean.TRUE);
 //              this.highlightRenderer = new StreamingRenderer();  
 //              this.selectionRenderer = new StreamingRenderer();  
247    
248                  hints.put("memoryPreloadingEnabled", Boolean.FALSE);                          renderer.setRendererHints(hints);
249  //              highlightRenderer.setRendererHints(hints);  //              }
 //              selectionRenderer.setRendererHints(hints);  
250    
251                  if (this.context != null) {                  if (this.context != null) {
252                          this.renderer.setContext(this.context);                          this.renderer.setContext(this.context);
# Line 361  public class JMapPane extends JPanel imp Line 257  public class JMapPane extends JPanel imp
257                  return context;                  return context;
258          }          }
259    
260          public void setContext(MapContext context) {          public void setContext(final MapContext context) {
261                  if (this.context != null) {                  if (this.context != null) {
262                          this.context.removeMapLayerListListener(this);                          this.context.removeMapLayerListListener(this);
263                  }                  }
# Line 381  public class JMapPane extends JPanel imp Line 277  public class JMapPane extends JPanel imp
277                  return mapArea;                  return mapArea;
278          }          }
279    
280          public void setMapArea(Envelope mapArea) {          public void setMapArea(final Envelope mapArea) {
281                  this.mapArea = mapArea;                  this.mapArea = mapArea;
282          }          }
283    
# Line 389  public class JMapPane extends JPanel imp Line 285  public class JMapPane extends JPanel imp
285                  return state;                  return state;
286          }          }
287    
288          public void setState(int state) {          public void setState(final int state) {
289                  this.state = state;                  this.state = state;
290    
291                  // System.out.println("State: " + state);                  // System.out.println("State: " + state);
# Line 399  public class JMapPane extends JPanel imp Line 295  public class JMapPane extends JPanel imp
295                  return zoomFactor;                  return zoomFactor;
296          }          }
297    
298          public void setZoomFactor(double zoomFactor) {          public void setZoomFactor(final double zoomFactor) {
299                  this.zoomFactor = zoomFactor;                  this.zoomFactor = zoomFactor;
300          }          }
301    
302          public MapLayer getSelectionLayer() {          protected void paintComponent(final Graphics g) {
                 return selectionLayer;  
         }  
   
         public void setSelectionLayer(MapLayer selectionLayer) {  
                 this.selectionLayer = selectionLayer;  
                 if (selectionManager != null) {  
                         selectionManager.setSelectionLayer(selectionLayer);  
                 }  
         }  
   
         public boolean isHighlight() {  
                 return highlight;  
         }  
   
         public void setHighlight(boolean highlight) {  
                 this.highlight = highlight;  
         }  
   
         public MapLayer getHighlightLayer() {  
                 return highlightLayer;  
         }  
   
         public void setHighlightLayer(MapLayer highlightLayer) {  
                 this.highlightLayer = highlightLayer;  
   
                 if (highlightManager != null) {  
                         highlightManager.setHighlightLayer(highlightLayer);  
                 }  
         }  
   
         public HighlightManager getHighlightManager() {  
                 return highlightManager;  
         }  
   
         public void setHighlightManager(HighlightManager highlightManager) {  
                 this.highlightManager = highlightManager;  
                 this.highlightManager.addHighlightChangeListener(this);  
                 this.addMouseMotionListener(this.highlightManager);  
         }  
   
         public Style getLineHighlightStyle() {  
                 return lineHighlightStyle;  
         }  
   
         public void setLineHighlightStyle(Style lineHighlightStyle) {  
                 this.lineHighlightStyle = lineHighlightStyle;  
         }  
   
         public Style getLineSelectionStyle() {  
                 return lineSelectionStyle;  
         }  
   
         public void setLineSelectionStyle(Style lineSelectionStyle) {  
                 this.lineSelectionStyle = lineSelectionStyle;  
         }  
   
         public Style getPointHighlightStyle() {  
                 return pointHighlightStyle;  
         }  
   
         public void setPointHighlightStyle(Style pointHighlightStyle) {  
                 this.pointHighlightStyle = pointHighlightStyle;  
         }  
   
         public Style getPointSelectionStyle() {  
                 return pointSelectionStyle;  
         }  
   
         public void setPointSelectionStyle(Style pointSelectionStyle) {  
                 this.pointSelectionStyle = pointSelectionStyle;  
         }  
   
         public Style getPolygonHighlightStyle() {  
                 return polygonHighlightStyle;  
         }  
   
         public void setPolygonHighlightStyle(Style polygonHighlightStyle) {  
                 this.polygonHighlightStyle = polygonHighlightStyle;  
         }  
   
         public Style getPolygonSelectionStyle() {  
                 return polygonSelectionStyle;  
         }  
   
         public void setPolygonSelectionStyle(Style polygonSelectionStyle) {  
                 this.polygonSelectionStyle = polygonSelectionStyle;  
         }  
   
         protected void paintComponent(Graphics g) {  
303                  super.paintComponent(g);                  super.paintComponent(g);
304    
305                  if ((renderer == null) || (mapArea == null)) {                  if ((renderer == null) || (mapArea == null)) {
306                          return;                          return;
307                  }                  }
308    
309                  Rectangle r = getBounds();                  final Rectangle r = getBounds();
310                  Rectangle dr = new Rectangle(r.width, r.height);                  final Rectangle dr = new Rectangle(r.width, r.height);
311    
312                  if (!r.equals(oldRect) || reset) {                  if (!r.equals(oldRect) || reset) {
313                          if (!r.equals(oldRect) && (mapArea == null)) {                          if (!r.equals(oldRect) && (mapArea == null)) {
314                                  try {                                  try {
315                                          mapArea = context.getLayerBounds();                                          mapArea = context.getLayerBounds();
316                                  } catch (IOException e) {                                  } catch (final IOException e) {
317                                          // TODO Auto-generated catch block                                          LOGGER.warn("context.getLayerBounds()", e);
                                         e.printStackTrace();  
318                                  }                                  }
319                          }                          }
320    
# Line 518  public class JMapPane extends JPanel imp Line 324  public class JMapPane extends JPanel imp
324                                  reset = false; /* forget about the reset */                                  reset = false; /* forget about the reset */
325                                  oldRect = r; /* store what the current size is */                                  oldRect = r; /* store what the current size is */
326    
327                                  mapArea = fixAspectRatio(r, mapArea);                                  mapArea = fixAspectRatio(r, mapArea, false);
328                          }                          }
329                  }                  }
330    
# Line 531  public class JMapPane extends JPanel imp Line 337  public class JMapPane extends JPanel imp
337                                          .getCoordinateReferenceSystem());                                          .getCoordinateReferenceSystem());
338                  }                  }
339    
340                  if (changed ) { /* if the map changed then redraw */                  if (changed) { /* if the map changed then redraw */
341                          changed = false;                          changed = false;
342                          baseImage = new BufferedImage(dr.width, dr.height,                          baseImage = new BufferedImage(dr.width, dr.height,
343                                          BufferedImage.TYPE_INT_ARGB);                                          BufferedImage.TYPE_INT_ARGB);
344    
345                          Graphics2D ig = baseImage.createGraphics();                          final Graphics2D ig = baseImage.createGraphics();
346                          /* System.out.println("rendering"); */                          /* System.out.println("rendering"); */
347                          renderer.setContext(context);                          if (renderer.getContext() != null)
348                                    renderer.setContext(context);
349                          labelCache.clear(); // work around anoying labelcache bug                          labelCache.clear(); // work around anoying labelcache bug
350    
351                          // draw the map                          // draw the map
# Line 551  public class JMapPane extends JPanel imp Line 358  public class JMapPane extends JPanel imp
358                  }                  }
359    
360                  ((Graphics2D) g).drawImage(baseImage, 0, 0, this);                  ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
   
                 if ((selection != null) && (selection.size() > 0)) {  
                         // paint selection  
   
                         String type = selectionLayer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getType().getName();  
                         /* String type = selection.getDefaultGeometry().getGeometryType(); */  
                         /* System.out.println(type); */  
                         if (type == null)  
                                 type = "polygon";  
   
                         /* String type = "point"; */  
   
                         if (type.toLowerCase().endsWith("polygon")) {  
                                 selectionStyle = polygonSelectionStyle;  
                         } else if (type.toLowerCase().endsWith("point")) {  
                                 selectionStyle = pointSelectionStyle;  
                         } else if (type.toLowerCase().endsWith("line")) {  
                                 selectionStyle = lineSelectionStyle;  
                         }  
   
                         selectionContext = new DefaultMapContext(DefaultGeographicCRS.WGS84);  
   
                         selectionContext.addLayer(selection, selectionStyle);  
                         selectionRenderer.setContext(selectionContext);  
   
                         selectImage = new BufferedImage(dr.width, dr.height,  
                                         BufferedImage.TYPE_INT_ARGB);  
   
                         Graphics2D ig = selectImage.createGraphics();  
                         /* System.out.println("rendering selection"); */  
                         selectionRenderer.paint((Graphics2D) ig, dr, mapArea);  
   
                         ((Graphics2D) g).drawImage(selectImage, 0, 0, this);  
                 }  
   
                 if (highlight && (highlightFeature != null)  
                                 && (highlightFeature.size() > 0)) {  
                         /*  
                          * String type = selection.getDefaultGeometry().getGeometryType();  
                          * System.out.println(type); if(type==null) type="polygon";  
                          */  
                         String type = highlightLayer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getType().getName();  
                         /* String type = selection.getDefaultGeometry().getGeometryType(); */  
                         // System.out.println(type);  
                         if (type == null)  
                                 type = "polygon";  
   
                         /* String type = "point"; */  
                         Style highlightStyle = null;  
                         if (type.toLowerCase().endsWith("polygon")) {  
                                 highlightStyle = polygonHighlightStyle;  
                         } else if (type.toLowerCase().endsWith("point")) {  
                                 highlightStyle = pointHighlightStyle;  
                         } else if (type.toLowerCase().endsWith("line")) {  
                                 highlightStyle = lineHighlightStyle;  
                         }  
   
                         MapContext highlightContext = new DefaultMapContext(  
                                         DefaultGeographicCRS.WGS84);  
   
                         highlightContext.addLayer(highlightFeature, highlightStyle);  
                         highlightRenderer.setContext(highlightContext);  
   
                         /* System.out.println("rendering highlight"); */  
                         highlightRenderer.paint((Graphics2D) g, dr, mapArea);  
                 }  
361          }          }
362    
363          private Envelope fixAspectRatio(Rectangle r, Envelope mapArea) {          /**
364             * Returns an {@link Envelope} that has the same aspect ratio as the given rectangle
365                  double mapWidth = mapArea.getWidth(); /* get the extent of the map */           * @param grow
366                  double mapHeight = mapArea.getHeight();           *            If <code>true</code>, than the area will be enlarged to match
367                  double scaleX = r.getWidth() / mapArea.getWidth(); /*           *            the aspect ratio. If <code>false</code>, it will only shrink.
368                                                                                                                           * calculate the new           */
369                                                                                                                           * scale          private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea,
370                                                                                                                           */                          boolean grow) {
371                    
372                    if (mapArea == null) {
373                            LOGGER.warn("mapArea has been null in method fixAspectRatio, returning an unmodified ");
374                    }
375    
376                    final double mapWidth = mapArea.getWidth(); /* get the extent of the map */
377                    final double mapHeight = mapArea.getHeight();
378                    final double scaleX = r.getWidth() / mapArea.getWidth(); /*
379                                                                                                                                     * calculate the
380                                                                                                                                     * new scale
381                                                                                                                                     */
382    
383                  double scaleY = r.getHeight() / mapArea.getHeight();                  final double scaleY = r.getHeight() / mapArea.getHeight();
384                  double scale = 1.0; // stupid compiler!                  double scale = 1.0; // stupid compiler!
385    
386                  if (scaleX < scaleY) { /* pick the smaller scale */                  if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) {
387                          scale = scaleX;                          scale = scaleX;
388                  } else {                  } else {
389                          scale = scaleY;                          scale = scaleY;
390                  }                  }
391    
392                  /* calculate the difference in width and height of the new extent */                  /* calculate the difference in width and height of the new extent */
393                  double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);                  final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);
394                  double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);                  final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);
395    
396                  /*                  /*
397                   * System.out.println("delta x " + deltaX);                   * System.out.println("delta x " + deltaX);
# Line 649  public class JMapPane extends JPanel imp Line 399  public class JMapPane extends JPanel imp
399                   */                   */
400    
401                  /* create the new extent */                  /* create the new extent */
402                  Coordinate ll = new Coordinate(mapArea.getMinX() - (deltaX / 2.0),                  final Coordinate ll = new Coordinate(
403                                  mapArea.getMinY() - (deltaY / 2.0));                                  mapArea.getMinX() - (deltaX / 2.0), mapArea.getMinY()
404                  Coordinate ur = new Coordinate(mapArea.getMaxX() + (deltaX / 2.0),                                                  - (deltaY / 2.0));
405                                  mapArea.getMaxY() + (deltaY / 2.0));                  final Coordinate ur = new Coordinate(
406                                    mapArea.getMaxX() + (deltaX / 2.0), mapArea.getMaxY()
407                                                    + (deltaY / 2.0));
408    
409                  return new Envelope(ll, ur);                  return new Envelope(ll, ur);
410          }          }
411    
412          public void doSelection(double x, double y, MapLayer layer) {          public void mouseClicked(final MouseEvent e) {
413                    if (mapArea == null)
                 Geometry geometry = gf.createPoint(new Coordinate(x, y));  
   
                 // org.opengis.geometry.Geometry geometry = new Point();  
   
                 findFeature(geometry, layer);  
   
         }  
   
         /**  
          * @param geometry  
          *            - a geometry to construct the filter with  
          * @param i  
          *            - the index of the layer to search  
          * @throws IndexOutOfBoundsException  
          */  
         private void findFeature(Geometry geometry, MapLayer layer)  
                         throws IndexOutOfBoundsException {  
                 org.opengis.filter.spatial.BinarySpatialOperator f = null;  
   
                 if ((context == null) || (layer == null)) {  
414                          return;                          return;
                 }  
   
                 try {  
                         String name = layer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getLocalName();  
   
                         if (name == "") {  
                                 name = "the_geom";  
                         }  
   
                         try {  
                                 f = ff.contains(ff.property(name), ff.literal(geometry));  
                                 if (selectionManager != null) {  
 //                                      System.out.println("selection changed");  
                                         selectionManager.selectionChanged(this, f);  
   
                                 }  
                         } catch (IllegalFilterException e) {  
                                 // TODO Auto-generated catch block  
                                 e.printStackTrace();  
                         }  
   
                         /*  
                          * // 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); }  
                          */  
   
                         /*  
                          * GeometryAttributeType gat =  
                          * layer.getFeatureSource().getSchema().getDefaultGeometry();  
                          * fcol.setDefaultGeometry((Geometry)gat.createDefaultValue());  
                          */  
   
                         /*  
                          * Iterator fi = fc.iterator(); while (fi.hasNext()) { Feature feat  
                          * = (Feature) fi.next(); System.out.println("selected " +  
                          * feat.getAttribute("STATE_NAME")); }  
                          */  
                 } catch (IllegalFilterException e) {  
                         // TODO Auto-generated catch block  
                         e.printStackTrace();  
                 }  
                 return;  
         }  
   
         public void mouseClicked(MouseEvent e) {  
                 if (mapArea == null) return;  
415                  // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"                  // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
416                  // h:"+mapArea.getHeight());                  // h:"+mapArea.getHeight());
417                  Rectangle bounds = this.getBounds();                  final Rectangle bounds = this.getBounds();
418                  double x = (double) (e.getX());                  final double x = (double) (e.getX());
419                  double y = (double) (e.getY());                  final double y = (double) (e.getY());
420                  double width = mapArea.getWidth();                  final double width = mapArea.getWidth();
421                  double height = mapArea.getHeight();                  final double height = mapArea.getHeight();
422                  // xulu.sc                  // xulu.sc
423                  // double width2 = mapArea.getWidth() / 2.0;                  // double width2 = mapArea.getWidth() / 2.0;
424                  // double height2 = mapArea.getHeight() / 2.0;                  // double height2 = mapArea.getHeight() / 2.0;
425                  double width2 = width / 2.0;                  final double width2 = width / 2.0;
426                  double height2 = height / 2.0;                  final double height2 = height / 2.0;
427                  // xulu.ec                  // xulu.ec
428                  double mapX = ((x * width) / (double) bounds.width) + mapArea.getMinX();                  final double mapX = ((x * width) / (double) bounds.width)
429                  double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)                                  + mapArea.getMinX();
430                    final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
431                                  + mapArea.getMinY();                                  + mapArea.getMinY();
432    
433                  /*                  /*
# Line 779  public class JMapPane extends JPanel imp Line 457  public class JMapPane extends JPanel imp
457                          zlevel = 1.0 / zoomFactor;                          zlevel = 1.0 / zoomFactor;
458    
459                          break;                          break;
460                    //
461                  case Select:                  // case Select:
462                          doSelection(mapX, mapY, selectionLayer);                  // doSelection(mapX, mapY, selectionLayer);
463                    //
464                          return;                  // return;
465    
466                  default:                  default:
467                          return;                          return;
468                  }                  }
469    
470                  Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY                  final Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
471                                  - (height2 / zlevel));                                  - (height2 / zlevel));
472                  Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY                  final Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
473                                  + (height2 / zlevel));                                  + (height2 / zlevel));
474                  // xulu.sc SK: Check for min/max scale                  // xulu.sc SK: Check for min/max scale
475                  // mapArea = new Envelope(ll, ur);                  // mapArea = new Envelope(ll, ur);
# Line 806  public class JMapPane extends JPanel imp Line 484  public class JMapPane extends JPanel imp
484                  repaint();                  repaint();
485          }          }
486    
487          public void mouseEntered(MouseEvent e) {          public void mouseEntered(final MouseEvent e) {
488          }          }
489    
490          public void mouseExited(MouseEvent e) {          public void mouseExited(final MouseEvent e) {
491          }          }
492    
493          public void mousePressed(MouseEvent e) {          public void mousePressed(final MouseEvent e) {
494                  startX = e.getX();                  startX = e.getX();
495                  startY = e.getY();                  startY = e.getY();
496                  lastX = 0;                  lastX = 0;
497                  lastY = 0;                  lastY = 0;
498          }          }
499    
500          public void mouseReleased(MouseEvent e) {          public void mouseReleased(final MouseEvent e) {
501                  int endX = e.getX();                  final int endX = e.getX();
502                  int endY = e.getY();                  final int endY = e.getY();
503    
504                  processDrag(startX, startY, endX, endY, e);                  processDrag(startX, startY, endX, endY, e);
505                  lastX = 0;                  lastX = 0;
# Line 833  public class JMapPane extends JPanel imp Line 511  public class JMapPane extends JPanel imp
511                  panning_started = false;                  panning_started = false;
512          }          }
513    
514          public void mouseDragged(MouseEvent e) {          public void mouseDragged(final MouseEvent e) {
515                  Graphics graphics = this.getGraphics();                  final Graphics graphics = this.getGraphics();
516                  int x = e.getX();                  final int x = e.getX();
517                  int y = e.getY();                  final int y = e.getY();
518    
519                  if ((state == JMapPane.Pan)                  if ((state == JMapPane.Pan)
520                                  || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {                                  || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
# Line 850  public class JMapPane extends JPanel imp Line 528  public class JMapPane extends JPanel imp
528    
529                          // move the image with the mouse                          // move the image with the mouse
530                          if ((lastX > 0) && (lastY > 0)) {                          if ((lastX > 0) && (lastY > 0)) {
531                                  int dx = lastX - startX;                                  final int dx = lastX - startX;
532                                  int dy = lastY - startY;                                  final int dy = lastY - startY;
533                                  // System.out.println("translate "+dx+","+dy);                                  // System.out.println("translate "+dx+","+dy);
534                                  final Graphics2D g2 = panningImage.createGraphics();                                  final Graphics2D g2 = panningImage.createGraphics();
535                                  g2.setBackground(new Color(240, 240, 240)); // TODO richtige                                  g2.setBackground(new Color(240, 240, 240)); // TODO richtige
536                                                                                                                          // farbe? am besten                                  // farbe? am besten
537                                                                                                                          // vom L&F die                                  // vom L&F die
538                                                                                                                          // hintergrundfarbe                                  // hintergrundfarbe
539                                                                                                                          // auslesen...                                  // auslesen...
540                                    
541                                  g2.clearRect(0, 0, this.getWidth(), this.getHeight());                                  g2.clearRect(0, 0, this.getWidth(), this.getHeight());
542                                  g2.drawImage(baseImage, dx, dy, this);                                  g2.drawImage(baseImage, dx, dy, this);
543                                  graphics.drawImage(panningImage, 0, 0, this);                                  graphics.drawImage(panningImage, 0, 0, this);
# Line 881  public class JMapPane extends JPanel imp Line 559  public class JMapPane extends JPanel imp
559                          lastX = x;                          lastX = x;
560                          lastY = y;                          lastY = y;
561                          drawRectangle(graphics);                          drawRectangle(graphics);
                 } else if (state == JMapPane.Select && selectionLayer != null) {  
   
                         // construct a new bbox filter  
                         Rectangle bounds = this.getBounds();  
   
                         double mapWidth = mapArea.getWidth();  
                         double mapHeight = mapArea.getHeight();  
   
                         double x1 = ((this.startX * mapWidth) / (double) bounds.width)  
                                         + mapArea.getMinX();  
                         double y1 = (((bounds.getHeight() - this.startY) * mapHeight) / (double) bounds.height)  
                                         + mapArea.getMinY();  
                         double x2 = ((x * mapWidth) / (double) bounds.width)  
                                         + mapArea.getMinX();  
                         double y2 = (((bounds.getHeight() - y) * mapHeight) / (double) bounds.height)  
                                         + mapArea.getMinY();  
                         double left = Math.min(x1, x2);  
                         double right = Math.max(x1, x2);  
                         double bottom = Math.min(y1, y2);  
                         double top = Math.max(y1, y2);  
   
                         String name = selectionLayer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getName();  
   
                         if (name == "") {  
                                 name = "the_geom";  
                         }  
                         Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,  
                                         getContext().getCoordinateReferenceSystem().toString());  
                         if (selectionManager != null) {  
                                 selectionManager.selectionChanged(this, bb);  
                         }  
   
                         graphics.setXORMode(Color.green);  
   
                         /*  
                          * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }  
                          */  
   
                         // draw new box  
                         lastX = x;  
                         lastY = y;  
                         drawRectangle(graphics);  
562                  }                  }
563                    // else if (state == JMapPane.Select && selectionLayer != null) {
564                    //
565                    // // construct a new bbox filter
566                    // final Rectangle bounds = this.getBounds();
567                    //
568                    // final double mapWidth = mapArea.getWidth();
569                    // final double mapHeight = mapArea.getHeight();
570                    //
571                    // final double x1 = ((this.startX * mapWidth) / (double) bounds.width)
572                    // + mapArea.getMinX();
573                    // final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) /
574                    // (double) bounds.height)
575                    // + mapArea.getMinY();
576                    // final double x2 = ((x * mapWidth) / (double) bounds.width)
577                    // + mapArea.getMinX();
578                    // final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double)
579                    // bounds.height)
580                    // + mapArea.getMinY();
581                    // final double left = Math.min(x1, x2);
582                    // final double right = Math.max(x1, x2);
583                    // final double bottom = Math.min(y1, y2);
584                    // final double top = Math.max(y1, y2);
585                    //
586                    // String name = selectionLayer.getFeatureSource().getSchema()
587                    // .getDefaultGeometry().getName();
588                    //
589                    // if (name == "") {
590                    // name = "the_geom";
591                    // }
592                    // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
593                    // top,
594                    // getContext().getCoordinateReferenceSystem().toString());
595                    // if (selectionManager != null) {
596                    // selectionManager.selectionChanged(this, bb);
597                    // }
598                    //
599                    // graphics.setXORMode(Color.green);
600                    //
601                    // /*
602                    // * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
603                    // */
604                    //
605                    // // draw new box
606                    // lastX = x;
607                    // lastY = y;
608                    // drawRectangle(graphics);
609                    // }
610    
611          }          }
612    
# Line 932  public class JMapPane extends JPanel imp Line 614  public class JMapPane extends JPanel imp
614          // private void processDrag(int x1, int y1, int x2, int y2) {          // private void processDrag(int x1, int y1, int x2, int y2) {
615          // sk.ce          // sk.ce
616          protected void processDrag(final int x1, final int y1, final int x2,          protected void processDrag(final int x1, final int y1, final int x2,
617                          final int y2, MouseEvent e) {                          final int y2, final MouseEvent e) {
618    
619    
620                  /****                  /****
621                   * If no layer is availabe we dont want a NullPointerException                   * If no layers exist, we ignore the drag.
622                   */                   */
623                  if (mapArea == null)                  if (context.getLayerCount() <= 0) {
624                          return;                          return;
625                    }
626                    
627                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
628                  // + x2 + "," + y2);                  // + x2 + "," + y2);
629                  if ((x1 == x2) && (y1 == y2)) {                  if ((x1 == x2) && (y1 == y2)) {
# Line 951  public class JMapPane extends JPanel imp Line 635  public class JMapPane extends JPanel imp
635                          return;                          return;
636                  }                  }
637    
638                  Rectangle bounds = this.getBounds();                  final Rectangle bounds = this.getBounds();
639    
640                  double mapWidth = mapArea.getWidth();                  final double mapWidth = mapArea.getWidth();
641                  double mapHeight = mapArea.getHeight();                  final double mapHeight = mapArea.getHeight();
642    
643                  double startX = ((x1 * mapWidth) / (double) bounds.width)                  final double startX = ((x1 * mapWidth) / (double) bounds.width)
644                                  + mapArea.getMinX();                                  + mapArea.getMinX();
645                  double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)                  final double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)
646                                  + mapArea.getMinY();                                  + mapArea.getMinY();
647                  double endX = ((x2 * mapWidth) / (double) bounds.width)                  final double endX = ((x2 * mapWidth) / (double) bounds.width)
648                                  + mapArea.getMinX();                                  + mapArea.getMinX();
649                  double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)                  final double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)
650                                  + mapArea.getMinY();                                  + mapArea.getMinY();
651    
652                  if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {                  if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {
653                          // move the image with the mouse                          // move the image with the mouse
654                          // calculate X offsets from start point to the end Point                          // calculate X offsets from start point to the end Point
655                          double deltaX1 = endX - startX;                          final double deltaX1 = endX - startX;
656    
657                          // System.out.println("deltaX " + deltaX1);                          // System.out.println("deltaX " + deltaX1);
658                          // new edges                          // new edges
659                          double left = mapArea.getMinX() - deltaX1;                          final double left = mapArea.getMinX() - deltaX1;
660                          double right = mapArea.getMaxX() - deltaX1;                          final double right = mapArea.getMaxX() - deltaX1;
661    
662                          // now for Y                          // now for Y
663                          double deltaY1 = endY - startY;                          final double deltaY1 = endY - startY;
664    
665                          // System.out.println("deltaY " + deltaY1);                          // System.out.println("deltaY " + deltaY1);
666                          double bottom = mapArea.getMinY() - deltaY1;                          final double bottom = mapArea.getMinY() - deltaY1;
667                          double top = mapArea.getMaxY() - deltaY1;                          final double top = mapArea.getMaxY() - deltaY1;
668                          Coordinate ll = new Coordinate(left, bottom);                          final Coordinate ll = new Coordinate(left, bottom);
669                          Coordinate ur = new Coordinate(right, top);                          final Coordinate ur = new Coordinate(right, top);
670                          // xulu.sc                          // xulu.sc
671                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
672                          setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur)));                          
673    
674                            setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
675                          // xulu.ec                          // xulu.ec
676                  } else if (state == JMapPane.ZoomIn) {                  } else if (state == JMapPane.ZoomIn) {
677    
# Line 997  public class JMapPane extends JPanel imp Line 683  public class JMapPane extends JPanel imp
683    
684                          drawRectangle(this.getGraphics());                          drawRectangle(this.getGraphics());
685                          // make the dragged rectangle (in map coords) the new BBOX                          // make the dragged rectangle (in map coords) the new BBOX
686                          double left = Math.min(startX, endX);                          final double left = Math.min(startX, endX);
687                          double right = Math.max(startX, endX);                          final double right = Math.max(startX, endX);
688                          double bottom = Math.min(startY, endY);                          final double bottom = Math.min(startY, endY);
689                          double top = Math.max(startY, endY);                          final double top = Math.max(startY, endY);
690                          Coordinate ll = new Coordinate(left, bottom);                          final Coordinate ll = new Coordinate(left, bottom);
691                          Coordinate ur = new Coordinate(right, top);                          final Coordinate ur = new Coordinate(right, top);
692                          // xulu.sc                          // xulu.sc
693    
694                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
695                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
696    
697                          // sk.sc                          // sk.sc
698  //                      {                          // {
699  //                      // SK tries to paint a preview of the zoom ;-9 aha.... well                          // // SK tries to paint a preview of the zoom ;-9 aha.... well
700  //                      Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();                          // Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
701  //                      graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,                          // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
702  //                                      RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);                          // RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
703  //                      graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,                          // graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
704  //                                      RenderingHints.VALUE_ANTIALIAS_OFF);                          // RenderingHints.VALUE_ANTIALIAS_OFF);
705  //                      graphics.setRenderingHint(RenderingHints.KEY_RENDERING,                          // graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
706  //                                      RenderingHints.VALUE_RENDER_SPEED);                          // RenderingHints.VALUE_RENDER_SPEED);
707  //                      graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),                          // graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
708  //                                      JMapPane.this.getHeight(), x1, y1, x2, y2, null);                          // JMapPane.this.getHeight(), x1, y1, x2, y2, null);
709  //                      }                          // }
710                          // xulu.ec                          // xulu.ec
711                  } else if (state == JMapPane.ZoomOut) {                  } else if (state == JMapPane.ZoomOut) {
712                          drawRectangle(this.getGraphics());                          drawRectangle(this.getGraphics());
713    
714                          // make the dragged rectangle in screen coords the new map size?                          // make the dragged rectangle in screen coords the new map size?
715                          double left = Math.min(startX, endX);                          final double left = Math.min(startX, endX);
716                          double right = Math.max(startX, endX);                          final double right = Math.max(startX, endX);
717                          double bottom = Math.min(startY, endY);                          final double bottom = Math.min(startY, endY);
718                          double top = Math.max(startY, endY);                          final double top = Math.max(startY, endY);
719                          double nWidth = (mapWidth * mapWidth) / (right - left);                          final double nWidth = (mapWidth * mapWidth) / (right - left);
720                          double nHeight = (mapHeight * mapHeight) / (top - bottom);                          final double nHeight = (mapHeight * mapHeight) / (top - bottom);
721                          double deltaX1 = left - mapArea.getMinX();                          final double deltaX1 = left - mapArea.getMinX();
722                          double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;                          final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
723                          double deltaY1 = bottom - mapArea.getMinY();                          final double deltaY1 = bottom - mapArea.getMinY();
724                          double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;                          final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
725                          Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,                          final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
726                                          mapArea.getMinY() - nDeltaY1);                                          mapArea.getMinY() - nDeltaY1);
727                          double deltaX2 = mapArea.getMaxX() - right;                          final double deltaX2 = mapArea.getMaxX() - right;
728                          double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;                          final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
729                          double deltaY2 = mapArea.getMaxY() - top;                          final double deltaY2 = mapArea.getMaxY() - top;
730                          double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;                          final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
731                          Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,                          final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
732                                          mapArea.getMaxY() + nDeltaY2);                                          mapArea.getMaxY() + nDeltaY2);
733                          // xulu.sc                          // xulu.sc
734                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
735                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
736    
737                          // xulu.ec                          // xulu.ec
                 } else if (state == JMapPane.Select && selectionLayer != null) {  
                         double left = Math.min(startX, endX);  
                         double right = Math.max(startX, endX);  
                         double bottom = Math.min(startY, endY);  
                         double top = Math.max(startY, endY);  
   
                         String name = selectionLayer.getFeatureSource().getSchema()  
                                         .getDefaultGeometry().getLocalName();  
   
                         if (name == "") {  
                                 name = "the_geom";  
                         }  
                         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(); }  
                          */  
738                  }                  }
739                    // else if (state == JMapPane.Select && selectionLayer != null) {
740                    // final double left = Math.min(startX, endX);
741                    // final double right = Math.max(startX, endX);
742                    // final double bottom = Math.min(startY, endY);
743                    // final double top = Math.max(startY, endY);
744                    //
745                    // String name = selectionLayer.getFeatureSource().getSchema()
746                    // .getDefaultGeometry().getLocalName();
747                    //
748                    // if (name == "") {
749                    // name = "the_geom";
750                    // }
751                    // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
752                    // top,
753                    // getContext().getCoordinateReferenceSystem().toString());
754                    // // System.out.println(bb.toString());
755                    // if (selectionManager != null) {
756                    // selectionManager.selectionChanged(this, bb);
757                    // }
758                    // /*
759                    // * FeatureCollection fc; selection = null; try { fc =
760                    // * selectionLayer.getFeatureSource().getFeatures(bb); selection =
761                    // * fc; } catch (IOException e) { e.printStackTrace(); }
762                    // */
763                    // }
764    
765                  // xulu.so                  // xulu.so
766                  // setMapArea(mapArea);                  // setMapArea(mapArea);
# Line 1084  public class JMapPane extends JPanel imp Line 772  public class JMapPane extends JPanel imp
772                  return clickable;                  return clickable;
773          }          }
774    
775          private org.geotools.styling.Style setupStyle(int type, Color color) {          //
776                  StyleFactory sf = org.geotools.factory.CommonFactoryFinder          // private org.geotools.styling.Style setupStyle(final int type, final Color
777                                  .getStyleFactory(null);          // color) {
778                  StyleBuilder sb = new StyleBuilder();          // final StyleFactory sf = org.geotools.factory.CommonFactoryFinder
779            // .getStyleFactory(null);
780                  org.geotools.styling.Style s = sf.createStyle();          // final StyleBuilder sb = new StyleBuilder();
781                  s.setTitle("selection");          //
782            // org.geotools.styling.Style s = sf.createStyle();
783                  // TODO parameterise the color          // s.setTitle("selection");
784                  PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);          //
785                  ps.setStroke(sb.createStroke(color));          // // TODO parameterise the color
786            // final PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);
787                  LineSymbolizer ls = sb.createLineSymbolizer(color);          // ps.setStroke(sb.createStroke(color));
788                  Graphic h = sb.createGraphic();          //
789                  h.setMarks(new Mark[] { sb.createMark("square", color) });          // final LineSymbolizer ls = sb.createLineSymbolizer(color);
790            // final Graphic h = sb.createGraphic();
791                  PointSymbolizer pts = sb.createPointSymbolizer(h);          // h.setMarks(new Mark[] { sb.createMark("square", color) });
792            //
793                  // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});          // final PointSymbolizer pts = sb.createPointSymbolizer(h);
794                  switch (type) {          //
795                  case POLYGON:          // // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});
796                          s = sb.createStyle(ps);          // switch (type) {
797            // case POLYGON:
798            // s = sb.createStyle(ps);
799            //
800            // break;
801            //
802            // case POINT:
803            // s = sb.createStyle(pts);
804            //
805            // break;
806            //
807            // case LINE:
808            // s = sb.createStyle(ls);
809            // }
810            //
811            // return s;
812            // }
813    
814                          break;          public void propertyChange(final PropertyChangeEvent evt) {
815                    final String prop = evt.getPropertyName();
                 case POINT:  
                         s = sb.createStyle(pts);  
   
                         break;  
   
                 case LINE:  
                         s = sb.createStyle(ls);  
                 }  
   
                 return s;  
         }  
   
         public void highlightChanged(HighlightChangedEvent e) {  
                 org.opengis.filter.Filter f = e.getFilter();  
   
                 try {  
                         highlightFeature = highlightLayer.getFeatureSource().getFeatures(f);  
                 } catch (IOException e1) {  
                         // TODO Auto-generated catch block  
                         e1.printStackTrace();  
                 }  
   
                 repaint();  
         }  
   
         public void propertyChange(PropertyChangeEvent evt) {  
                 String prop = evt.getPropertyName();  
816    
817                  if (prop.equalsIgnoreCase("crs")) {                  if (prop.equalsIgnoreCase("crs")) {
818                          context.setAreaOfInterest(context.getAreaOfInterest(),                          context.setAreaOfInterest(context.getAreaOfInterest(),
# Line 1147  public class JMapPane extends JPanel imp Line 824  public class JMapPane extends JPanel imp
824                  return reset;                  return reset;
825          }          }
826    
827          public void setReset(boolean reset) {          public void setReset(final boolean reset) {
828                  this.reset = reset;                  this.reset = reset;
829          }          }
830    
831          public void layerAdded(MapLayerListEvent event) {          public void layerAdded(final MapLayerListEvent event) {
832                  changed = true;                  changed = true;
833    
834                  if (context.getLayers().length == 1) { // the first one                  if (context.getLayers().length == 1) { // the first one
# Line 1163  public class JMapPane extends JPanel imp Line 840  public class JMapPane extends JPanel imp
840                                  if (mapArea == null)                                  if (mapArea == null)
841                                          mapArea = context.getLayerBounds();                                          mapArea = context.getLayerBounds();
842                                  // xulu.ec                                  // xulu.ec
843                          } catch (IOException e) {                          } catch (final IOException e) {
844                                  // TODO Auto-generated catch block                                  // TODO Auto-generated catch block
845                                  e.printStackTrace();                                  e.printStackTrace();
846                          }                          }
# Line 1174  public class JMapPane extends JPanel imp Line 851  public class JMapPane extends JPanel imp
851                  repaint();                  repaint();
852          }          }
853    
854          public void layerRemoved(MapLayerListEvent event) {          public void layerRemoved(final MapLayerListEvent event) {
855                  changed = true;                  changed = true;
856                  repaint();                  repaint();
857          }          }
858    
859          public void layerChanged(MapLayerListEvent event) {          public void layerChanged(final MapLayerListEvent event) {
860                  changed = true;                  changed = true;
861                  // System.out.println("layer changed - repaint");                  // System.out.println("layer changed - repaint");
862                  repaint();                  repaint();
863          }          }
864    
865          public void layerMoved(MapLayerListEvent event) {          public void layerMoved(final MapLayerListEvent event) {
866                  changed = true;                  changed = true;
867                  repaint();                  repaint();
868          }          }
869    
870          protected void drawRectangle(Graphics graphics) {          protected void drawRectangle(final Graphics graphics) {
871                  // undraw last box/draw new box                  // undraw last box/draw new box
872                  int left = Math.min(startX, lastX);                  final int left = Math.min(startX, lastX);
873                  int right = Math.max(startX, lastX);                  final int right = Math.max(startX, lastX);
874                  int top = Math.max(startY, lastY);                  final int top = Math.max(startY, lastY);
875                  int bottom = Math.min(startY, lastY);                  final int bottom = Math.min(startY, lastY);
876                  int width = right - left;                  final int width = right - left;
877                  int height = top - bottom;                  final int height = top - bottom;
878                  // System.out.println("drawing rect("+left+","+bottom+","+ width+","+                  // System.out.println("drawing rect("+left+","+bottom+","+ width+","+
879                  // height+")");                  // height+")");
880                  graphics.drawRect(left, bottom, width, height);                  graphics.drawRect(left, bottom, width, height);
# Line 1209  public class JMapPane extends JPanel imp Line 886  public class JMapPane extends JPanel imp
886           *           *
887           * @param clickable           * @param clickable
888           */           */
889          public void setClickable(boolean clickable) {          public void setClickable(final boolean clickable) {
890                  this.clickable = clickable;                  this.clickable = clickable;
891          }          }
892    
893          public void mouseMoved(MouseEvent e) {          public void mouseMoved(final MouseEvent e) {
         }  
   
         public FeatureCollection getSelection() {  
                 return selection;  
         }  
   
         public void setSelection(FeatureCollection selection) {  
                 this.selection = selection;  
                 repaint();  
         }  
   
         /*  
          * (non-Javadoc)  
          *  
          * @see  
          * org.geotools.gui.swing.event.SelectionChangeListener#selectionChanged  
          * (org.geotools.gui.swing.event.SelectionChangedEvent)  
          */  
         public void selectionChanged(SelectionChangedEvent e) {  
   
                 try {  
                         selection = selectionLayer.getFeatureSource().getFeatures(  
                                         e.getFilter());  
                         repaint();  
                 } catch (IOException e1) {  
                         e1.printStackTrace();  
                 }  
         }  
   
         public SelectionManager getSelectionManager() {  
                 return selectionManager;  
         }  
   
         public void setSelectionManager(SelectionManager selectionManager) {  
                 this.selectionManager = selectionManager;  
                 this.selectionManager.addSelectionChangeListener(this);  
   
894          }          }
895    
896          // xulu.sn          // xulu.sn
# Line 1271  public class JMapPane extends JPanel imp Line 911  public class JMapPane extends JPanel imp
911                  if (env == null)                  if (env == null)
912                          return env;                          return env;
913    
914                    Envelope newArea = null;
915    
916                  /**                  /**
917                   * Correct the aspect Ratio before we check the rest. Otherwise we might                   * Correct the aspect Ratio before we check the rest. Otherwise we might
918                   * easily fail.                   * easily fail.
919                   */                   */
920                  env = fixAspectRatio(this.getBounds(), env);                  env = fixAspectRatio(this.getBounds(), env, false);
921    
922                  double scale = env.getWidth() / getWidth();                  final double scale = env.getWidth() / getWidth();
923                  double centerX = env.getMinX() + env.getWidth() / 2.;                  final double centerX = env.getMinX() + env.getWidth() / 2.;
924                  double centerY = env.getMinY() + env.getHeight() / 2.;                  final double centerY = env.getMinY() + env.getHeight() / 2.;
925                  double newWidth2;                  double newWidth2 = 0;
926                  double newHeight2;                  double newHeight2 = 0;
927                  if (scale < getMaxZoomScale()) {                  if (scale < getMaxZoomScale()) {
928                          // ****************************************************************************                          // ****************************************************************************
929                          // Wir zoomen weiter rein als erlaubt => Anpassen des envelope                          // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
# Line 1298  public class JMapPane extends JPanel imp Line 940  public class JMapPane extends JPanel imp
940                          // ****************************************************************************                          // ****************************************************************************
941                          // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen                          // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
942                          // ****************************************************************************                          // ****************************************************************************
943                          return env;                          newArea = env;
944                  }                  }
945    
946                  Coordinate ll = new Coordinate(centerX - newWidth2, centerY                  if (newArea == null) {
                                 - newHeight2);  
                 Coordinate ur = new Coordinate(centerX + newWidth2, centerY  
                                 + newHeight2);  
947    
948                  return new Envelope(ll, ur);                          final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
949                                            - newHeight2);
950                            final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
951                                            + newHeight2);
952    
953                            newArea = new Envelope(ll, ur);
954                    }
955    
956                    Envelope maxAllowedExtend = getMaxExtend();
957                    while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea) && newArea != null && !newArea.isNull() && !Double.isNaN(newArea.getMinX()) && !Double.isNaN(newArea.getMaxX()) && !Double.isNaN(newArea.getMinY()) && !Double.isNaN(newArea.getMaxY()) ) {
958                            /*
959                             * If a maxExtend is set, we have to honour that...
960                             */
961    
962                            // Exceeds top? Move down and maybe cut
963                            if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
964                                    double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
965    //                              LOGGER.debug("Moving area down by " + divY);
966    
967                                    newArea = new Envelope(new Coordinate(newArea.getMinX(),
968                                                    newArea.getMinY() - divY), new Coordinate(newArea
969                                                    .getMaxX(), newArea.getMaxY() - divY));
970    
971                                    if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
972    //                                      LOGGER.debug("Now it exeeds the bottom border.. cut!");
973                                            // And cut the bottom if it moved out of the area
974                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
975                                                            maxAllowedExtend.getMinY()), new Coordinate(newArea
976                                                            .getMaxX(), newArea.getMaxY()));
977    
978    //                                      LOGGER.debug("and fix aspect ratio");
979    
980                                            newArea = fixAspectRatio(this.getBounds(), newArea, false);
981                                    }
982                            }
983    
984                            // Exceeds bottom? Move up and maybe cut
985                            if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
986                                    double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
987    //                              LOGGER.debug("Moving area up by " + divY);
988    
989                                    newArea = new Envelope(new Coordinate(newArea.getMinX(),
990                                                    newArea.getMinY() - divY), new Coordinate(newArea
991                                                    .getMaxX(), newArea.getMaxY() - divY));
992    
993                                    if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
994    //                                      LOGGER.debug("Now it exeeds the top border.. cut!");
995                                            // And cut the bottom if it moved out of the area
996                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
997                                                            newArea.getMinY()), new Coordinate(newArea
998                                                            .getMaxX(), maxAllowedExtend.getMaxY()));
999    
1000    //                                      LOGGER.debug("and fix aspect ratio");
1001    
1002                                            newArea = fixAspectRatio(this.getBounds(), newArea, false);
1003                                    }
1004                            }
1005    
1006                            // Exceeds to the right? move and maybe cut
1007                            if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1008    
1009                                    // Move left..
1010                                    double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
1011    //                              LOGGER.debug("Moving area left by " + divX);
1012    
1013                                    newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1014                                                    newArea.getMinY()), new Coordinate(newArea.getMaxX()
1015                                                    - divX, newArea.getMaxY()));
1016    
1017                                    if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1018    //                                      LOGGER.debug("Now it exeeds the left border.. cut!");
1019                                            // And cut the left if it moved out of the area
1020                                            newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(),
1021                                                            newArea.getMinY()), new Coordinate(newArea
1022                                                            .getMaxX(), newArea.getMaxY()));
1023    
1024    //                                      LOGGER.debug("and fix aspect ratio");
1025    
1026                                            newArea = fixAspectRatio(this.getBounds(), newArea, false);
1027                                    }
1028                            }
1029    
1030                            // Exceeds to the left? move and maybe cut
1031                            if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1032    
1033                                    // Move right..
1034                                    double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
1035    //                              LOGGER.debug("Moving area right by " + divX);
1036    
1037                                    newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1038                                                    newArea.getMinY()), new Coordinate(newArea.getMaxX()
1039                                                    - divX, newArea.getMaxY()));
1040    
1041                                    if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1042    //                                      LOGGER.debug("Now it exeeds the right border.. cut!");
1043                                            // And cut the left if it moved out of the area
1044                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
1045                                                            newArea.getMinY()), new Coordinate(maxAllowedExtend
1046                                                            .getMaxX(), newArea.getMaxY()));
1047    
1048    //                                      LOGGER.debug("and fix aspect ratio");
1049    
1050                                            newArea = fixAspectRatio(this.getBounds(), newArea, false);
1051                                    }
1052                            }
1053    
1054                    }
1055    
1056                    return newArea;
1057          }          }
1058    
1059          /**          /**
# Line 1338  public class JMapPane extends JPanel imp Line 1085  public class JMapPane extends JPanel imp
1085           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
1086           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
1087           */           */
1088          public void setMaxZoomScale(Double maxZoomScale) {          public void setMaxZoomScale(final Double maxZoomScale) {
                 // System.out.println("setting max scale to "+maxZoomScale);  
1089                  this.maxZoomScale = maxZoomScale;                  this.maxZoomScale = maxZoomScale;
1090          }          }
1091    
# Line 1350  public class JMapPane extends JPanel imp Line 1096  public class JMapPane extends JPanel imp
1096           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
1097           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
1098           */           */
1099          public void setMinZoomScale(Double minZoomScale) {          public void setMinZoomScale(final Double minZoomScale) {
1100                  this.minZoomScale = minZoomScale;                  this.minZoomScale = minZoomScale;
1101          }          }
1102          // xulu.en  
1103            /**
1104             * Defines an evelope of the viwable area. The JMapPane will never show
1105             * anything outside of this extend.
1106             *
1107             * @param maxExtend
1108             *            <code>null</code> to not have this restriction.
1109             */
1110            public void setMaxExtend(Envelope maxExtend) {
1111                    this.maxExtend = maxExtend;
1112            }
1113    
1114            /**
1115             * Returns the evelope of the viewable area. The JMapPane will never show
1116             * anything outside of this extend. If this has been set to
1117             * <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return
1118             * quickly the context's bounds. It it takes to long to determine the
1119             * context bounds, <code>null</code> is returned.
1120             *
1121             * @param maxExtend
1122             *            <code>null</code> to not have this restriction.
1123             */
1124    
1125            public Envelope getMaxExtend() {
1126                    if (maxExtend == null) {
1127                            try {
1128                                    return fixAspectRatio(
1129                                        this.getBounds(),
1130                            // Kartenbereich um 10% vergroessern
1131                                        JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1),
1132                                        true
1133                                    );
1134                            } catch (IOException e) {
1135                                    LOGGER.warn(
1136                                                                    "maxExtend == null; failed to getLayerBounds of context",
1137                                                                    e);
1138                            }
1139                    }
1140                    return maxExtend;
1141            }
1142    
1143  }  }

Legend:
Removed from v.226  
changed lines
  Added in v.485

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26