/[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 336 by alfonx, Thu Aug 27 09:10:10 2009 UTC branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java revision 506 by alfonx, Sat Oct 31 12:15:37 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 43  import java.awt.Graphics; Line 43  import java.awt.Graphics;
43  import java.awt.Graphics2D;  import java.awt.Graphics2D;
44  import java.awt.LayoutManager;  import java.awt.LayoutManager;
45  import java.awt.Rectangle;  import java.awt.Rectangle;
46    import java.awt.RenderingHints;
47  import java.awt.event.InputEvent;  import java.awt.event.InputEvent;
48  import java.awt.event.MouseEvent;  import java.awt.event.MouseEvent;
49  import java.awt.event.MouseListener;  import java.awt.event.MouseListener;
50  import java.awt.event.MouseMotionListener;  import java.awt.event.MouseMotionListener;
51    import java.awt.geom.AffineTransform;
52  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
53  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
54  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
# Line 58  import java.util.Map; Line 60  import java.util.Map;
60  import javax.swing.JPanel;  import javax.swing.JPanel;
61    
62  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
 import org.geotools.feature.FeatureCollection;  
 import org.geotools.filter.IllegalFilterException;  
 import org.geotools.map.DefaultMapContext;  
63  import org.geotools.map.MapContext;  import org.geotools.map.MapContext;
 import org.geotools.map.MapLayer;  
64  import org.geotools.map.event.MapLayerListEvent;  import org.geotools.map.event.MapLayerListEvent;
65  import org.geotools.map.event.MapLayerListListener;  import org.geotools.map.event.MapLayerListListener;
 import org.geotools.referencing.crs.DefaultGeographicCRS;  
66  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
67  import org.geotools.renderer.label.LabelCacheImpl;  import org.geotools.renderer.label.LabelCacheImpl;
68  import org.geotools.renderer.lite.LabelCache;  import org.geotools.renderer.lite.LabelCache;
 import org.geotools.renderer.lite.LabelCacheDefault;  
69  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;  
70  import org.opengis.filter.FilterFactory2;  import org.opengis.filter.FilterFactory2;
71  import org.opengis.referencing.crs.CoordinateReferenceSystem;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
72    
73    import schmitzm.geotools.JTSUtil;
74  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
75    
76  import com.vividsolutions.jts.geom.Coordinate;  import com.vividsolutions.jts.geom.Coordinate;
77  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
 import com.vividsolutions.jts.geom.Geometry;  
78  import com.vividsolutions.jts.geom.GeometryFactory;  import com.vividsolutions.jts.geom.GeometryFactory;
79    
80  public class JMapPane extends JPanel implements MouseListener,  public class JMapPane extends JPanel implements MouseListener,
81                  MouseMotionListener, PropertyChangeListener, MapLayerListListener {                  MouseMotionListener, PropertyChangeListener, MapLayerListListener {
82          private static Logger LOGGER = Logger.getLogger(JMapPane.class.getName());          private static Logger LOGGER = Logger.getLogger(JMapPane.class);
83    
84          private static final long serialVersionUID = -8647971481359690499L;          private static final long serialVersionUID = -8647971481359690499L;
85    
# Line 107  public class JMapPane extends JPanel imp Line 93  public class JMapPane extends JPanel imp
93    
94          public static final int Select = 4;          public static final int Select = 4;
95    
         private static final int POLYGON = 0;  
   
         private static final int LINE = 1;  
   
         private static final int POINT = 2;  
   
96          /**          /**
97           * what renders the map           * what renders the map
98           */           */
99          GTRenderer renderer;          GTRenderer renderer;
100    
         private GTRenderer highlightRenderer, selectionRenderer;  
   
101          /**          /**
102           * the map context to render           * the map context to render
103           */           */
104          MapContext context;          MapContext context;
105    
         private MapContext selectionContext;  
   
106          /**          /**
107           * the area of the map to draw           * the area of the map to draw
108           */           */
         // xulu.sc  
         // Envelope mapArea;  
109          protected Envelope mapArea;          protected Envelope mapArea;
         // xulu.ec  
110    
111          /**          /**
112           * the size of the pane last time we drew           * the size of the pane last time we drew
113           */           */
         // xulu.sc  
         // private Rectangle oldRect = null;  
114          protected Rectangle oldRect = null;          protected Rectangle oldRect = null;
         // xulu.ec  
115    
116          /**          /** We store the old mapArea for a moment to use it for the
117           * the last map area drawn.          "quick scaled preview" in case of ZoomOut **/
          */  
         // xulu.sc  
         // private Envelope oldMapArea = null;  
118          protected Envelope oldMapArea = null;          protected Envelope oldMapArea = null;
         // xulu.ec  
119    
120          /**          /**
121           * the base image of the map           * the base image of the map
122           */           */
123          protected BufferedImage baseImage, panningImage;          protected BufferedImage baseImage, panningImage;
         // SK: private BufferedImage baseImage, panningImage;  
124    
125          /**          /**
126           * a factory for filters           * a factory for filters
# Line 178  public class JMapPane extends JPanel imp Line 143  public class JMapPane extends JPanel imp
143    
144          LabelCache labelCache = new LabelCacheImpl();          LabelCache labelCache = new LabelCacheImpl();
145    
         // xulu.sc  
         // private boolean reset = false;  
146          protected boolean reset = false;          protected boolean reset = false;
         // xulu.ec  
147    
148          int startX;          int startX;
149    
150          int startY;          int startY;
151    
152            /**
153             * If not <code>null</code>, the {@link JMapPane} will not allow to zoom/pan
154             * out of that area
155             **/
156            private Envelope maxExtend = null;
157    
158            // /**
159            // * Is max. 1 or 0 of the 2 axised allowed to extend the maxExtend? If
160            // * <code>true</code> the extends has to be fully inside maxExtend
161            // **/
162            // boolean maxExtendForceMode = true;
163    
164          private boolean clickable;          private boolean clickable;
165    
166          int lastX;          int lastX;
167    
168          int lastY;          int lastY;
169    
         // xulu.sn  
170          private Double maxZoomScale = Double.MIN_VALUE;          private Double maxZoomScale = Double.MIN_VALUE;
171          private Double minZoomScale = Double.MAX_VALUE;          private Double minZoomScale = Double.MAX_VALUE;
         // xulu.en  
172    
         // sk.sn  
173          /**          /**
174           * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses           * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses
175           * 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 206  public class JMapPane extends JPanel imp Line 177  public class JMapPane extends JPanel imp
177           */           */
178          private boolean panning_started = false;          private boolean panning_started = false;
179    
180          // sk.en          /**
181             * This color is used as the default background color when painting a map.
182             */
183            private Color mapBackgroundColor = Color.WHITE;
184    
185          public JMapPane() {          public JMapPane() {
186                  this(null, true, null, null);                  this(null, true, null, null);
# Line 259  public class JMapPane extends JPanel imp Line 233  public class JMapPane extends JPanel imp
233          }          }
234    
235          public void setRenderer(final GTRenderer renderer) {          public void setRenderer(final GTRenderer renderer) {
236                  Map hints = new HashMap();                  Map<Object, Object> hints = new HashMap<Object, Object>();
                   
                 this.renderer = renderer;  
                   
                 if (renderer instanceof StreamingRenderer || renderer instanceof TransitionShapefileRenderer) {  
                         hints = renderer.getRendererHints();  
                         if (hints == null) {  
                                 hints = new HashMap();  
                         }  
                         if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {  
                                 labelCache = (LabelCache) hints  
                                                 .get(StreamingRenderer.LABEL_CACHE_KEY);  
                         } else {  
                                 hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);  
                         }  
237    
238                          hints.put("memoryPreloadingEnabled", Boolean.TRUE);                  this.renderer = renderer;
239                                            // MS: Apply hint also for ShapeFileRenderer
240                          renderer.setRendererHints(hints);                  // if (renderer instanceof StreamingRenderer) {
241                    hints = renderer.getRendererHints();
242                    if (hints == null) {
243                            hints = new HashMap<Object, Object>();
244                    }
245                    if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {
246                            labelCache = (LabelCache) hints
247                                            .get(StreamingRenderer.LABEL_CACHE_KEY);
248                    } else {
249                            hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
250                  }                  }
251    
252  //              this.highlightRenderer = new StreamingRenderer();                  hints.put("memoryPreloadingEnabled", Boolean.TRUE);
 //              this.selectionRenderer = new StreamingRenderer();  
253    
254  //              highlightRenderer.setRendererHints(hints);                  renderer.setRendererHints(hints);
255  //              selectionRenderer.setRendererHints(hints);                  // }
                   
 //              renderer.setRendererHints(hints);  
256    
257                  if (this.context != null) {                  if (this.context != null) {
258                          this.renderer.setContext(this.context);                          this.renderer.setContext(this.context);
# Line 339  public class JMapPane extends JPanel imp Line 305  public class JMapPane extends JPanel imp
305                  this.zoomFactor = zoomFactor;                  this.zoomFactor = zoomFactor;
306          }          }
307    
   
308          protected void paintComponent(final Graphics g) {          protected void paintComponent(final Graphics g) {
309                  super.paintComponent(g);                  super.paintComponent(g);
310    
# Line 365  public class JMapPane extends JPanel imp Line 330  public class JMapPane extends JPanel imp
330                                  reset = false; /* forget about the reset */                                  reset = false; /* forget about the reset */
331                                  oldRect = r; /* store what the current size is */                                  oldRect = r; /* store what the current size is */
332    
333                                  mapArea = fixAspectRatio(r, mapArea);                                  mapArea = JTSUtil.fixAspectRatio(r, mapArea, false);
334                          }                          }
335                  }                  }
336    
# Line 378  public class JMapPane extends JPanel imp Line 343  public class JMapPane extends JPanel imp
343                                          .getCoordinateReferenceSystem());                                          .getCoordinateReferenceSystem());
344                  }                  }
345    
346                  if (changed ) { /* if the map changed then redraw */                  if (changed) { /* if the map changed then redraw */
347                          changed = false;                          changed = false;
348                            
349                            // The baseImage should not have alpha
350                          baseImage = new BufferedImage(dr.width, dr.height,                          baseImage = new BufferedImage(dr.width, dr.height,
351                                          BufferedImage.TYPE_INT_ARGB);                                          BufferedImage.TYPE_INT_RGB);
352    
353                          final Graphics2D ig = baseImage.createGraphics();                          final Graphics2D ig = baseImage.createGraphics();
354                          /* System.out.println("rendering"); */                          ig.setBackground(getMapBackgroundColor());
355                          if (renderer.getContext() != null)                          ig.fillRect(0, 0, dr.width, dr.height);
356                            
357    //                      /* System.out.println("rendering"); */
358                            if (renderer.getContext() != null)
359                                  renderer.setContext(context);                                  renderer.setContext(context);
360                          labelCache.clear(); // work around anoying labelcache bug  
361                            // TODO is this clearing still needed? We already replace all that stuff whenever we change the style. Deactivated 31.10.09.. let's see
362    //                      labelCache.clear(); // work around anoying labelcache bug
363    
364                          // draw the map                          // draw the map
365                          renderer.paint((Graphics2D) ig, dr, mapArea);                          renderer.paint(ig, dr, mapArea);
366    
367                          // TODO nur machen, wenn panning beginnt                          panningImage = new BufferedImage(dr.width, dr.height, BufferedImage.TYPE_INT_RGB);
                         panningImage = new BufferedImage(dr.width, dr.height,  
                                         BufferedImage.TYPE_INT_RGB);  
368    
369                  }                  }
370    
371                  ((Graphics2D) g).drawImage(baseImage, 0, 0, this);                  ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
372          }          }
373    
         private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea) {  
   
                 final double mapWidth = mapArea.getWidth(); /* get the extent of the map */  
                 final double mapHeight = mapArea.getHeight();  
                 final double scaleX = r.getWidth() / mapArea.getWidth(); /*  
                                                                                                                          * calculate the new  
                                                                                                                          * scale  
                                                                                                                          */  
   
                 final double scaleY = r.getHeight() / mapArea.getHeight();  
                 double scale = 1.0; // stupid compiler!  
   
                 if (scaleX < scaleY) { /* pick the smaller scale */  
                         scale = scaleX;  
                 } else {  
                         scale = scaleY;  
                 }  
   
                 /* calculate the difference in width and height of the new extent */  
                 final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);  
                 final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);  
   
                 /*  
                  * System.out.println("delta x " + deltaX);  
                  * System.out.println("delta y " + deltaY);  
                  */  
   
                 /* create the new extent */  
                 final Coordinate ll = new Coordinate(mapArea.getMinX() - (deltaX / 2.0),  
                                 mapArea.getMinY() - (deltaY / 2.0));  
                 final Coordinate ur = new Coordinate(mapArea.getMaxX() + (deltaX / 2.0),  
                                 mapArea.getMaxY() + (deltaY / 2.0));  
   
                 return new Envelope(ll, ur);  
         }  
   
 //      public void doSelection(final double x, final double y, final MapLayer layer) {  
 //  
 //              final 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(final Geometry geometry, final MapLayer layer)  
 //                      throws IndexOutOfBoundsException {  
 //              org.opengis.filter.spatial.BinarySpatialOperator f = null;  
 //  
 //              if ((context == null) || (layer == null)) {  
 //                      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 (final 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()) { SimpleFeature feat  
 //                       * = (SimpleFeature) fi.next(); System.out.println("selected " +  
 //                       * feat.getAttribute("STATE_NAME")); }  
 //                       */  
 //              } catch (final IllegalFilterException e) {  
 //                      // TODO Auto-generated catch block  
 //                      e.printStackTrace();  
 //              }  
 //              return;  
 //      }  
   
