| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.gui; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
|---|
| 5 | |
|---|
| 6 | import java.beans.PropertyChangeEvent; |
|---|
| 7 | import java.beans.PropertyChangeListener; |
|---|
| 8 | |
|---|
| 9 | import javax.swing.JSlider; |
|---|
| 10 | import javax.swing.event.ChangeEvent; |
|---|
| 11 | import javax.swing.event.ChangeListener; |
|---|
| 12 | |
|---|
| 13 | import org.openstreetmap.josm.data.ProjectionBounds; |
|---|
| 14 | import org.openstreetmap.josm.gui.help.Helpful; |
|---|
| 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 | // Call this manually once so it gets setup correctly |
|---|
| 28 | propertyChange(null); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | public void propertyChange(PropertyChangeEvent evt) { |
|---|
| 32 | if (getModel().getValueIsAdjusting()) return; |
|---|
| 33 | |
|---|
| 34 | ProjectionBounds world = this.mv.getMaxProjectionBounds(); |
|---|
| 35 | ProjectionBounds current = this.mv.getProjectionBounds(); |
|---|
| 36 | |
|---|
| 37 | double cur_e = current.maxEast-current.minEast; |
|---|
| 38 | double cur_n = current.maxNorth-current.minNorth; |
|---|
| 39 | double e = world.maxEast-world.minEast; |
|---|
| 40 | double n = world.maxNorth-world.minNorth; |
|---|
| 41 | int zoom = 0; |
|---|
| 42 | |
|---|
| 43 | while(zoom <= 150) { |
|---|
| 44 | e /= 1.1; |
|---|
| 45 | n /= 1.1; |
|---|
| 46 | if(e < cur_e && n < cur_n) { |
|---|
| 47 | break; |
|---|
| 48 | } |
|---|
| 49 | ++zoom; |
|---|
| 50 | } |
|---|
| 51 | preventChange=true; |
|---|
| 52 | setValue(zoom); |
|---|
| 53 | preventChange=false; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | public void stateChanged(ChangeEvent e) { |
|---|
| 57 | if (preventChange) return; |
|---|
| 58 | |
|---|
| 59 | ProjectionBounds world = this.mv.getMaxProjectionBounds(); |
|---|
| 60 | double fact = Math.pow(1.1, getValue()); |
|---|
| 61 | double es = world.maxEast-world.minEast; |
|---|
| 62 | double n = world.maxNorth-world.minNorth; |
|---|
| 63 | |
|---|
| 64 | this.mv.zoomTo(new ProjectionBounds(this.mv.getCenter(), es/fact, n/fact)); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public String helpTopic() { |
|---|
| 68 | return ht("/MapView/Slider"); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|