/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26