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