/[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 478 - (hide annotations)
Tue Oct 20 10:37:01 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: 34440 byte(s)
GT-JMapPane, GeoMapPane: "our" Reference to ShapefileRenderer removed
defaults/.classpath updated to gt2-2.6.0
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     LOGGER.warn("mapArea has been null in method fixAspectRatio, returning an unmodified ");
374     }
375 mojays 2
376 alfonx 307 final double mapWidth = mapArea.getWidth(); /* get the extent of the map */
377     final double mapHeight = mapArea.getHeight();
378     final double scaleX = r.getWidth() / mapArea.getWidth(); /*
379 alfonx 414 * calculate the
380     * new scale
381     */
382 mojays 2
383 alfonx 307 final double scaleY = r.getHeight() / mapArea.getHeight();
384 alfonx 144 double scale = 1.0; // stupid compiler!
385 mojays 2
386 alfonx 418 if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) {
387 alfonx 144 scale = scaleX;
388     } else {
389     scale = scaleY;
390     }
391 mojays 2
392 alfonx 144 /* calculate the difference in width and height of the new extent */
393 alfonx 307 final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);
394     final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);
395 mojays 2
396 alfonx 144 /*
397     * System.out.println("delta x " + deltaX);
398     * System.out.println("delta y " + deltaY);
399     */
400 mojays 2
401 alfonx 144 /* create the new extent */
402 alfonx 414 final Coordinate ll = new Coordinate(
403     mapArea.getMinX() - (deltaX / 2.0), mapArea.getMinY()
404     - (deltaY / 2.0));
405     final Coordinate ur = new Coordinate(
406     mapArea.getMaxX() + (deltaX / 2.0), mapArea.getMaxY()
407     + (deltaY / 2.0));
408 mojays 2
409 alfonx 144 return new Envelope(ll, ur);
410     }
411 mojays 2
412 alfonx 307 public void mouseClicked(final MouseEvent e) {
413 alfonx 414 if (mapArea == null)
414     return;
415 alfonx 144 // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
416     // h:"+mapArea.getHeight());
417 alfonx 307 final Rectangle bounds = this.getBounds();
418     final double x = (double) (e.getX());
419     final double y = (double) (e.getY());
420     final double width = mapArea.getWidth();
421     final double height = mapArea.getHeight();
422 alfonx 144 // xulu.sc
423     // double width2 = mapArea.getWidth() / 2.0;
424     // double height2 = mapArea.getHeight() / 2.0;
425 alfonx 307 final double width2 = width / 2.0;
426     final double height2 = height / 2.0;
427 alfonx 144 // xulu.ec
428 alfonx 414 final double mapX = ((x * width) / (double) bounds.width)
429     + mapArea.getMinX();
430 alfonx 307 final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
431 alfonx 144 + mapArea.getMinY();
432 mojays 2
433 alfonx 144 /*
434     * System.out.println(""+x+"->"+mapX);
435     * System.out.println(""+y+"->"+mapY);
436     */
437 mojays 2
438 alfonx 144 /*
439     * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());
440     * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());
441     */
442     double zlevel = 1.0;
443 mojays 2
444 alfonx 144 switch (state) {
445     case Pan:
446     zlevel = 1.0;
447     // xulu.sc SK: return here.. a mouselistener is managing the PANNING
448     // break;
449     return;
450     // xulu.ec
451     case ZoomIn:
452     zlevel = zoomFactor;
453 mojays 2
454 alfonx 144 break;
455 mojays 2
456 alfonx 144 case ZoomOut:
457     zlevel = 1.0 / zoomFactor;
458 mojays 2
459 alfonx 144 break;
460 alfonx 414 //
461     // case Select:
462     // doSelection(mapX, mapY, selectionLayer);
463     //
464     // return;
465 mojays 2
466 alfonx 144 default:
467     return;
468     }
469 mojays 2
470 alfonx 307 final Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
471 alfonx 144 - (height2 / zlevel));
472 alfonx 307 final Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
473 alfonx 144 + (height2 / zlevel));
474     // xulu.sc SK: Check for min/max scale
475     // mapArea = new Envelope(ll, ur);
476     final Envelope newMapArea = new Envelope(ll, ur);
477     setMapArea(bestAllowedMapArea(newMapArea));
478     // xulu.ec
479 mojays 2
480 alfonx 144 // sk.ec
481 mojays 2
482 alfonx 144 // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"
483     // h:"+mapArea.getHeight());
484     repaint();
485     }
486 mojays 2
487 alfonx 307 public void mouseEntered(final MouseEvent e) {
488 alfonx 144 }
489 mojays 2
490 alfonx 307 public void mouseExited(final MouseEvent e) {
491 alfonx 144 }
492 mojays 2
493 alfonx 307 public void mousePressed(final MouseEvent e) {
494 alfonx 144 startX = e.getX();
495     startY = e.getY();
496     lastX = 0;
497     lastY = 0;
498     }
499 mojays 2
500 alfonx 307 public void mouseReleased(final MouseEvent e) {
501     final int endX = e.getX();
502     final int endY = e.getY();
503 mojays 2
504 alfonx 144 processDrag(startX, startY, endX, endY, e);
505     lastX = 0;
506     lastY = 0;
507 mojays 2
508 alfonx 144 /**
509     * Es wird nicht (mehr) gepannt!
510     */
511     panning_started = false;
512     }
513 mojays 2
514 alfonx 307 public void mouseDragged(final MouseEvent e) {
515     final Graphics graphics = this.getGraphics();
516     final int x = e.getX();
517     final int y = e.getY();
518 mojays 2
519 alfonx 144 if ((state == JMapPane.Pan)
520     || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
521     /**
522     * SK: Der Cursor wird auf PANNING gesetzt.
523     */
524     if (panning_started == false) {
525     panning_started = true;
526     setCursor(SwingUtil.PANNING_CURSOR);
527     }
528 mojays 2
529 alfonx 144 // move the image with the mouse
530     if ((lastX > 0) && (lastY > 0)) {
531 alfonx 307 final int dx = lastX - startX;
532     final int dy = lastY - startY;
533 alfonx 144 // System.out.println("translate "+dx+","+dy);
534     final Graphics2D g2 = panningImage.createGraphics();
535     g2.setBackground(new Color(240, 240, 240)); // TODO richtige
536 alfonx 414 // farbe? am besten
537     // vom L&F die
538     // hintergrundfarbe
539     // auslesen...
540    
541 mojays 2 g2.clearRect(0, 0, this.getWidth(), this.getHeight());
542     g2.drawImage(baseImage, dx, dy, this);
543 alfonx 144 graphics.drawImage(panningImage, 0, 0, this);
544     }
545 mojays 2
546 alfonx 144 lastX = x;
547     lastY = y;
548     } else
549 mojays 2
550 alfonx 144 if ((state == JMapPane.ZoomIn) || (state == JMapPane.ZoomOut)) {
551 mojays 2
552 alfonx 144 graphics.setXORMode(Color.WHITE);
553 mojays 2
554 alfonx 144 if ((lastX > 0) && (lastY > 0)) {
555     drawRectangle(graphics);
556     }
557 mojays 2
558 alfonx 144 // draw new box
559     lastX = x;
560     lastY = y;
561     drawRectangle(graphics);
562 alfonx 414 }
563     // else if (state == JMapPane.Select && selectionLayer != null) {
564     //
565     // // construct a new bbox filter
566     // final Rectangle bounds = this.getBounds();
567     //
568     // final double mapWidth = mapArea.getWidth();
569     // final double mapHeight = mapArea.getHeight();
570     //
571     // final double x1 = ((this.startX * mapWidth) / (double) bounds.width)
572     // + mapArea.getMinX();
573     // final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) /
574     // (double) bounds.height)
575     // + mapArea.getMinY();
576     // final double x2 = ((x * mapWidth) / (double) bounds.width)
577     // + mapArea.getMinX();
578     // final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double)
579     // bounds.height)
580     // + mapArea.getMinY();
581     // final double left = Math.min(x1, x2);
582     // final double right = Math.max(x1, x2);
583     // final double bottom = Math.min(y1, y2);
584     // final double top = Math.max(y1, y2);
585     //
586     // String name = selectionLayer.getFeatureSource().getSchema()
587     // .getDefaultGeometry().getName();
588     //
589     // if (name == "") {
590     // name = "the_geom";
591     // }
592     // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
593     // top,
594     // getContext().getCoordinateReferenceSystem().toString());
595     // if (selectionManager != null) {
596     // selectionManager.selectionChanged(this, bb);
597     // }
598     //
599     // graphics.setXORMode(Color.green);
600     //
601     // /*
602     // * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
603     // */
604     //
605     // // draw new box
606     // lastX = x;
607     // lastY = y;
608     // drawRectangle(graphics);
609     // }
610 mojays 2
611 alfonx 144 }
612 mojays 2
613 alfonx 144 // sk.cs
614     // private void processDrag(int x1, int y1, int x2, int y2) {
615     // sk.ce
616     protected void processDrag(final int x1, final int y1, final int x2,
617 alfonx 307 final int y2, final MouseEvent e) {
618 mojays 2
619 alfonx 431
620 alfonx 144 /****
621 alfonx 431 * If no layers exist, we ignore the drag.
622 alfonx 144 */
623 alfonx 431 if (context.getLayerCount() <= 0) {
624 alfonx 144 return;
625 alfonx 431 }
626    
627 alfonx 144 // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
628     // + x2 + "," + y2);
629     if ((x1 == x2) && (y1 == y2)) {
630     if (isClickable()) {
631     mouseClicked(new MouseEvent(this, 0, new Date().getTime(), 0,
632     x1, y1, y2, false));
633     }
634 mojays 2
635 alfonx 144 return;
636     }
637 mojays 2
638 alfonx 307 final Rectangle bounds = this.getBounds();
639 mojays 2
640 alfonx 307 final double mapWidth = mapArea.getWidth();
641     final double mapHeight = mapArea.getHeight();
642 mojays 2
643 alfonx 307 final double startX = ((x1 * mapWidth) / (double) bounds.width)
644 alfonx 144 + mapArea.getMinX();
645 alfonx 307 final double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)
646 alfonx 144 + mapArea.getMinY();
647 alfonx 307 final double endX = ((x2 * mapWidth) / (double) bounds.width)
648 alfonx 144 + mapArea.getMinX();
649 alfonx 307 final double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)
650 alfonx 144 + mapArea.getMinY();
651 mojays 2
652 alfonx 144 if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {
653     // move the image with the mouse
654     // calculate X offsets from start point to the end Point
655 alfonx 307 final double deltaX1 = endX - startX;
656 mojays 2
657 alfonx 144 // System.out.println("deltaX " + deltaX1);
658     // new edges
659 alfonx 307 final double left = mapArea.getMinX() - deltaX1;
660     final double right = mapArea.getMaxX() - deltaX1;
661 mojays 2
662 alfonx 144 // now for Y
663 alfonx 307 final double deltaY1 = endY - startY;
664 mojays 2
665 alfonx 144 // System.out.println("deltaY " + deltaY1);
666 alfonx 307 final double bottom = mapArea.getMinY() - deltaY1;
667     final double top = mapArea.getMaxY() - deltaY1;
668     final Coordinate ll = new Coordinate(left, bottom);
669     final Coordinate ur = new Coordinate(right, top);
670 alfonx 144 // xulu.sc
671     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
672 alfonx 431
673    
674 alfonx 418 setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
675 alfonx 144 // xulu.ec
676     } else if (state == JMapPane.ZoomIn) {
677 mojays 2
678 alfonx 144 // Zu kleine Flächen sollen nicht gezoomt werden.
679     // sk.bc
680     if ((Math.abs(x1 - x2) * Math.abs(y2 - y1)) < 150)
681     return;
682     // sk.ec
683 mojays 2
684 alfonx 144 drawRectangle(this.getGraphics());
685     // make the dragged rectangle (in map coords) the new BBOX
686 alfonx 307 final double left = Math.min(startX, endX);
687     final double right = Math.max(startX, endX);
688     final double bottom = Math.min(startY, endY);
689     final double top = Math.max(startY, endY);
690     final Coordinate ll = new Coordinate(left, bottom);
691     final Coordinate ur = new Coordinate(right, top);
692 alfonx 144 // xulu.sc
693 mojays 2
694 alfonx 144 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
695     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
696 mojays 2
697 alfonx 144 // sk.sc
698 alfonx 414 // {
699     // // SK tries to paint a preview of the zoom ;-9 aha.... well
700     // Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
701     // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
702     // RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
703     // graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
704     // RenderingHints.VALUE_ANTIALIAS_OFF);
705     // graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
706     // RenderingHints.VALUE_RENDER_SPEED);
707     // graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
708     // JMapPane.this.getHeight(), x1, y1, x2, y2, null);
709     // }
710 alfonx 144 // xulu.ec
711     } else if (state == JMapPane.ZoomOut) {
712     drawRectangle(this.getGraphics());
713 mojays 2
714 alfonx 144 // make the dragged rectangle in screen coords the new map size?
715 alfonx 307 final double left = Math.min(startX, endX);
716     final double right = Math.max(startX, endX);
717     final double bottom = Math.min(startY, endY);
718     final double top = Math.max(startY, endY);
719     final double nWidth = (mapWidth * mapWidth) / (right - left);
720     final double nHeight = (mapHeight * mapHeight) / (top - bottom);
721     final double deltaX1 = left - mapArea.getMinX();
722     final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
723     final double deltaY1 = bottom - mapArea.getMinY();
724     final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
725     final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
726 alfonx 144 mapArea.getMinY() - nDeltaY1);
727 alfonx 307 final double deltaX2 = mapArea.getMaxX() - right;
728     final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
729     final double deltaY2 = mapArea.getMaxY() - top;
730     final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
731     final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
732 alfonx 144 mapArea.getMaxY() + nDeltaY2);
733     // xulu.sc
734     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
735     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
736 mojays 2
737 alfonx 144 // xulu.ec
738 alfonx 414 }
739     // else if (state == JMapPane.Select && selectionLayer != null) {
740     // final double left = Math.min(startX, endX);
741     // final double right = Math.max(startX, endX);
742     // final double bottom = Math.min(startY, endY);
743     // final double top = Math.max(startY, endY);
744     //
745     // String name = selectionLayer.getFeatureSource().getSchema()
746     // .getDefaultGeometry().getLocalName();
747     //
748     // if (name == "") {
749     // name = "the_geom";
750     // }
751     // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
752     // top,
753     // getContext().getCoordinateReferenceSystem().toString());
754     // // System.out.println(bb.toString());
755     // if (selectionManager != null) {
756     // selectionManager.selectionChanged(this, bb);
757     // }
758     // /*
759     // * FeatureCollection fc; selection = null; try { fc =
760     // * selectionLayer.getFeatureSource().getFeatures(bb); selection =
761     // * fc; } catch (IOException e) { e.printStackTrace(); }
762     // */
763     // }
764 mojays 2
765 alfonx 144 // xulu.so
766     // setMapArea(mapArea);
767     // xulu.eo
768     repaint();
769     }
770 mojays 2
771 alfonx 144 private boolean isClickable() {
772     return clickable;
773     }
774 mojays 2
775 alfonx 414 //
776     // private org.geotools.styling.Style setupStyle(final int type, final Color
777     // color) {
778     // final StyleFactory sf = org.geotools.factory.CommonFactoryFinder
779     // .getStyleFactory(null);
780     // final StyleBuilder sb = new StyleBuilder();
781     //
782     // org.geotools.styling.Style s = sf.createStyle();
783     // s.setTitle("selection");
784     //
785     // // TODO parameterise the color
786     // final PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);
787     // ps.setStroke(sb.createStroke(color));
788     //
789     // final LineSymbolizer ls = sb.createLineSymbolizer(color);
790     // final Graphic h = sb.createGraphic();
791     // h.setMarks(new Mark[] { sb.createMark("square", color) });
792     //
793     // final PointSymbolizer pts = sb.createPointSymbolizer(h);
794     //
795     // // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});
796     // switch (type) {
797     // case POLYGON:
798     // s = sb.createStyle(ps);
799     //
800     // break;
801     //
802     // case POINT:
803     // s = sb.createStyle(pts);
804     //
805     // break;
806     //
807     // case LINE:
808     // s = sb.createStyle(ls);
809     // }
810     //
811     // return s;
812     // }
813 mojays 2
814 alfonx 307 public void propertyChange(final PropertyChangeEvent evt) {
815     final String prop = evt.getPropertyName();
816 mojays 2
817 alfonx 144 if (prop.equalsIgnoreCase("crs")) {
818     context.setAreaOfInterest(context.getAreaOfInterest(),
819     (CoordinateReferenceSystem) evt.getNewValue());
820     }
821     }
822 mojays 2
823 alfonx 144 public boolean isReset() {
824     return reset;
825     }
826 mojays 2
827 alfonx 307 public void setReset(final boolean reset) {
828 alfonx 144 this.reset = reset;
829     }
830 mojays 2
831 alfonx 307 public void layerAdded(final MapLayerListEvent event) {
832 alfonx 144 changed = true;
833 mojays 2
834 alfonx 144 if (context.getLayers().length == 1) { // the first one
835 mojays 2
836 alfonx 144 try {
837     // xulu.sc
838     // mapArea = context.getLayerBounds();
839     mapArea = context.getAreaOfInterest();
840     if (mapArea == null)
841     mapArea = context.getLayerBounds();
842     // xulu.ec
843 alfonx 307 } catch (final IOException e) {
844 alfonx 144 // TODO Auto-generated catch block
845     e.printStackTrace();
846     }
847 mojays 2
848 alfonx 144 reset = true;
849     }
850 mojays 2
851 alfonx 144 repaint();
852     }
853 mojays 2
854 alfonx 307 public void layerRemoved(final MapLayerListEvent event) {
855 alfonx 144 changed = true;
856     repaint();
857     }
858 mojays 2
859 alfonx 307 public void layerChanged(final MapLayerListEvent event) {
860 alfonx 144 changed = true;
861     // System.out.println("layer changed - repaint");
862     repaint();
863     }
864 mojays 2
865 alfonx 307 public void layerMoved(final MapLayerListEvent event) {
866 alfonx 144 changed = true;
867     repaint();
868     }
869 mojays 2
870 alfonx 307 protected void drawRectangle(final Graphics graphics) {
871 alfonx 144 // undraw last box/draw new box
872 alfonx 307 final int left = Math.min(startX, lastX);
873     final int right = Math.max(startX, lastX);
874     final int top = Math.max(startY, lastY);
875     final int bottom = Math.min(startY, lastY);
876     final int width = right - left;
877     final int height = top - bottom;
878 alfonx 144 // System.out.println("drawing rect("+left+","+bottom+","+ width+","+
879     // height+")");
880     graphics.drawRect(left, bottom, width, height);
881     }
882 mojays 2
883 alfonx 144 /**
884     * if clickable is set to true then a single click on the map pane will zoom
885     * or pan the map.
886     *
887     * @param clickable
888     */
889 alfonx 307 public void setClickable(final boolean clickable) {
890 alfonx 144 this.clickable = clickable;
891     }
892 mojays 2
893 alfonx 307 public void mouseMoved(final MouseEvent e) {
894 alfonx 144 }
895    
896     // xulu.sn
897 alfonx 76 /**
898     * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
899     * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
900     * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>
901     * Since 21. April 09: Before thecalculation starts, the aspect ratio is
902     * corrected. This change implies, that setMapArea() will most of the time
903     * not allow setting to a wrong aspectRatio.
904     *
905     * @author <a href="mailto:[email protected]">Stefan Alfons
906     * Kr&uuml;ger</a>
907     */
908     public Envelope bestAllowedMapArea(Envelope env) {
909     if (getWidth() == 0)
910     return env;
911     if (env == null)
912     return env;
913 mojays 2
914 alfonx 414 Envelope newArea = null;
915    
916 alfonx 76 /**
917 alfonx 144 * Correct the aspect Ratio before we check the rest. Otherwise we might
918     * easily fail.
919 alfonx 76 */
920 alfonx 418 env = fixAspectRatio(this.getBounds(), env, false);
921 mojays 2
922 alfonx 307 final double scale = env.getWidth() / getWidth();
923     final double centerX = env.getMinX() + env.getWidth() / 2.;
924     final double centerY = env.getMinY() + env.getHeight() / 2.;
925 alfonx 414 double newWidth2 = 0;
926     double newHeight2 = 0;
927 alfonx 76 if (scale < getMaxZoomScale()) {
928     // ****************************************************************************
929     // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
930     // ****************************************************************************
931     newWidth2 = getMaxZoomScale() * getWidth() / 2.;
932     newHeight2 = getMaxZoomScale() * getHeight() / 2.;
933     } else if (scale > getMinZoomScale()) {
934     // ****************************************************************************
935     // Wir zoomen weiter raus als erlaubt => Anpassen des envelope
936     // ****************************************************************************
937     newWidth2 = getMinZoomScale() * getWidth() / 2.;
938     newHeight2 = getMinZoomScale() * getHeight() / 2.;
939     } else {
940     // ****************************************************************************
941     // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
942     // ****************************************************************************
943 alfonx 414 newArea = env;
944 alfonx 76 }
945 mojays 2
946 alfonx 414 if (newArea == null) {
947 mojays 2
948 alfonx 414 final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
949     - newHeight2);
950     final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
951     + newHeight2);
952    
953     newArea = new Envelope(ll, ur);
954     }
955    
956 alfonx 418 Envelope maxAllowedExtend = getMaxExtend();
957 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()) ) {
958 alfonx 418 /*
959     * If a maxExtend is set, we have to honour that...
960     */
961 alfonx 414
962 alfonx 418 // Exceeds top? Move down and maybe cut
963     if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
964     double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
965 mojays 436 // LOGGER.debug("Moving area down by " + divY);
966 alfonx 414
967 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX(),
968     newArea.getMinY() - divY), new Coordinate(newArea
969     .getMaxX(), newArea.getMaxY() - divY));
970 alfonx 414
971 alfonx 418 if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
972 mojays 436 // LOGGER.debug("Now it exeeds the bottom border.. cut!");
973 alfonx 418 // And cut the bottom if it moved out of the area
974 alfonx 414 newArea = new Envelope(new Coordinate(newArea.getMinX(),
975 alfonx 418 maxAllowedExtend.getMinY()), new Coordinate(newArea
976     .getMaxX(), newArea.getMaxY()));
977 alfonx 414
978 mojays 436 // LOGGER.debug("and fix aspect ratio");
979 alfonx 414
980 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
981 alfonx 414 }
982 alfonx 418 }
983 alfonx 414
984 alfonx 418 // Exceeds bottom? Move up and maybe cut
985     if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
986     double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
987 mojays 436 // LOGGER.debug("Moving area up by " + divY);
988 alfonx 414
989 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX(),
990     newArea.getMinY() - divY), new Coordinate(newArea
991     .getMaxX(), newArea.getMaxY() - divY));
992    
993     if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
994 alfonx 438 // LOGGER.debug("Now it exeeds the top border.. cut!");
995 alfonx 418 // And cut the bottom if it moved out of the area
996 alfonx 414 newArea = new Envelope(new Coordinate(newArea.getMinX(),
997 alfonx 418 newArea.getMinY()), new Coordinate(newArea
998     .getMaxX(), maxAllowedExtend.getMaxY()));
999 alfonx 414
1000 alfonx 438 // LOGGER.debug("and fix aspect ratio");
1001 alfonx 414
1002 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1003 alfonx 414 }
1004 alfonx 418 }
1005 alfonx 414
1006 alfonx 418 // Exceeds to the right? move and maybe cut
1007     if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1008 alfonx 414
1009 alfonx 418 // Move left..
1010     double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
1011 mojays 436 // LOGGER.debug("Moving area left by " + divX);
1012 alfonx 414
1013 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1014     newArea.getMinY()), new Coordinate(newArea.getMaxX()
1015     - divX, newArea.getMaxY()));
1016 alfonx 414
1017 alfonx 418 if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1018 alfonx 438 // LOGGER.debug("Now it exeeds the left border.. cut!");
1019 alfonx 418 // And cut the left if it moved out of the area
1020 alfonx 420 newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(),
1021 alfonx 418 newArea.getMinY()), new Coordinate(newArea
1022     .getMaxX(), newArea.getMaxY()));
1023 alfonx 414
1024 alfonx 438 // LOGGER.debug("and fix aspect ratio");
1025 alfonx 414
1026 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1027 alfonx 414 }
1028 alfonx 418 }
1029 alfonx 414
1030 alfonx 418 // Exceeds to the left? move and maybe cut
1031     if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1032 alfonx 414
1033 alfonx 418 // Move right..
1034     double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
1035 mojays 436 // LOGGER.debug("Moving area right by " + divX);
1036 alfonx 414
1037 alfonx 418 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1038     newArea.getMinY()), new Coordinate(newArea.getMaxX()
1039     - divX, newArea.getMaxY()));
1040 alfonx 414
1041 alfonx 418 if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1042 alfonx 438 // LOGGER.debug("Now it exeeds the right border.. cut!");
1043 alfonx 418 // And cut the left if it moved out of the area
1044     newArea = new Envelope(new Coordinate(newArea.getMinX(),
1045     newArea.getMinY()), new Coordinate(maxAllowedExtend
1046     .getMaxX(), newArea.getMaxY()));
1047 alfonx 414
1048 alfonx 438 // LOGGER.debug("and fix aspect ratio");
1049 alfonx 414
1050 alfonx 418 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1051 alfonx 414 }
1052     }
1053    
1054     }
1055    
1056     return newArea;
1057 alfonx 76 }
1058    
1059 alfonx 144 /**
1060     * Retuns the minimum allowed zoom scale. This is the bigger number value of
1061     * the two. Defaults to {@link Double}.MAX_VALUE
1062     *
1063     * @author <a href="mailto:[email protected]">Stefan Alfons
1064     * Kr&uuml;ger</a>
1065     */
1066     public Double getMinZoomScale() {
1067     return minZoomScale;
1068     }
1069 mojays 2
1070 alfonx 144 /**
1071     * Retuns the maximum allowed zoom scale. This is the smaller number value
1072     * of the two. Defaults to {@link Double}.MIN_VALUE
1073     *
1074     * @author <a href="mailto:[email protected]">Stefan Alfons
1075     * Kr&uuml;ger</a>
1076     */
1077     public Double getMaxZoomScale() {
1078     return maxZoomScale;
1079     }
1080 mojays 2
1081 alfonx 144 /**
1082     * Set the maximum allowed zoom scale. This is the smaller number value of
1083     * the two.
1084     *
1085     * @author <a href="mailto:[email protected]">Stefan Alfons
1086     * Kr&uuml;ger</a>
1087     */
1088 alfonx 307 public void setMaxZoomScale(final Double maxZoomScale) {
1089 alfonx 144 this.maxZoomScale = maxZoomScale;
1090     }
1091 mojays 2
1092 alfonx 144 /**
1093     * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1094     * value of the two.
1095     *
1096     * @author <a href="mailto:[email protected]">Stefan Alfons
1097     * Kr&uuml;ger</a>
1098     */
1099 alfonx 307 public void setMinZoomScale(final Double minZoomScale) {
1100 alfonx 144 this.minZoomScale = minZoomScale;
1101     }
1102 mojays 2
1103 alfonx 414 /**
1104     * Defines an evelope of the viwable area. The JMapPane will never show
1105     * anything outside of this extend.
1106     *
1107     * @param maxExtend
1108     * <code>null</code> to not have this restriction.
1109     */
1110     public void setMaxExtend(Envelope maxExtend) {
1111     this.maxExtend = maxExtend;
1112     }
1113    
1114     /**
1115     * Returns the evelope of the viewable area. The JMapPane will never show
1116 alfonx 418 * anything outside of this extend. If this has been set to
1117     * <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return
1118     * quickly the context's bounds. It it takes to long to determine the
1119     * context bounds, <code>null</code> is returned.
1120 alfonx 414 *
1121     * @param maxExtend
1122     * <code>null</code> to not have this restriction.
1123     */
1124    
1125     public Envelope getMaxExtend() {
1126 alfonx 418 if (maxExtend == null) {
1127     try {
1128 mojays 436 return fixAspectRatio(
1129     this.getBounds(),
1130     // Kartenbereich um 10% vergroessern
1131     JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1),
1132     true
1133     );
1134 alfonx 418 } catch (IOException e) {
1135 mojays 436 LOGGER.warn(
1136 alfonx 418 "maxExtend == null; faild to getLayerBounds of context",
1137     e);
1138     }
1139     }
1140 alfonx 414 return maxExtend;
1141     }
1142    
1143 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26