/[schmitzm]/trunk/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (hide annotations)
Sat Jun 20 12:48:24 2009 UTC (15 years, 8 months ago) by alfonx
Original Path: trunk/src/org/geotools/gui/swing/JMapPane.java
File size: 38257 byte(s)
* Organized Imports
* Fixed a BuG in AS, PolygonSymbolEditGUI (Had to be Float instead of Double)
* Renamed DesignLayerPanel to DesginMapLegend etc...
* Bugfixed ManageLayerSytlesForMapDialog to trigger a preview of the new legend.
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     this.highlightRenderer = new StreamingRenderer();
348     this.selectionRenderer = new StreamingRenderer();
349 mojays 2
350 alfonx 144 hints.put("memoryPreloadingEnabled", Boolean.FALSE);
351     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 144 if (changed) { /* if the map changed then redraw */
534     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 144 // TODO , nur machen, wenn panning beginnt
547     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     // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
737     // h:"+mapArea.getHeight());
738     Rectangle bounds = this.getBounds();
739     double x = (double) (e.getX());
740     double y = (double) (e.getY());
741     double width = mapArea.getWidth();
742     double height = mapArea.getHeight();
743     // xulu.sc
744     // double width2 = mapArea.getWidth() / 2.0;
745     // double height2 = mapArea.getHeight() / 2.0;
746     double width2 = width / 2.0;
747     double height2 = height / 2.0;
748     // xulu.ec
749     double mapX = ((x * width) / (double) bounds.width) + mapArea.getMinX();
750     double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
751     + mapArea.getMinY();
752 mojays 2
753 alfonx 144 /*
754     * System.out.println(""+x+"->"+mapX);
755     * System.out.println(""+y+"->"+mapY);
756     */
757 mojays 2
758 alfonx 144 /*
759     * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());
760     * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());
761     */
762     double zlevel = 1.0;
763 mojays 2
764 alfonx 144 switch (state) {
765     case Pan:
766     zlevel = 1.0;
767     // xulu.sc SK: return here.. a mouselistener is managing the PANNING
768     // break;
769     return;
770     // xulu.ec
771     case ZoomIn:
772     zlevel = zoomFactor;
773 mojays 2
774 alfonx 144 break;
775 mojays 2
776 alfonx 144 case ZoomOut:
777     zlevel = 1.0 / zoomFactor;
778 mojays 2
779 alfonx 144 break;
780 mojays 2
781 alfonx 144 case Select:
782     doSelection(mapX, mapY, selectionLayer);
783 mojays 2
784 alfonx 144 return;
785 mojays 2
786 alfonx 144 default:
787     return;
788     }
789 mojays 2
790 alfonx 144 Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
791     - (height2 / zlevel));
792     Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
793     + (height2 / zlevel));
794     // xulu.sc SK: Check for min/max scale
795     // mapArea = new Envelope(ll, ur);
796     final Envelope newMapArea = new Envelope(ll, ur);
797     setMapArea(bestAllowedMapArea(newMapArea));
798     // xulu.ec
799 mojays 2
800 alfonx 144 // sk.ec
801 mojays 2
802 alfonx 144 // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"
803     // h:"+mapArea.getHeight());
804     repaint();
805     }
806 mojays 2
807 alfonx 144 public void mouseEntered(MouseEvent e) {
808     }
809 mojays 2
810 alfonx 144 public void mouseExited(MouseEvent e) {
811     }
812 mojays 2
813 alfonx 144 public void mousePressed(MouseEvent e) {
814     startX = e.getX();
815     startY = e.getY();
816     lastX = 0;
817     lastY = 0;
818     }
819 mojays 2
820 alfonx 144 public void mouseReleased(MouseEvent e) {
821     int endX = e.getX();
822     int endY = e.getY();
823 mojays 2
824 alfonx 144 processDrag(startX, startY, endX, endY, e);
825     lastX = 0;
826     lastY = 0;
827 mojays 2
828 alfonx 144 /**
829     * Es wird nicht (mehr) gepannt!
830     */
831     panning_started = false;
832     }
833 mojays 2
834 alfonx 144 public void mouseDragged(MouseEvent e) {
835     Graphics graphics = this.getGraphics();
836     int x = e.getX();
837     int y = e.getY();
838 mojays 2
839 alfonx 144 if ((state == JMapPane.Pan)
840     || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
841     /**
842     * SK: Der Cursor wird auf PANNING gesetzt.
843     */
844     if (panning_started == false) {
845     panning_started = true;
846     setCursor(SwingUtil.PANNING_CURSOR);
847     }
848 mojays 2
849 alfonx 144 // move the image with the mouse
850     if ((lastX > 0) && (lastY > 0)) {
851     int dx = lastX - startX;
852     int dy = lastY - startY;
853     // System.out.println("translate "+dx+","+dy);
854     final Graphics2D g2 = panningImage.createGraphics();
855     g2.setBackground(new Color(240, 240, 240)); // TODO richtige
856     // farbe? am besten
857     // vom L&F die
858     // hintergrundfarbe
859     // auslesen...
860 alfonx 148
861 mojays 2 g2.clearRect(0, 0, this.getWidth(), this.getHeight());
862     g2.drawImage(baseImage, dx, dy, this);
863 alfonx 144 graphics.drawImage(panningImage, 0, 0, this);
864     }
865 mojays 2
866 alfonx 144 lastX = x;
867     lastY = y;
868     } else
869 mojays 2
870 alfonx 144 if ((state == JMapPane.ZoomIn) || (state == JMapPane.ZoomOut)) {
871 mojays 2
872 alfonx 144 graphics.setXORMode(Color.WHITE);
873 mojays 2
874 alfonx 144 if ((lastX > 0) && (lastY > 0)) {
875     drawRectangle(graphics);
876     }
877 mojays 2
878 alfonx 144 // draw new box
879     lastX = x;
880     lastY = y;
881     drawRectangle(graphics);
882     } else if (state == JMapPane.Select && selectionLayer != null) {
883 mojays 2
884 alfonx 144 // construct a new bbox filter
885     Rectangle bounds = this.getBounds();
886 mojays 2
887 alfonx 144 double mapWidth = mapArea.getWidth();
888     double mapHeight = mapArea.getHeight();
889 mojays 2
890 alfonx 144 double x1 = ((this.startX * mapWidth) / (double) bounds.width)
891     + mapArea.getMinX();
892     double y1 = (((bounds.getHeight() - this.startY) * mapHeight) / (double) bounds.height)
893     + mapArea.getMinY();
894     double x2 = ((x * mapWidth) / (double) bounds.width)
895     + mapArea.getMinX();
896     double y2 = (((bounds.getHeight() - y) * mapHeight) / (double) bounds.height)
897     + mapArea.getMinY();
898     double left = Math.min(x1, x2);
899     double right = Math.max(x1, x2);
900     double bottom = Math.min(y1, y2);
901     double top = Math.max(y1, y2);
902 mojays 2
903 alfonx 144 String name = selectionLayer.getFeatureSource().getSchema()
904     .getDefaultGeometry().getName();
905 mojays 2
906 alfonx 144 if (name == "") {
907     name = "the_geom";
908     }
909     Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,
910     getContext().getCoordinateReferenceSystem().toString());
911     if (selectionManager != null) {
912     selectionManager.selectionChanged(this, bb);
913     }
914 mojays 2
915 alfonx 144 graphics.setXORMode(Color.green);
916 mojays 2
917 alfonx 144 /*
918     * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
919     */
920 mojays 2
921 alfonx 144 // draw new box
922     lastX = x;
923     lastY = y;
924     drawRectangle(graphics);
925     }
926 mojays 2
927 alfonx 144 }
928 mojays 2
929 alfonx 144 // sk.cs
930     // private void processDrag(int x1, int y1, int x2, int y2) {
931     // sk.ce
932     protected void processDrag(final int x1, final int y1, final int x2,
933     final int y2, MouseEvent e) {
934 mojays 2
935 alfonx 144 /****
936     * If no layer is availabe we dont want a NullPointerException
937     */
938     if (mapArea == null)
939     return;
940 mojays 2
941 alfonx 144 // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
942     // + x2 + "," + y2);
943     if ((x1 == x2) && (y1 == y2)) {
944     if (isClickable()) {
945     mouseClicked(new MouseEvent(this, 0, new Date().getTime(), 0,
946     x1, y1, y2, false));
947     }
948 mojays 2
949 alfonx 144 return;
950     }
951 mojays 2
952 alfonx 144 Rectangle bounds = this.getBounds();
953 mojays 2
954 alfonx 144 double mapWidth = mapArea.getWidth();
955     double mapHeight = mapArea.getHeight();
956 mojays 2
957 alfonx 144 double startX = ((x1 * mapWidth) / (double) bounds.width)
958     + mapArea.getMinX();
959     double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)
960     + mapArea.getMinY();
961     double endX = ((x2 * mapWidth) / (double) bounds.width)
962     + mapArea.getMinX();
963     double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)
964     + mapArea.getMinY();
965 mojays 2
966 alfonx 144 if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {
967     // move the image with the mouse
968     // calculate X offsets from start point to the end Point
969     double deltaX1 = endX - startX;
970 mojays 2
971 alfonx 144 // System.out.println("deltaX " + deltaX1);
972     // new edges
973     double left = mapArea.getMinX() - deltaX1;
974     double right = mapArea.getMaxX() - deltaX1;
975 mojays 2
976 alfonx 144 // now for Y
977     double deltaY1 = endY - startY;
978 mojays 2
979 alfonx 144 // System.out.println("deltaY " + deltaY1);
980     double bottom = mapArea.getMinY() - deltaY1;
981     double top = mapArea.getMaxY() - deltaY1;
982     Coordinate ll = new Coordinate(left, bottom);
983     Coordinate ur = new Coordinate(right, top);
984     // xulu.sc
985     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
986     setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur)));
987     // xulu.ec
988     } else if (state == JMapPane.ZoomIn) {
989 mojays 2
990 alfonx 144 // Zu kleine Flächen sollen nicht gezoomt werden.
991     // sk.bc
992     if ((Math.abs(x1 - x2) * Math.abs(y2 - y1)) < 150)
993     return;
994     // sk.ec
995 mojays 2
996 alfonx 144 drawRectangle(this.getGraphics());
997     // make the dragged rectangle (in map coords) the new BBOX
998     double left = Math.min(startX, endX);
999     double right = Math.max(startX, endX);
1000     double bottom = Math.min(startY, endY);
1001     double top = Math.max(startY, endY);
1002     Coordinate ll = new Coordinate(left, bottom);
1003     Coordinate ur = new Coordinate(right, top);
1004     // xulu.sc
1005 mojays 2
1006 alfonx 144 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
1007     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
1008 mojays 2
1009 alfonx 144 // sk.sc
1010     // {
1011     // // SK tries to paint a preview of the zoom ;-9 aha.... well
1012     // Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
1013     // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
1014     // RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
1015     // graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1016     // RenderingHints.VALUE_ANTIALIAS_OFF);
1017     // graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
1018     // RenderingHints.VALUE_RENDER_SPEED);
1019     // graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
1020     // JMapPane.this.getHeight(), x1, y1, x2, y2, null);
1021     // }
1022     // xulu.ec
1023     } else if (state == JMapPane.ZoomOut) {
1024     drawRectangle(this.getGraphics());
1025 mojays 2
1026 alfonx 144 // make the dragged rectangle in screen coords the new map size?
1027     double left = Math.min(startX, endX);
1028     double right = Math.max(startX, endX);
1029     double bottom = Math.min(startY, endY);
1030     double top = Math.max(startY, endY);
1031     double nWidth = (mapWidth * mapWidth) / (right - left);
1032     double nHeight = (mapHeight * mapHeight) / (top - bottom);
1033     double deltaX1 = left - mapArea.getMinX();
1034     double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
1035     double deltaY1 = bottom - mapArea.getMinY();
1036     double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
1037     Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
1038     mapArea.getMinY() - nDeltaY1);
1039     double deltaX2 = mapArea.getMaxX() - right;
1040     double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
1041     double deltaY2 = mapArea.getMaxY() - top;
1042     double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
1043     Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
1044     mapArea.getMaxY() + nDeltaY2);
1045     // xulu.sc
1046     // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
1047     setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
1048 mojays 2
1049 alfonx 144 // xulu.ec
1050     } else if (state == JMapPane.Select && selectionLayer != null) {
1051     double left = Math.min(startX, endX);
1052     double right = Math.max(startX, endX);
1053     double bottom = Math.min(startY, endY);
1054     double top = Math.max(startY, endY);
1055 mojays 2
1056 alfonx 144 String name = selectionLayer.getFeatureSource().getSchema()
1057     .getDefaultGeometry().getLocalName();
1058 mojays 2
1059 alfonx 144 if (name == "") {
1060     name = "the_geom";
1061     }
1062     Filter bb = ff.bbox(ff.property(name), left, bottom, right, top,
1063     getContext().getCoordinateReferenceSystem().toString());
1064     // System.out.println(bb.toString());
1065     if (selectionManager != null) {
1066     selectionManager.selectionChanged(this, bb);
1067     }
1068     /*
1069     * FeatureCollection fc; selection = null; try { fc =
1070     * selectionLayer.getFeatureSource().getFeatures(bb); selection =
1071     * fc; } catch (IOException e) { e.printStackTrace(); }
1072     */
1073     }
1074 mojays 2
1075 alfonx 144 // xulu.so
1076     // setMapArea(mapArea);
1077     // xulu.eo
1078     repaint();
1079     }
1080 mojays 2
1081 alfonx 144 private boolean isClickable() {
1082     return clickable;
1083     }
1084 mojays 2
1085 alfonx 144 private org.geotools.styling.Style setupStyle(int type, Color color) {
1086     StyleFactory sf = org.geotools.factory.CommonFactoryFinder
1087     .getStyleFactory(null);
1088     StyleBuilder sb = new StyleBuilder();
1089 mojays 2
1090 alfonx 144 org.geotools.styling.Style s = sf.createStyle();
1091     s.setTitle("selection");
1092 mojays 2
1093 alfonx 144 // TODO parameterise the color
1094     PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);
1095     ps.setStroke(sb.createStroke(color));
1096 mojays 2
1097 alfonx 144 LineSymbolizer ls = sb.createLineSymbolizer(color);
1098     Graphic h = sb.createGraphic();
1099     h.setMarks(new Mark[] { sb.createMark("square", color) });
1100 mojays 2
1101 alfonx 144 PointSymbolizer pts = sb.createPointSymbolizer(h);
1102 mojays 2
1103 alfonx 144 // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});
1104     switch (type) {
1105     case POLYGON:
1106     s = sb.createStyle(ps);
1107 mojays 2
1108 alfonx 144 break;
1109 mojays 2
1110 alfonx 144 case POINT:
1111     s = sb.createStyle(pts);
1112 mojays 2
1113 alfonx 144 break;
1114 mojays 2
1115 alfonx 144 case LINE:
1116     s = sb.createStyle(ls);
1117     }
1118 mojays 2
1119 alfonx 144 return s;
1120     }
1121 mojays 2
1122 alfonx 144 public void highlightChanged(HighlightChangedEvent e) {
1123     org.opengis.filter.Filter f = e.getFilter();
1124 mojays 2
1125 alfonx 144 try {
1126     highlightFeature = highlightLayer.getFeatureSource().getFeatures(f);
1127     } catch (IOException e1) {
1128     // TODO Auto-generated catch block
1129     e1.printStackTrace();
1130     }
1131 mojays 2
1132 alfonx 144 repaint();
1133     }
1134 mojays 2
1135 alfonx 144 public void propertyChange(PropertyChangeEvent evt) {
1136     String prop = evt.getPropertyName();
1137 mojays 2
1138 alfonx 144 if (prop.equalsIgnoreCase("crs")) {
1139     context.setAreaOfInterest(context.getAreaOfInterest(),
1140     (CoordinateReferenceSystem) evt.getNewValue());
1141     }
1142     }
1143 mojays 2
1144 alfonx 144 public boolean isReset() {
1145     return reset;
1146     }
1147 mojays 2
1148 alfonx 144 public void setReset(boolean reset) {
1149     this.reset = reset;
1150     }
1151 mojays 2
1152 alfonx 144 public void layerAdded(MapLayerListEvent event) {
1153     changed = true;
1154 mojays 2
1155 alfonx 144 if (context.getLayers().length == 1) { // the first one
1156 mojays 2
1157 alfonx 144 try {
1158     // xulu.sc
1159     // mapArea = context.getLayerBounds();
1160     mapArea = context.getAreaOfInterest();
1161     if (mapArea == null)
1162     mapArea = context.getLayerBounds();
1163     // xulu.ec
1164     } catch (IOException e) {
1165     // TODO Auto-generated catch block
1166     e.printStackTrace();
1167     }
1168 mojays 2
1169 alfonx 144 reset = true;
1170     }
1171 mojays 2
1172 alfonx 144 repaint();
1173     }
1174 mojays 2
1175 alfonx 144 public void layerRemoved(MapLayerListEvent event) {
1176     changed = true;
1177     repaint();
1178     }
1179 mojays 2
1180 alfonx 144 public void layerChanged(MapLayerListEvent event) {
1181     changed = true;
1182     // System.out.println("layer changed - repaint");
1183     repaint();
1184     }
1185 mojays 2
1186 alfonx 144 public void layerMoved(MapLayerListEvent event) {
1187     changed = true;
1188     repaint();
1189     }
1190 mojays 2
1191 alfonx 144 protected void drawRectangle(Graphics graphics) {
1192     // undraw last box/draw new box
1193     int left = Math.min(startX, lastX);
1194     int right = Math.max(startX, lastX);
1195     int top = Math.max(startY, lastY);
1196     int bottom = Math.min(startY, lastY);
1197     int width = right - left;
1198     int height = top - bottom;
1199     // System.out.println("drawing rect("+left+","+bottom+","+ width+","+
1200     // height+")");
1201     graphics.drawRect(left, bottom, width, height);
1202     }
1203 mojays 2
1204 alfonx 144 /**
1205     * if clickable is set to true then a single click on the map pane will zoom
1206     * or pan the map.
1207     *
1208     * @param clickable
1209     */
1210     public void setClickable(boolean clickable) {
1211     this.clickable = clickable;
1212     }
1213 mojays 2
1214 alfonx 144 public void mouseMoved(MouseEvent e) {
1215     }
1216    
1217     public FeatureCollection getSelection() {
1218     return selection;
1219     }
1220    
1221     public void setSelection(FeatureCollection selection) {
1222     this.selection = selection;
1223     repaint();
1224     }
1225    
1226     /*
1227     * (non-Javadoc)
1228     *
1229     * @see
1230     * org.geotools.gui.swing.event.SelectionChangeListener#selectionChanged
1231     * (org.geotools.gui.swing.event.SelectionChangedEvent)
1232     */
1233     public void selectionChanged(SelectionChangedEvent e) {
1234    
1235     try {
1236     selection = selectionLayer.getFeatureSource().getFeatures(
1237     e.getFilter());
1238     repaint();
1239     } catch (IOException e1) {
1240     e1.printStackTrace();
1241     }
1242     }
1243    
1244     public SelectionManager getSelectionManager() {
1245     return selectionManager;
1246     }
1247    
1248     public void setSelectionManager(SelectionManager selectionManager) {
1249     this.selectionManager = selectionManager;
1250     this.selectionManager.addSelectionChangeListener(this);
1251    
1252     }
1253    
1254     // xulu.sn
1255 alfonx 76 /**
1256     * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
1257     * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
1258     * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>
1259     * Since 21. April 09: Before thecalculation starts, the aspect ratio is
1260     * corrected. This change implies, that setMapArea() will most of the time
1261     * not allow setting to a wrong aspectRatio.
1262     *
1263     * @author <a href="mailto:[email protected]">Stefan Alfons
1264     * Kr&uuml;ger</a>
1265     */
1266     public Envelope bestAllowedMapArea(Envelope env) {
1267     if (getWidth() == 0)
1268     return env;
1269     if (env == null)
1270     return env;
1271 mojays 2
1272 alfonx 76 /**
1273 alfonx 144 * Correct the aspect Ratio before we check the rest. Otherwise we might
1274     * easily fail.
1275 alfonx 76 */
1276     env = fixAspectRatio(this.getBounds(), env);
1277 mojays 2
1278 alfonx 76 double scale = env.getWidth() / getWidth();
1279     double centerX = env.getMinX() + env.getWidth() / 2.;
1280     double centerY = env.getMinY() + env.getHeight() / 2.;
1281     double newWidth2;
1282     double newHeight2;
1283     if (scale < getMaxZoomScale()) {
1284     // ****************************************************************************
1285     // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
1286     // ****************************************************************************
1287     newWidth2 = getMaxZoomScale() * getWidth() / 2.;
1288     newHeight2 = getMaxZoomScale() * getHeight() / 2.;
1289     } else if (scale > getMinZoomScale()) {
1290     // ****************************************************************************
1291     // Wir zoomen weiter raus als erlaubt => Anpassen des envelope
1292     // ****************************************************************************
1293     newWidth2 = getMinZoomScale() * getWidth() / 2.;
1294     newHeight2 = getMinZoomScale() * getHeight() / 2.;
1295     } else {
1296     // ****************************************************************************
1297     // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
1298     // ****************************************************************************
1299     return env;
1300     }
1301 mojays 2
1302 alfonx 76 Coordinate ll = new Coordinate(centerX - newWidth2, centerY
1303     - newHeight2);
1304     Coordinate ur = new Coordinate(centerX + newWidth2, centerY
1305     + newHeight2);
1306 mojays 2
1307 alfonx 76 return new Envelope(ll, ur);
1308     }
1309    
1310 alfonx 144 /**
1311     * Retuns the minimum allowed zoom scale. This is the bigger number value of
1312     * the two. Defaults to {@link Double}.MAX_VALUE
1313     *
1314     * @author <a href="mailto:[email protected]">Stefan Alfons
1315     * Kr&uuml;ger</a>
1316     */
1317     public Double getMinZoomScale() {
1318     return minZoomScale;
1319     }
1320 mojays 2
1321 alfonx 144 /**
1322     * Retuns the maximum allowed zoom scale. This is the smaller number value
1323     * of the two. Defaults to {@link Double}.MIN_VALUE
1324     *
1325     * @author <a href="mailto:[email protected]">Stefan Alfons
1326     * Kr&uuml;ger</a>
1327     */
1328     public Double getMaxZoomScale() {
1329     return maxZoomScale;
1330     }
1331 mojays 2
1332 alfonx 144 /**
1333     * Set the maximum allowed zoom scale. This is the smaller number value of
1334     * the two.
1335     *
1336     * @author <a href="mailto:[email protected]">Stefan Alfons
1337     * Kr&uuml;ger</a>
1338     */
1339     public void setMaxZoomScale(Double maxZoomScale) {
1340     // System.out.println("setting max scale to "+maxZoomScale);
1341     this.maxZoomScale = maxZoomScale;
1342     }
1343 mojays 2
1344 alfonx 144 /**
1345     * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1346     * value of the two.
1347     *
1348     * @author <a href="mailto:[email protected]">Stefan Alfons
1349     * Kr&uuml;ger</a>
1350     */
1351     public void setMinZoomScale(Double minZoomScale) {
1352     this.minZoomScale = minZoomScale;
1353     }
1354     // xulu.en
1355 mojays 2
1356     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26