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.actions.HelpAction.Helpful;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 |
|
---|
16 | class MapSlider extends JSlider implements PropertyChangeListener, ChangeListener, Helpful {
|
---|
17 |
|
---|
18 | private final MapView mv;
|
---|
19 | boolean preventChange = false;
|
---|
20 |
|
---|
21 | public MapSlider(MapView mv) {
|
---|
22 | super(35, 150);
|
---|
23 | setOpaque(false);
|
---|
24 | this.mv = mv;
|
---|
25 | mv.addPropertyChangeListener("scale", this);
|
---|
26 | addChangeListener(this);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
30 | if (getModel().getValueIsAdjusting()) return;
|
---|
31 |
|
---|
32 | ProjectionBounds world = Main.proj.getWorldBounds();
|
---|
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 | ++zoom;
|
---|
47 | }
|
---|
48 | preventChange=true;
|
---|
49 | setValue(zoom);
|
---|
50 | preventChange=false;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void stateChanged(ChangeEvent e) {
|
---|
54 | if (preventChange) return;
|
---|
55 |
|
---|
56 | ProjectionBounds world = Main.proj.getWorldBounds();
|
---|
57 | double fact = Math.pow(1.1, getValue());
|
---|
58 | double es = world.max.east()-world.min.east();
|
---|
59 | double n = world.max.north()-world.min.north();
|
---|
60 |
|
---|
61 | this.mv.zoomTo(new ProjectionBounds(this.mv.getCenter(), es/fact, n/fact));
|
---|
62 | }
|
---|
63 |
|
---|
64 | public String helpTopic() {
|
---|
65 | return "MapView/Slider";
|
---|
66 | }
|
---|
67 | }
|
---|