/[schmitzm]/trunk/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 506 - (hide annotations)
Sat Oct 31 12:15:37 2009 UTC (15 years, 4 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
File size: 32288 byte(s)
* Cleaned up the new methods for drawing preview image when zooming.
1 mojays 2 /*
2     * GeoTools - OpenSource mapping toolkit
3     * http://geotools.org
4     * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
5     *
6     * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation;
9     * version 2.1 of the License.
10     *
11     * This library is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15     */
16 alfonx 388 package gtmig.org.geotools.swing;
17 mojays 2
18     /**
19     * <b>Xulu:<br>
20 mojays 114 * Code taken from gt-2.4.5 to make some changes (marked with {@code xulu}),
21 mojays 2 * which can not be realized in a subclass:</b>
22     * <ul>
23     * <li>{@link #getMapArea()} declared as {@code final}<li>
24     * <li>some variables declared as {@code protected}</li>
25     * <li>minimal/maximal zoom scale</li>
26     * <li>zoom in and zoom out via mouse click realized by setMapArea(..)</li>
27     * </ul>
28     * <br><br>
29     * A simple map container that is a JPanel with a map in. provides simple
30     * pan,zoom, highlight and selection The mappane stores an image of the map
31     * (drawn from the context) and an image of the slected feature(s) to speed up
32     * rendering of the highlights. Thus the whole map is only redrawn when the bbox
33     * changes, selection is only redrawn when the selected feature changes.
34     *
35     *
36     * @author Ian Turton
37     *
38     */
39    
40     import java.awt.Color;
41     import java.awt.Cursor;
42     import java.awt.Graphics;
43     import java.awt.Graphics2D;
44     import java.awt.LayoutManager;
45     import java.awt.Rectangle;
46 alfonx 505 import java.awt.RenderingHints;
47 mojays 2 import java.awt.event.InputEvent;
48     import java.awt.event.MouseEvent;
49     import java.awt.event.MouseListener;
50     import java.awt.event.MouseMotionListener;
51 alfonx 505 import java.awt.geom.AffineTransform;
52 mojays 2 import java.awt.image.BufferedImage;
53     import java.beans.PropertyChangeEvent;
54     import java.beans.PropertyChangeListener;
55     import java.io.IOException;
56     import java.util.Date;
57     import java.util.HashMap;
58     import java.util.Map;
59    
60     import javax.swing.JPanel;
61    
62     import org.apache.log4j.Logger;
63     import org.geotools.map.MapContext;
64     import org.geotools.map.event.MapLayerListEvent;
65     import org.geotools.map.event.MapLayerListListener;
66     import org.geotools.renderer.GTRenderer;
67 alfonx 336 import org.geotools.renderer.label.LabelCacheImpl;
68 mojays 2 import org.geotools.renderer.lite.LabelCache;
69     import org.geotools.renderer.lite.StreamingRenderer;
70     import org.opengis.filter.FilterFactory2;
71     import org.opengis.referencing.crs.CoordinateReferenceSystem;
72    
73 mojays 436 import schmitzm.geotools.JTSUtil;
74 mojays 2 import schmitzm.swing.SwingUtil;
75    
76     import com.vividsolutions.jts.geom.Coordinate;
77     import com.vividsolutions.jts.geom.Envelope;
78     import com.vividsolutions.jts.geom.GeometryFactory;
79    
80     public class JMapPane extends JPanel implements MouseListener,
81 alfonx 336 MouseMotionListener, PropertyChangeListener, MapLayerListListener {
82 alfonx 452 private static Logger LOGGER = Logger.getLogger(JMapPane.class);
83 alfonx 153
84 alfonx 144 private static final long serialVersionUID = -8647971481359690499L;
85 mojays 2
86 alfonx 144 public static final int Reset = 0;
87 mojays 2
88 alfonx 144 public static final int ZoomIn = 1;
89 mojays 2
90 alfonx 144 public static final int ZoomOut = 2;
91 mojays 2
92 alfonx 144 public static final int Pan = 3;
93 mojays 2
94 alfonx 144 public static final int Select = 4;
95 mojays 2
96 alfonx 144 /**
97     * what renders the map
98     */
99     GTRenderer renderer;
100 mojays 2
101 alfonx 144 /**
102     * the map context to render
103     */
104     MapContext context;
105 mojays 2
106 alfonx 144 /**
107     * the area of the map to draw
108     */
109     protected Envelope mapArea;
110 mojays 2
111 alfonx 144 /**
112     * the size of the pane last time we drew
113     */
114     protected Rectangle oldRect = null;
115 mojays 2
116 alfonx 506 /** We store the old mapArea for a moment to use it for the
117     "quick scaled preview" in case of ZoomOut **/
118 alfonx 144 protected Envelope oldMapArea = null;
119 mojays 2
120 alfonx 144 /**
121     * the base image of the map
122     */
123     protected BufferedImage baseImage, panningImage;
124 mojays 2
125 alfonx 144 /**
126     * a factory for filters
127     */
128     FilterFactory2 ff;
129 mojays 2
130 alfonx 144 /**
131     * a factory for geometries
132     */
133     GeometryFactory gf = new GeometryFactory(); // FactoryFinder.getGeometryFactory(null);
134 mojays 2
135 alfonx 144 private int state = ZoomIn;
136 mojays 2
137 alfonx 144 /**
138     * how far to zoom in or out
139     */
140     private double zoomFactor = 2.0;
141 mojays 2
142 alfonx 144 boolean changed = true;
143 mojays 2
144 alfonx 336 LabelCache labelCache = new LabelCacheImpl();
145 mojays 2
146 alfonx 144 protected boolean reset = false;
147 mojays 2
148 alfonx 144 int startX;
149 mojays 2
150 alfonx 144 int startY;
151 mojays 2
152 alfonx 414 /**
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 alfonx 144 private boolean clickable;
165 mojays 2
166 alfonx 144 int lastX;
167 mojays 2
168 alfonx 144 int lastY;
169 mojays 2
170 alfonx 144 private Double maxZoomScale = Double.MIN_VALUE;
171     private Double minZoomScale = Double.MAX_VALUE;
172 mojays 2
173 alfonx 144 /**
174     * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses
175     * Flag wird benutzt um nur einmal den passenden Cursor nur einmal zu
176     * setzen.
177     */
178 mojays 2 private boolean panning_started = false;
179    
180 alfonx 505 /**
181     * This color is used as the default background color when painting a map.
182     */
183     private Color mapBackgroundColor = Color.WHITE;
184    
185 alfonx 144 public JMapPane() {
186     this(null, true, null, null);
187     }
188 mojays 2
189 alfonx 144 /**
190     * create a basic JMapPane
191     *
192     * @param render
193     * - how to draw the map
194     * @param context
195     * - the map context to display
196     */
197 alfonx 307 public JMapPane(final GTRenderer render, final MapContext context) {
198 alfonx 144 this(null, true, render, context);
199     }
200 mojays 2
201 alfonx 144 /**
202     * full constructor extending JPanel
203     *
204     * @param layout
205     * - layout (probably shouldn't be set)
206     * @param isDoubleBuffered
207     * - a Swing thing I don't really understand
208     * @param render
209     * - what to draw the map with
210     * @param context
211     * - what to draw
212     */
213 alfonx 307 public JMapPane(final LayoutManager layout, final boolean isDoubleBuffered,
214     final GTRenderer render, final MapContext context) {
215 alfonx 144 super(layout, isDoubleBuffered);
216 mojays 2
217 alfonx 144 ff = (FilterFactory2) org.geotools.factory.CommonFactoryFinder
218     .getFilterFactory(null);
219     setRenderer(render);
220 mojays 2
221 alfonx 144 setContext(context);
222 mojays 2
223 alfonx 144 this.addMouseListener(this);
224     this.addMouseMotionListener(this);
225     setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
226     }
227 mojays 2
228 alfonx 144 /**
229     * get the renderer
230     */
231     public GTRenderer getRenderer() {
232     return renderer;
233     }
234 mojays 2
235 alfonx 307 public void setRenderer(final GTRenderer renderer) {
236 alfonx 414 Map<Object, Object> hints = new HashMap<Object, Object>();
237    
238 alfonx 307 this.renderer = renderer;
239 alfonx 504 // MS: Apply hint also for ShapeFileRenderer
240     // 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 alfonx 307
252 alfonx 504 hints.put("memoryPreloadingEnabled", Boolean.TRUE);
253 alfonx 414
254 alfonx 504 renderer.setRendererHints(hints);
255     // }
256 mojays 2
257 alfonx 144 if (this.context != null) {
258     this.renderer.setContext(this.context);
259     }
260     }
261 mojays 2
262 alfonx 144 public MapContext getContext() {
263     return context;
264     }
265 mojays 2
266 alfonx 307 public void setContext(final MapContext context) {
267 alfonx 144 if (this.context != null) {
268     this.context.removeMapLayerListListener(this);
269     }
270 mojays 2
271 alfonx 144 this.context = context;
272 mojays 2
273 alfonx 144 if (context != null) {
274     this.context.addMapLayerListListener(this);
275     }
276 mojays 2
277 alfonx 144 if (renderer != null) {
278     renderer.setContext(this.context);
279     }
280     }
281 mojays 2
282 alfonx 144 public Envelope getMapArea() {
283     return mapArea;
284     }
285 mojays 2
286 alfonx 307 public void setMapArea(final Envelope mapArea) {
287 alfonx 144 this.mapArea = mapArea;
288     }
289 mojays 2
290 alfonx 144 public int getState() {
291     return state;
292     }
293 mojays 2
294 alfonx 307 public void setState(final int state) {
295 alfonx 144 this.state = state;
296 mojays 2
297 alfonx 144 // System.out.println("State: " + state);
298     }
299 mojays 2
300 alfonx 144 public double getZoomFactor() {
301     return zoomFactor;
302     }
303 mojays 2
304 alfonx 307 public void setZoomFactor(final double zoomFactor) {
305 alfonx 144 this.zoomFactor = zoomFactor;
306     }
307 mojays 2
308 alfonx 307 protected void paintComponent(final Graphics g) {
309 alfonx 144 super.paintComponent(g);
310 mojays 2
311 alfonx 144 if ((renderer == null) || (mapArea == null)) {
312     return;
313     }
314 mojays 2
315 alfonx 307 final Rectangle r = getBounds();
316     final Rectangle dr = new Rectangle(r.width, r.height);
317 mojays 2
318 alfonx 144 if (!r.equals(oldRect) || reset) {
319     if (!r.equals(oldRect) && (mapArea == null)) {
320     try {
321     mapArea = context.getLayerBounds();
322 alfonx 307 } catch (final IOException e) {
323 alfonx 336 LOGGER.warn("context.getLayerBounds()", e);
324 alfonx 144 }
325     }
326 mojays 2
327 alfonx 144 if (mapArea != null) {
328     /* either the viewer size has changed or we've done a reset */
329     changed = true; /* note we need to redraw */
330     reset = false; /* forget about the reset */
331     oldRect = r; /* store what the current size is */
332 mojays 2
333 mojays 494 mapArea = JTSUtil.fixAspectRatio(r, mapArea, false);
334 alfonx 144 }
335     }
336 mojays 2
337 alfonx 144 if (!mapArea.equals(oldMapArea)) { /* did the map extent change? */
338     changed = true;
339     oldMapArea = mapArea;
340     // when we tell the context that the bounds have changed WMSLayers
341     // can refresh them selves
342     context.setAreaOfInterest(mapArea, context
343     .getCoordinateReferenceSystem());
344     }
345 mojays 2
346 alfonx 414 if (changed) { /* if the map changed then redraw */
347 alfonx 144 changed = false;
348 alfonx 505
349     // The baseImage should not have alpha
350 alfonx 144 baseImage = new BufferedImage(dr.width, dr.height,
351 alfonx 505 BufferedImage.TYPE_INT_RGB);
352 mojays 2
353 alfonx 307 final Graphics2D ig = baseImage.createGraphics();
354 alfonx 505 ig.setBackground(getMapBackgroundColor());
355     ig.fillRect(0, 0, dr.width, dr.height);
356    
357     // /* System.out.println("rendering"); */
358 alfonx 414 if (renderer.getContext() != null)
359 alfonx 307 renderer.setContext(context);
360 mojays 2
361 alfonx 505 // 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 alfonx 144 // draw the map
365 alfonx 505 renderer.paint(ig, dr, mapArea);
366 mojays 2
367 alfonx 506 panningImage = new BufferedImage(dr.width, dr.height, BufferedImage.TYPE_INT_RGB);
368 mojays 2
369 alfonx 144 }
370 mojays 2
371 alfonx 144 ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
372     }
373 mojays 2
374 alfonx 307 public void mouseClicked(final MouseEvent e) {
375 alfonx 414 if (mapArea == null)
376     return;
377 alfonx 144 // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
378     // h:"+mapArea.getHeight());
379 alfonx 307 final Rectangle bounds = this.getBounds();
380     final double x = (double) (e.getX());
381     final double y = (double) (e.getY());
382     final double width = mapArea.getWidth();
383     final double height = mapArea.getHeight();
384 alfonx 144 // xulu.sc
385     // double width2 = mapArea.getWidth() / 2.0;
386     // double height2 = mapArea.getHeight() / 2.0;
387 alfonx 307 final double width2 = width / 2.0;
388     final double height2 = height / 2.0;
389 alfonx 144 // xulu.ec
390 alfonx 414 final double mapX = ((x * width) / (double) bounds.width)
391     + mapArea.getMinX();
392 alfonx 307 final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
393 alfonx 144 + mapArea.getMinY();
394 mojays 2
395 alfonx 144 /*
396     * System.out.println(""+x+"->"+mapX);
397     * System.out.println(""+y+"->"+mapY);
398     */
399 mojays 2
400 alfonx 144 /*
401     * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());
402     * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());
403     */
404     double zlevel = 1.0;
405 mojays 2
406 alfonx 144 switch (state) {
407     case Pan:
408     zlevel = 1.0;
409     // xulu.sc SK: return here.. a mouselistener is managing the PANNING
410     // break;
411     return;
412     // xulu.ec
413     case ZoomIn:
414     zlevel = zoomFactor;
415 mojays 2
416 alfonx 144 break;
417 mojays 2
418 alfonx 144 case ZoomOut:
419     zlevel = 1.0 / zoomFactor;
420 mojays 2
421 alfonx 144 break;
422 alfonx 414 //
423     // case Select:
424     // doSelection(mapX, mapY, selectionLayer);
425     //
426     // return;
427 mojays 2
428 alfonx 144 default:
429     return;
430     }
431 mojays 2
432 alfonx 307 final Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
433 alfonx 144 - (height2 / zlevel));
434 alfonx 307 final Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
435 alfonx 144 + (height2 / zlevel));
436     // xulu.sc SK: Check for min/max scale
437     // mapArea = new Envelope(ll, ur);
438     final Envelope newMapArea = new Envelope(ll, ur);
439     setMapArea(bestAllowedMapArea(newMapArea));
440     // xulu.ec
441 mojays 2
442 alfonx 144 // sk.ec
443 mojays 2
444 alfonx 144 // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"
445     // h:"+mapArea.getHeight());
446     repaint();
447     }
448 mojays 2
449 alfonx 307 public void mouseEntered(final MouseEvent e) {
450 alfonx 144 }
451 mojays 2
452 alfonx 307 public void mouseExited(final MouseEvent e) {
453 alfonx 144 }
454 mojays 2
455 alfonx 307 public void mousePressed(final MouseEvent e) {
456 alfonx 144 startX = e.getX();
457     startY = e.getY();
458     lastX = 0;
459     lastY = 0;
460     }
461 mojays 2
462 alfonx 307 public void mouseReleased(final MouseEvent e) {
463     final int endX = e.getX();
464     final int endY = e.getY();
465 mojays 2
466 alfonx 144 processDrag(startX, startY, endX, endY, e);
467     lastX = 0;
468     lastY = 0;
469 mojays 2
470 alfonx 144 /**
471     * Es wird nicht (mehr) gepannt!
472     */
473     panning_started = false;
474     }
475 mojays 2
476 alfonx 307 public void mouseDragged(final MouseEvent e) {
477     final Graphics graphics = this.getGraphics();
478     final int x = e.getX();
479     final int y = e.getY();
480 mojays 2
481 alfonx 144 if ((state == JMapPane.Pan)
482     || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
483     /**
484     * SK: Der Cursor wird auf PANNING gesetzt.
485     */
486     if (panning_started == false) {
487     panning_started = true;
488     setCursor(SwingUtil.PANNING_CURSOR);
489     }
490 mojays 2
491 alfonx 144 // move the image with the mouse
492     if ((lastX > 0) && (lastY > 0)) {
493 alfonx 307 final int dx = lastX - startX;
494     final int dy = lastY - startY;
495 alfonx 144 // System.out.println("translate "+dx+","+dy);
496     final Graphics2D g2 = panningImage.createGraphics();
497 alfonx 505 g2.setBackground(getMapBackgroundColor());
498 alfonx 414
499 mojays 2 g2.clearRect(0, 0, this.getWidth(), this.getHeight());
500     g2.drawImage(baseImage, dx, dy, this);
501 alfonx 144 graphics.drawImage(panningImage, 0, 0, this);
502     }
503 mojays 2
504 alfonx 144 lastX = x;
505     lastY = y;
506     } else
507 mojays 2
508 alfonx 144 if ((state == JMapPane.ZoomIn) || (state == JMapPane.ZoomOut)) {
509 mojays 2
510 alfonx 144 graphics.setXORMode(Color.WHITE);
511 mojays 2
512 alfonx 144 if ((lastX > 0) && (lastY > 0)) {
513     drawRectangle(graphics);
514     }
515 mojays 2
516 alfonx 144 // draw new box
517     lastX = x;
518     lastY = y;
519     drawRectangle(graphics);
520 alfonx 414 }
521     // else if (state == JMapPane.Select && selectionLayer != null) {
522     //
523     // // construct a new bbox filter
524     // final Rectangle bounds = this.getBounds();
525     //
526     // final double mapWidth = mapArea.getWidth();
527     // final double mapHeight = mapArea.getHeight();
528     //
529     // final double x1 = ((this.startX * mapWidth) / (double) bounds.width)
530     // + mapArea.getMinX();
531     // final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) /
532     // (double) bounds.height)
533     // + mapArea.getMinY();
534     // final double x2 = ((x * mapWidth) / (double) bounds.width)
535     // + mapArea.getMinX();
536     // final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double)
537     // bounds.height)
538     // + mapArea.getMinY();
539     // final double left = Math.min(x1, x2);
540     // final double right = Math.max(x1, x2);
541     // final double bottom = Math.min(y1, y2);
542     // final double top = Math.max(y1, y2);
543     //
544     // String name = selectionLayer.getFeatureSource().getSchema()
545     // .getDefaultGeometry().getName();
546     //
547     // if (name == "") {
548     // name = "the_geom";
549     // }
550     // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
551     // top,
552     // getContext().getCoordinateReferenceSystem().toString());
553     // if (selectionManager != null) {
554     // selectionManager.selectionChanged(this, bb);
555     // }
556     //
557     // graphics.setXORMode(Color.green);
558     //
559     // /*
560     // * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
561     // */
562     //
563     // // draw new box
564     // lastX = x;
565     // lastY = y;
566     // drawRectangle(graphics);
567     // }
568 mojays 2
569 alfonx 144 }
570 mojays 2
571 alfonx 144 // sk.cs
572     // private void processDrag(int x1, int y1, int x2, int y2) {
573     // sk.ce
574     protected void processDrag(final int x1, final int y1, final int x2,
575 alfonx 307 final int y2, final MouseEvent e) {
576 mojays 2
577 alfonx 144 /****
578 alfonx 431 * If no layers exist, we ignore the drag.
579 alfonx 144 */
580 alfonx 431 if (context.getLayerCount() <= 0) {
581 alfonx 144 return;
582 alfonx 431 }
583 alfonx 504
584 alfonx 144 // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
585     // + x2 + "," + y2);
586     if ((x1 == x2) && (y1 == y2)) {
587     if (isClickable()) {
588     mouseClicked(new MouseEvent(this, 0, new Date().getTime(), 0,
589     x1, y1, y2, false));
590     }
591 mojays 2
592 alfonx 144 return;
593     }
594 mojays 2
595 alfonx 307 final Rectangle bounds = this.getBounds();
596 mojays 2
597 alfonx 307 final double mapWidth = mapArea.getWidth();
598     final double mapHeight = mapArea.getHeight();
599 mojays 2
600 alfonx 307 final double startX = ((x1 * mapWidth) / (double) bounds.width)
601 alfonx 144 + mapArea.getMinX();
602 alfonx 307 final double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)
603 alfonx 144 + mapArea.getMinY();
604 alfonx 307 final double endX = ((x2 * mapWidth) / (double) bounds.width)
605 alfonx 144 + mapArea.getMinX();
606 alfonx 307 final double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)
607 alfonx 144 + mapArea.getMinY();
608 mojays 2
609 alfonx 144 if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {
610     // move the image with the mouse
611     // calculate X offsets from start point to the end Point
612 alfonx 307 final double deltaX1 = endX - startX;
613 mojays 2
614 alfonx 144 // System.out.println("deltaX " + deltaX1);
615     // new edges
616 alfonx 307 final double left = mapArea.getMinX() - deltaX1;
617     final double right = mapArea.getMaxX() - deltaX1;
618     final double deltaY1 = endY - startY;
619 mojays 2
620 alfonx 144 // System.out.println("deltaY " + deltaY1);
621 alfonx 307 final double bottom = mapArea.getMinY() - deltaY1;
622     final double top = mapArea.getMaxY() - deltaY1;
623     final Coordinate ll = new Coordinate(left, bottom);
624     final Coordinate ur = new Coordinate(right, top);
625 alfonx 144 // xulu.sc
626     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
627 alfonx 431
628 alfonx 418 setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
629 alfonx 144 // xulu.ec
630     } else if (state == JMapPane.ZoomIn) {
631 mojays 2
632 alfonx 144 // Zu kleine Flächen sollen nicht gezoomt werden.
633     // sk.bc
634     if ((Math.abs(x1 - x2) * Math.abs(y2 - y1)) < 150)
635     return;
636     // sk.ec
637 mojays 2
638 alfonx 144 drawRectangle(this.getGraphics());
639     // make the dragged rectangle (in map coords) the new BBOX
640 alfonx 307 final double left = Math.min(startX, endX);
641     final double right = Math.max(startX, endX);
642     final double bottom = Math.min(startY, endY);
643     final double top = Math.max(startY, endY);
644     final Coordinate ll = new Coordinate(left, bottom);
645     final Coordinate ur = new Coordinate(right, top);
646 alfonx 144 // xulu.sc
647 mojays 2
648 alfonx 144 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
649     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
650 mojays 2
651 alfonx 144 } else if (state == JMapPane.ZoomOut) {
652     drawRectangle(this.getGraphics());
653 mojays 2
654 alfonx 144 // make the dragged rectangle in screen coords the new map size?
655 alfonx 307 final double left = Math.min(startX, endX);
656     final double right = Math.max(startX, endX);
657     final double bottom = Math.min(startY, endY);
658     final double top = Math.max(startY, endY);
659     final double nWidth = (mapWidth * mapWidth) / (right - left);
660     final double nHeight = (mapHeight * mapHeight) / (top - bottom);
661     final double deltaX1 = left - mapArea.getMinX();
662     final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
663     final double deltaY1 = bottom - mapArea.getMinY();
664     final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
665     final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
666 alfonx 144 mapArea.getMinY() - nDeltaY1);
667 alfonx 307 final double deltaX2 = mapArea.getMaxX() - right;
668     final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
669     final double deltaY2 = mapArea.getMaxY() - top;
670     final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
671     final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
672 alfonx 144 mapArea.getMaxY() + nDeltaY2);
673     // xulu.sc
674     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
675     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
676 mojays 2
677 alfonx 144 // xulu.ec
678 alfonx 414 }
679     // else if (state == JMapPane.Select && selectionLayer != null) {
680     // final double left = Math.min(startX, endX);
681     // final double right = Math.max(startX, endX);
682     // final double bottom = Math.min(startY, endY);
683     // final double top = Math.max(startY, endY);
684     //
685     // String name = selectionLayer.getFeatureSource().getSchema()
686     // .getDefaultGeometry().getLocalName();
687     //
688     // if (name == "") {
689     // name = "the_geom";
690     // }
691     // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
692     // top,
693     // getContext().getCoordinateReferenceSystem().toString());
694     // // System.out.println(bb.toString());
695     // if (selectionManager != null) {
696     // selectionManager.selectionChanged(this, bb);
697     // }
698     // /*
699     // * FeatureCollection fc; selection = null; try { fc =
700     // * selectionLayer.getFeatureSource().getFeatures(bb); selection =
701     // * fc; } catch (IOException e) { e.printStackTrace(); }
702     // */
703     // }
704 mojays 2
705 alfonx 144 // xulu.so
706     // setMapArea(mapArea);
707     // xulu.eo
708     repaint();
709     }
710 mojays 2
711 alfonx 144 private boolean isClickable() {
712     return clickable;
713     }
714 mojays 2
715 alfonx 307 public void propertyChange(final PropertyChangeEvent evt) {
716     final String prop = evt.getPropertyName();
717 mojays 2
718 alfonx 144 if (prop.equalsIgnoreCase("crs")) {
719     context.setAreaOfInterest(context.getAreaOfInterest(),
720     (CoordinateReferenceSystem) evt.getNewValue());
721     }
722     }
723 mojays 2
724 alfonx 144 public boolean isReset() {
725     return reset;
726     }
727 mojays 2
728 alfonx 307 public void setReset(final boolean reset) {
729 alfonx 144 this.reset = reset;
730     }
731 mojays 2
732 alfonx 307 public void layerAdded(final MapLayerListEvent event) {
733 alfonx 144 changed = true;
734 mojays 2
735 alfonx 144 if (context.getLayers().length == 1) { // the first one
736 mojays 2
737 alfonx 144 try {
738     // xulu.sc
739     // mapArea = context.getLayerBounds();
740     mapArea = context.getAreaOfInterest();
741     if (mapArea == null)
742     mapArea = context.getLayerBounds();
743     // xulu.ec
744 alfonx 307 } catch (final IOException e) {
745 alfonx 506 LOGGER.error(e);
746 alfonx 144 }
747 mojays 2
748 alfonx 144 reset = true;
749     }
750 mojays 2
751 alfonx 144 repaint();
752     }
753 mojays 2
754 alfonx 307 public void layerRemoved(final MapLayerListEvent event) {
755 alfonx 144 changed = true;
756     repaint();
757     }
758 mojays 2
759 alfonx 307 public void layerChanged(final MapLayerListEvent event) {
760 alfonx 144 changed = true;
761     // System.out.println("layer changed - repaint");
762     repaint();
763     }
764 mojays 2
765 alfonx 307 public void layerMoved(final MapLayerListEvent event) {
766 alfonx 144 changed = true;
767     repaint();
768     }
769 mojays 2
770 alfonx 307 protected void drawRectangle(final Graphics graphics) {
771 alfonx 144 // undraw last box/draw new box
772 alfonx 307 final int left = Math.min(startX, lastX);
773     final int right = Math.max(startX, lastX);
774     final int top = Math.max(startY, lastY);
775     final int bottom = Math.min(startY, lastY);
776     final int width = right - left;
777     final int height = top - bottom;
778 alfonx 144 // System.out.println("drawing rect("+left+","+bottom+","+ width+","+
779     // height+")");
780     graphics.drawRect(left, bottom, width, height);
781     }
782 mojays 2
783 alfonx 144 /**
784     * if clickable is set to true then a single click on the map pane will zoom
785     * or pan the map.
786     *
787     * @param clickable
788     */
789 alfonx 307 public void setClickable(final boolean clickable) {
790 alfonx 144 this.clickable = clickable;
791     }
792 mojays 2
793 alfonx 307 public void mouseMoved(final MouseEvent e) {
794 alfonx 144 }
795    
796     // xulu.sn
797 alfonx 76 /**
798     * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
799     * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
800     * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>
801     * Since 21. April 09: Before thecalculation starts, the aspect ratio is
802     * corrected. This change implies, that setMapArea() will most of the time
803     * not allow setting to a wrong aspectRatio.
804     *
805     * @author <a href="mailto:[email protected]">Stefan Alfons
806     * Kr&uuml;ger</a>
807     */
808     public Envelope bestAllowedMapArea(Envelope env) {
809     if (getWidth() == 0)
810     return env;
811     if (env == null)
812     return env;
813 mojays 2
814 alfonx 414 Envelope newArea = null;
815    
816 alfonx 76 /**
817 alfonx 144 * Correct the aspect Ratio before we check the rest. Otherwise we might
818 alfonx 504 * easily fail. We allow to grow here, because we don't check against
819     * the maxExtend
820 alfonx 76 */
821 alfonx 497 env = JTSUtil.fixAspectRatio(this.getBounds(), env, true);
822 mojays 2
823 alfonx 307 final double scale = env.getWidth() / getWidth();
824     final double centerX = env.getMinX() + env.getWidth() / 2.;
825     final double centerY = env.getMinY() + env.getHeight() / 2.;
826 alfonx 414 double newWidth2 = 0;
827     double newHeight2 = 0;
828 alfonx 76 if (scale < getMaxZoomScale()) {
829     // ****************************************************************************
830     // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
831     // ****************************************************************************
832     newWidth2 = getMaxZoomScale() * getWidth() / 2.;
833     newHeight2 = getMaxZoomScale() * getHeight() / 2.;
834     } else if (scale > getMinZoomScale()) {
835     // ****************************************************************************
836     // Wir zoomen weiter raus als erlaubt => Anpassen des envelope
837     // ****************************************************************************
838     newWidth2 = getMinZoomScale() * getWidth() / 2.;
839     newHeight2 = getMinZoomScale() * getHeight() / 2.;
840     } else {
841     // ****************************************************************************
842     // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
843     // ****************************************************************************
844 alfonx 414 newArea = env;
845 alfonx 76 }
846 mojays 2
847 alfonx 414 if (newArea == null) {
848 mojays 2
849 alfonx 414 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 alfonx 418 Envelope maxAllowedExtend = getMaxExtend();
858 alfonx 504 while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
859     && newArea != null && !newArea.isNull()
860     && !Double.isNaN(newArea.getMinX())
861     && !Double.isNaN(newArea.getMaxX())
862     && !Double.isNaN(newArea.getMinY())
863     && !Double.isNaN(newArea.getMaxY())) {
864 alfonx 418 /*
865     * If a maxExtend is set, we have to honour that...
866     */
867 alfonx 414
868 alfonx 418 // Exceeds top? Move down and maybe cut
869     if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
870     double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
871 alfonx 504 // LOGGER.debug("Moving area down by " + divY);
872 alfonx 414
873 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX(),
874     newArea.getMinY() - divY), new Coordinate(newArea
875     .getMaxX(), newArea.getMaxY() - divY));
876 alfonx 414
877 alfonx 418 if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
878 alfonx 504 // LOGGER.debug("Now it exeeds the bottom border.. cut!");
879 alfonx 418 // And cut the bottom if it moved out of the area
880 alfonx 414 newArea = new Envelope(new Coordinate(newArea.getMinX(),
881 alfonx 418 maxAllowedExtend.getMinY()), new Coordinate(newArea
882     .getMaxX(), newArea.getMaxY()));
883 alfonx 414
884 alfonx 504 // LOGGER.debug("and fix aspect ratio");
885 alfonx 414
886 alfonx 504 newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
887     false);
888 alfonx 414 }
889 alfonx 418 }
890 alfonx 414
891 alfonx 418 // Exceeds bottom? Move up and maybe cut
892     if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
893     double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
894 alfonx 504 // LOGGER.debug("Moving area up by " + divY);
895 alfonx 414
896 alfonx 418 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 alfonx 504 // LOGGER.debug("Now it exeeds the top border.. cut!");
902 alfonx 418 // And cut the bottom if it moved out of the area
903 alfonx 414 newArea = new Envelope(new Coordinate(newArea.getMinX(),
904 alfonx 418 newArea.getMinY()), new Coordinate(newArea
905     .getMaxX(), maxAllowedExtend.getMaxY()));
906 alfonx 414
907 alfonx 504 // LOGGER.debug("and fix aspect ratio");
908 alfonx 414
909 alfonx 504 newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
910     false);
911 alfonx 414 }
912 alfonx 418 }
913 alfonx 414
914 alfonx 418 // Exceeds to the right? move and maybe cut
915     if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
916 alfonx 414
917 alfonx 418 // Move left..
918     double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
919 alfonx 504 // LOGGER.debug("Moving area left by " + divX);
920 alfonx 414
921 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
922     newArea.getMinY()), new Coordinate(newArea.getMaxX()
923     - divX, newArea.getMaxY()));
924 alfonx 414
925 alfonx 418 if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
926 alfonx 504 // LOGGER.debug("Now it exeeds the left border.. cut!");
927 alfonx 418 // And cut the left if it moved out of the area
928 alfonx 504 newArea = new Envelope(new Coordinate(maxAllowedExtend
929     .getMinX(), newArea.getMinY()), new Coordinate(
930     newArea.getMaxX(), newArea.getMaxY()));
931 alfonx 414
932 alfonx 504 // LOGGER.debug("and fix aspect ratio");
933 alfonx 414
934 alfonx 504 newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
935     false);
936 alfonx 414 }
937 alfonx 418 }
938 alfonx 414
939 alfonx 418 // Exceeds to the left? move and maybe cut
940     if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
941 alfonx 414
942 alfonx 418 // Move right..
943     double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
944 alfonx 504 // LOGGER.debug("Moving area right by " + divX);
945 alfonx 414
946 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
947     newArea.getMinY()), new Coordinate(newArea.getMaxX()
948     - divX, newArea.getMaxY()));
949 alfonx 414
950 alfonx 418 if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
951 alfonx 504 // LOGGER.debug("Now it exeeds the right border.. cut!");
952 alfonx 418 // 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 alfonx 414
957 alfonx 504 // LOGGER.debug("and fix aspect ratio");
958 alfonx 414
959 alfonx 504 newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
960     false);
961 alfonx 414 }
962     }
963    
964     }
965    
966     return newArea;
967 alfonx 76 }
968    
969 alfonx 144 /**
970     * Retuns the minimum allowed zoom scale. This is the bigger number value of
971     * the two. Defaults to {@link Double}.MAX_VALUE
972     *
973     * @author <a href="mailto:[email protected]">Stefan Alfons
974     * Kr&uuml;ger</a>
975     */
976     public Double getMinZoomScale() {
977     return minZoomScale;
978     }
979 mojays 2
980 alfonx 144 /**
981     * Retuns the maximum allowed zoom scale. This is the smaller number value
982     * of the two. Defaults to {@link Double}.MIN_VALUE
983     *
984     * @author <a href="mailto:[email protected]">Stefan Alfons
985     * Kr&uuml;ger</a>
986     */
987     public Double getMaxZoomScale() {
988     return maxZoomScale;
989     }
990 mojays 2
991 alfonx 144 /**
992     * Set the maximum allowed zoom scale. This is the smaller number value of
993 alfonx 504 * the two. If <code>null</code> is passed, Double.MINVALUE are used which
994     * mean there is no restriction.
995 alfonx 144 *
996     * @author <a href="mailto:[email protected]">Stefan Alfons
997     * Kr&uuml;ger</a>
998     */
999 alfonx 307 public void setMaxZoomScale(final Double maxZoomScale) {
1000 alfonx 504 this.maxZoomScale = maxZoomScale == null ? Double.MIN_VALUE
1001     : maxZoomScale;
1002 alfonx 144 }
1003 mojays 2
1004 alfonx 144 /**
1005     * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1006 alfonx 504 * value of the two. If <code>null</code> is passed, Double.MAXVALUE are
1007     * used which mean there is no restriction.
1008 alfonx 144 *
1009 alfonx 504 *
1010 alfonx 144 * @author <a href="mailto:[email protected]">Stefan Alfons
1011     * Kr&uuml;ger</a>
1012     */
1013 alfonx 307 public void setMinZoomScale(final Double minZoomScale) {
1014 alfonx 504 this.minZoomScale = minZoomScale == null ? Double.MAX_VALUE
1015     : minZoomScale;
1016 alfonx 144 }
1017 mojays 2
1018 alfonx 414 /**
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 alfonx 418 * 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 alfonx 414 *
1036     * @param maxExtend
1037     * <code>null</code> to not have this restriction.
1038     */
1039    
1040     public Envelope getMaxExtend() {
1041 alfonx 418 if (maxExtend == null) {
1042     try {
1043 alfonx 504 return JTSUtil.fixAspectRatio(this.getBounds(),
1044     // Kartenbereich um 10% vergroessern
1045     JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1),
1046     true);
1047 alfonx 418 } catch (IOException e) {
1048 alfonx 504 LOGGER
1049     .warn(
1050 mojays 485 "maxExtend == null; failed to getLayerBounds of context",
1051 alfonx 418 e);
1052     }
1053     }
1054 alfonx 414 return maxExtend;
1055     }
1056    
1057 alfonx 505 /**
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     }
1072    
1073 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26