374          public void mouseClicked(final MouseEvent e) {          public void mouseClicked(final MouseEvent e) {
375                  if (mapArea == null) return;                  if (mapArea == null)
376                            return;
377                  // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"                  // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
378                  // h:"+mapArea.getHeight());                  // h:"+mapArea.getHeight());
379                  final Rectangle bounds = this.getBounds();                  final Rectangle bounds = this.getBounds();
# Line 528  public class JMapPane extends JPanel imp Line 387  public class JMapPane extends JPanel imp
387                  final double width2 = width / 2.0;                  final double width2 = width / 2.0;
388                  final double height2 = height / 2.0;                  final double height2 = height / 2.0;
389                  // xulu.ec                  // xulu.ec
390                  final double mapX = ((x * width) / (double) bounds.width) + mapArea.getMinX();                  final double mapX = ((x * width) / (double) bounds.width)
391                                    + mapArea.getMinX();
392                  final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)                  final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
393                                  + mapArea.getMinY();                                  + mapArea.getMinY();
394    
# Line 559  public class JMapPane extends JPanel imp Line 419  public class JMapPane extends JPanel imp
419                          zlevel = 1.0 / zoomFactor;                          zlevel = 1.0 / zoomFactor;
420    
421                          break;                          break;
422  //                  //
423  //              case Select:                  // case Select:
424  //                      doSelection(mapX, mapY, selectionLayer);                  // doSelection(mapX, mapY, selectionLayer);
425  //                  //
426  //                      return;                  // return;
427    
428                  default:                  default:
429                          return;                          return;
# Line 634  public class JMapPane extends JPanel imp Line 494  public class JMapPane extends JPanel imp
494                                  final int dy = lastY - startY;                                  final int dy = lastY - startY;
495                                  // System.out.println("translate "+dx+","+dy);                                  // System.out.println("translate "+dx+","+dy);
496                                  final Graphics2D g2 = panningImage.createGraphics();                                  final Graphics2D g2 = panningImage.createGraphics();
497                                  g2.setBackground(new Color(240, 240, 240)); // TODO richtige                                  g2.setBackground(getMapBackgroundColor());
498                                                                                                                          // farbe? am besten  
                                                                                                                         // vom L&F die  
                                                                                                                         // hintergrundfarbe  
                                                                                                                         // auslesen...  
                                   
499                                  g2.clearRect(0, 0, this.getWidth(), this.getHeight());                                  g2.clearRect(0, 0, this.getWidth(), this.getHeight());
500                                  g2.drawImage(baseImage, dx, dy, this);                                  g2.drawImage(baseImage, dx, dy, this);
501                                  graphics.drawImage(panningImage, 0, 0, this);                                  graphics.drawImage(panningImage, 0, 0, this);
# Line 661  public class JMapPane extends JPanel imp Line 517  public class JMapPane extends JPanel imp
517                          lastX = x;                          lastX = x;
518                          lastY = y;                          lastY = y;
519                          drawRectangle(graphics);                          drawRectangle(graphics);
520                  }                  }
521  //              else if (state == JMapPane.Select && selectionLayer != null) {                  // else if (state == JMapPane.Select && selectionLayer != null) {
522  //                  //
523  //                      // construct a new bbox filter                  // // construct a new bbox filter
524  //                      final Rectangle bounds = this.getBounds();                  // final Rectangle bounds = this.getBounds();
525  //                  //
526  //                      final double mapWidth = mapArea.getWidth();                  // final double mapWidth = mapArea.getWidth();
527  //                      final double mapHeight = mapArea.getHeight();                  // final double mapHeight = mapArea.getHeight();
528  //                  //
529  //                      final double x1 = ((this.startX * mapWidth) / (double) bounds.width)                  // final double x1 = ((this.startX * mapWidth) / (double) bounds.width)
530  //                                      + mapArea.getMinX();                  // + mapArea.getMinX();
531  //                      final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) / (double) bounds.height)                  // final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) /
532  //                                      + mapArea.getMinY();                  // (double) bounds.height)
533  //                      final double x2 = ((x * mapWidth) / (double) bounds.width)                  // + mapArea.getMinY();
534  //                                      + mapArea.getMinX();                  // final double x2 = ((x * mapWidth) / (double) bounds.width)
535  //                      final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double) bounds.height)                  // + mapArea.getMinX();
536  //                                      + mapArea.getMinY();                  // final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double)
537  //                      final double left = Math.min(x1, x2);                  // bounds.height)
538  //                      final double right = Math.max(x1, x2);                  // + mapArea.getMinY();
539  //                      final double bottom = Math.min(y1, y2);                  // final double left = Math.min(x1, x2);
540  //                      final double top = Math.max(y1, y2);                  // final double right = Math.max(x1, x2);
541  //                  // final double bottom = Math.min(y1, y2);
542  //                      String name = selectionLayer.getFeatureSource().getSchema()                  // final double top = Math.max(y1, y2);
543  //                                      .getDefaultGeometry().getName();                  //
544  //                  // String name = selectionLayer.getFeatureSource().getSchema()
545  //                      if (name == "") {                  // .getDefaultGeometry().getName();
546  //                              name = "the_geom";                  //
547  //                      }                  // if (name == "") {
548  //                      final Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,                  // name = "the_geom";
549  //                                      getContext().getCoordinateReferenceSystem().toString());                  // }
550  //                      if (selectionManager != null) {                  // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
551  //                              selectionManager.selectionChanged(this, bb);                  // top,
552  //                      }                  // getContext().getCoordinateReferenceSystem().toString());
553  //                  // if (selectionManager != null) {
554  //                      graphics.setXORMode(Color.green);                  // selectionManager.selectionChanged(this, bb);
555  //                  // }
556  //                      /*                  //
557  //                       * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }                  // graphics.setXORMode(Color.green);
558  //                       */                  //
559  //                  // /*
560  //                      // draw new box                  // * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
561  //                      lastX = x;                  // */
562  //                      lastY = y;                  //
563  //                      drawRectangle(graphics);                  // // draw new box
564  //              }                  // lastX = x;
565                    // lastY = y;
566                    // drawRectangle(graphics);
567                    // }
568    
569          }          }
570    
# Line 716  public class JMapPane extends JPanel imp Line 575  public class JMapPane extends JPanel imp
575                          final int y2, final MouseEvent e) {                          final int y2, final MouseEvent e) {
576    
577                  /****                  /****
578                   * If no layer is availabe we dont want a NullPointerException                   * If no layers exist, we ignore the drag.
579                   */                   */
580                  if (mapArea == null)                  if (context.getLayerCount() <= 0) {
581                          return;                          return;
582                    }
583    
584                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
585                  // + x2 + "," + y2);                  // + x2 + "," + y2);
# Line 755  public class JMapPane extends JPanel imp Line 615  public class JMapPane extends JPanel imp
615                          // new edges                          // new edges
616                          final double left = mapArea.getMinX() - deltaX1;                          final double left = mapArea.getMinX() - deltaX1;
617                          final double right = mapArea.getMaxX() - deltaX1;                          final double right = mapArea.getMaxX() - deltaX1;
   
                         // now for Y  
618                          final double deltaY1 = endY - startY;                          final double deltaY1 = endY - startY;
619    
620                          // System.out.println("deltaY " + deltaY1);                          // System.out.println("deltaY " + deltaY1);
# Line 766  public class JMapPane extends JPanel imp Line 624  public class JMapPane extends JPanel imp
624                          final Coordinate ur = new Coordinate(right, top);                          final Coordinate ur = new Coordinate(right, top);
625                          // xulu.sc                          // xulu.sc
626                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
627                          setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur)));  
628                            setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
629                          // xulu.ec                          // xulu.ec
630                  } else if (state == JMapPane.ZoomIn) {                  } else if (state == JMapPane.ZoomIn) {
631    
# Line 789  public class JMapPane extends JPanel imp Line 648  public class JMapPane extends JPanel imp
648                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
649                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
650    
                         // 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  
651                  } else if (state == JMapPane.ZoomOut) {                  } else if (state == JMapPane.ZoomOut) {
652                          drawRectangle(this.getGraphics());                          drawRectangle(this.getGraphics());
653    
# Line 830  public class JMapPane extends JPanel imp Line 675  public class JMapPane extends JPanel imp
675                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
676    
677                          // xulu.ec                          // xulu.ec
678                  }                  }
679  //              else if (state == JMapPane.Select && selectionLayer != null) {                  // else if (state == JMapPane.Select && selectionLayer != null) {
680  //                      final double left = Math.min(startX, endX);                  // final double left = Math.min(startX, endX);
681  //                      final double right = Math.max(startX, endX);                  // final double right = Math.max(startX, endX);
682  //                      final double bottom = Math.min(startY, endY);                  // final double bottom = Math.min(startY, endY);
683  //                      final double top = Math.max(startY, endY);                  // final double top = Math.max(startY, endY);
684  //                  //
685  //                      String name = selectionLayer.getFeatureSource().getSchema()                  // String name = selectionLayer.getFeatureSource().getSchema()
686  //                                      .getDefaultGeometry().getLocalName();                  // .getDefaultGeometry().getLocalName();
687  //                  //
688  //                      if (name == "") {                  // if (name == "") {
689  //                              name = "the_geom";                  // name = "the_geom";
690  //                      }                  // }
691  //                      final Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,                  // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
692  //                                      getContext().getCoordinateReferenceSystem().toString());                  // top,
693  //                      // System.out.println(bb.toString());                  // getContext().getCoordinateReferenceSystem().toString());
694  //                      if (selectionManager != null) {                  // // System.out.println(bb.toString());
695  //                              selectionManager.selectionChanged(this, bb);                  // if (selectionManager != null) {
696  //                      }                  // selectionManager.selectionChanged(this, bb);
697  //                      /*                  // }
698  //                       * FeatureCollection fc; selection = null; try { fc =                  // /*
699  //                       * selectionLayer.getFeatureSource().getFeatures(bb); selection =                  // * FeatureCollection fc; selection = null; try { fc =
700  //                       * fc; } catch (IOException e) { e.printStackTrace(); }                  // * selectionLayer.getFeatureSource().getFeatures(bb); selection =
701  //                       */                  // * fc; } catch (IOException e) { e.printStackTrace(); }
702  //              }                  // */
703                    // }
704    
705                  // xulu.so                  // xulu.so
706                  // setMapArea(mapArea);                  // setMapArea(mapArea);
# Line 866  public class JMapPane extends JPanel imp Line 712  public class JMapPane extends JPanel imp
712                  return clickable;                  return clickable;
713          }          }
714    
         private org.geotools.styling.Style setupStyle(final int type, final Color color) {  
                 final StyleFactory sf = org.geotools.factory.CommonFactoryFinder  
                                 .getStyleFactory(null);  
                 final StyleBuilder sb = new StyleBuilder();  
   
                 org.geotools.styling.Style s = sf.createStyle();  
                 s.setTitle("selection");  
   
                 // TODO parameterise the color  
                 final PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);  
                 ps.setStroke(sb.createStroke(color));  
   
                 final LineSymbolizer ls = sb.createLineSymbolizer(color);  
                 final Graphic h = sb.createGraphic();  
                 h.setMarks(new Mark[] { sb.createMark("square", color) });  
   
                 final PointSymbolizer pts = sb.createPointSymbolizer(h);  
   
                 // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});  
                 switch (type) {  
                 case POLYGON:  
                         s = sb.createStyle(ps);  
   
                         break;  
   
                 case POINT:  
                         s = sb.createStyle(pts);  
   
                         break;  
   
                 case LINE:  
                         s = sb.createStyle(ls);  
                 }  
   
                 return s;  
         }  
   
   
