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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26