/[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 420 - (show annotations)
Thu Oct 1 20:22:48 2009 UTC (15 years, 5 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
File size: 34267 byte(s)
* Lots of changes in this big commit for GP 1.3 
* New Interfaces: Checkable, Copyable, Cancellable, CancellableDialogAdapter to improve the GUI
* New DialogManager to unify the handling of all dialogs.
* GP-Feature: The dialog for editing/translating a DpEntry has been "enriched".
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.geotools.renderer.shape.ShapefileRenderer;
69 import org.opengis.filter.FilterFactory2;
70 import org.opengis.referencing.crs.CoordinateReferenceSystem;
71
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.getName());
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
234 if (renderer instanceof StreamingRenderer
235 || renderer instanceof ShapefileRenderer) {
236 hints = renderer.getRendererHints();
237 if (hints == null) {
238 hints = new HashMap<Object, Object>();
239 }
240 if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {
241 labelCache = (LabelCache) hints
242 .get(StreamingRenderer.LABEL_CACHE_KEY);
243 } else {
244 hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
245 }
246
247 hints.put("memoryPreloadingEnabled", Boolean.TRUE);
248
249 renderer.setRendererHints(hints);
250 }
251
252 // this.highlightRenderer = new StreamingRenderer();
253 // this.selectionRenderer = new StreamingRenderer();
254
255 // highlightRenderer.setRendererHints(hints);
256 // selectionRenderer.setRendererHints(hints);
257
258 // renderer.setRendererHints(hints);
259
260 if (this.context != null) {
261 this.renderer.setContext(this.context);
262 }
263 }
264
265 public MapContext getContext() {
266 return context;
267 }
268
269 public void setContext(final MapContext context) {
270 if (this.context != null) {
271 this.context.removeMapLayerListListener(this);
272 }
273
274 this.context = context;
275
276 if (context != null) {
277 this.context.addMapLayerListListener(this);
278 }
279
280 if (renderer != null) {
281 renderer.setContext(this.context);
282 }
283 }
284
285 public Envelope getMapArea() {
286 return mapArea;
287 }
288
289 public void setMapArea(final Envelope mapArea) {
290 this.mapArea = mapArea;
291 }
292
293 public int getState() {
294 return state;
295 }
296
297 public void setState(final int state) {
298 this.state = state;
299
300 // System.out.println("State: " + state);
301 }
302
303 public double getZoomFactor() {
304 return zoomFactor;
305 }
306
307 public void setZoomFactor(final double zoomFactor) {
308 this.zoomFactor = zoomFactor;
309 }
310
311 protected void paintComponent(final Graphics g) {
312 super.paintComponent(g);
313
314 if ((renderer == null) || (mapArea == null)) {
315 return;
316 }
317
318 final Rectangle r = getBounds();
319 final Rectangle dr = new Rectangle(r.width, r.height);
320
321 if (!r.equals(oldRect) || reset) {
322 if (!r.equals(oldRect) && (mapArea == null)) {
323 try {
324 mapArea = context.getLayerBounds();
325 } catch (final IOException e) {
326 LOGGER.warn("context.getLayerBounds()", e);
327 }
328 }
329
330 if (mapArea != null) {
331 /* either the viewer size has changed or we've done a reset */
332 changed = true; /* note we need to redraw */
333 reset = false; /* forget about the reset */
334 oldRect = r; /* store what the current size is */
335
336 mapArea = fixAspectRatio(r, mapArea, false);
337 }
338 }
339
340 if (!mapArea.equals(oldMapArea)) { /* did the map extent change? */
341 changed = true;
342 oldMapArea = mapArea;
343 // when we tell the context that the bounds have changed WMSLayers
344 // can refresh them selves
345 context.setAreaOfInterest(mapArea, context
346 .getCoordinateReferenceSystem());
347 }
348
349 if (changed) { /* if the map changed then redraw */
350 changed = false;
351 baseImage = new BufferedImage(dr.width, dr.height,
352 BufferedImage.TYPE_INT_ARGB);
353
354 final Graphics2D ig = baseImage.createGraphics();
355 /* System.out.println("rendering"); */
356 if (renderer.getContext() != null)
357 renderer.setContext(context);
358 labelCache.clear(); // work around anoying labelcache bug
359
360 // draw the map
361 renderer.paint((Graphics2D) ig, dr, mapArea);
362
363 // TODO nur machen, wenn panning beginnt
364 panningImage = new BufferedImage(dr.width, dr.height,
365 BufferedImage.TYPE_INT_RGB);
366
367 }
368
369 ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
370 }
371
372 /**
373 * Returns an {@link Envelope} that has the same aspect ratio as the given rectangle
374 * @param grow
375 * If <code>true</code>, than the area will be enlarged to match
376 * the aspect ratio. If <code>false</code>, it will only shrink.
377 */
378 private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea,
379 boolean grow) {
380
381 final double mapWidth = mapArea.getWidth(); /* get the extent of the map */
382 final double mapHeight = mapArea.getHeight();
383 final double scaleX = r.getWidth() / mapArea.getWidth(); /*
384 * calculate the
385 * new scale
386 */
387
388 final double scaleY = r.getHeight() / mapArea.getHeight();
389 double scale = 1.0; // stupid compiler!
390
391 if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) {
392 scale = scaleX;
393 } else {
394 scale = scaleY;
395 }
396
397 /* calculate the difference in width and height of the new extent */
398 final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);
399 final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);
400
401 /*
402 * System.out.println("delta x " + deltaX);
403 * System.out.println("delta y " + deltaY);
404 */
405
406 /* create the new extent */
407 final Coordinate ll = new Coordinate(
408 mapArea.getMinX() - (deltaX / 2.0), mapArea.getMinY()
409 - (deltaY / 2.0));
410 final Coordinate ur = new Coordinate(
411 mapArea.getMaxX() + (deltaX / 2.0), mapArea.getMaxY()
412 + (deltaY / 2.0));
413
414 return new Envelope(ll, ur);
415 }
416
417 public void mouseClicked(final MouseEvent e) {
418 if (mapArea == null)
419 return;
420 // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
421 // h:"+mapArea.getHeight());
422 final Rectangle bounds = this.getBounds();
423 final double x = (double) (e.getX());
424 final double y = (double) (e.getY());
425 final double width = mapArea.getWidth();
426 final double height = mapArea.getHeight();
427 // xulu.sc
428 // double width2 = mapArea.getWidth() / 2.0;
429 // double height2 = mapArea.getHeight() / 2.0;
430 final double width2 = width / 2.0;
431 final double height2 = height / 2.0;
432 // xulu.ec
433 final double mapX = ((x * width) / (double) bounds.width)
434 + mapArea.getMinX();
435 final double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
436 + mapArea.getMinY();
437
438 /*
439 * System.out.println(""+x+"->"+mapX);
440 * System.out.println(""+y+"->"+mapY);
441 */
442
443 /*
444 * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());
445 * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());
446 */
447 double zlevel = 1.0;
448
449 switch (state) {
450 case Pan:
451 zlevel = 1.0;
452 // xulu.sc SK: return here.. a mouselistener is managing the PANNING
453 // break;
454 return;
455 // xulu.ec
456 case ZoomIn:
457 zlevel = zoomFactor;
458
459 break;
460
461 case ZoomOut:
462 zlevel = 1.0 / zoomFactor;
463
464 break;
465 //
466 // case Select:
467 // doSelection(mapX, mapY, selectionLayer);
468 //
469 // return;
470
471 default:
472 return;
473 }
474
475 final Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
476 - (height2 / zlevel));
477 final Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
478 + (height2 / zlevel));
479 // xulu.sc SK: Check for min/max scale
480 // mapArea = new Envelope(ll, ur);
481 final Envelope newMapArea = new Envelope(ll, ur);
482 setMapArea(bestAllowedMapArea(newMapArea));
483 // xulu.ec
484
485 // sk.ec
486
487 // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"
488 // h:"+mapArea.getHeight());
489 repaint();
490 }
491
492 public void mouseEntered(final MouseEvent e) {
493 }
494
495 public void mouseExited(final MouseEvent e) {
496 }
497
498 public void mousePressed(final MouseEvent e) {
499 startX = e.getX();
500 startY = e.getY();
501 lastX = 0;
502 lastY = 0;
503 }
504
505 public void mouseReleased(final MouseEvent e) {
506 final int endX = e.getX();
507 final int endY = e.getY();
508
509 processDrag(startX, startY, endX, endY, e);
510 lastX = 0;
511 lastY = 0;
512
513 /**
514 * Es wird nicht (mehr) gepannt!
515 */
516 panning_started = false;
517 }
518
519 public void mouseDragged(final MouseEvent e) {
520 final Graphics graphics = this.getGraphics();
521 final int x = e.getX();
522 final int y = e.getY();
523
524 if ((state == JMapPane.Pan)
525 || ((e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
526 /**
527 * SK: Der Cursor wird auf PANNING gesetzt.
528 */
529 if (panning_started == false) {
530 panning_started = true;
531 setCursor(SwingUtil.PANNING_CURSOR);
532 }
533
534 // move the image with the mouse
535 if ((lastX > 0) && (lastY > 0)) {
536 final int dx = lastX - startX;
537 final int dy = lastY - startY;
538 // System.out.println("translate "+dx+","+dy);
539 final Graphics2D g2 = panningImage.createGraphics();
540 g2.setBackground(new Color(240, 240, 240)); // TODO richtige
541 // farbe? am besten
542 // vom L&F die
543 // hintergrundfarbe
544 // auslesen...
545
546 g2.clearRect(0, 0, this.getWidth(), this.getHeight());
547 g2.drawImage(baseImage, dx, dy, this);
548 graphics.drawImage(panningImage, 0, 0, this);
549 }
550
551 lastX = x;
552 lastY = y;
553 } else
554
555 if ((state == JMapPane.ZoomIn) || (state == JMapPane.ZoomOut)) {
556
557 graphics.setXORMode(Color.WHITE);
558
559 if ((lastX > 0) && (lastY > 0)) {
560 drawRectangle(graphics);
561 }
562
563 // draw new box
564 lastX = x;
565 lastY = y;
566 drawRectangle(graphics);
567 }
568 // else if (state == JMapPane.Select && selectionLayer != null) {
569 //
570 // // construct a new bbox filter
571 // final Rectangle bounds = this.getBounds();
572 //
573 // final double mapWidth = mapArea.getWidth();
574 // final double mapHeight = mapArea.getHeight();
575 //
576 // final double x1 = ((this.startX * mapWidth) / (double) bounds.width)
577 // + mapArea.getMinX();
578 // final double y1 = (((bounds.getHeight() - this.startY) * mapHeight) /
579 // (double) bounds.height)
580 // + mapArea.getMinY();
581 // final double x2 = ((x * mapWidth) / (double) bounds.width)
582 // + mapArea.getMinX();
583 // final double y2 = (((bounds.getHeight() - y) * mapHeight) / (double)
584 // bounds.height)
585 // + mapArea.getMinY();
586 // final double left = Math.min(x1, x2);
587 // final double right = Math.max(x1, x2);
588 // final double bottom = Math.min(y1, y2);
589 // final double top = Math.max(y1, y2);
590 //
591 // String name = selectionLayer.getFeatureSource().getSchema()
592 // .getDefaultGeometry().getName();
593 //
594 // if (name == "") {
595 // name = "the_geom";
596 // }
597 // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
598 // top,
599 // getContext().getCoordinateReferenceSystem().toString());
600 // if (selectionManager != null) {
601 // selectionManager.selectionChanged(this, bb);
602 // }
603 //
604 // graphics.setXORMode(Color.green);
605 //
606 // /*
607 // * if ((lastX > 0) && (lastY > 0)) { drawRectangle(graphics); }
608 // */
609 //
610 // // draw new box
611 // lastX = x;
612 // lastY = y;
613 // drawRectangle(graphics);
614 // }
615
616 }
617
618 // sk.cs
619 // private void processDrag(int x1, int y1, int x2, int y2) {
620 // sk.ce
621 protected void processDrag(final int x1, final int y1, final int x2,
622 final int y2, final MouseEvent e) {
623
624 /****
625 * If no layer is availabe we dont want a NullPointerException
626 */
627 if (mapArea == null)
628 return;
629
630 // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
631 // + x2 + "," + y2);
632 if ((x1 == x2) && (y1 == y2)) {
633 if (isClickable()) {
634 mouseClicked(new MouseEvent(this, 0, new Date().getTime(), 0,
635 x1, y1, y2, false));
636 }
637
638 return;
639 }
640
641 final Rectangle bounds = this.getBounds();
642
643 final double mapWidth = mapArea.getWidth();
644 final double mapHeight = mapArea.getHeight();
645
646 final double startX = ((x1 * mapWidth) / (double) bounds.width)
647 + mapArea.getMinX();
648 final double startY = (((bounds.getHeight() - y1) * mapHeight) / (double) bounds.height)
649 + mapArea.getMinY();
650 final double endX = ((x2 * mapWidth) / (double) bounds.width)
651 + mapArea.getMinX();
652 final double endY = (((bounds.getHeight() - y2) * mapHeight) / (double) bounds.height)
653 + mapArea.getMinY();
654
655 if ((state == JMapPane.Pan) || (e.getButton() == MouseEvent.BUTTON3)) {
656 // move the image with the mouse
657 // calculate X offsets from start point to the end Point
658 final double deltaX1 = endX - startX;
659
660 // System.out.println("deltaX " + deltaX1);
661 // new edges
662 final double left = mapArea.getMinX() - deltaX1;
663 final double right = mapArea.getMaxX() - deltaX1;
664
665 // now for Y
666 final double deltaY1 = endY - startY;
667
668 // System.out.println("deltaY " + deltaY1);
669 final double bottom = mapArea.getMinY() - deltaY1;
670 final double top = mapArea.getMaxY() - deltaY1;
671 final Coordinate ll = new Coordinate(left, bottom);
672 final Coordinate ur = new Coordinate(right, top);
673 // xulu.sc
674 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
675 setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
676 // xulu.ec
677 } else if (state == JMapPane.ZoomIn) {
678
679 // Zu kleine Flächen sollen nicht gezoomt werden.
680 // sk.bc
681 if ((Math.abs(x1 - x2) * Math.abs(y2 - y1)) < 150)
682 return;
683 // sk.ec
684
685 drawRectangle(this.getGraphics());
686 // make the dragged rectangle (in map coords) the new BBOX
687 final double left = Math.min(startX, endX);
688 final double right = Math.max(startX, endX);
689 final double bottom = Math.min(startY, endY);
690 final double top = Math.max(startY, endY);
691 final Coordinate ll = new Coordinate(left, bottom);
692 final Coordinate ur = new Coordinate(right, top);
693 // xulu.sc
694
695 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
696 setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
697
698 // sk.sc
699 // {
700 // // SK tries to paint a preview of the zoom ;-9 aha.... well
701 // Graphics2D graphics = (Graphics2D) JMapPane.this.getGraphics();
702 // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
703 // RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
704 // graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
705 // RenderingHints.VALUE_ANTIALIAS_OFF);
706 // graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
707 // RenderingHints.VALUE_RENDER_SPEED);
708 // graphics.drawImage(baseImage, 0, 0, JMapPane.this.getWidth(),
709 // JMapPane.this.getHeight(), x1, y1, x2, y2, null);
710 // }
711 // xulu.ec
712 } else if (state == JMapPane.ZoomOut) {
713 drawRectangle(this.getGraphics());
714
715 // make the dragged rectangle in screen coords the new map size?
716 final double left = Math.min(startX, endX);
717 final double right = Math.max(startX, endX);
718 final double bottom = Math.min(startY, endY);
719 final double top = Math.max(startY, endY);
720 final double nWidth = (mapWidth * mapWidth) / (right - left);
721 final double nHeight = (mapHeight * mapHeight) / (top - bottom);
722 final double deltaX1 = left - mapArea.getMinX();
723 final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth;
724 final double deltaY1 = bottom - mapArea.getMinY();
725 final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight;
726 final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1,
727 mapArea.getMinY() - nDeltaY1);
728 final double deltaX2 = mapArea.getMaxX() - right;
729 final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth;
730 final double deltaY2 = mapArea.getMaxY() - top;
731 final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight;
732 final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2,
733 mapArea.getMaxY() + nDeltaY2);
734 // xulu.sc
735 // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
736 setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
737
738 // xulu.ec
739 }
740 // else if (state == JMapPane.Select && selectionLayer != null) {
741 // final double left = Math.min(startX, endX);
742 // final double right = Math.max(startX, endX);
743 // final double bottom = Math.min(startY, endY);
744 // final double top = Math.max(startY, endY);
745 //
746 // String name = selectionLayer.getFeatureSource().getSchema()
747 // .getDefaultGeometry().getLocalName();
748 //
749 // if (name == "") {
750 // name = "the_geom";
751 // }
752 // final Filter bb = ff.bbox(ff.property(name), left, bottom, right,
753 // top,
754 // getContext().getCoordinateReferenceSystem().toString());
755 // // System.out.println(bb.toString());
756 // if (selectionManager != null) {
757 // selectionManager.selectionChanged(this, bb);
758 // }
759 // /*
760 // * FeatureCollection fc; selection = null; try { fc =
761 // * selectionLayer.getFeatureSource().getFeatures(bb); selection =
762 // * fc; } catch (IOException e) { e.printStackTrace(); }
763 // */
764 // }
765
766 // xulu.so
767 // setMapArea(mapArea);
768 // xulu.eo
769 repaint();
770 }
771
772 private boolean isClickable() {
773 return clickable;
774 }
775
776 //
777 // private org.geotools.styling.Style setupStyle(final int type, final Color
778 // color) {
779 // final StyleFactory sf = org.geotools.factory.CommonFactoryFinder
780 // .getStyleFactory(null);
781 // final StyleBuilder sb = new StyleBuilder();
782 //
783 // org.geotools.styling.Style s = sf.createStyle();
784 // s.setTitle("selection");
785 //
786 // // TODO parameterise the color
787 // final PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);
788 // ps.setStroke(sb.createStroke(color));
789 //
790 // final LineSymbolizer ls = sb.createLineSymbolizer(color);
791 // final Graphic h = sb.createGraphic();
792 // h.setMarks(new Mark[] { sb.createMark("square", color) });
793 //
794 // final PointSymbolizer pts = sb.createPointSymbolizer(h);
795 //
796 // // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});
797 // switch (type) {
798 // case POLYGON:
799 // s = sb.createStyle(ps);
800 //
801 // break;
802 //
803 // case POINT:
804 // s = sb.createStyle(pts);
805 //
806 // break;
807 //
808 // case LINE:
809 // s = sb.createStyle(ls);
810 // }
811 //
812 // return s;
813 // }
814
815 public void propertyChange(final PropertyChangeEvent evt) {
816 final String prop = evt.getPropertyName();
817
818 if (prop.equalsIgnoreCase("crs")) {
819 context.setAreaOfInterest(context.getAreaOfInterest(),
820 (CoordinateReferenceSystem) evt.getNewValue());
821 }
822 }
823
824 public boolean isReset() {
825 return reset;
826 }
827
828 public void setReset(final boolean reset) {
829 this.reset = reset;
830 }
831
832 public void layerAdded(final MapLayerListEvent event) {
833 changed = true;
834
835 if (context.getLayers().length == 1) { // the first one
836
837 try {
838 // xulu.sc
839 // mapArea = context.getLayerBounds();
840 mapArea = context.getAreaOfInterest();
841 if (mapArea == null)
842 mapArea = context.getLayerBounds();
843 // xulu.ec
844 } catch (final IOException e) {
845 // TODO Auto-generated catch block
846 e.printStackTrace();
847 }
848
849 reset = true;
850 }
851
852 repaint();
853 }
854
855 public void layerRemoved(final MapLayerListEvent event) {
856 changed = true;
857 repaint();
858 }
859
860 public void layerChanged(final MapLayerListEvent event) {
861 changed = true;
862 // System.out.println("layer changed - repaint");
863 repaint();
864 }
865
866 public void layerMoved(final MapLayerListEvent event) {
867 changed = true;
868 repaint();
869 }
870
871 protected void drawRectangle(final Graphics graphics) {
872 // undraw last box/draw new box
873 final int left = Math.min(startX, lastX);
874 final int right = Math.max(startX, lastX);
875 final int top = Math.max(startY, lastY);
876 final int bottom = Math.min(startY, lastY);
877 final int width = right - left;
878 final int height = top - bottom;
879 // System.out.println("drawing rect("+left+","+bottom+","+ width+","+
880 // height+")");
881 graphics.drawRect(left, bottom, width, height);
882 }
883
884 /**
885 * if clickable is set to true then a single click on the map pane will zoom
886 * or pan the map.
887 *
888 * @param clickable
889 */
890 public void setClickable(final boolean clickable) {
891 this.clickable = clickable;
892 }
893
894 public void mouseMoved(final MouseEvent e) {
895 }
896
897 // xulu.sn
898 /**
899 * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
900 * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
901 * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>
902 * Since 21. April 09: Before thecalculation starts, the aspect ratio is
903 * corrected. This change implies, that setMapArea() will most of the time
904 * not allow setting to a wrong aspectRatio.
905 *
906 * @author <a href="mailto:[email protected]">Stefan Alfons
907 * Kr&uuml;ger</a>
908 */
909 public Envelope bestAllowedMapArea(Envelope env) {
910 if (getWidth() == 0)
911 return env;
912 if (env == null)
913 return env;
914
915 Envelope newArea = null;
916
917 /**
918 * Correct the aspect Ratio before we check the rest. Otherwise we might
919 * easily fail.
920 */
921 env = fixAspectRatio(this.getBounds(), env, false);
922
923 final double scale = env.getWidth() / getWidth();
924 final double centerX = env.getMinX() + env.getWidth() / 2.;
925 final double centerY = env.getMinY() + env.getHeight() / 2.;
926 double newWidth2 = 0;
927 double newHeight2 = 0;
928 if (scale < getMaxZoomScale()) {
929 // ****************************************************************************
930 // Wir zoomen weiter rein als erlaubt => Anpassen des envelope
931 // ****************************************************************************
932 newWidth2 = getMaxZoomScale() * getWidth() / 2.;
933 newHeight2 = getMaxZoomScale() * getHeight() / 2.;
934 } else if (scale > getMinZoomScale()) {
935 // ****************************************************************************
936 // Wir zoomen weiter raus als erlaubt => Anpassen des envelope
937 // ****************************************************************************
938 newWidth2 = getMinZoomScale() * getWidth() / 2.;
939 newHeight2 = getMinZoomScale() * getHeight() / 2.;
940 } else {
941 // ****************************************************************************
942 // Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
943 // ****************************************************************************
944 newArea = env;
945 }
946
947 if (newArea == null) {
948
949 final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
950 - newHeight2);
951 final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
952 + newHeight2);
953
954 newArea = new Envelope(ll, ur);
955 }
956
957 Envelope maxAllowedExtend = getMaxExtend();
958 while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)) {
959 /*
960 * If a maxExtend is set, we have to honour that...
961 */
962
963 // Exceeds top? Move down and maybe cut
964 if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
965 double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
966 LOGGER.debug("Moving area down by " + divY);
967
968 newArea = new Envelope(new Coordinate(newArea.getMinX(),
969 newArea.getMinY() - divY), new Coordinate(newArea
970 .getMaxX(), newArea.getMaxY() - divY));
971
972 if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
973 LOGGER.debug("Now it exeeds the bottom border.. cut!");
974 // And cut the bottom if it moved out of the area
975 newArea = new Envelope(new Coordinate(newArea.getMinX(),
976 maxAllowedExtend.getMinY()), new Coordinate(newArea
977 .getMaxX(), newArea.getMaxY()));
978
979 LOGGER.debug("and fix aspect ratio");
980
981 newArea = fixAspectRatio(this.getBounds(), newArea, false);
982 }
983 }
984
985 // Exceeds bottom? Move up and maybe cut
986 if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
987 double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
988 LOGGER.debug("Moving area up by " + divY);
989
990 newArea = new Envelope(new Coordinate(newArea.getMinX(),
991 newArea.getMinY() - divY), new Coordinate(newArea
992 .getMaxX(), newArea.getMaxY() - divY));
993
994 if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
995 LOGGER.debug("Now it exeeds the top border.. cut!");
996 // And cut the bottom if it moved out of the area
997 newArea = new Envelope(new Coordinate(newArea.getMinX(),
998 newArea.getMinY()), new Coordinate(newArea
999 .getMaxX(), maxAllowedExtend.getMaxY()));
1000
1001 LOGGER.debug("and fix aspect ratio");
1002
1003 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1004 }
1005 }
1006
1007 // Exceeds to the right? move and maybe cut
1008 if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1009
1010 // Move left..
1011 double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
1012 LOGGER.debug("Moving area left by " + divX);
1013
1014 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1015 newArea.getMinY()), new Coordinate(newArea.getMaxX()
1016 - divX, newArea.getMaxY()));
1017
1018 if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1019 LOGGER.debug("Now it exeeds the left border.. cut!");
1020 // And cut the left if it moved out of the area
1021 newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(),
1022 newArea.getMinY()), new Coordinate(newArea
1023 .getMaxX(), newArea.getMaxY()));
1024
1025 LOGGER.debug("and fix aspect ratio");
1026
1027 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1028 }
1029 }
1030
1031 // Exceeds to the left? move and maybe cut
1032 if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
1033
1034 // Move right..
1035 double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
1036 LOGGER.debug("Moving area right by " + divX);
1037
1038 newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
1039 newArea.getMinY()), new Coordinate(newArea.getMaxX()
1040 - divX, newArea.getMaxY()));
1041
1042 if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1043 LOGGER.debug("Now it exeeds the right border.. cut!");
1044 // And cut the left if it moved out of the area
1045 newArea = new Envelope(new Coordinate(newArea.getMinX(),
1046 newArea.getMinY()), new Coordinate(maxAllowedExtend
1047 .getMaxX(), newArea.getMaxY()));
1048
1049 LOGGER.debug("and fix aspect ratio");
1050
1051 newArea = fixAspectRatio(this.getBounds(), newArea, false);
1052 }
1053 }
1054
1055 }
1056
1057 return newArea;
1058 }
1059
1060 /**
1061 * Retuns the minimum allowed zoom scale. This is the bigger number value of
1062 * the two. Defaults to {@link Double}.MAX_VALUE
1063 *
1064 * @author <a href="mailto:[email protected]">Stefan Alfons
1065 * Kr&uuml;ger</a>
1066 */
1067 public Double getMinZoomScale() {
1068 return minZoomScale;
1069 }
1070
1071 /**
1072 * Retuns the maximum allowed zoom scale. This is the smaller number value
1073 * of the two. Defaults to {@link Double}.MIN_VALUE
1074 *
1075 * @author <a href="mailto:[email protected]">Stefan Alfons
1076 * Kr&uuml;ger</a>
1077 */
1078 public Double getMaxZoomScale() {
1079 return maxZoomScale;
1080 }
1081
1082 /**
1083 * Set the maximum allowed zoom scale. This is the smaller number value of
1084 * the two.
1085 *
1086 * @author <a href="mailto:[email protected]">Stefan Alfons
1087 * Kr&uuml;ger</a>
1088 */
1089 public void setMaxZoomScale(final Double maxZoomScale) {
1090 this.maxZoomScale = maxZoomScale;
1091 }
1092
1093 /**
1094 * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1095 * value of the two.
1096 *
1097 * @author <a href="mailto:[email protected]">Stefan Alfons
1098 * Kr&uuml;ger</a>
1099 */
1100 public void setMinZoomScale(final Double minZoomScale) {
1101 this.minZoomScale = minZoomScale;
1102 }
1103
1104 /**
1105 * Defines an evelope of the viwable area. The JMapPane will never show
1106 * anything outside of this extend.
1107 *
1108 * @param maxExtend
1109 * <code>null</code> to not have this restriction.
1110 */
1111 public void setMaxExtend(Envelope maxExtend) {
1112 this.maxExtend = maxExtend;
1113 }
1114
1115 /**
1116 * Returns the evelope of the viewable area. The JMapPane will never show
1117 * anything outside of this extend. If this has been set to
1118 * <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return
1119 * quickly the context's bounds. It it takes to long to determine the
1120 * context bounds, <code>null</code> is returned.
1121 *
1122 * @param maxExtend
1123 * <code>null</code> to not have this restriction.
1124 */
1125
1126 public Envelope getMaxExtend() {
1127 if (maxExtend == null) {
1128 try {
1129 return fixAspectRatio(this.getBounds(), context.getLayerBounds(), true);
1130 } catch (IOException e) {
1131 LOGGER
1132 .warn(
1133 "maxExtend == null; faild to getLayerBounds of context",
1134 e);
1135 }
1136 }
1137 return maxExtend;
1138 }
1139
1140 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26