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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26