source: josm/trunk/src/org/openstreetmap/josm/gui/MapSlider.java@ 12891

Last change on this file since 12891 was 12537, checked in by Don-vip, 7 years ago

PMD - VariableNamingConventions

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5
6import java.awt.Dimension;
7import java.beans.PropertyChangeEvent;
8import java.beans.PropertyChangeListener;
9
10import javax.swing.JSlider;
11import javax.swing.UIManager;
12import javax.swing.event.ChangeEvent;
13import javax.swing.event.ChangeListener;
14
15import org.openstreetmap.josm.gui.help.Helpful;
16
17/**
18 * This is the slider used in the top left corner of the map view. It allows the user to select the scale
19 */
20class MapSlider extends JSlider implements PropertyChangeListener, ChangeListener, Helpful {
21
22 private static final double ZOOM_STEP = 1.1;
23 private final MapView mv;
24 private boolean preventChange;
25 private int lastValue;
26
27 MapSlider(MapView mv) {
28 super(0, 160);
29 setOpaque(false);
30 this.mv = mv;
31 mv.addPropertyChangeListener("scale", this);
32 addChangeListener(this);
33 // Call this manually once so it gets setup correctly
34 propertyChange(null);
35 int w = UIManager.getDefaults().getInt("Slider.thumbWidth") + 150;
36 setPreferredSize(new Dimension(w, 27));
37 }
38
39 @Override
40 public void propertyChange(PropertyChangeEvent evt) {
41 double maxScale = this.mv.getMaxScale();
42 int zoom = (int) Math.round(Math.log(maxScale/mv.getScale())/Math.log(ZOOM_STEP));
43 preventChange = true;
44 setValue(zoom);
45 lastValue = zoom;
46 preventChange = false;
47 }
48
49 @Override
50 public void stateChanged(ChangeEvent e) {
51 if (preventChange) return;
52
53 if (!getModel().getValueIsAdjusting() && mv.getNativeScaleLayer() != null) {
54 if (getValue() < lastValue) {
55 mv.zoomOut();
56 } else if (getValue() > lastValue) {
57 mv.zoomIn();
58 }
59 } else {
60 double maxScale = this.mv.getMaxScale();
61 double scale = maxScale/Math.pow(ZOOM_STEP, getValue());
62 double snapped = mv.scaleFloor(scale);
63 mv.zoomTo(this.mv.getCenter(), snapped);
64 }
65 propertyChange(null);
66 }
67
68 @Override
69 public String helpTopic() {
70 return ht("/MapView/Slider");
71 }
72}
Note: See TracBrowser for help on using the repository browser.