1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.awt.event.ComponentAdapter;
|
---|
9 | import java.awt.event.ComponentEvent;
|
---|
10 | import java.awt.event.MouseAdapter;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import javax.swing.JButton;
|
---|
16 | import javax.swing.JComponent;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JViewport;
|
---|
19 | import javax.swing.Timer;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 |
|
---|
23 | /** A viewport with UP and DOWN arrow buttons, so that the user can make the
|
---|
24 | * content scroll.
|
---|
25 | */
|
---|
26 | public class ScrollViewport extends JPanel {
|
---|
27 |
|
---|
28 | private static final int NO_SCROLL = 0;
|
---|
29 |
|
---|
30 | public static final int UP_DIRECTION = 1;
|
---|
31 | public static final int DOWN_DIRECTION = 2;
|
---|
32 | public static final int LEFT_DIRECTION = 4;
|
---|
33 | public static final int RIGHT_DIRECTION = 8;
|
---|
34 | public static final int VERTICAL_DIRECTION = UP_DIRECTION | DOWN_DIRECTION;
|
---|
35 | public static final int HORIZONTAL_DIRECTION = LEFT_DIRECTION | RIGHT_DIRECTION;
|
---|
36 | public static final int ALL_DIRECTION = HORIZONTAL_DIRECTION | VERTICAL_DIRECTION;
|
---|
37 |
|
---|
38 | private class ScrollViewPortMouseListener extends MouseAdapter {
|
---|
39 | private final int direction;
|
---|
40 |
|
---|
41 | ScrollViewPortMouseListener(int direction) {
|
---|
42 | this.direction = direction;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void mouseExited(MouseEvent arg0) {
|
---|
47 | ScrollViewport.this.scrollDirection = NO_SCROLL;
|
---|
48 | timer.stop();
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void mouseReleased(MouseEvent arg0) {
|
---|
53 | ScrollViewport.this.scrollDirection = NO_SCROLL;
|
---|
54 | timer.stop();
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override public void mousePressed(MouseEvent arg0) {
|
---|
58 | ScrollViewport.this.scrollDirection = direction;
|
---|
59 | scroll();
|
---|
60 | timer.restart();
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | private final JViewport vp = new JViewport();
|
---|
66 | private JComponent component;
|
---|
67 |
|
---|
68 | private final List<JButton> buttons = new ArrayList<>();
|
---|
69 |
|
---|
70 | private final Timer timer = new Timer(100, evt -> scroll());
|
---|
71 |
|
---|
72 | private int scrollDirection = NO_SCROLL;
|
---|
73 |
|
---|
74 | public ScrollViewport(JComponent c, int direction) {
|
---|
75 | this(direction);
|
---|
76 | add(c);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public ScrollViewport(int direction) {
|
---|
80 | setLayout(new BorderLayout());
|
---|
81 |
|
---|
82 | JButton button;
|
---|
83 |
|
---|
84 | // UP
|
---|
85 | if ((direction & UP_DIRECTION) != 0) {
|
---|
86 | button = new JButton();
|
---|
87 | button.addMouseListener(new ScrollViewPortMouseListener(UP_DIRECTION));
|
---|
88 | button.setPreferredSize(new Dimension(10, 10));
|
---|
89 | button.setIcon(ImageProvider.get("svpUp"));
|
---|
90 | add(button, BorderLayout.NORTH);
|
---|
91 | buttons.add(button);
|
---|
92 | }
|
---|
93 |
|
---|
94 | // DOWN
|
---|
95 | if ((direction & DOWN_DIRECTION) != 0) {
|
---|
96 | button = new JButton();
|
---|
97 | button.addMouseListener(new ScrollViewPortMouseListener(DOWN_DIRECTION));
|
---|
98 | button.setPreferredSize(new Dimension(10, 10));
|
---|
99 | button.setIcon(ImageProvider.get("svpDown"));
|
---|
100 | add(button, BorderLayout.SOUTH);
|
---|
101 | buttons.add(button);
|
---|
102 | }
|
---|
103 |
|
---|
104 | // LEFT
|
---|
105 | if ((direction & LEFT_DIRECTION) != 0) {
|
---|
106 | button = new JButton();
|
---|
107 | button.addMouseListener(new ScrollViewPortMouseListener(LEFT_DIRECTION));
|
---|
108 | button.setPreferredSize(new Dimension(10, 10));
|
---|
109 | button.setIcon(ImageProvider.get("svpLeft"));
|
---|
110 | add(button, BorderLayout.WEST);
|
---|
111 | buttons.add(button);
|
---|
112 | }
|
---|
113 |
|
---|
114 | // RIGHT
|
---|
115 | if ((direction & RIGHT_DIRECTION) != 0) {
|
---|
116 | button = new JButton();
|
---|
117 | button.addMouseListener(new ScrollViewPortMouseListener(RIGHT_DIRECTION));
|
---|
118 | button.setPreferredSize(new Dimension(10, 10));
|
---|
119 | button.setIcon(ImageProvider.get("svpRight"));
|
---|
120 | add(button, BorderLayout.EAST);
|
---|
121 | buttons.add(button);
|
---|
122 | }
|
---|
123 |
|
---|
124 | add(vp, BorderLayout.CENTER);
|
---|
125 |
|
---|
126 | this.addComponentListener(new ComponentAdapter() {
|
---|
127 | @Override public void componentResized(ComponentEvent e) {
|
---|
128 | showOrHideButtons();
|
---|
129 | }
|
---|
130 | });
|
---|
131 |
|
---|
132 | showOrHideButtons();
|
---|
133 |
|
---|
134 | timer.setRepeats(true);
|
---|
135 | timer.setInitialDelay(400);
|
---|
136 | }
|
---|
137 |
|
---|
138 | public synchronized void scroll() {
|
---|
139 | int direction = scrollDirection;
|
---|
140 |
|
---|
141 | if (component == null || direction == NO_SCROLL)
|
---|
142 | return;
|
---|
143 |
|
---|
144 | Rectangle viewRect = vp.getViewRect();
|
---|
145 |
|
---|
146 | int deltaX = 0;
|
---|
147 | int deltaY = 0;
|
---|
148 |
|
---|
149 | if (direction < LEFT_DIRECTION) {
|
---|
150 | deltaY = viewRect.height * 2 / 7;
|
---|
151 | } else {
|
---|
152 | deltaX = viewRect.width * 2 / 7;
|
---|
153 | }
|
---|
154 |
|
---|
155 | switch (direction) {
|
---|
156 | case UP_DIRECTION :
|
---|
157 | deltaY *= -1;
|
---|
158 | break;
|
---|
159 | case LEFT_DIRECTION :
|
---|
160 | deltaX *= -1;
|
---|
161 | break;
|
---|
162 | default: // Do nothing
|
---|
163 | }
|
---|
164 |
|
---|
165 | scroll(deltaX, deltaY);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public synchronized void scroll(int deltaX, int deltaY) {
|
---|
169 | if (component == null)
|
---|
170 | return;
|
---|
171 | Dimension compSize = component.getSize();
|
---|
172 | Rectangle viewRect = vp.getViewRect();
|
---|
173 |
|
---|
174 | int newX = viewRect.x + deltaX;
|
---|
175 | int newY = viewRect.y + deltaY;
|
---|
176 |
|
---|
177 | if (newY < 0) {
|
---|
178 | newY = 0;
|
---|
179 | }
|
---|
180 | if (newY > compSize.height - viewRect.height) {
|
---|
181 | newY = compSize.height - viewRect.height;
|
---|
182 | }
|
---|
183 | if (newX < 0) {
|
---|
184 | newX = 0;
|
---|
185 | }
|
---|
186 | if (newX > compSize.width - viewRect.width) {
|
---|
187 | newX = compSize.width - viewRect.width;
|
---|
188 | }
|
---|
189 |
|
---|
190 | vp.setViewPosition(new Point(newX, newY));
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Update the visibility of the buttons
|
---|
195 | * Only show them if the Viewport is too small for the content.
|
---|
196 | */
|
---|
197 | public void showOrHideButtons() {
|
---|
198 | boolean needButtons = vp.getViewSize().height > vp.getViewRect().height ||
|
---|
199 | vp.getViewSize().width > vp.getViewRect().width;
|
---|
200 | for (JButton b : buttons) {
|
---|
201 | b.setVisible(needButtons);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | public Rectangle getViewRect() {
|
---|
206 | return vp.getViewRect();
|
---|
207 | }
|
---|
208 |
|
---|
209 | public Dimension getViewSize() {
|
---|
210 | return vp.getViewSize();
|
---|
211 | }
|
---|
212 |
|
---|
213 | public Point getViewPosition() {
|
---|
214 | return vp.getViewPosition();
|
---|
215 | }
|
---|
216 |
|
---|
217 | public void add(JComponent c) {
|
---|
218 | vp.removeAll();
|
---|
219 | this.component = c;
|
---|
220 | vp.add(c);
|
---|
221 | }
|
---|
222 | }
|
---|