source: josm/trunk/src/org/openstreetmap/josm/gui/MapScaler.java@ 9997

Last change on this file since 9997 was 9954, checked in by Don-vip, 8 years ago

make MapScaler and SizeButton implement Accessible interface, add unit tests, fix checkstyle violation

  • Property svn:eol-style set to native
File size: 2.5 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;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6
7import java.awt.Color;
8import java.awt.Graphics;
9import java.awt.geom.Rectangle2D;
10
11import javax.accessibility.Accessible;
12import javax.accessibility.AccessibleContext;
13import javax.accessibility.AccessibleValue;
14import javax.swing.JComponent;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.gui.help.Helpful;
18
19/**
20 * Map scale bar, displaying the distance in meter that correspond to 100 px on screen.
21 * @since 115
22 */
23public class MapScaler extends JComponent implements Helpful, Accessible {
24
25 private final NavigatableComponent mv;
26
27 private static final int PADDING_RIGHT = 100;
28
29 /**
30 * Constructs a new {@code MapScaler}.
31 * @param mv map view
32 */
33 public MapScaler(NavigatableComponent mv) {
34 this.mv = mv;
35 setSize(100+PADDING_RIGHT, 30);
36 setOpaque(false);
37 }
38
39 @Override
40 public void paint(Graphics g) {
41 String text = mv.getDist100PixelText();
42 Rectangle2D bound = g.getFontMetrics().getStringBounds(text, g);
43 g.setColor(getColor());
44 g.drawLine(0, 5, 99, 5);
45 g.drawLine(0, 0, 0, 10);
46 g.drawLine(99, 0, 99, 10);
47 g.drawLine(49, 3, 49, 7);
48 g.drawLine(24, 3, 24, 7);
49 g.drawLine(74, 3, 74, 7);
50 g.drawString(text, (int) (100-bound.getWidth()/2), 23);
51 g.drawString("0", 0, 23);
52 }
53
54 /**
55 * Returns the color of map scaler.
56 * @return the color of map scaler
57 */
58 public static Color getColor() {
59 return Main.pref.getColor(marktr("scale"), Color.white);
60 }
61
62 @Override
63 public String helpTopic() {
64 return ht("/MapView/Scaler");
65 }
66
67 @Override
68 public AccessibleContext getAccessibleContext() {
69 if (accessibleContext == null) {
70 accessibleContext = new AccessibleMapScaler();
71 }
72 return accessibleContext;
73 }
74
75 class AccessibleMapScaler extends AccessibleJComponent implements AccessibleValue {
76
77 @Override
78 public Number getCurrentAccessibleValue() {
79 return mv.getDist100Pixel();
80 }
81
82 @Override
83 public boolean setCurrentAccessibleValue(Number n) {
84 return false;
85 }
86
87 @Override
88 public Number getMinimumAccessibleValue() {
89 return null;
90 }
91
92 @Override
93 public Number getMaximumAccessibleValue() {
94 return null;
95 }
96 }
97}
Note: See TracBrowser for help on using the repository browser.