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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26