1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.beans.PropertyChangeEvent;
|
---|
5 | import java.beans.PropertyChangeListener;
|
---|
6 |
|
---|
7 | import javax.swing.JSlider;
|
---|
8 | import javax.swing.event.ChangeEvent;
|
---|
9 | import javax.swing.event.ChangeListener;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
12 | import org.openstreetmap.josm.gui.help.Helpful;
|
---|
13 |
|
---|
14 | class MapSlider extends JSlider implements PropertyChangeListener, ChangeListener, Helpful {
|
---|
15 |
|
---|
16 | private final MapView mv;
|
---|
17 | boolean preventChange = false;
|
---|
18 |
|
---|
19 | public MapSlider(MapView mv) {
|
---|
20 | super(35, 150);
|
---|
21 | setOpaque(false);
|
---|
22 | this.mv = mv;
|
---|
23 | mv.addPropertyChangeListener("scale", this);
|
---|
24 | addChangeListener(this);
|
---|
25 | // Call this manually once so it gets setup correctly
|
---|
26 | propertyChange(null);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
30 | if (getModel().getValueIsAdjusting()) return;
|
---|
31 |
|
---|
32 | ProjectionBounds world = this.mv.getMaxProjectionBounds();
|
---|
33 | ProjectionBounds current = this.mv.getProjectionBounds();
|
---|
34 |
|
---|
35 | double cur_e = current.max.east()-current.min.east();
|
---|
36 | double cur_n = current.max.north()-current.min.north();
|
---|
37 | double e = world.max.east()-world.min.east();
|
---|
38 | double n = world.max.north()-world.min.north();
|
---|
39 | int zoom = 0;
|
---|
40 |
|
---|
41 | while(zoom <= 150) {
|
---|
42 | e /= 1.1;
|
---|
43 | n /= 1.1;
|
---|
44 | if(e < cur_e && n < cur_n) {
|
---|
45 | break;
|
---|
46 | }
|
---|
47 | ++zoom;
|
---|
48 | }
|
---|
49 | preventChange=true;
|
---|
50 | setValue(zoom);
|
---|
51 | preventChange=false;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void stateChanged(ChangeEvent e) {
|
---|
55 | if (preventChange) return;
|
---|
56 |
|
---|
57 | ProjectionBounds world = this.mv.getMaxProjectionBounds();
|
---|
58 | double fact = Math.pow(1.1, getValue());
|
---|
59 | double es = world.max.east()-world.min.east();
|
---|
60 | double n = world.max.north()-world.min.north();
|
---|
61 |
|
---|
62 | this.mv.zoomTo(new ProjectionBounds(this.mv.getCenter(), es/fact, n/fact));
|
---|
63 | }
|
---|
64 |
|
---|
65 | public String helpTopic() {
|
---|
66 | return "MapView/Slider";
|
---|
67 | }
|
---|
68 | }
|
---|