/[schmitzm]/branches/2.0-RC1/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Annotation of /branches/2.0-RC1/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (hide annotations)
Thu Jul 9 20:14:21 2009 UTC (15 years, 7 months ago) by alfonx
Original Path: trunk/src/org/geotools/gui/swing/JMapPane.java
File size: 38296 byte(s)
* AS FEATURE: Changing the TextSymbolizer (label settings) in AS is now directly presented in the preview JMapPane(s). Up to now, the LabelCache of the JMapPane didn't clear properly and the changes were only shown after a while.

* CodeCleanup: Removed some unneeded SwingUtil.getParentWindow (because most atlas classes now use a Component object as GUI-parent parameter)

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26