1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.bbox;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.Graphics;
|
---|
8 | import java.awt.event.MouseAdapter;
|
---|
9 | import java.awt.event.MouseEvent;
|
---|
10 |
|
---|
11 | import javax.accessibility.Accessible;
|
---|
12 | import javax.accessibility.AccessibleAction;
|
---|
13 | import javax.accessibility.AccessibleContext;
|
---|
14 | import javax.swing.ImageIcon;
|
---|
15 | import javax.swing.JComponent;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Button allowing to control the dimension of a slippy map between two states (normal/enlarged).
|
---|
21 | * @author Tim Haussmann
|
---|
22 | * @since 1390
|
---|
23 | */
|
---|
24 | public class SizeButton extends JComponent implements Accessible {
|
---|
25 |
|
---|
26 | private final ImageIcon enlargeImage;
|
---|
27 | private final ImageIcon shrinkImage;
|
---|
28 | private boolean isEnlarged;
|
---|
29 | private final SlippyMapBBoxChooser slippyMapBBoxChooser;
|
---|
30 |
|
---|
31 | private final transient MouseAdapter mouseAdapter = new MouseAdapter() {
|
---|
32 | @Override
|
---|
33 | public void mouseReleased(MouseEvent e) {
|
---|
34 | if (e.getButton() == MouseEvent.BUTTON1) {
|
---|
35 | toggle();
|
---|
36 | }
|
---|
37 | }
|
---|
38 | };
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code SizeButton}.
|
---|
42 | * @param slippyMapBBoxChooser the associated slippy map
|
---|
43 | */
|
---|
44 | public SizeButton(SlippyMapBBoxChooser slippyMapBBoxChooser) {
|
---|
45 | this.slippyMapBBoxChooser = slippyMapBBoxChooser;
|
---|
46 | enlargeImage = ImageProvider.get("view-fullscreen");
|
---|
47 | shrinkImage = ImageProvider.get("view-fullscreen-revert");
|
---|
48 | setPreferredSize(new Dimension(enlargeImage.getIconWidth(), enlargeImage.getIconHeight()));
|
---|
49 | addMouseListener(mouseAdapter);
|
---|
50 | setToolTipText(tr("Enlarge"));
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | protected void paintComponent(Graphics g) {
|
---|
55 | if (isEnlarged) {
|
---|
56 | if (shrinkImage != null)
|
---|
57 | g.drawImage(shrinkImage.getImage(), 0, 0, null);
|
---|
58 | } else {
|
---|
59 | if (enlargeImage != null)
|
---|
60 | g.drawImage(enlargeImage.getImage(), 0, 0, null);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Toggles button state.
|
---|
66 | */
|
---|
67 | public void toggle() {
|
---|
68 | isEnlarged = !isEnlarged;
|
---|
69 | setToolTipText(isEnlarged ? tr("Shrink") : tr("Enlarge"));
|
---|
70 | slippyMapBBoxChooser.resizeSlippyMap();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Determines if the slippy map is enlarged.
|
---|
75 | * @return {@code true} if the slippy map is enlarged, {@code false} otherwise
|
---|
76 | */
|
---|
77 | public boolean isEnlarged() {
|
---|
78 | return isEnlarged;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public AccessibleContext getAccessibleContext() {
|
---|
83 | if (accessibleContext == null) {
|
---|
84 | accessibleContext = new AccessibleSizeButton();
|
---|
85 | }
|
---|
86 | return accessibleContext;
|
---|
87 | }
|
---|
88 |
|
---|
89 | class AccessibleSizeButton extends AccessibleJComponent implements AccessibleAction {
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public int getAccessibleActionCount() {
|
---|
93 | return 1;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public String getAccessibleActionDescription(int i) {
|
---|
98 | return "toggle";
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public boolean doAccessibleAction(int i) {
|
---|
103 | toggle();
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|