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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26