/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Annotation of /branches/2.0-RC2/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 493 - (hide annotations)
Fri Oct 23 14:09:27 2009 UTC (15 years, 4 months ago) by mojays
Original Path: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
File size: 34642 byte(s)
BugFix: Handle empty Rectangle in JMapPane#fixAspectRatio(..)
FeatureTablePane: check for "valueAdjusting" removed (because this must be done in event firing, not in event handling!)
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     import java.awt.event.InputEvent;
47     import java.awt.event.MouseEvent;
48     import java.awt.event.MouseListener;
49     import java.awt.event.MouseMotionListener;
50     import java.awt.image.BufferedImage;
51     import java.beans.PropertyChangeEvent;
52     import java.beans.PropertyChangeListener;
53     import java.io.IOException;
54     import java.util.Date;
55     import java.util.HashMap;
56     import java.util.Map;
57    
58     import javax.swing.JPanel;
59    
60     import org.apache.log4j.Logger;
61     import org.geotools.map.MapContext;
62     import org.geotools.map.event.MapLayerListEvent;
63     import org.geotools.map.event.MapLayerListListener;
64     import org.geotools.renderer.GTRenderer;
65 alfonx 336 import org.geotools.renderer.label.LabelCacheImpl;
66 mojays 2 import org.geotools.renderer.lite.LabelCache;
67     import org.geotools.renderer.lite.StreamingRenderer;
68     import org.opengis.filter.FilterFactory2;
69     import org.opengis.referencing.crs.CoordinateReferenceSystem;
70    
71 mojays 436 import schmitzm.geotools.JTSUtil;
72 mojays 2 import schmitzm.swing.SwingUtil;
73    
74     import com.vividsolutions.jts.geom.Coordinate;
75     import com.vividsolutions.jts.geom.Envelope;
76     import com.vividsolutions.jts.geom.GeometryFactory;
77    
78     public class JMapPane extends JPanel implements MouseListener,
79 alfonx 336 MouseMotionListener, PropertyChangeListener, MapLayerListListener {
80 alfonx 452 private static Logger LOGGER = Logger.getLogger(JMapPane.class);
81 alfonx 153
82 alfonx 144 private static final long serialVersionUID = -8647971481359690499L;
83 mojays 2
84 alfonx 144 public static final int Reset = 0;
85 mojays 2
86 alfonx 144 public static final int ZoomIn = 1;
87 mojays 2
88 alfonx 144 public static final int ZoomOut = 2;
89 mojays 2
90 alfonx 144 public static final int Pan = 3;
91 mojays 2
92 alfonx 144 public static final int Select = 4;
93 mojays 2
94 alfonx 144 /**
95     * what renders the map
96     */
97     GTRenderer renderer;
98 mojays 2
99 alfonx 144 /**
100     * the map context to render
101     */
102     MapContext context;
103 mojays 2
104 alfonx 144 /**
105     * the area of the map to draw
106     */
107     protected Envelope mapArea;
108 mojays 2
109 alfonx 144 /**
110     * the size of the pane last time we drew
111     */
112     protected Rectangle oldRect = null;
113 mojays 2
114 alfonx 144 /**
115     * the last map area drawn.
116     */
117     protected Envelope oldMapArea = null;
118 mojays 2
119 alfonx 144 /**
120     * the base image of the map
121     */
122     protected BufferedImage baseImage, panningImage;
123 mojays 2
124 alfonx 144 /**
125     * a factory for filters
126     */
127     FilterFactory2 ff;
128 mojays 2
129 alfonx 144 /**
130     * a factory for geometries
131     */
132     GeometryFactory gf = new GeometryFactory(); // FactoryFinder.getGeometryFactory(null);
133 mojays 2
134 alfonx 144 private int state = ZoomIn;
135 mojays 2
136 alfonx 144 /**
137     * how far to zoom in or out
138     */
139     private double zoomFactor = 2.0;
140 mojays 2
141 alfonx 144 boolean changed = true;
142 mojays 2
143 alfonx 336 LabelCache labelCache = new LabelCacheImpl();
144 mojays 2
145 alfonx 144 protected boolean reset = false;
146 mojays 2
147 alfonx 144 int startX;
148 mojays 2
149 alfonx 144 int startY;
150 mojays 2
151 alfonx 414 /**
152     * If not <code>null</code>, the {@link JMapPane} will not allow to zoom/pan
153     * out of that area
154     **/
155     private Envelope maxExtend = null;
156    
157     // /**
158     // * Is max. 1 or 0 of the 2 axised allowed to extend the maxExtend? If
159     // * <code>true</code> the extends has to be fully inside maxExtend
160     // **/
161     // boolean maxExtendForceMode = true;
162    
163 alfonx 144 private boolean clickable;
164 mojays 2
165 alfonx 144 int lastX;
166 mojays 2
167 alfonx 144 int lastY;
168 mojays 2
169 alfonx 144 private Double maxZoomScale = Double.MIN_VALUE;
170     private Double minZoomScale = Double.MAX_VALUE;
171 mojays 2
172 alfonx 144 /**
173     * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses
174     * Flag wird benutzt um nur einmal den passenden Cursor nur einmal zu
175     * setzen.
176     */
177 mojays 2 private boolean panning_started = false;
178    
179 alfonx 144 public JMapPane() {
180     this(null, true, null, null);
181     }
182 mojays 2
183 alfonx 144 /**
184     * create a basic JMapPane
185     *
186     * @param render
187     * - how to draw the map
188     * @param context
189     * - the map context to display
190     */
191 alfonx 307 public JMapPane(final GTRenderer render, final MapContext context) {
192 alfonx 144 this(null, true, render, context);
193     }
194 mojays 2
195 alfonx 144 /**
196     * full constructor extending JPanel
197     *
198     * @param layout
199     * - layout (probably shouldn't be set)
200     * @param isDoubleBuffered
201     * - a Swing thing I don't really understand
202     * @param render
203     * - what to draw the map with
204     * @param context
205     * - what to draw
206     */
207 alfonx 307 public JMapPane(final LayoutManager layout, final boolean isDoubleBuffered,
208     final GTRenderer render, final MapContext context) {
209 alfonx 144 super(layout, isDoubleBuffered);
210 mojays 2
211 alfonx 144 ff = (FilterFactory2) org.geotools.factory.CommonFactoryFinder
212     .getFilterFactory(null);
213     setRenderer(render);
214 mojays 2
215 alfonx 144 setContext(context);
216 mojays 2
217 alfonx 144 this.addMouseListener(this);
218     this.addMouseMotionListener(this);
219     setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
220     }
221 mojays 2
222 alfonx 144 /**
223     * get the renderer
224     */
225     public GTRenderer getRenderer() {
226     return renderer;
227     }
228 mojays 2
229 alfonx 307 public void setRenderer(final GTRenderer renderer) {
230 alfonx 414 Map<Object, Object> hints = new HashMap<Object, Object>();
231    
232 alfonx 307 this.renderer = renderer;
233 mojays 478 //MS: Apply hint also for ShapeFileRenderer
234     // if (renderer instanceof StreamingRenderer) {
235 alfonx 144 hints = renderer.getRendererHints();
236     if (hints == null) {
237 alfonx 414 hints = new HashMap<Object, Object>();
238 alfonx 144 }
239     if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {
240     labelCache = (LabelCache) hints
241     .get(StreamingRenderer.LABEL_CACHE_KEY);
242     } else {
243     hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
244     }
245 alfonx 307
246     hints.put("memoryPreloadingEnabled", Boolean.TRUE);
247 alfonx 414
248 alfonx 144 renderer.setRendererHints(hints);
249 mojays 478 // }
250 mojays 2
251 alfonx 144 if (this.context != null) {
252     this.renderer.setContext(this.context);
253     }
254     }
255 mojays 2
256 alfonx 144 public MapContext getContext() {
257     return context;
258     }
259 mojays 2
260 alfonx 307 public void setContext(final MapContext context) {
261 alfonx 144 if (this.context != null) {
262     this.context.removeMapLayerListListener(this);
263     }
264 mojays 2
265 alfonx 144 this.context = context;
266 mojays 2
267 alfonx 144 if (context != null) {
268     this.context.addMapLayerListListener(this);
269     }
270 mojays 2
271 alfonx 144 if (renderer != null) {
272     renderer.setContext(this.context);
273     }
274     }
275 mojays 2
276 alfonx 144 public Envelope getMapArea() {
277     return mapArea;
278     }
279 mojays 2
280 alfonx 307 public void setMapArea(final Envelope mapArea) {
281 alfonx 144 this.mapArea = mapArea;
282     }
283 mojays 2
284 alfonx 144 public int getState() {
285     return state;
286     }
287 mojays 2
288 alfonx 307 public void setState(final int state) {
289 alfonx 144 this.state = state;
290 mojays 2
291 alfonx 144 // System.out.println("State: " + state);
292     }
293 mojays 2
294 alfonx 144 public double getZoomFactor() {
295     return zoomFactor;
296     }
297 mojays 2
298 alfonx 307 public void setZoomFactor(final double zoomFactor) {
299 alfonx 144 this.zoomFactor = zoomFactor;
300     }
301 mojays 2
302 alfonx 307 protected void paintComponent(final Graphics g) {
303 alfonx 144 super.paintComponent(g);
304 mojays 2
305 alfonx 144 if ((renderer == null) || (mapArea == null)) {
306     return;
307     }
308 mojays 2
309 alfonx 307 final Rectangle r = getBounds();
310     final Rectangle dr = new Rectangle(r.width, r.height);
311 mojays 2
312 alfonx 144 if (!r.equals(oldRect) || reset) {
313     if (!r.equals(oldRect) && (mapArea == null)) {
314     try {
315     mapArea = context.getLayerBounds();
316 alfonx 307 } catch (final IOException e) {
317 alfonx 336 LOGGER.warn("context.getLayerBounds()", e);
318 alfonx 144 }
319     }
320 mojays 2
321 alfonx 144 if (mapArea != null) {
322     /* either the viewer size has changed or we've done a reset */
323     changed = true; /* note we need to redraw */
324     reset = false; /* forget about the reset */
325     oldRect = r; /* store what the current size is */
326 mojays 2
327 alfonx 418 mapArea = fixAspectRatio(r, mapArea, false);
328 alfonx 144 }
329     }
330 mojays 2
331 alfonx 144 if (!mapArea.equals(oldMapArea)) { /* did the map extent change? */
332     changed = true;
333     oldMapArea = mapArea;
334     // when we tell the context that the bounds have changed WMSLayers
335     // can refresh them selves
336     context.setAreaOfInterest(mapArea, context
337     .getCoordinateReferenceSystem());
338     }
339 mojays 2
340 alfonx 414 if (changed) { /* if the map changed then redraw */
341 alfonx 144 changed = false;
342     baseImage = new BufferedImage(dr.width, dr.height,
343     BufferedImage.TYPE_INT_ARGB);
344 mojays 2
345 alfonx 307 final Graphics2D ig = baseImage.createGraphics();
346 alfonx 144 /* System.out.println("rendering"); */
347 alfonx 414 if (renderer.getContext() != null)
348 alfonx 307 renderer.setContext(context);
349 alfonx 144 labelCache.clear(); // work around anoying labelcache bug
350 mojays 2
351 alfonx 144 // draw the map
352     renderer.paint((Graphics2D) ig, dr, mapArea);
353 mojays 2
354 alfonx 167 // TODO nur machen, wenn panning beginnt
355 alfonx 144 panningImage = new BufferedImage(dr.width, dr.height,
356     BufferedImage.TYPE_INT_RGB);
357 mojays 2
358 alfonx 144 }
359 mojays 2
360 alfonx 144 ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
361     }
362 mojays 2
363 alfonx 418 /**
364     * Returns an {@link Envelope} that has the same aspect ratio as the given rectangle
365     * @param grow
366     * If <code>true</code>, than the area will be enlarged to match
367     * the aspect ratio. If <code>false</code>, it will only shrink.
368     */
369     private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea,
370     boolean grow) {
371 alfonx 431
372     if (mapArea == null) {
373 mojays 493 LOGGER.warn("mapArea has been null in method fixAspectRatio, returning also NULL");
374     return null;
375 alfonx 431 }
376 mojays 493 if ( r == null || r.width == 0 || r.height == 0 ) {
377     LOGGER.warn("Empty rectangle in method fixAspectRatio, returning an unmodified mapArea!");
378     return mapArea;
379     }
380 mojays 2
381 alfonx 307 final double mapWidth = mapArea.getWidth(); /* get the extent of the map */
382     final double mapHeight = mapArea.getHeight();
383     final double scaleX = r.getWidth() / mapArea.getWidth(); /*
384 alfonx 414 * calculate the
385     * new scale
386     */
387 mojays 2
388 alfonx 307 final double scaleY = r.getHeight() / mapArea.getHeight();
389 alfonx 144 double scale = 1.0; // stupid compiler!
390 mojays 2
391 alfonx 418 if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) {
392 alfonx 144 scale = scaleX;
393     } else {
394     scale = scaleY;
395     }
396 mojays 2
397 alfonx 144 /* calculate the difference in width and height of the new extent */
398 alfonx 307 final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);
399     final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);
400 mojays 2
401 alfonx 144 /*
402     * System.out.println("delta x " + deltaX);
403     * System.out.println("delta y " + deltaY);
404     */
405 mojays 2
406 alfonx 144 /* create the new extent */
407 alfonx 414 final Coordinate ll = new Coordinate(
408     mapArea.getMinX() - (deltaX / 2.0), mapArea.getMinY()
409     - (deltaY / 2.0));
410     final Coordinate ur = new Coordinate(
411     mapArea.getMaxX() + (deltaX / 2.0), mapArea.getMaxY()
412     + (deltaY / 2.0));
413 mojays 2
414 alfonx 144 return new Envelope(ll, ur);
415     }
416 mojays 2
417 alfonx 307 public void mouseClicked(final MouseEvent e) {
418 alfonx 414 if (mapArea == null)
419     return;
420 alfonx 144 // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
421     // h:"+mapArea.getHeight());
422 alfonx 307 final Rectangle bounds = this.getBounds();
423     final double x = (double) (e.getX());
424     final double y = (double) (e.getY());
425     final double width = mapArea.getWidth();
426     final double height = mapArea.getHeight();
427 alfonx 144 // xulu.sc
428     // double width2 = mapArea.getWidth() / 2.0;
429     // double height2 = mapArea.getHeight() / 2.0;
430 alfonx 307 final double width2 = width / 2.0;
431     final double height2 = height / 2.0;
432 alfonx 144 // xulu.ec
433 alfonx 414 final double mapX = ((x * width) / (double) bounds.width)
434     + mapArea.getMinX();
435 alfonx 307 final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
436 alfonx 144 + mapArea.getMinY();
437 mojays 2
438 alfonx 144 /*
439     * System.out.println(""+x+"->"+mapX);
440     * System.out.println(""+y+"->"+mapY);
441     */
442 mojays 2
443 alfonx 144 /*
444     * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());
445     * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());
446     */
447     double zlevel = 1.0;
448 mojays 2
449 alfonx 144 switch (state) {
450     case Pan:
451     zlevel = 1.0;
452     // xulu.sc SK: return here.. a mouselistener is managing the PANNING
453     // break;
454     return;
455     // xulu.ec
456     case ZoomIn:
457     zlevel = zoomFactor;
458 mojays 2
459 alfonx 144 break;
460 mojays 2
461 alfonx 144 case ZoomOut:
462     zlevel = 1.0 / zoomFactor;
463 mojays 2
464 alfonx 144 break;
465 alfonx 414 //
466     // case Select:
467     // doSelection(mapX, mapY, selectionLayer);
468     //
469     // return;
470 mojays 2
471 alfonx 144 default:
472     return;
473     }
474 mojays 2
475 alfonx 307 final Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
476 alfonx 144 - (height2 / zlevel));
477 alfonx 307 final Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
478 alfonx 144 + (height2 / zlevel));
479     // xulu.sc SK: Check for min/max scale
480     // mapArea = new Envelope(ll, ur);
481     final Envelope newMapArea = new Envelope(ll, ur);
482     setMapArea(bestAllowedMapArea(newMapArea));
483     // xulu.ec
484 mojays 2
485 alfonx 144 // sk.ec
486 mojays 2
487 alfonx 144 // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"
488     // h:"+mapArea.getHeight());
489     repaint();
490     }
491 mojays 2
492 alfonx 307 public void mouseEntered(final MouseEvent e) {
493 alfonx 144 }
494 mojays 2
495 alfonx 307 public void mouseExited(final MouseEvent e) {
496 alfonx 144 }
497 mojays 2
498 alfonx 307 public void mousePressed(final MouseEvent e) {
499 alfonx 144 startX = e.getX();
500     startY = e.getY();
501     lastX = 0;
502     lastY = 0;
503     }
504 mojays 2
505 alfonx 307 public void mouseReleased(final MouseEvent e) {
506     final int endX = e.getX();
507     final int endY = e.getY();
508 mojays 2
509 alfonx 144 processDrag(startX, startY, endX, endY, e);
510     lastX = 0;
511     lastY = 0;
512 mojays 2
513 alfonx 144 /**
514     * Es wird nicht (mehr) gepannt!
515     */
516     panning_started = false;
517     }
518 mojays 2
519 alfonx 307 public void mouseDragged(final MouseEvent e) {
520     final Graphics graphics = this.getGraphics();
521     final int x = e.getX();
522     final int y = e.getY();
523 mojays 2
524 alfonx 144 if ((state == JMapPane.Pan)
525     || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
526     /**
527     * SK: Der Cursor wird auf PANNING gesetzt.
528     */
529     if (panning_started == false) {
530     panning_started = true;
531     setCursor(SwingUtil.PANNING_CURSOR);
532     }
533 mojays 2
534 alfonx 144 // move the image with the mouse
535     if ((lastX > 0) && (lastY > 0)) {
536 alfonx 307 final int dx = lastX - startX;
537     final int dy = lastY - startY;
538 alfonx 144 // System.out.println("translate "+dx+","+dy);
539     final Graphics2D g2 = panningImage.createGraphics();
540     g2.setBackground(new Color(240, 240, 240)); // TODO richtige
541 alfonx 414 // farbe? am besten
542     // vom L&F die
543     // hintergrundfarbe
544     // auslesen...
545    
546 mojays 2 g2.clearRect(0, 0, this.getWidth(), this.getHeight());
547     g2.drawImage(baseImage, dx, dy, this);
548 alfonx 144 graphics.drawImage(panningImage, 0, 0, this);
549     }
550 mojays 2
551 alfonx 144 lastX = x;
552     lastY = y;
553     } else
554 mojays 2
555 alfonx 144 if ((state == JMapPane.ZoomIn) || (state == JMapPane.ZoomOut)) {
556 mojays 2
557 alfonx 144 graphics.setXORMode(Color.WHITE);
558 mojays 2
559 alfonx 144 if ((lastX > 0) && (lastY > 0)) {
560     drawRectangle(graphics);
561     }
562 mojays 2
563 alfonx 144 // draw new box
564     lastX = x;
565     lastY = y;
566     drawRectangle(graphics);
567 alfonx 414 }
568     // else if (state == JMapPane.Select && selectionLayer != null) {
569     //
570     // // construct a new bbox filter
571     // final Rectangle bounds = this.getBounds();
572     //
573     // final double mapWidth = mapArea.getWidth();
574     // final double mapHeight = mapArea.getHeight();
575     //
576     // final double x1 = ((this.startX * mapWidth) / (double) bounds.width)
577     // + mapArea.getMinX();
578     // final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) /
579     // (double) bounds.height)
580     // + mapArea.getMinY();
581     // final double x2 = ((x * mapWidth) / (double) bounds.width)
582     // + mapArea.getMinX();
583     // final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double)
584     // bounds.height)
585     // + mapArea.getMinY();
586     // final double left = Math.min(x1, x2);
587     // final double right = Math.max(x1, x2);
588     // final double bottom = Math.min(y1, y2);
589     // final double top = Math.max(y1, y2);
590     //
591     // String name = selectionLayer.getFeatureSource().getSchema()
592     // .getDefaultGeometry().getName();
593     //
594     // if (name == "") {
595     // name = "the_geom";
596     // }
597     // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
598     // top,
599     // getContext().getCoordinateReferenceSystem().toString());
600     // if (selectionManager != null) {
601     // selectionManager.selectionChanged(this, bb);
602     // }
603     //
604     // graphics.setXORMode(Color.green);
605     //
606     // /*
607     // * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
608     // */
609     //
610     // // draw new box
611     // lastX = x;
612     // lastY = y;
613     // drawRectangle(graphics);
614     // }
615 mojays 2
616 alfonx 144 }
617 mojays 2
618 alfonx 144 // sk.cs
619     // private void processDrag(int x1, int y1, int x2, int y2) {
620     // sk.ce
621     protected void processDrag(final int x1, final int y1, final int x2,
622 alfonx 307 final int y2, final MouseEvent e) {
623 mojays 2
624 alfonx 431
625 alfonx 144 /****
626 alfonx 431 * If no layers exist, we ignore the drag.
627 alfonx 144 */
628 alfonx 431 if (context.getLayerCount() <= 0) {
629 alfonx 144 return;
630 alfonx 431 }
631    
632 alfonx 144 // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
633     // + x2 + "," + y2);
634     if ((x1 == x2) && (y1 == y2)) {
635     if (isClickable()) {
636     mouseClicked(new MouseEvent(this, 0, new Date().getTime(), 0,
637     x1, y1, y2, false));
638     }
639 mojays 2
640 alfonx 144 return;
641     }
642 mojays 2
643 alfonx 307 final Rectangle bounds = this.getBounds();
644 mojays 2
645 alfonx 307 final double mapWidth = mapArea.getWidth();
646     final double mapHeight = mapArea.getHeight();
647 mojays 2
648 alfonx 307 final double startX = ((x1 * mapWidth) / (double) bounds.width)
649 alfonx 144 + mapArea.getMinX();
650 alfonx 307 final double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)
651 alfonx 144 + mapArea.getMinY();
652 alfonx 307 final double endX = ((x2 * mapWidth) / (double) bounds.width)
653 alfonx 144 + mapArea.getMinX();
654 alfonx 307 final double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)
655 alfonx 144 + mapArea.getMinY();
656 mojays 2
657 alfonx 144 if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {
658     // move the image with the mouse
659     // calculate X offsets from start point to the end Point
660 alfonx 307 final double deltaX1 = endX - startX;
661 mojays 2
662 alfonx 144 // System.out.println("deltaX " + deltaX1);
663     // new edges
664 alfonx 307 final double left = mapArea.getMinX() - deltaX1;
665     final double right = mapArea.getMaxX() - deltaX1;
666 mojays 2
667 alfonx 144 // now for Y
668 alfonx 307 final double deltaY1 = endY - startY;
669 mojays 2
670 alfonx 144 // System.out.println("deltaY " + deltaY1);
671 alfonx 307 final double bottom = mapArea.getMinY() - deltaY1;
672     final double top = mapArea.getMaxY() - deltaY1;
673     final Coordinate ll = new Coordinate(left, bottom);
674     final Coordinate ur = new Coordinate(right, top);
675 alfonx 144 // xulu.sc
676     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
677 alfonx 431
678    
679 alfonx 418 setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
680 alfonx 144 // xulu.ec
681     } else if (state == JMapPane.ZoomIn) {
682 mojays 2
683 alfonx 144 // Zu kleine Flächen sollen nicht gezoomt werden.
684     // sk.bc
685     if ((Math.abs(x1 - x2) * Math.abs(y2 - y1)) < 150)
686     return;
687     // sk.ec
688 mojays 2
689 alfonx 144 drawRectangle(this.getGraphics());
690     // make the dragged rectangle (in map coords) the new BBOX
691 alfonx 307 final double left = Math.min(startX, endX);
692     final double right = Math.max(startX, endX);
693     final double bottom = Math.min(startY, endY);
694     final double top = Math.max(startY, endY);
695     final Coordinate ll = new Coordinate(left, bottom);
696     final Coordinate ur = new Coordinate(right, top);
697 alfonx 144 // xulu.sc
698 mojays 2
699 alfonx 144 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
700     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
701 mojays 2
702 alfonx 144 // sk.sc
703 alfonx 414 // {
704     // // SK tries to paint a preview of the zoom ;-9 aha.... well
705     // Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
706     // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
707     // RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
708     // graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
709     // RenderingHints.VALUE_ANTIALIAS_OFF);
710     // graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
711     // RenderingHints.VALUE_RENDER_SPEED);
712     // graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
713     // JMapPane.this.getHeight(), x1, y1, x2, y2, null);
714     // }
715 alfonx 144 // xulu.ec
716     } else if (state == JMapPane.ZoomOut) {
717     drawRectangle(this.getGraphics());
718 mojays 2
719 alfonx 144 // make the dragged rectangle in screen coords the new map size?
720 alfonx 307 final double left = Math.min(startX, endX);
721     final double right = Math.max(startX, endX);
722     final double bottom = Math.min(startY, endY);
723     final double top = Math.max(startY, endY);
724     final double nWidth = (mapWidth * mapWidth) / (right - left);
725     final double nHeight = (mapHeight * mapHeight) / (top - bottom);
726     final double deltaX1 = left - mapArea.getMinX();
727     final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
728     final double deltaY1 = bottom - mapArea.getMinY();
729     final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
730     final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
731 alfonx 144 mapArea.getMinY() - nDeltaY1);
732 alfonx 307 final double deltaX2 = mapArea.getMaxX() - right;
733     final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
734     final double deltaY2 = mapArea.getMaxY() - top;
735     final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
736     final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
737 alfonx 144 mapArea.getMaxY() + nDeltaY2);
738     // xulu.sc
739     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
740     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
741 mojays 2
742 alfonx 144 // xulu.ec
743 alfonx 414 }
744     // else if (state == JMapPane.Select && selectionLayer != null) {
745     // final double left = Math.min(startX, endX);
746     // final double right = Math.max(startX, endX);
747     // final double bottom = Math.min(startY, endY);
748     // final double top = Math.max(startY, endY);
749     //
750     // String name = selectionLayer.getFeatureSource().getSchema()
751     // .getDefaultGeometry().getLocalName();
752     //
753     // if (name == "") {
754     // name = "the_geom";
755     // }
756     // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
757     // top,
758     // getContext().getCoordinateReferenceSystem().toString());
759     // // System.out.println(bb.toString());
760     // if (selectionManager != null) {
761     // selectionManager.selectionChanged(this, bb);
762     // }
763     // /*
764     // * FeatureCollection fc; selection = null; try { fc =
765     // * selectionLayer.getFeatureSource().getFeatures(bb); selection =
766     // * fc; } catch (IOException e) { e.printStackTrace(); }
767     // */
768     // }
769 mojays 2
770 alfonx 144 // xulu.so
771     // setMapArea(mapArea);
772     // xulu.eo
773     repaint();
774     }
775 mojays 2
776 alfonx 144 private boolean isClickable() {
777     return clickable;
778     }
779 mojays 2
780 alfonx 414 //
781     // private org.geotools.styling.Style setupStyle(final int type, final Color
782     // color) {
783     // final StyleFactory sf = org.geotools.factory.CommonFactoryFinder
784     // .getStyleFactory(null);
785     // final StyleBuilder sb = new StyleBuilder();
786     //
787     // org.geotools.styling.Style s = sf.createStyle();
788     // s.setTitle("selection");
789     //
790     // // TODO parameterise the color
791     // final PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);
792     // ps.setStroke(sb.createStroke(color));
793     //
794     // final LineSymbolizer ls = sb.createLineSymbolizer(color);
795     // final Graphic h = sb.createGraphic();
796     // h.setMarks(new Mark[] { sb.createMark("square", color) });
797     //
798     // final PointSymbolizer pts = sb.createPointSymbolizer(h);
799     //
800     // // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});
801     // switch (type) {
802     // case POLYGON:
803     // s = sb.createStyle(ps);
804     //
805     // break;
806     //
807     // case POINT:
808     // s = sb.createStyle(pts);
809     //
810     // break;
811     //
812     // case LINE:
813     // s = sb.createStyle(ls);
814     // }
815     //
816     // return s;
817     // }
818 mojays 2
819 alfonx 307 public void propertyChange(final PropertyChangeEvent evt) {
820     final String prop = evt.getPropertyName();
821 mojays 2
822 alfonx 144 if (prop.equalsIgnoreCase("crs")) {
823     context.setAreaOfInterest(context.getAreaOfInterest(),
824     (CoordinateReferenceSystem) evt.getNewValue());
825     }
826     }
827 mojays 2
828 alfonx 144 public boolean isReset() {
829     return reset;
830     }
831 mojays 2
832 alfonx 307 public void setReset(final boolean reset) {
833 alfonx 144 this.reset = reset;
834     }
835 mojays 2
836 alfonx 307 public void layerAdded(final MapLayerListEvent event) {
837 alfonx 144 changed = true;
838 mojays 2
839 alfonx 144 if (context.getLayers().length == 1) { // the first one
840 mojays 2
841 alfonx 144 try {
842     // xulu.sc
843     // mapArea = context.getLayerBounds();
844     mapArea = context.getAreaOfInterest();
845     if (mapArea == null)
846     mapArea = context.getLayerBounds();
847     // xulu.ec
848 alfonx 307 } catch (final IOException e) {
849 alfonx 144 // TODO Auto-generated catch block
850     e.printStackTrace();
851     }
852 mojays 2
853 alfonx 144 reset = true;
854     }
855 mojays 2
856 alfonx 144 repaint();
857     }
858 mojays 2
859 alfonx 307 public void layerRemoved(final MapLayerListEvent event) {
860 alfonx 144 changed = true;
861     repaint();
862     }
863 mojays 2
864 alfonx 307 public void layerChanged(final MapLayerListEvent event) {
865 alfonx 144 changed = true;
866     // System.out.println("layer changed - repaint");
867     repaint();
868     }
869 mojays 2
870 alfonx 307 public void layerMoved(final MapLayerListEvent event) {
871 alfonx 144 changed = true;
872     repaint();
873     }
874 mojays 2
875 alfonx 307 protected void drawRectangle(final Graphics graphics) {
876 alfonx 144 // undraw last box/draw new box
877 alfonx 307 final int left = Math.min(startX, lastX);
878     final int right = Math.max(startX, lastX);
879     final int top = Math.max(startY, lastY);
880     final int bottom = Math.min(startY, lastY);
881     final int width = right - left;
882     final int height = top - bottom;
883 alfonx 144 // System.out.println("drawing rect("+left+","+bottom+","+ width+","+
884     // height+")");
885     graphics.drawRect(left, bottom, width, height);
886     }
887 mojays 2
888 alfonx 144 /**
889     * if clickable is set to true then a single click on the map pane will zoom
890     * or pan the map.
891     *
892     * @param clickable
893     */
894 alfonx 307 public void setClickable(final boolean clickable) {
895 alfonx 144 this.clickable = clickable;
896     }
897 mojays 2
898 alfonx 307 public void mouseMoved(final MouseEvent e) {
899 alfonx 144 }
900    
901     // xulu.sn
902 alfonx 76 /**
903     * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
904     * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
905     * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>
906     * Since 21. April 09: Before thecalculation starts, the aspect ratio is
907     * corrected. This change implies, that setMapArea() will most of the time
908     * not allow setting to a wrong aspectRatio.
909     *
910     * @author <a href="mailto:[email protected]">Stefan Alfons
911     * Kr&uuml;ger</a>
912     */
913     public Envelope bestAllowedMapArea(Envelope env) {
914     if (getWidth() == 0)
915     return env;
916     if (env == null)
917     return env;
918 mojays 2
919 alfonx 414 Envelope newArea = null;
920    
921 alfonx 76 /**
922 alfonx 144 * Correct the aspect Ratio before we check the rest. Otherwise we might
923     * easily fail.
924 alfonx 76 */
925 alfonx 418 env = fixAspectRatio(this.getBounds(), env, false);
926 mojays 2
927 alfonx 307 final double scale = env.getWidth() / getWidth();
928     final double centerX = env.getMinX() + env.getWidth() / 2.;
929     final double centerY = env.getMinY() + env.getHeight() / 2.;
930 alfonx 414 double newWidth2 = 0;
931     double newHeight2 = 0;
932 alfonx 76 if (scale < getMaxZoomScale()) {
933     // ****************************************************************************
934     // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
935     // ****************************************************************************
936     newWidth2 = getMaxZoomScale() * getWidth() / 2.;
937     newHeight2 = getMaxZoomScale() * getHeight() / 2.;
938     } else if (scale > getMinZoomScale()) {
939     // ****************************************************************************
940     // Wir zoomen weiter raus als erlaubt => Anpassen des envelope
941     // ****************************************************************************
942     newWidth2 = getMinZoomScale() * getWidth() / 2.;
943     newHeight2 = getMinZoomScale() * getHeight() / 2.;
944     } else {
945     // ****************************************************************************
946     // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
947     // ****************************************************************************
948 alfonx 414 newArea = env;
949 alfonx 76 }
950 mojays 2
951 alfonx 414 if (newArea == null) {
952 mojays 2
953 alfonx 414 final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
954     - newHeight2);
955     final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
956     + newHeight2);
957    
958     newArea = new Envelope(ll, ur);
959     }
960    
961 alfonx 418 Envelope maxAllowedExtend = getMaxExtend();
962 alfonx 445 while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea) && newArea != null && !newArea.isNull() && !Double.isNaN(newArea.getMinX()) && !Double.isNaN(newArea.getMaxX()) && !Double.isNaN(newArea.getMinY()) && !Double.isNaN(newArea.getMaxY()) ) {
963 alfonx 418 /*
964     * If a maxExtend is set, we have to honour that...
965     */
966 alfonx 414
967 alfonx 418 // Exceeds top? Move down and maybe cut
968     if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
969     double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
970 mojays 436 // LOGGER.debug("Moving area down by " + divY);
971 alfonx 414
972 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX(),
973     newArea.getMinY() - divY), new Coordinate(newArea
974     .getMaxX(), newArea.getMaxY() - divY));
975 alfonx 414
976 alfonx 418 if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
977 mojays 436 // LOGGER.debug("Now it exeeds the bottom border.. cut!");
978 alfonx 418 // And cut the bottom if it moved out of the area
979 alfonx 414 newArea = new Envelope(new Coordinate(newArea.getMinX(),
980 alfonx 418 maxAllowedExtend.getMinY()), new Coordinate(newArea
981     .getMaxX(), newArea.getMaxY()));
982 alfonx 414
983 mojays 436 // LOGGER.debug("and fix aspect ratio");
984 alfonx 414
985 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
986 alfonx 414 }
987 alfonx 418 }
988 alfonx 414
989 alfonx 418 // Exceeds bottom? Move up and maybe cut
990     if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
991     double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
992 mojays 436 // LOGGER.debug("Moving area up by " + divY);
993 alfonx 414
994 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX(),
995     newArea.getMinY() - divY), new Coordinate(newArea
996     .getMaxX(), newArea.getMaxY() - divY));
997    
998     if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
999 alfonx 438 // LOGGER.debug("Now it exeeds the top border.. cut!");
1000 alfonx 418 // And cut the bottom if it moved out of the area
1001 alfonx 414 newArea = new Envelope(new Coordinate(newArea.getMinX(),
1002 alfonx 418 newArea.getMinY()), new Coordinate(newArea
1003     .getMaxX(), maxAllowedExtend.getMaxY()));
1004 alfonx 414
1005 alfonx 438 // LOGGER.debug("and fix aspect ratio");
1006 alfonx 414
1007 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1008 alfonx 414 }
1009 alfonx 418 }
1010 alfonx 414
1011 alfonx 418 // Exceeds to the right? move and maybe cut
1012     if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1013 alfonx 414
1014 alfonx 418 // Move left..
1015     double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
1016 mojays 436 // LOGGER.debug("Moving area left by " + divX);
1017 alfonx 414
1018 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1019     newArea.getMinY()), new Coordinate(newArea.getMaxX()
1020     - divX, newArea.getMaxY()));
1021 alfonx 414
1022 alfonx 418 if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1023 alfonx 438 // LOGGER.debug("Now it exeeds the left border.. cut!");
1024 alfonx 418 // And cut the left if it moved out of the area
1025 alfonx 420 newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(),
1026 alfonx 418 newArea.getMinY()), new Coordinate(newArea
1027     .getMaxX(), newArea.getMaxY()));
1028 alfonx 414
1029 alfonx 438 // LOGGER.debug("and fix aspect ratio");
1030 alfonx 414
1031 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1032 alfonx 414 }
1033 alfonx 418 }
1034 alfonx 414
1035 alfonx 418 // Exceeds to the left? move and maybe cut
1036     if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1037 alfonx 414
1038 alfonx 418 // Move right..
1039     double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
1040 mojays 436 // LOGGER.debug("Moving area right by " + divX);
1041 alfonx 414
1042 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1043     newArea.getMinY()), new Coordinate(newArea.getMaxX()
1044     - divX, newArea.getMaxY()));
1045 alfonx 414
1046 alfonx 418 if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1047 alfonx 438 // LOGGER.debug("Now it exeeds the right border.. cut!");
1048 alfonx 418 // And cut the left if it moved out of the area
1049     newArea = new Envelope(new Coordinate(newArea.getMinX(),
1050     newArea.getMinY()), new Coordinate(maxAllowedExtend
1051     .getMaxX(), newArea.getMaxY()));
1052 alfonx 414
1053 alfonx 438 // LOGGER.debug("and fix aspect ratio");
1054 alfonx 414
1055 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1056 alfonx 414 }
1057     }
1058    
1059     }
1060    
1061     return newArea;
1062 alfonx 76 }
1063    
1064 alfonx 144 /**
1065     * Retuns the minimum allowed zoom scale. This is the bigger number value of
1066     * the two. Defaults to {@link Double}.MAX_VALUE
1067     *
1068     * @author <a href="mailto:[email protected]">Stefan Alfons
1069     * Kr&uuml;ger</a>
1070     */
1071     public Double getMinZoomScale() {
1072     return minZoomScale;
1073     }
1074 mojays 2
1075 alfonx 144 /**
1076     * Retuns the maximum allowed zoom scale. This is the smaller number value
1077     * of the two. Defaults to {@link Double}.MIN_VALUE
1078     *
1079     * @author <a href="mailto:[email protected]">Stefan Alfons
1080     * Kr&uuml;ger</a>
1081     */
1082     public Double getMaxZoomScale() {
1083     return maxZoomScale;
1084     }
1085 mojays 2
1086 alfonx 144 /**
1087     * Set the maximum allowed zoom scale. This is the smaller number value of
1088     * the two.
1089     *
1090     * @author <a href="mailto:[email protected]">Stefan Alfons
1091     * Kr&uuml;ger</a>
1092     */
1093 alfonx 307 public void setMaxZoomScale(final Double maxZoomScale) {
1094 alfonx 144 this.maxZoomScale = maxZoomScale;
1095     }
1096 mojays 2
1097 alfonx 144 /**
1098     * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1099     * value of the two.
1100     *
1101     * @author <a href="mailto:[email protected]">Stefan Alfons
1102     * Kr&uuml;ger</a>
1103     */
1104 alfonx 307 public void setMinZoomScale(final Double minZoomScale) {
1105 alfonx 144 this.minZoomScale = minZoomScale;
1106     }
1107 mojays 2
1108 alfonx 414 /**
1109     * Defines an evelope of the viwable area. The JMapPane will never show
1110     * anything outside of this extend.
1111     *
1112     * @param maxExtend
1113     * <code>null</code> to not have this restriction.
1114     */
1115     public void setMaxExtend(Envelope maxExtend) {
1116     this.maxExtend = maxExtend;
1117     }
1118    
1119     /**
1120     * Returns the evelope of the viewable area. The JMapPane will never show
1121 alfonx 418 * anything outside of this extend. If this has been set to
1122     * <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return
1123     * quickly the context's bounds. It it takes to long to determine the
1124     * context bounds, <code>null</code> is returned.
1125 alfonx 414 *
1126     * @param maxExtend
1127     * <code>null</code> to not have this restriction.
1128     */
1129    
1130     public Envelope getMaxExtend() {
1131 alfonx 418 if (maxExtend == null) {
1132     try {
1133 mojays 436 return fixAspectRatio(
1134     this.getBounds(),
1135     // Kartenbereich um 10% vergroessern
1136     JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1),
1137     true
1138     );
1139 alfonx 418 } catch (IOException e) {
1140 mojays 436 LOGGER.warn(
1141 mojays 485 "maxExtend == null; failed to getLayerBounds of context",
1142 alfonx 418 e);
1143     }
1144     }
1145 alfonx 414 return maxExtend;
1146     }
1147    
1148 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26