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

Last change on this file since 2627 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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