/[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 148 - (hide annotations)
Thu Jun 18 16:31:56 2009 UTC (15 years, 8 months ago) by alfonx
Original Path: trunk/src/org/geotools/gui/swing/JMapPane.java
File size: 38337 byte(s)
* Reorganized the UI of the Legend
* Solved the never-wide-enough legend, including the "..."
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     package org.geotools.gui.swing;
17    
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 144 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     import java.awt.image.BufferedImage;
52     import java.beans.PropertyChangeEvent;
53     import java.beans.PropertyChangeListener;
54     import java.io.IOException;
55     import java.util.Date;
56     import java.util.HashMap;
57     import java.util.Map;
58    
59     import javax.swing.JPanel;
60 alfonx 148 import javax.swing.LookAndFeel;
61 mojays 2
62     import org.apache.log4j.Logger;
63     import org.geotools.feature.FeatureCollection;
64     import org.geotools.filter.IllegalFilterException;
65     import org.geotools.gui.swing.event.HighlightChangeListener;
66     import org.geotools.gui.swing.event.HighlightChangedEvent;
67     import org.geotools.gui.swing.event.SelectionChangeListener;
68     import org.geotools.gui.swing.event.SelectionChangedEvent;
69     import org.geotools.map.DefaultMapContext;
70     import org.geotools.map.MapContext;
71     import org.geotools.map.MapLayer;
72     import org.geotools.map.event.MapLayerListEvent;
73     import org.geotools.map.event.MapLayerListListener;
74     import org.geotools.referencing.crs.DefaultGeographicCRS;
75     import org.geotools.renderer.GTRenderer;
76     import org.geotools.renderer.lite.LabelCache;
77     import org.geotools.renderer.lite.LabelCacheDefault;
78     import org.geotools.renderer.lite.StreamingRenderer;
79     import org.geotools.styling.Graphic;
80     import org.geotools.styling.LineSymbolizer;
81     import org.geotools.styling.Mark;
82     import org.geotools.styling.PointSymbolizer;
83     import org.geotools.styling.PolygonSymbolizer;
84     import org.geotools.styling.Style;
85     import org.geotools.styling.StyleBuilder;
86     import org.geotools.styling.StyleFactory;
87     import org.opengis.filter.Filter;
88     import org.opengis.filter.FilterFactory2;
89     import org.opengis.referencing.crs.CoordinateReferenceSystem;
90    
91     import schmitzm.swing.SwingUtil;
92    
93     import com.vividsolutions.jts.geom.Coordinate;
94     import com.vividsolutions.jts.geom.Envelope;
95     import com.vividsolutions.jts.geom.Geometry;
96     import com.vividsolutions.jts.geom.GeometryFactory;
97    
98     public class JMapPane extends JPanel implements MouseListener,
99 alfonx 144 MouseMotionListener, HighlightChangeListener, SelectionChangeListener,
100     PropertyChangeListener, MapLayerListListener {
101     private static Logger LOGGER = Logger.getLogger(JMapPane.class.getName());
102     /**
103 mojays 2 *
104     */
105 alfonx 144 private static final long serialVersionUID = -8647971481359690499L;
106 mojays 2
107 alfonx 144 public static final int Reset = 0;
108 mojays 2
109 alfonx 144 public static final int ZoomIn = 1;
110 mojays 2
111 alfonx 144 public static final int ZoomOut = 2;
112 mojays 2
113 alfonx 144 public static final int Pan = 3;
114 mojays 2
115 alfonx 144 public static final int Select = 4;
116 mojays 2
117 alfonx 144 private static final int POLYGON = 0;
118 mojays 2
119 alfonx 144 private static final int LINE = 1;
120 mojays 2
121 alfonx 144 private static final int POINT = 2;
122 mojays 2
123 alfonx 144 /**
124     * what renders the map
125     */
126     GTRenderer renderer;
127 mojays 2
128 alfonx 144 private GTRenderer highlightRenderer, selectionRenderer;
129 mojays 2
130 alfonx 144 /**
131     * the map context to render
132     */
133     MapContext context;
134 mojays 2
135 alfonx 144 private MapContext selectionContext;
136 mojays 2
137 alfonx 144 /**
138     * the area of the map to draw
139     */
140     // xulu.sc
141     // Envelope mapArea;
142     protected Envelope mapArea;
143     // xulu.ec
144 mojays 2
145 alfonx 144 /**
146     * the size of the pane last time we drew
147     */
148     // xulu.sc
149     // private Rectangle oldRect = null;
150     protected Rectangle oldRect = null;
151     // xulu.ec
152 mojays 2
153 alfonx 144 /**
154     * the last map area drawn.
155     */
156     // xulu.sc
157     // private Envelope oldMapArea = null;
158     protected Envelope oldMapArea = null;
159     // xulu.ec
160 mojays 2
161 alfonx 144 /**
162     * the base image of the map
163     */
164     protected BufferedImage baseImage, panningImage;
165     // SK: private BufferedImage baseImage, panningImage;
166 mojays 2
167 alfonx 144 /**
168     * image of selection
169     */
170     private BufferedImage selectImage;
171 mojays 2
172 alfonx 144 /**
173     * style for selected items
174     */
175     private Style selectionStyle;
176 mojays 2
177 alfonx 144 /**
178     * layer that selection works on
179     */
180     private MapLayer selectionLayer;
181 mojays 2
182 alfonx 144 /**
183     * layer that highlight works on
184     */
185     private MapLayer highlightLayer;
186 mojays 2
187 alfonx 144 /**
188     * the object which manages highlighting
189     */
190     private HighlightManager highlightManager;
191 mojays 2
192 alfonx 144 /**
193     * is highlighting on or off
194     */
195     private boolean highlight = true;
196 mojays 2
197 alfonx 144 /**
198     * a factory for filters
199     */
200     FilterFactory2 ff;
201 mojays 2
202 alfonx 144 /**
203     * a factory for geometries
204     */
205     GeometryFactory gf = new GeometryFactory(); // FactoryFinder.getGeometryFactory(null);
206 mojays 2
207 alfonx 144 /**
208     * the collections of features to be selected or highlighted
209     */
210     FeatureCollection selection;
211 mojays 2
212 alfonx 144 /**
213     * the collections of features to be selected or highlighted
214     */
215     FeatureCollection highlightFeature;
216 mojays 2
217 alfonx 144 private int state = ZoomIn;
218 mojays 2
219 alfonx 144 /**
220     * how far to zoom in or out
221     */
222     private double zoomFactor = 2.0;
223 mojays 2
224 alfonx 144 Style lineHighlightStyle;
225 mojays 2
226 alfonx 144 Style pointHighlightStyle;
227 mojays 2
228 alfonx 144 Style polygonHighlightStyle;
229 mojays 2
230 alfonx 144 Style polygonSelectionStyle;
231 mojays 2
232 alfonx 144 Style pointSelectionStyle;
233 mojays 2
234 alfonx 144 Style lineSelectionStyle;
235 mojays 2
236 alfonx 144 boolean changed = true;
237 mojays 2
238 alfonx 144 LabelCache labelCache = new LabelCacheDefault();
239 mojays 2
240 alfonx 144 // xulu.sc
241     // private boolean reset = false;
242     protected boolean reset = false;
243     // xulu.ec
244 mojays 2
245 alfonx 144 int startX;
246 mojays 2
247 alfonx 144 int startY;
248 mojays 2
249 alfonx 144 private boolean clickable;
250 mojays 2
251 alfonx 144 int lastX;
252 mojays 2
253 alfonx 144 int lastY;
254 mojays 2
255 alfonx 144 private SelectionManager selectionManager;
256     // xulu.sn
257     private Double maxZoomScale = Double.MIN_VALUE;
258     private Double minZoomScale = Double.MAX_VALUE;
259     // xulu.en
260 mojays 2
261 alfonx 144 // sk.sn
262     /**
263     * Wenn true, dann wurde PANNING via mouseDraged-Events begonnen. Dieses
264     * Flag wird benutzt um nur einmal den passenden Cursor nur einmal zu
265     * setzen.
266     */
267 mojays 2 private boolean panning_started = false;
268    
269 alfonx 144 // sk.en
270 mojays 2
271 alfonx 144 public JMapPane() {
272     this(null, true, null, null);
273     }
274 mojays 2
275 alfonx 144 /**
276     * create a basic JMapPane
277     *
278     * @param render
279     * - how to draw the map
280     * @param context
281     * - the map context to display
282     */
283     public JMapPane(GTRenderer render, MapContext context) {
284     this(null, true, render, context);
285     }
286 mojays 2
287 alfonx 144 /**
288     * full constructor extending JPanel
289     *
290     * @param layout
291     * - layout (probably shouldn't be set)
292     * @param isDoubleBuffered
293     * - a Swing thing I don't really understand
294     * @param render
295     * - what to draw the map with
296     * @param context
297     * - what to draw
298     */
299     public JMapPane(LayoutManager layout, boolean isDoubleBuffered,
300     GTRenderer render, MapContext context) {
301     super(layout, isDoubleBuffered);
302 mojays 2
303 alfonx 144 ff = (FilterFactory2) org.geotools.factory.CommonFactoryFinder
304     .getFilterFactory(null);
305     setRenderer(render);
306 mojays 2
307 alfonx 144 setContext(context);
308 mojays 2
309 alfonx 144 this.addMouseListener(this);
310     this.addMouseMotionListener(this);
311     setHighlightManager(new HighlightManager(highlightLayer));
312     setSelectionManager(new SelectionManager(selectionLayer));
313     lineHighlightStyle = setupStyle(LINE, Color.red);
314 mojays 2
315 alfonx 144 pointHighlightStyle = setupStyle(POINT, Color.red);
316 mojays 2
317 alfonx 144 polygonHighlightStyle = setupStyle(POLYGON, Color.red);
318 mojays 2
319 alfonx 144 polygonSelectionStyle = setupStyle(POLYGON, Color.cyan);
320 mojays 2
321 alfonx 144 pointSelectionStyle = setupStyle(POINT, Color.cyan);
322 mojays 2
323 alfonx 144 lineSelectionStyle = setupStyle(LINE, Color.cyan);
324     setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
325     }
326 mojays 2
327 alfonx 144 /**
328     * get the renderer
329     */
330     public GTRenderer getRenderer() {
331     return renderer;
332     }
333 mojays 2
334 alfonx 144 public void setRenderer(GTRenderer renderer) {
335     Map hints = new HashMap();
336     if (renderer instanceof StreamingRenderer) {
337     hints = renderer.getRendererHints();
338     if (hints == null) {
339     hints = new HashMap();
340     }
341     if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {
342     labelCache = (LabelCache) hints
343     .get(StreamingRenderer.LABEL_CACHE_KEY);
344     } else {
345     hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
346     }
347     renderer.setRendererHints(hints);
348     }
349 mojays 2
350 alfonx 144 this.renderer = renderer;
351     this.highlightRenderer = new StreamingRenderer();
352     this.selectionRenderer = new StreamingRenderer();
353 mojays 2
354 alfonx 144 hints.put("memoryPreloadingEnabled", Boolean.FALSE);
355     highlightRenderer.setRendererHints(hints);
356     selectionRenderer.setRendererHints(hints);
357 mojays 2
358 alfonx 144 if (this.context != null) {
359     this.renderer.setContext(this.context);
360     }
361     }
362 mojays 2
363 alfonx 144 public MapContext getContext() {
364     return context;
365     }
366 mojays 2
367 alfonx 144 public void setContext(MapContext context) {
368     if (this.context != null) {
369     this.context.removeMapLayerListListener(this);
370     }
371 mojays 2
372 alfonx 144 this.context = context;
373 mojays 2
374 alfonx 144 if (context != null) {
375     this.context.addMapLayerListListener(this);
376     }
377 mojays 2
378 alfonx 144 if (renderer != null) {
379     renderer.setContext(this.context);
380     }
381     }
382 mojays 2
383 alfonx 144 public Envelope getMapArea() {
384     return mapArea;
385     }
386 mojays 2
387 alfonx 144 public void setMapArea(Envelope mapArea) {
388     this.mapArea = mapArea;
389     }
390 mojays 2
391 alfonx 144 public int getState() {
392     return state;
393     }
394 mojays 2
395 alfonx 144 public void setState(int state) {
396     this.state = state;
397 mojays 2
398 alfonx 144 // System.out.println("State: " + state);
399     }
400 mojays 2
401 alfonx 144 public double getZoomFactor() {
402     return zoomFactor;
403     }
404 mojays 2
405 alfonx 144 public void setZoomFactor(double zoomFactor) {
406     this.zoomFactor = zoomFactor;
407     }
408 mojays 2
409 alfonx 144 public MapLayer getSelectionLayer() {
410     return selectionLayer;
411     }
412 mojays 2
413 alfonx 144 public void setSelectionLayer(MapLayer selectionLayer) {
414     this.selectionLayer = selectionLayer;
415     if (selectionManager != null) {
416     selectionManager.setSelectionLayer(selectionLayer);
417     }
418     }
419 mojays 2
420 alfonx 144 public boolean isHighlight() {
421     return highlight;
422     }
423 mojays 2
424 alfonx 144 public void setHighlight(boolean highlight) {
425     this.highlight = highlight;
426     }
427 mojays 2
428 alfonx 144 public MapLayer getHighlightLayer() {
429     return highlightLayer;
430     }
431 mojays 2
432 alfonx 144 public void setHighlightLayer(MapLayer highlightLayer) {
433     this.highlightLayer = highlightLayer;
434 mojays 2
435 alfonx 144 if (highlightManager != null) {
436     highlightManager.setHighlightLayer(highlightLayer);
437     }
438     }
439 mojays 2
440 alfonx 144 public HighlightManager getHighlightManager() {
441     return highlightManager;
442     }
443 mojays 2
444 alfonx 144 public void setHighlightManager(HighlightManager highlightManager) {
445     this.highlightManager = highlightManager;
446     this.highlightManager.addHighlightChangeListener(this);
447     this.addMouseMotionListener(this.highlightManager);
448     }
449 mojays 2
450 alfonx 144 public Style getLineHighlightStyle() {
451     return lineHighlightStyle;
452     }
453 mojays 2
454 alfonx 144 public void setLineHighlightStyle(Style lineHighlightStyle) {
455     this.lineHighlightStyle = lineHighlightStyle;
456     }
457 mojays 2
458 alfonx 144 public Style getLineSelectionStyle() {
459     return lineSelectionStyle;
460     }
461 mojays 2
462 alfonx 144 public void setLineSelectionStyle(Style lineSelectionStyle) {
463     this.lineSelectionStyle = lineSelectionStyle;
464     }
465 mojays 2
466 alfonx 144 public Style getPointHighlightStyle() {
467     return pointHighlightStyle;
468     }
469 mojays 2
470 alfonx 144 public void setPointHighlightStyle(Style pointHighlightStyle) {
471     this.pointHighlightStyle = pointHighlightStyle;
472     }
473 mojays 2
474 alfonx 144 public Style getPointSelectionStyle() {
475     return pointSelectionStyle;
476     }
477 mojays 2
478 alfonx 144 public void setPointSelectionStyle(Style pointSelectionStyle) {
479     this.pointSelectionStyle = pointSelectionStyle;
480     }
481 mojays 2
482 alfonx 144 public Style getPolygonHighlightStyle() {
483     return polygonHighlightStyle;
484     }
485 mojays 2
486 alfonx 144 public void setPolygonHighlightStyle(Style polygonHighlightStyle) {
487     this.polygonHighlightStyle = polygonHighlightStyle;
488     }
489 mojays 2
490 alfonx 144 public Style getPolygonSelectionStyle() {
491     return polygonSelectionStyle;
492     }
493 mojays 2
494 alfonx 144 public void setPolygonSelectionStyle(Style polygonSelectionStyle) {
495     this.polygonSelectionStyle = polygonSelectionStyle;
496     }
497 mojays 2
498 alfonx 144 protected void paintComponent(Graphics g) {
499     super.paintComponent(g);
500 mojays 2
501 alfonx 144 if ((renderer == null) || (mapArea == null)) {
502     return;
503     }
504 mojays 2
505 alfonx 144 Rectangle r = getBounds();
506     Rectangle dr = new Rectangle(r.width, r.height);
507 mojays 2
508 alfonx 144 if (!r.equals(oldRect) || reset) {
509     if (!r.equals(oldRect) && (mapArea == null)) {
510     try {
511     mapArea = context.getLayerBounds();
512     } catch (IOException e) {
513     // TODO Auto-generated catch block
514     e.printStackTrace();
515     }
516     }
517 mojays 2
518 alfonx 144 if (mapArea != null) {
519     /* either the viewer size has changed or we've done a reset */
520     changed = true; /* note we need to redraw */
521     reset = false; /* forget about the reset */
522     oldRect = r; /* store what the current size is */
523 mojays 2
524 alfonx 144 mapArea = fixAspectRatio(r, mapArea);
525     }
526     }
527 mojays 2
528 alfonx 144 if (!mapArea.equals(oldMapArea)) { /* did the map extent change? */
529     changed = true;
530     oldMapArea = mapArea;
531     // when we tell the context that the bounds have changed WMSLayers
532     // can refresh them selves
533     context.setAreaOfInterest(mapArea, context
534     .getCoordinateReferenceSystem());
535     }
536 mojays 2
537 alfonx 144 if (changed) { /* if the map changed then redraw */
538     changed = false;
539     baseImage = new BufferedImage(dr.width, dr.height,
540     BufferedImage.TYPE_INT_ARGB);
541 mojays 2
542 alfonx 144 Graphics2D ig = baseImage.createGraphics();
543     /* System.out.println("rendering"); */
544     renderer.setContext(context);
545     labelCache.clear(); // work around anoying labelcache bug
546 mojays 2
547 alfonx 144 // draw the map
548     renderer.paint((Graphics2D) ig, dr, mapArea);
549 mojays 2
550 alfonx 144 // TODO , nur machen, wenn panning beginnt
551     panningImage = new BufferedImage(dr.width, dr.height,
552     BufferedImage.TYPE_INT_RGB);
553 mojays 2
554 alfonx 144 }
555 mojays 2
556 alfonx 144 ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
557 mojays 2
558 alfonx 144 if ((selection != null) && (selection.size() > 0)) {
559     // paint selection
560 mojays 2
561 alfonx 144 String type = selectionLayer.getFeatureSource().getSchema()
562     .getDefaultGeometry().getType().getName();
563     /* String type = selection.getDefaultGeometry().getGeometryType(); */
564     /* System.out.println(type); */
565     if (type == null)
566     type = "polygon";
567 mojays 2
568 alfonx 144 /* String type = "point"; */
569 mojays 2
570 alfonx 144 if (type.toLowerCase().endsWith("polygon")) {
571     selectionStyle = polygonSelectionStyle;
572     } else if (type.toLowerCase().endsWith("point")) {
573     selectionStyle = pointSelectionStyle;
574     } else if (type.toLowerCase().endsWith("line")) {
575     selectionStyle = lineSelectionStyle;
576     }
577 mojays 2
578 alfonx 144 selectionContext = new DefaultMapContext(DefaultGeographicCRS.WGS84);
579 mojays 2
580 alfonx 144 selectionContext.addLayer(selection, selectionStyle);
581     selectionRenderer.setContext(selectionContext);
582 mojays 2
583 alfonx 144 selectImage = new BufferedImage(dr.width, dr.height,
584     BufferedImage.TYPE_INT_ARGB);
585 mojays 2
586 alfonx 144 Graphics2D ig = selectImage.createGraphics();
587     /* System.out.println("rendering selection"); */
588     selectionRenderer.paint((Graphics2D) ig, dr, mapArea);
589 mojays 2
590 alfonx 144 ((Graphics2D) g).drawImage(selectImage, 0, 0, this);
591     }
592 mojays 2
593 alfonx 144 if (highlight && (highlightFeature != null)
594     && (highlightFeature.size() > 0)) {
595     /*
596     * String type = selection.getDefaultGeometry().getGeometryType();
597     * System.out.println(type); if(type==null) type="polygon";
598     */
599     String type = highlightLayer.getFeatureSource().getSchema()
600     .getDefaultGeometry().getType().getName();
601     /* String type = selection.getDefaultGeometry().getGeometryType(); */
602     // System.out.println(type);
603     if (type == null)
604     type = "polygon";
605 mojays 2
606 alfonx 144 /* String type = "point"; */
607     Style highlightStyle = null;
608     if (type.toLowerCase().endsWith("polygon")) {
609     highlightStyle = polygonHighlightStyle;
610     } else if (type.toLowerCase().endsWith("point")) {
611     highlightStyle = pointHighlightStyle;
612     } else if (type.toLowerCase().endsWith("line")) {
613     highlightStyle = lineHighlightStyle;
614     }
615 mojays 2
616 alfonx 144 MapContext highlightContext = new DefaultMapContext(
617     DefaultGeographicCRS.WGS84);
618 mojays 2
619 alfonx 144 highlightContext.addLayer(highlightFeature, highlightStyle);
620     highlightRenderer.setContext(highlightContext);
621 mojays 2
622 alfonx 144 /* System.out.println("rendering highlight"); */
623     highlightRenderer.paint((Graphics2D) g, dr, mapArea);
624     }
625     }
626 mojays 2
627 alfonx 144 private Envelope fixAspectRatio(Rectangle r, Envelope mapArea) {
628 mojays 2
629 alfonx 144 double mapWidth = mapArea.getWidth(); /* get the extent of the map */
630     double mapHeight = mapArea.getHeight();
631     double scaleX = r.getWidth() / mapArea.getWidth(); /*
632     * calculate the new
633     * scale
634     */
635 mojays 2
636 alfonx 144 double scaleY = r.getHeight() / mapArea.getHeight();
637     double scale = 1.0; // stupid compiler!
638 mojays 2
639 alfonx 144 if (scaleX < scaleY) { /* pick the smaller scale */
640     scale = scaleX;
641     } else {
642     scale = scaleY;
643     }
644 mojays 2
645 alfonx 144 /* calculate the difference in width and height of the new extent */
646     double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);
647     double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);
648 mojays 2
649 alfonx 144 /*
650     * System.out.println("delta x " + deltaX);
651     * System.out.println("delta y " + deltaY);
652     */
653 mojays 2
654 alfonx 144 /* create the new extent */
655     Coordinate ll = new Coordinate(mapArea.getMinX() - (deltaX / 2.0),
656     mapArea.getMinY() - (deltaY / 2.0));
657     Coordinate ur = new Coordinate(mapArea.getMaxX() + (deltaX / 2.0),
658     mapArea.getMaxY() + (deltaY / 2.0));
659 mojays 2
660 alfonx 144 return new Envelope(ll, ur);
661     }
662 mojays 2
663 alfonx 144 public void doSelection(double x, double y, MapLayer layer) {
664 mojays 2
665 alfonx 144 Geometry geometry = gf.createPoint(new Coordinate(x, y));
666 mojays 2
667 alfonx 144 // org.opengis.geometry.Geometry geometry = new Point();
668 mojays 2
669 alfonx 144 findFeature(geometry, layer);
670 mojays 2
671 alfonx 144 }
672 mojays 2
673 alfonx 144 /**
674     * @param geometry
675     * - a geometry to construct the filter with
676     * @param i
677     * - the index of the layer to search
678     * @throws IndexOutOfBoundsException
679     */
680     private void findFeature(Geometry geometry, MapLayer layer)
681     throws IndexOutOfBoundsException {
682     org.opengis.filter.spatial.BinarySpatialOperator f = null;
683 mojays 2
684 alfonx 144 if ((context == null) || (layer == null)) {
685     return;
686     }
687 mojays 2
688 alfonx 144 try {
689     String name = layer.getFeatureSource().getSchema()
690     .getDefaultGeometry().getName();
691 mojays 2
692 alfonx 144 if (name == "") {
693     name = "the_geom";
694     }
695 mojays 2
696 alfonx 144 try {
697     f = ff.contains(ff.property(name), ff.literal(geometry));
698     if (selectionManager != null) {
699     System.out.println("selection changed");
700     selectionManager.selectionChanged(this, f);
701 mojays 2
702 alfonx 144 }
703     } catch (IllegalFilterException e) {
704     // TODO Auto-generated catch block
705     e.printStackTrace();
706     }
707 mojays 2
708 alfonx 144 /*
709     * // f.addLeftGeometry(ff.property(name)); //
710     * System.out.println("looking with " + f); FeatureCollection fc =
711     * layer.getFeatureSource().getFeatures(f);
712     *
713     *
714     *
715     * if (fcol == null) { fcol = fc;
716     *
717     * // here we should set the defaultgeom type } else {
718     * fcol.addAll(fc); }
719     */
720 mojays 2
721 alfonx 144 /*
722     * GeometryAttributeType gat =
723     * layer.getFeatureSource().getSchema().getDefaultGeometry();
724     * fcol.setDefaultGeometry((Geometry)gat.createDefaultValue());
725     */
726 mojays 2
727 alfonx 144 /*
728     * Iterator fi = fc.iterator(); while (fi.hasNext()) { Feature feat
729     * = (Feature) fi.next(); System.out.println("selected " +
730     * feat.getAttribute("STATE_NAME")); }
731     */
732     } catch (IllegalFilterException e) {
733     // TODO Auto-generated catch block
734     e.printStackTrace();
735     }
736     return;
737     }
738 mojays 2
739 alfonx 144 public void mouseClicked(MouseEvent e) {
740     // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
741     // h:"+mapArea.getHeight());
742     Rectangle bounds = this.getBounds();
743     double x = (double) (e.getX());
744     double y = (double) (e.getY());
745     double width = mapArea.getWidth();
746     double height = mapArea.getHeight();
747     // xulu.sc
748     // double width2 = mapArea.getWidth() / 2.0;
749     // double height2 = mapArea.getHeight() / 2.0;
750     double width2 = width / 2.0;
751     double height2 = height / 2.0;
752     // xulu.ec
753     double mapX = ((x * width) / (double) bounds.width) + mapArea.getMinX();
754     double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
755     + mapArea.getMinY();
756 mojays 2
757 alfonx 144 /*
758     * System.out.println(""+x+"->"+mapX);
759     * System.out.println(""+y+"->"+mapY);
760     */
761 mojays 2
762 alfonx 144 /*
763     * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());
764     * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());
765     */
766     double zlevel = 1.0;
767 mojays 2
768 alfonx 144 switch (state) {
769     case Pan:
770     zlevel = 1.0;
771     // xulu.sc SK: return here.. a mouselistener is managing the PANNING
772     // break;
773     return;
774     // xulu.ec
775     case ZoomIn:
776     zlevel = zoomFactor;
777 mojays 2
778 alfonx 144 break;
779 mojays 2
780 alfonx 144 case ZoomOut:
781     zlevel = 1.0 / zoomFactor;
782 mojays 2
783 alfonx 144 break;
784 mojays 2
785 alfonx 144 case Select:
786     doSelection(mapX, mapY, selectionLayer);
787 mojays 2
788 alfonx 144 return;
789 mojays 2
790 alfonx 144 default:
791     return;
792     }
793 mojays 2
794 alfonx 144 Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
795     - (height2 / zlevel));
796     Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
797     + (height2 / zlevel));
798     // xulu.sc SK: Check for min/max scale
799     // mapArea = new Envelope(ll, ur);
800     final Envelope newMapArea = new Envelope(ll, ur);
801     setMapArea(bestAllowedMapArea(newMapArea));
802     // xulu.ec
803 mojays 2
804 alfonx 144 // sk.ec
805 mojays 2
806 alfonx 144 // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"
807     // h:"+mapArea.getHeight());
808     repaint();
809     }
810 mojays 2
811 alfonx 144 public void mouseEntered(MouseEvent e) {
812     }
813 mojays 2
814 alfonx 144 public void mouseExited(MouseEvent e) {
815     }
816 mojays 2
817 alfonx 144 public void mousePressed(MouseEvent e) {
818     startX = e.getX();
819     startY = e.getY();
820     lastX = 0;
821     lastY = 0;
822     }
823 mojays 2
824 alfonx 144 public void mouseReleased(MouseEvent e) {
825     int endX = e.getX();
826     int endY = e.getY();
827 mojays 2
828 alfonx 144 processDrag(startX, startY, endX, endY, e);
829     lastX = 0;
830     lastY = 0;
831 mojays 2
832 alfonx 144 /**
833     * Es wird nicht (mehr) gepannt!
834     */
835     panning_started = false;
836     }
837 mojays 2
838 alfonx 144 public void mouseDragged(MouseEvent e) {
839     Graphics graphics = this.getGraphics();
840     int x = e.getX();
841     int y = e.getY();
842 mojays 2
843 alfonx 144 if ((state == JMapPane.Pan)
844     || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
845     /**
846     * SK: Der Cursor wird auf PANNING gesetzt.
847     */
848     if (panning_started == false) {
849     panning_started = true;
850     setCursor(SwingUtil.PANNING_CURSOR);
851     }
852 mojays 2
853 alfonx 144 // move the image with the mouse
854     if ((lastX > 0) && (lastY > 0)) {
855     int dx = lastX - startX;
856     int dy = lastY - startY;
857     // System.out.println("translate "+dx+","+dy);
858     final Graphics2D g2 = panningImage.createGraphics();
859     g2.setBackground(new Color(240, 240, 240)); // TODO richtige
860     // farbe? am besten
861     // vom L&F die
862     // hintergrundfarbe
863     // auslesen...
864 alfonx 148
865 mojays 2 g2.clearRect(0, 0, this.getWidth(), this.getHeight());
866     g2.drawImage(baseImage, dx, dy, this);
867 alfonx 144 graphics.drawImage(panningImage, 0, 0, this);
868     }
869 mojays 2
870 alfonx 144 lastX = x;
871     lastY = y;
872     } else
873 mojays 2
874 alfonx 144 if ((state == JMapPane.ZoomIn) || (state == JMapPane.ZoomOut)) {
875 mojays 2
876 alfonx 144 graphics.setXORMode(Color.WHITE);
877 mojays 2
878 alfonx 144 if ((lastX > 0) && (lastY > 0)) {
879     drawRectangle(graphics);
880     }
881 mojays 2
882 alfonx 144 // draw new box
883     lastX = x;
884     lastY = y;
885     drawRectangle(graphics);
886     } else if (state == JMapPane.Select && selectionLayer != null) {
887 mojays 2
888 alfonx 144 // construct a new bbox filter
889     Rectangle bounds = this.getBounds();
890 mojays 2
891 alfonx 144 double mapWidth = mapArea.getWidth();
892     double mapHeight = mapArea.getHeight();
893 mojays 2
894 alfonx 144 double x1 = ((this.startX * mapWidth) / (double) bounds.width)
895     + mapArea.getMinX();
896     double y1 = (((bounds.getHeight() - this.startY) * mapHeight) / (double) bounds.height)
897     + mapArea.getMinY();
898     double x2 = ((x * mapWidth) / (double) bounds.width)
899     + mapArea.getMinX();
900     double y2 = (((bounds.getHeight() - y) * mapHeight) / (double) bounds.height)
901     + mapArea.getMinY();
902     double left = Math.min(x1, x2);
903     double right = Math.max(x1, x2);
904     double bottom = Math.min(y1, y2);
905     double top = Math.max(y1, y2);
906 mojays 2
907 alfonx 144 String name = selectionLayer.getFeatureSource().getSchema()
908     .getDefaultGeometry().getName();
909 mojays 2
910 alfonx 144 if (name == "") {
911     name = "the_geom";
912     }
913     Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,
914     getContext().getCoordinateReferenceSystem().toString());
915     if (selectionManager != null) {
916     selectionManager.selectionChanged(this, bb);
917     }
918 mojays 2
919 alfonx 144 graphics.setXORMode(Color.green);
920 mojays 2
921 alfonx 144 /*
922     * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
923     */
924 mojays 2
925 alfonx 144 // draw new box
926     lastX = x;
927     lastY = y;
928     drawRectangle(graphics);
929     }
930 mojays 2
931 alfonx 144 }
932 mojays 2
933 alfonx 144 // sk.cs
934     // private void processDrag(int x1, int y1, int x2, int y2) {
935     // sk.ce
936     protected void processDrag(final int x1, final int y1, final int x2,
937     final int y2, MouseEvent e) {
938 mojays 2
939 alfonx 144 /****
940     * If no layer is availabe we dont want a NullPointerException
941     */
942     if (mapArea == null)
943     return;
944 mojays 2
945 alfonx 144 // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
946     // + x2 + "," + y2);
947     if ((x1 == x2) && (y1 == y2)) {
948     if (isClickable()) {
949     mouseClicked(new MouseEvent(this, 0, new Date().getTime(), 0,
950     x1, y1, y2, false));
951     }
952 mojays 2
953 alfonx 144 return;
954     }
955 mojays 2
956 alfonx 144 Rectangle bounds = this.getBounds();
957 mojays 2
958 alfonx 144 double mapWidth = mapArea.getWidth();
959     double mapHeight = mapArea.getHeight();
960 mojays 2
961 alfonx 144 double startX = ((x1 * mapWidth) / (double) bounds.width)
962     + mapArea.getMinX();
963     double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)
964     + mapArea.getMinY();
965     double endX = ((x2 * mapWidth) / (double) bounds.width)
966     + mapArea.getMinX();
967     double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)
968     + mapArea.getMinY();
969 mojays 2
970 alfonx 144 if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {
971     // move the image with the mouse
972     // calculate X offsets from start point to the end Point
973     double deltaX1 = endX - startX;
974 mojays 2
975 alfonx 144 // System.out.println("deltaX " + deltaX1);
976     // new edges
977     double left = mapArea.getMinX() - deltaX1;
978     double right = mapArea.getMaxX() - deltaX1;
979 mojays 2
980 alfonx 144 // now for Y
981     double deltaY1 = endY - startY;
982 mojays 2
983 alfonx 144 // System.out.println("deltaY " + deltaY1);
984     double bottom = mapArea.getMinY() - deltaY1;
985     double top = mapArea.getMaxY() - deltaY1;
986     Coordinate ll = new Coordinate(left, bottom);
987     Coordinate ur = new Coordinate(right, top);
988     // xulu.sc
989     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
990     setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur)));
991     // xulu.ec
992     } else if (state == JMapPane.ZoomIn) {
993 mojays 2
994 alfonx 144 // Zu kleine Flächen sollen nicht gezoomt werden.
995     // sk.bc
996     if ((Math.abs(x1 - x2) * Math.abs(y2 - y1)) < 150)
997     return;
998     // sk.ec
999 mojays 2
1000 alfonx 144 drawRectangle(this.getGraphics());
1001     // make the dragged rectangle (in map coords) the new BBOX
1002     double left = Math.min(startX, endX);
1003     double right = Math.max(startX, endX);
1004     double bottom = Math.min(startY, endY);
1005     double top = Math.max(startY, endY);
1006     Coordinate ll = new Coordinate(left, bottom);
1007     Coordinate ur = new Coordinate(right, top);
1008     // xulu.sc
1009 mojays 2
1010 alfonx 144 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
1011     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
1012 mojays 2
1013 alfonx 144 // sk.sc
1014     // {
1015     // // SK tries to paint a preview of the zoom ;-9 aha.... well
1016     // Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
1017     // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
1018     // RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
1019     // graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1020     // RenderingHints.VALUE_ANTIALIAS_OFF);
1021     // graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
1022     // RenderingHints.VALUE_RENDER_SPEED);
1023     // graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
1024     // JMapPane.this.getHeight(), x1, y1, x2, y2, null);
1025     // }
1026     // xulu.ec
1027     } else if (state == JMapPane.ZoomOut) {
1028     drawRectangle(this.getGraphics());
1029 mojays 2
1030 alfonx 144 // make the dragged rectangle in screen coords the new map size?
1031     double left = Math.min(startX, endX);
1032     double right = Math.max(startX, endX);
1033     double bottom = Math.min(startY, endY);
1034     double top = Math.max(startY, endY);
1035     double nWidth = (mapWidth * mapWidth) / (right - left);
1036     double nHeight = (mapHeight * mapHeight) / (top - bottom);
1037     double deltaX1 = left - mapArea.getMinX();
1038     double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
1039     double deltaY1 = bottom - mapArea.getMinY();
1040     double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
1041     Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
1042     mapArea.getMinY() - nDeltaY1);
1043     double deltaX2 = mapArea.getMaxX() - right;
1044     double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
1045     double deltaY2 = mapArea.getMaxY() - top;
1046     double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
1047     Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
1048     mapArea.getMaxY() + nDeltaY2);
1049     // xulu.sc
1050     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
1051     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
1052 mojays 2
1053 alfonx 144 // xulu.ec
1054     } else if (state == JMapPane.Select && selectionLayer != null) {
1055     double left = Math.min(startX, endX);
1056     double right = Math.max(startX, endX);
1057     double bottom = Math.min(startY, endY);
1058     double top = Math.max(startY, endY);
1059 mojays 2
1060 alfonx 144 String name = selectionLayer.getFeatureSource().getSchema()
1061     .getDefaultGeometry().getLocalName();
1062 mojays 2
1063 alfonx 144 if (name == "") {
1064     name = "the_geom";
1065     }
1066     Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,
1067     getContext().getCoordinateReferenceSystem().toString());
1068     // System.out.println(bb.toString());
1069     if (selectionManager != null) {
1070     selectionManager.selectionChanged(this, bb);
1071     }
1072     /*
1073     * FeatureCollection fc; selection = null; try { fc =
1074     * selectionLayer.getFeatureSource().getFeatures(bb); selection =
1075     * fc; } catch (IOException e) { e.printStackTrace(); }
1076     */
1077     }
1078 mojays 2
1079 alfonx 144 // xulu.so
1080     // setMapArea(mapArea);
1081     // xulu.eo
1082     repaint();
1083     }
1084 mojays 2
1085 alfonx 144 private boolean isClickable() {
1086     return clickable;
1087     }
1088 mojays 2
1089 alfonx 144 private org.geotools.styling.Style setupStyle(int type, Color color) {
1090     StyleFactory sf = org.geotools.factory.CommonFactoryFinder
1091     .getStyleFactory(null);
1092     StyleBuilder sb = new StyleBuilder();
1093 mojays 2
1094 alfonx 144 org.geotools.styling.Style s = sf.createStyle();
1095     s.setTitle("selection");
1096 mojays 2
1097 alfonx 144 // TODO parameterise the color
1098     PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);
1099     ps.setStroke(sb.createStroke(color));
1100 mojays 2
1101 alfonx 144 LineSymbolizer ls = sb.createLineSymbolizer(color);
1102     Graphic h = sb.createGraphic();
1103     h.setMarks(new Mark[] { sb.createMark("square", color) });
1104 mojays 2
1105 alfonx 144 PointSymbolizer pts = sb.createPointSymbolizer(h);
1106 mojays 2
1107 alfonx 144 // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});
1108     switch (type) {
1109     case POLYGON:
1110     s = sb.createStyle(ps);
1111 mojays 2
1112 alfonx 144 break;
1113 mojays 2
1114 alfonx 144 case POINT:
1115     s = sb.createStyle(pts);
1116 mojays 2
1117 alfonx 144 break;
1118 mojays 2
1119 alfonx 144 case LINE:
1120     s = sb.createStyle(ls);
1121     }
1122 mojays 2
1123 alfonx 144 return s;
1124     }
1125 mojays 2
1126 alfonx 144 public void highlightChanged(HighlightChangedEvent e) {
1127     org.opengis.filter.Filter f = e.getFilter();
1128 mojays 2
1129 alfonx 144 try {
1130     highlightFeature = highlightLayer.getFeatureSource().getFeatures(f);
1131     } catch (IOException e1) {
1132     // TODO Auto-generated catch block
1133     e1.printStackTrace();
1134     }
1135 mojays 2
1136 alfonx 144 repaint();
1137     }
1138 mojays 2
1139 alfonx 144 public void propertyChange(PropertyChangeEvent evt) {
1140     String prop = evt.getPropertyName();
1141 mojays 2
1142 alfonx 144 if (prop.equalsIgnoreCase("crs")) {
1143     context.setAreaOfInterest(context.getAreaOfInterest(),
1144     (CoordinateReferenceSystem) evt.getNewValue());
1145     }
1146     }
1147 mojays 2
1148 alfonx 144 public boolean isReset() {
1149     return reset;
1150     }
1151 mojays 2
1152 alfonx 144 public void setReset(boolean reset) {
1153     this.reset = reset;
1154     }
1155 mojays 2
1156 alfonx 144 public void layerAdded(MapLayerListEvent event) {
1157     changed = true;
1158 mojays 2
1159 alfonx 144 if (context.getLayers().length == 1) { // the first one
1160 mojays 2
1161 alfonx 144 try {
1162     // xulu.sc
1163     // mapArea = context.getLayerBounds();
1164     mapArea = context.getAreaOfInterest();
1165     if (mapArea == null)
1166     mapArea = context.getLayerBounds();
1167     // xulu.ec
1168     } catch (IOException e) {
1169     // TODO Auto-generated catch block
1170     e.printStackTrace();
1171     }
1172 mojays 2
1173 alfonx 144 reset = true;
1174     }
1175 mojays 2
1176 alfonx 144 repaint();
1177     }
1178 mojays 2
1179 alfonx 144 public void layerRemoved(MapLayerListEvent event) {
1180     changed = true;
1181     repaint();
1182     }
1183 mojays 2
1184 alfonx 144 public void layerChanged(MapLayerListEvent event) {
1185     changed = true;
1186     // System.out.println("layer changed - repaint");
1187     repaint();
1188     }
1189 mojays 2
1190 alfonx 144 public void layerMoved(MapLayerListEvent event) {
1191     changed = true;
1192     repaint();
1193     }
1194 mojays 2
1195 alfonx 144 protected void drawRectangle(Graphics graphics) {
1196     // undraw last box/draw new box
1197     int left = Math.min(startX, lastX);
1198     int right = Math.max(startX, lastX);
1199     int top = Math.max(startY, lastY);
1200     int bottom = Math.min(startY, lastY);
1201     int width = right - left;
1202     int height = top - bottom;
1203     // System.out.println("drawing rect("+left+","+bottom+","+ width+","+
1204     // height+")");
1205     graphics.drawRect(left, bottom, width, height);
1206     }
1207 mojays 2
1208 alfonx 144 /**
1209     * if clickable is set to true then a single click on the map pane will zoom
1210     * or pan the map.
1211     *
1212     * @param clickable
1213     */
1214     public void setClickable(boolean clickable) {
1215     this.clickable = clickable;
1216     }
1217 mojays 2
1218 alfonx 144 public void mouseMoved(MouseEvent e) {
1219     }
1220    
1221     public FeatureCollection getSelection() {
1222     return selection;
1223     }
1224    
1225     public void setSelection(FeatureCollection selection) {
1226     this.selection = selection;
1227     repaint();
1228     }
1229    
1230     /*
1231     * (non-Javadoc)
1232     *
1233     * @see
1234     * org.geotools.gui.swing.event.SelectionChangeListener#selectionChanged
1235     * (org.geotools.gui.swing.event.SelectionChangedEvent)
1236     */
1237     public void selectionChanged(SelectionChangedEvent e) {
1238    
1239     try {
1240     selection = selectionLayer.getFeatureSource().getFeatures(
1241     e.getFilter());
1242     repaint();
1243     } catch (IOException e1) {
1244     e1.printStackTrace();
1245     }
1246     }
1247    
1248     public SelectionManager getSelectionManager() {
1249     return selectionManager;
1250     }
1251    
1252     public void setSelectionManager(SelectionManager selectionManager) {
1253     this.selectionManager = selectionManager;
1254     this.selectionManager.addSelectionChangeListener(this);
1255    
1256     }
1257    
1258     // xulu.sn
1259 alfonx 76 /**
1260     * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
1261     * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
1262     * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>
1263     * Since 21. April 09: Before thecalculation starts, the aspect ratio is
1264     * corrected. This change implies, that setMapArea() will most of the time
1265     * not allow setting to a wrong aspectRatio.
1266     *
1267     * @author <a href="mailto:[email protected]">Stefan Alfons
1268     * Kr&uuml;ger</a>
1269     */
1270     public Envelope bestAllowedMapArea(Envelope env) {
1271     if (getWidth() == 0)
1272     return env;
1273     if (env == null)
1274     return env;
1275 mojays 2
1276 alfonx 76 /**
1277 alfonx 144 * Correct the aspect Ratio before we check the rest. Otherwise we might
1278     * easily fail.
1279 alfonx 76 */
1280     env = fixAspectRatio(this.getBounds(), env);
1281 mojays 2
1282 alfonx 76 double scale = env.getWidth() / getWidth();
1283     double centerX = env.getMinX() + env.getWidth() / 2.;
1284     double centerY = env.getMinY() + env.getHeight() / 2.;
1285     double newWidth2;
1286     double newHeight2;
1287     if (scale < getMaxZoomScale()) {
1288     // ****************************************************************************
1289     // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
1290     // ****************************************************************************
1291     newWidth2 = getMaxZoomScale() * getWidth() / 2.;
1292     newHeight2 = getMaxZoomScale() * getHeight() / 2.;
1293     } else if (scale > getMinZoomScale()) {
1294     // ****************************************************************************
1295     // Wir zoomen weiter raus als erlaubt => Anpassen des envelope
1296     // ****************************************************************************
1297     newWidth2 = getMinZoomScale() * getWidth() / 2.;
1298     newHeight2 = getMinZoomScale() * getHeight() / 2.;
1299     } else {
1300     // ****************************************************************************
1301     // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
1302     // ****************************************************************************
1303     return env;
1304     }
1305 mojays 2
1306 alfonx 76 Coordinate ll = new Coordinate(centerX - newWidth2, centerY
1307     - newHeight2);
1308     Coordinate ur = new Coordinate(centerX + newWidth2, centerY
1309     + newHeight2);
1310 mojays 2
1311 alfonx 76 return new Envelope(ll, ur);
1312     }
1313    
1314 alfonx 144 /**
1315     * Retuns the minimum allowed zoom scale. This is the bigger number value of
1316     * the two. Defaults to {@link Double}.MAX_VALUE
1317     *
1318     * @author <a href="mailto:[email protected]">Stefan Alfons
1319     * Kr&uuml;ger</a>
1320     */
1321     public Double getMinZoomScale() {
1322     return minZoomScale;
1323     }
1324 mojays 2
1325 alfonx 144 /**
1326     * Retuns the maximum allowed zoom scale. This is the smaller number value
1327     * of the two. Defaults to {@link Double}.MIN_VALUE
1328     *
1329     * @author <a href="mailto:[email protected]">Stefan Alfons
1330     * Kr&uuml;ger</a>
1331     */
1332     public Double getMaxZoomScale() {
1333     return maxZoomScale;
1334     }
1335 mojays 2
1336 alfonx 144 /**
1337     * Set the maximum allowed zoom scale. This is the smaller number value of
1338     * the two.
1339     *
1340     * @author <a href="mailto:[email protected]">Stefan Alfons
1341     * Kr&uuml;ger</a>
1342     */
1343     public void setMaxZoomScale(Double maxZoomScale) {
1344     // System.out.println("setting max scale to "+maxZoomScale);
1345     this.maxZoomScale = maxZoomScale;
1346     }
1347 mojays 2
1348 alfonx 144 /**
1349     * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1350     * value of the two.
1351     *
1352     * @author <a href="mailto:[email protected]">Stefan Alfons
1353     * Kr&uuml;ger</a>
1354     */
1355     public void setMinZoomScale(Double minZoomScale) {
1356     this.minZoomScale = minZoomScale;
1357     }
1358     // xulu.en
1359 mojays 2
1360     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26