/[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 414 - (hide annotations)
Thu Sep 24 13:23:08 2009 UTC (15 years, 5 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
File size: 33486 byte(s)
JMapPane can optionally be given an envelope. When zooming/panning it will never show anything outside this envelope.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26