-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFormScroller.java
More file actions
248 lines (202 loc) · 6.5 KB
/
FormScroller.java
File metadata and controls
248 lines (202 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.awt.*;
import java.beans.*;
import javax.swing.*;
/**
* The FormScroller will ensure that when a component gains focus it will
* always be visible within the viewport of the scrollpane.
*/
public class FormScroller implements PropertyChangeListener
{
public enum Type
{
COMPONENT,
PARENT,
CHILD;
}
private JScrollPane scrollPane;
private Type type;
private Insets scrollInsets = new Insets(0, 0, 0, 0);
private boolean scrollingEnabled;
private Point lastLocation = new Point();
/**
* Convenience constructor that set the scroll Type to COMPONENT
*/
public FormScroller(JScrollPane scrollPane)
{
this(scrollPane, Type.COMPONENT);
}
/**
* Create a FormScroller for the specified scroll Type
*/
public FormScroller(JScrollPane scrollPane, Type type)
{
this.scrollPane = scrollPane;
setType( type );
setScrollingEnabled( true );
}
/**
* Get the Type of scrolling to be attempted by the scroller
*/
public Type getType()
{
return type;
}
/**
* Set the Type of scrolling to be done by the scroller
*
* Type.COMPONENT - the focused component should be visible in the viewport.
* Type.PARENT - the parent Container of the focus component should be
* visible in the viewport. If the parent Container does not
* fit completely then use Type.COMPONENT.
* Type.CHILD - the child Contaier of the viewport view component which
* contains the focused component should be visible in the
* viewport. If the child Container does not fit completely
* then use Type.PARENT.
*
* @param Type - controls scrolling of the viewport (values given above)
*/
public void setType(Type type)
{
this.type = type;
}
/**
* Get the scroll insets.
*
* @return the scroll insets
*/
public Insets getScrollInsets()
{
return scrollInsets;
}
/**
* Set the scroll insets. The scroller will attempt to leave a gap between
* the scrolled Container and the edge of the scrollpane.
*
* @paran scrollInsets - Insets for the gap for each edge of the scrollpane
*/
public void setScrollInsets(Insets scrollInsets)
{
this.scrollInsets = scrollInsets;
}
/**
*
*/
public boolean isScrollingEnabled()
{
return scrollingEnabled;
}
/**
* Enable automatic scrolling on the form.
*
* @param scrollingEnabled enable/disable scrolling
*/
public void setScrollingEnabled(boolean scrollingEnabled)
{
this.scrollingEnabled = scrollingEnabled;
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.removePropertyChangeListener("permanentFocusOwner", this);
if (scrollingEnabled)
{
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("permanentFocusOwner", this);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt)
{
Component component = (Component) evt.getNewValue();
if (component == null) return;
// Make sure the component with focus is in the viewport
JComponent view = (JComponent)scrollPane.getViewport().getView();
if (! SwingUtilities.isDescendingFrom(component, view)) return;
// Scroll the viewport
Rectangle bounds = determineScrollBounds(component, view);
view.scrollRectToVisible(bounds);
}
/**
* Determine the bounds that must fit into the viewport of the scrollpane
*
* @param component the component that currently has focus
* @param view the component added to the viewport of the scrollpane
* @return a Rectangle representing the bounds to be scrolled
*/
protected Rectangle determineScrollBounds(Component component, JComponent view)
{
// Determine the scroll bounds based on the scroll Type
Rectangle bounds = null;
if (type == Type.COMPONENT)
bounds = determineComponentBounds(component, view);
else if (type == Type.PARENT)
bounds = determineParentBounds(component, view);
else
bounds = determineChildBounds(component, view);
// Adjust bounds to take scroll insets into consideration
Point location = component.getLocation();
location = SwingUtilities.convertPoint(component.getParent(), location, view);
if (location.x < lastLocation.x)
bounds.x -= scrollInsets.left;
else
bounds.width += scrollInsets.right;
if (location.y < lastLocation.y)
bounds.y -= scrollInsets.top;
else
bounds.height += scrollInsets.bottom;
lastLocation = location;
return bounds;
}
private Rectangle determineComponentBounds(Component component, JComponent view)
{
// Use the bounds of the focused component
Rectangle bounds = component.getBounds();
bounds = SwingUtilities.convertRectangle(component.getParent(), bounds, view);
return bounds;
}
private Rectangle determineParentBounds(Component component, JComponent view)
{
/*
// When focusing on a component in a tabbed pane attempt to keep the tabs
// in the viewport by treating the tabbed pane as the component with focus
Component tabbedPane = (Component)SwingUtilities.getAncestorOfClass(JTabbedPane.class, component);
if (tabbedPane != null)
component = tabbedPane;
*/
// Use the bounds of the parent Container
Component parent = component.getParent();
Rectangle bounds = parent.getBounds();
bounds = SwingUtilities.convertRectangle(parent.getParent(), bounds, view);
// Make sure the Container will fit into the viewport
if (rectangleFits(bounds))
return bounds;
else
return determineComponentBounds(component, view);
}
private Rectangle determineChildBounds(Component component, JComponent view)
{
// Search each child Component of the view to find the Container which
// contains the component which current has focus
Component child = null;
for (Component c: view.getComponents())
{
if (SwingUtilities.isDescendingFrom(component, c))
{
child = c;
break;
}
}
Rectangle bounds = child.getBounds();
bounds = SwingUtilities.convertRectangle(child.getParent(), bounds, view);
// Make sure this container will fit into the viewport
if (rectangleFits(bounds))
return bounds;
else
return determineParentBounds(component, view);
}
private boolean rectangleFits(Rectangle bounds)
{
Dimension viewport = scrollPane.getViewport().getSize();
if (bounds.width > viewport.width || bounds.height > viewport.height)
return false;
else
return true;
}
}