source: josm/trunk/src/org/openstreetmap/josm/gui/ScrollViewport.java@ 8612

Last change on this file since 8612 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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