/[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 505 - (hide annotations)
Sat Oct 31 10:53:43 2009 UTC (15 years, 4 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
File size: 34624 byte(s)
* Added fast scaled previews to JMapPane rendering. Works for ZoomIn and ZoomOut. 
* Added setMapbackground and getMapBackground to JMapPane

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26