715          public void propertyChange(final PropertyChangeEvent evt) {          public void propertyChange(final PropertyChangeEvent evt) {
716                  final String prop = evt.getPropertyName();                  final String prop = evt.getPropertyName();
717    
# Line 934  public class JMapPane extends JPanel imp Line 742  public class JMapPane extends JPanel imp
742                                          mapArea = context.getLayerBounds();                                          mapArea = context.getLayerBounds();
743                                  // xulu.ec                                  // xulu.ec
744                          } catch (final IOException e) {                          } catch (final IOException e) {
745                                  // TODO Auto-generated catch block                                  LOGGER.error(e);
                                 e.printStackTrace();  
746                          }                          }
747    
748                          reset = true;                          reset = true;
# Line 986  public class JMapPane extends JPanel imp Line 793  public class JMapPane extends JPanel imp
793          public void mouseMoved(final MouseEvent e) {          public void mouseMoved(final MouseEvent e) {
794          }          }
795    
   
   
796          // xulu.sn          // xulu.sn
797          /**          /**
798           * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste           * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
# Line 1006  public class JMapPane extends JPanel imp Line 811  public class JMapPane extends JPanel imp
811                  if (env == null)                  if (env == null)
812                          return env;                          return env;
813    
814                    Envelope newArea = null;
815    
816                  /**                  /**
817                   * Correct the aspect Ratio before we check the rest. Otherwise we might                   * Correct the aspect Ratio before we check the rest. Otherwise we might
818                   * easily fail.                   * easily fail. We allow to grow here, because we don't check against
819                     * the maxExtend
820                   */                   */
821                  env = fixAspectRatio(this.getBounds(), env);                  env = JTSUtil.fixAspectRatio(this.getBounds(), env, true);
822    
823                  final double scale = env.getWidth() / getWidth();                  final double scale = env.getWidth() / getWidth();
824                  final double centerX = env.getMinX() + env.getWidth() / 2.;                  final double centerX = env.getMinX() + env.getWidth() / 2.;
825                  final double centerY = env.getMinY() + env.getHeight() / 2.;                  final double centerY = env.getMinY() + env.getHeight() / 2.;
826                  double newWidth2;                  double newWidth2 = 0;
827                  double newHeight2;                  double newHeight2 = 0;
828                  if (scale < getMaxZoomScale()) {                  if (scale < getMaxZoomScale()) {
829                          // ****************************************************************************                          // ****************************************************************************
830                          // Wir zoomen weiter rein als erlaubt => Anpassen des envelope                          // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
# Line 1033  public class JMapPane extends JPanel imp Line 841  public class JMapPane extends JPanel imp
841                          // ****************************************************************************                          // ****************************************************************************
842                          // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen                          // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
843                          // ****************************************************************************                          // ****************************************************************************
844                          return env;                          newArea = env;
845                    }
846    
847                    if (newArea == null) {
848    
849                            final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
850                                            - newHeight2);
851                            final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
852                                            + newHeight2);
853    
854                            newArea = new Envelope(ll, ur);
855                  }                  }
856    
857                  final Coordinate ll = new Coordinate(centerX - newWidth2, centerY                  Envelope maxAllowedExtend = getMaxExtend();
858                                  - newHeight2);                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
859                  final Coordinate ur = new Coordinate(centerX + newWidth2, centerY                                  && newArea != null && !newArea.isNull()
860                                  + newHeight2);                                  && !Double.isNaN(newArea.getMinX())
861                                    && !Double.isNaN(newArea.getMaxX())
862                                    && !Double.isNaN(newArea.getMinY())
863                                    && !Double.isNaN(newArea.getMaxY())) {
864                            /*
865                             * If a maxExtend is set, we have to honour that...
866                             */
867    
868                            // Exceeds top? Move down and maybe cut
869                            if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
870                                    double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
871                                    // LOGGER.debug("Moving area down by " + divY);
872    
873                                    newArea = new Envelope(new Coordinate(newArea.getMinX(),
874                                                    newArea.getMinY() - divY), new Coordinate(newArea
875                                                    .getMaxX(), newArea.getMaxY() - divY));
876    
877                                    if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
878                                            // LOGGER.debug("Now it exeeds the bottom border.. cut!");
879                                            // And cut the bottom if it moved out of the area
880                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
881                                                            maxAllowedExtend.getMinY()), new Coordinate(newArea
882                                                            .getMaxX(), newArea.getMaxY()));
883    
884                                            // LOGGER.debug("and fix aspect ratio");
885    
886                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
887                                                            false);
888                                    }
889                            }
890    
891                            // Exceeds bottom? Move up and maybe cut
892                            if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
893                                    double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
894                                    // LOGGER.debug("Moving area up by " + divY);
895    
896                                    newArea = new Envelope(new Coordinate(newArea.getMinX(),
897                                                    newArea.getMinY() - divY), new Coordinate(newArea
898                                                    .getMaxX(), newArea.getMaxY() - divY));
899    
900                                    if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
901                                            // LOGGER.debug("Now it exeeds the top border.. cut!");
902                                            // And cut the bottom if it moved out of the area
903                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
904                                                            newArea.getMinY()), new Coordinate(newArea
905                                                            .getMaxX(), maxAllowedExtend.getMaxY()));
906    
907                                            // LOGGER.debug("and fix aspect ratio");
908    
909                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
910                                                            false);
911                                    }
912                            }
913    
914                            // Exceeds to the right? move and maybe cut
915                            if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
916    
917                                    // Move left..
918                                    double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
919                                    // LOGGER.debug("Moving area left by " + divX);
920    
921                                    newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
922                                                    newArea.getMinY()), new Coordinate(newArea.getMaxX()
923                                                    - divX, newArea.getMaxY()));
924    
925                                    if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
926                                            // LOGGER.debug("Now it exeeds the left border.. cut!");
927                                            // And cut the left if it moved out of the area
928                                            newArea = new Envelope(new Coordinate(maxAllowedExtend
929                                                            .getMinX(), newArea.getMinY()), new Coordinate(
930                                                            newArea.getMaxX(), newArea.getMaxY()));
931    
932                                            // LOGGER.debug("and fix aspect ratio");
933    
934                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
935                                                            false);
936                                    }
937                            }
938    
939                            // Exceeds to the left? move and maybe cut
940                            if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
941    
942                                    // Move right..
943                                    double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
944                                    // LOGGER.debug("Moving area right by " + divX);
945    
946                                    newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
947                                                    newArea.getMinY()), new Coordinate(newArea.getMaxX()
948                                                    - divX, newArea.getMaxY()));
949    
950                                    if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
951                                            // LOGGER.debug("Now it exeeds the right border.. cut!");
952                                            // And cut the left if it moved out of the area
953                                            newArea = new Envelope(new Coordinate(newArea.getMinX(),
954                                                            newArea.getMinY()), new Coordinate(maxAllowedExtend
955                                                            .getMaxX(), newArea.getMaxY()));
956    
957                  return new Envelope(ll, ur);                                          // LOGGER.debug("and fix aspect ratio");
958    
959                                            newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
960                                                            false);
961                                    }
962                            }
963    
964                    }
965    
966                    return newArea;
967          }          }
968    
969          /**          /**
# Line 1068  public class JMapPane extends JPanel imp Line 990  public class JMapPane extends JPanel imp
990    
991          /**          /**
992           * 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
993           * the two.           * the two. If <code>null</code> is passed, Double.MINVALUE are used which
994             * mean there is no restriction.
995           *           *
996           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
997           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
998           */           */
999          public void setMaxZoomScale(final Double maxZoomScale) {          public void setMaxZoomScale(final Double maxZoomScale) {
1000                  // System.out.println("setting max scale to "+maxZoomScale);                  this.maxZoomScale = maxZoomScale == null ? Double.MIN_VALUE
1001                  this.maxZoomScale = maxZoomScale;                                  : maxZoomScale;
1002          }          }
1003    
1004          /**          /**
1005           * Set the minimum (nearest) allowed zoom scale. This is the bigger number           * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1006           * value of the two.           * value of the two. If <code>null</code> is passed, Double.MAXVALUE are
1007             * used which mean there is no restriction.
1008             *
1009           *           *
1010           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
1011           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
1012           */           */
1013          public void setMinZoomScale(final Double minZoomScale) {          public void setMinZoomScale(final Double minZoomScale) {
1014                  this.minZoomScale = minZoomScale;                  this.minZoomScale = minZoomScale == null ? Double.MAX_VALUE
1015                                    : minZoomScale;
1016            }
1017    
1018            /**
1019             * Defines an evelope of the viwable area. The JMapPane will never show
1020             * anything outside of this extend.
1021             *
1022             * @param maxExtend
1023             *            <code>null</code> to not have this restriction.
1024             */
1025            public void setMaxExtend(Envelope maxExtend) {
1026                    this.maxExtend = maxExtend;
1027            }
1028    
1029            /**
1030             * Returns the evelope of the viewable area. The JMapPane will never show
1031             * anything outside of this extend. If this has been set to
1032             * <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return
1033             * quickly the context's bounds. It it takes to long to determine the
1034             * context bounds, <code>null</code> is returned.
1035             *
1036             * @param maxExtend
1037             *            <code>null</code> to not have this restriction.
1038             */
1039    
1040            public Envelope getMaxExtend() {
1041                    if (maxExtend == null) {
1042                            try {
1043                                    return JTSUtil.fixAspectRatio(this.getBounds(),
1044                                                    // Kartenbereich um 10% vergroessern
1045                                                    JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1),
1046                                                    true);
1047                            } catch (IOException e) {
1048                                    LOGGER
1049                                                    .warn(
1050                                                                    "maxExtend == null; failed to getLayerBounds of context",
1051                                                                    e);
1052                            }
1053                    }
1054                    return maxExtend;
1055            }
1056    
1057            /**
1058             * Set the background color of the map.
1059             * @param if <code>null</code>, white is used.
1060             */
1061            public void setMapBackgroundColor(Color bgColor) {
1062                    if (bgColor == null) bgColor = Color.WHITE;
1063                    this.mapBackgroundColor = bgColor;
1064            }
1065    
1066            /**
1067             * Returns the background {@link Color} of the map pane. Default is white.
1068            **/
1069            public Color getMapBackgroundColor() {
1070                    return mapBackgroundColor;
1071          }          }
         // xulu.en  
1072    
1073  }  }

Legend:
Removed from v.336  
changed lines
  Added in v.506

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26