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

Last change on this file since 1728 was 1677, checked in by stoecker, 15 years ago

remove all these ugly tab stops introduced in the last half year

File size: 5.7 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.MouseAdapter;
12import java.awt.event.MouseEvent;
13
14import javax.swing.JButton;
15import javax.swing.JComponent;
16import javax.swing.JPanel;
17import javax.swing.JViewport;
18import javax.swing.Timer;
19
20import org.openstreetmap.josm.tools.ImageProvider;
21
22/** A viewport with UP and DOWN arrow buttons, so that the user can make the
23 * content scroll.
24 */
25public class ScrollViewport extends JPanel {
26
27 private static final int NO_SCROLL = 0;
28
29 public static final int UP_DIRECTION = 1;
30 public static final int DOWN_DIRECTION = 2;
31 public static final int LEFT_DIRECTION = 4;
32 public static final int RIGHT_DIRECTION = 8;
33 public static final int VERTICAL_DIRECTION = UP_DIRECTION | DOWN_DIRECTION;
34 public static final int HORIZONTAL_DIRECTION = LEFT_DIRECTION | RIGHT_DIRECTION;
35 public static final int ALL_DIRECTION = HORIZONTAL_DIRECTION | VERTICAL_DIRECTION;
36
37 private class ScrollViewPortMouseListener extends MouseAdapter {
38 private int direction;
39
40 public ScrollViewPortMouseListener(int direction) {
41 this.direction = direction;
42 }
43
44 @Override public void mouseExited(MouseEvent arg0) {
45 ScrollViewport.this.scrollDirection = NO_SCROLL;
46 timer.stop();
47 }
48
49 @Override public void mouseReleased(MouseEvent arg0) {
50 ScrollViewport.this.scrollDirection = NO_SCROLL;
51 timer.stop();
52 }
53
54 @Override public void mousePressed(MouseEvent arg0) {
55 ScrollViewport.this.scrollDirection = direction;
56 scroll();
57 timer.restart();
58 }
59
60 }
61
62 private JViewport vp = new JViewport();
63 private JComponent component = null;
64
65 private Timer timer = new Timer(200, new ActionListener() {
66 public void actionPerformed(ActionEvent arg0) {
67 ScrollViewport.this.scroll();
68 }
69 });
70
71 private int scrollDirection = NO_SCROLL;
72
73 public ScrollViewport(JComponent c, int direction) {
74 this(direction);
75 add(c);
76 }
77
78 public ScrollViewport(int direction) {
79 setLayout(new BorderLayout());
80
81 JButton button;
82
83 // UP
84 if ((direction & UP_DIRECTION) > 0) {
85 button = new JButton();
86 button.addMouseListener(new ScrollViewPortMouseListener(UP_DIRECTION));
87 button.setPreferredSize(new Dimension(10,10));
88 button.setIcon(ImageProvider.get("svpUp"));
89 add(button, BorderLayout.NORTH);
90 }
91
92 // DOWN
93 if ((direction & DOWN_DIRECTION) > 0) {
94 button = new JButton();
95 button.addMouseListener(new ScrollViewPortMouseListener(DOWN_DIRECTION));
96 button.setPreferredSize(new Dimension(10,10));
97 button.setIcon(ImageProvider.get("svpDown"));
98 add(button, BorderLayout.SOUTH);
99 }
100
101 // LEFT
102 if ((direction & LEFT_DIRECTION) > 0) {
103 button = new JButton();
104 button.addMouseListener(new ScrollViewPortMouseListener(LEFT_DIRECTION));
105 button.setPreferredSize(new Dimension(10,10));
106 button.setIcon(ImageProvider.get("svpLeft"));
107 add(button, BorderLayout.WEST);
108 }
109
110 // RIGHT
111 if ((direction & RIGHT_DIRECTION) > 0) {
112 button = new JButton();
113 button.addMouseListener(new ScrollViewPortMouseListener(RIGHT_DIRECTION));
114 button.setPreferredSize(new Dimension(10,10));
115 button.setIcon(ImageProvider.get("svpRight"));
116 add(button, BorderLayout.EAST);
117 }
118
119 add(vp, BorderLayout.CENTER);
120
121 timer.setRepeats(true);
122 timer.setInitialDelay(400);
123 }
124
125 public synchronized void scroll() {
126 int direction = scrollDirection;
127
128 if (component == null || direction == NO_SCROLL) {
129 return;
130 }
131
132 Dimension compSize = component.getSize();
133 Rectangle viewRect = vp.getViewRect();
134
135 int delta;
136 int newY = 0;
137 int newX = 0;
138
139 if (direction < LEFT_DIRECTION) {
140 newX = viewRect.x;
141 delta = viewRect.height * 4 / 5;
142 } else {
143 newY = viewRect.y;
144 delta = viewRect.width * 4 / 5;
145 }
146
147 switch (direction) {
148 case UP_DIRECTION :
149 newY = viewRect.y - delta;
150 if (newY < 0) {
151 newY = 0;
152 }
153 break;
154 case DOWN_DIRECTION :
155 newY = viewRect.y + delta;
156 if (newY > compSize.height - viewRect.height) {
157 newY = compSize.height - viewRect.height;
158 }
159 break;
160 case LEFT_DIRECTION :
161 newX = viewRect.x - delta;
162 if (newX < 0) {
163 newX = 0;
164 }
165 break;
166 case RIGHT_DIRECTION :
167 newX = viewRect.x + delta;
168 if (newX > compSize.width - viewRect.width) {
169 newX = compSize.width - viewRect.width;
170 }
171 break;
172 default :
173 throw new IllegalStateException("Unknown direction : [0]" + direction);
174 }
175
176 vp.setViewPosition(new Point(newX, newY));
177 }
178
179 public Rectangle getViewRect() {
180 return vp.getViewRect();
181 }
182
183 public Dimension getViewSize() {
184 return vp.getViewSize();
185 }
186
187 public Point getViewPosition() {
188 return vp.getViewPosition();
189 }
190
191 public void add(JComponent c) {
192 vp.removeAll();
193 this.component = c;
194 vp.add(c);
195 }
196}
Note: See TracBrowser for help on using the repository browser.