1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
54: public class ScrollPaneLayout
55: implements LayoutManager, ScrollPaneConstants, Serializable
56: {
57: private static final long serialVersionUID = -4480022884523193743L;
58:
59: public static class UIResource extends ScrollPaneLayout
60: implements javax.swing.plaf.UIResource
61: {
62: public UIResource()
63: {
64: super();
65: }
66: }
67:
68: protected JViewport viewport;
69: protected JScrollBar vsb;
70: protected JScrollBar hsb;
71: protected JViewport rowHead;
72: protected JViewport colHead;
73: protected Component lowerLeft;
74: protected Component lowerRight;
75: protected Component upperLeft;
76: protected Component upperRight;
77: protected int vsbPolicy;
78: protected int hsbPolicy;
79:
80: public ScrollPaneLayout()
81: {
82:
83: }
84:
85: public void syncWithScrollPane(JScrollPane scrollPane) {
86: viewport = scrollPane.getViewport();
87: rowHead = scrollPane.getRowHeader();
88: colHead = scrollPane.getColumnHeader();
89: vsb = scrollPane.getVerticalScrollBar();
90: hsb = scrollPane.getHorizontalScrollBar();
91: vsbPolicy = scrollPane.getVerticalScrollBarPolicy();
92: hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();
93: lowerLeft = scrollPane.getCorner(LOWER_LEFT_CORNER);
94: lowerRight = scrollPane.getCorner(LOWER_RIGHT_CORNER);
95: upperLeft = scrollPane.getCorner(UPPER_LEFT_CORNER);
96: upperRight = scrollPane.getCorner(UPPER_RIGHT_CORNER);
97: }
98:
99:
107: protected Component addSingletonComponent(Component oldComponent,
108: Component newComponent)
109: {
110: if (oldComponent != null && oldComponent != newComponent)
111: oldComponent.getParent().remove(oldComponent);
112: return newComponent;
113: }
114:
115:
124: public void addLayoutComponent(String key, Component component)
125: {
126: if (key == VIEWPORT)
127: viewport = (JViewport) component;
128: else if (key == VERTICAL_SCROLLBAR)
129: vsb = (JScrollBar) component;
130: else if (key == HORIZONTAL_SCROLLBAR)
131: hsb = (JScrollBar) component;
132: else if (key == ROW_HEADER)
133: rowHead = (JViewport) component;
134: else if (key == COLUMN_HEADER)
135: colHead = (JViewport) component;
136: else if (key == LOWER_RIGHT_CORNER)
137: lowerRight = component;
138: else if (key == UPPER_RIGHT_CORNER)
139: upperRight = component;
140: else if (key == LOWER_LEFT_CORNER)
141: lowerLeft = component;
142: else if (key == UPPER_LEFT_CORNER)
143: upperLeft = component;
144: else
145: throw new IllegalArgumentException();
146: }
147:
148: public void removeLayoutComponent(Component component) {
149: if (component == viewport)
150: viewport = null;
151: else if (component == vsb)
152: vsb = null;
153: else if (component == hsb)
154: hsb = null;
155: else if (component == rowHead)
156: rowHead = null;
157: else if (component == colHead)
158: colHead = null;
159: else if (component == lowerRight)
160: lowerRight = null;
161: else if (component == upperRight)
162: upperRight = null;
163: else if (component == lowerLeft)
164: lowerLeft = null;
165: else if (component == upperLeft)
166: upperLeft = null;
167: }
168:
169: public int getVerticalScrollBarPolicy()
170: {
171: return vsbPolicy;
172: }
173:
174:
181: public void setVerticalScrollBarPolicy(int policy)
182: {
183: if (policy != VERTICAL_SCROLLBAR_AS_NEEDED &&
184: policy != VERTICAL_SCROLLBAR_NEVER &&
185: policy != VERTICAL_SCROLLBAR_ALWAYS)
186: throw new IllegalArgumentException("Illegal Scrollbar Policy");
187: vsbPolicy = policy;
188: }
189:
190: public int getHorizontalScrollBarPolicy()
191: {
192: return hsbPolicy;
193: }
194:
195:
202: public void setHorizontalScrollBarPolicy(int policy)
203: {
204: if (policy != HORIZONTAL_SCROLLBAR_AS_NEEDED &&
205: policy != HORIZONTAL_SCROLLBAR_NEVER &&
206: policy != HORIZONTAL_SCROLLBAR_ALWAYS)
207: throw new IllegalArgumentException("Illegal Scrollbar Policy");
208: hsbPolicy = policy;
209: }
210:
211: public JViewport getViewport()
212: {
213: return viewport;
214: }
215:
216: public JScrollBar getHorizontalScrollBar()
217: {
218: return hsb;
219: }
220:
221: public JScrollBar getVerticalScrollBar()
222: {
223: return vsb;
224: }
225:
226: public JViewport getRowHeader()
227: {
228: return rowHead;
229: }
230:
231: public JViewport getColumnHeader()
232: {
233: return colHead;
234: }
235:
236:
242: public Component getCorner(String key)
243: {
244: if (key == LOWER_RIGHT_CORNER)
245: return lowerRight;
246: else if (key == UPPER_RIGHT_CORNER)
247: return upperRight;
248: else if (key == LOWER_LEFT_CORNER)
249: return lowerLeft;
250: else if (key == UPPER_LEFT_CORNER)
251: return upperLeft;
252: return null;
253: }
254:
255: public Dimension preferredLayoutSize(Container parent)
256: {
257:
258:
259: JScrollPane sc = (JScrollPane) parent;
260: Dimension viewportSize = viewport.getPreferredSize();
261: Dimension viewSize = viewport.getViewSize();
262: int width = viewportSize.width;
263: int height = viewportSize.height;
264:
265:
266:
267: if (hsb != null && viewSize.width > viewportSize.width)
268: height += hsb.getPreferredSize().height;
269:
270:
271:
272: if (vsb != null && viewSize.height > viewportSize.height)
273: width += vsb.getPreferredSize().width;
274: if (rowHead != null && rowHead.isVisible())
275: width += rowHead.getPreferredSize().width;
276: if (colHead != null && colHead.isVisible())
277: height += colHead.getPreferredSize().height;
278: Insets i = sc.getInsets();
279: return new Dimension(width + i.left + i.right,
280: height + i.left + i.right);
281: }
282:
283: public Dimension minimumLayoutSize(Container parent)
284: {
285:
286:
287: JScrollPane sc = (JScrollPane) parent;
288: Dimension viewportSize = viewport.getMinimumSize();
289: int width = viewportSize.width;
290: int height = viewportSize.height;
291: if (hsb != null && hsb.isVisible())
292: height += hsb.getMinimumSize().height;
293: if (vsb != null && vsb.isVisible())
294: width += vsb.getMinimumSize().width;
295: if (rowHead != null && rowHead.isVisible())
296: width += rowHead.getMinimumSize().width;
297: if (colHead != null && colHead.isVisible())
298: height += colHead.getMinimumSize().height;
299: Insets i = sc.getInsets();
300: return new Dimension(width + i.left + i.right,
301: height + i.top + i.bottom);
302: }
303:
304:
325: public void layoutContainer(Container parent)
326: {
327:
328:
329: JScrollPane sc = (JScrollPane) parent;
330: JViewport viewport = sc.getViewport();
331: Dimension viewSize = viewport.getViewSize();
332:
333: int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
334: int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
335: Rectangle scrollPaneBounds = SwingUtilities.calculateInnerArea(sc, null);
336:
337: x1 = scrollPaneBounds.x;
338: y1 = scrollPaneBounds.y;
339: x4 = scrollPaneBounds.x + scrollPaneBounds.width;
340: y4 = scrollPaneBounds.y + scrollPaneBounds.height;
341: if (colHead != null)
342: y2 = y1 + colHead.getPreferredSize().height;
343: else
344: y2 = y1;
345:
346: if (rowHead != null)
347: x2 = x1 + rowHead.getPreferredSize().width;
348: else
349: x2 = x1;
350:
351: int vsbPolicy = sc.getVerticalScrollBarPolicy();
352: int hsbPolicy = sc.getHorizontalScrollBarPolicy();
353:
354: boolean showVsb =
355: (vsb != null)
356: && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
357: || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
358: && viewSize.height > (y4 - y2)));
359: boolean showHsb =
360: (hsb != null)
361: && ((hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS)
362: || (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED
363: && viewSize.width > (x4 - x2)));
364:
365: if (!showVsb)
366: x3 = x4;
367: else
368: x3 = x4 - vsb.getPreferredSize().width;
369:
370: if (!showHsb)
371: y3 = y4;
372: else
373: y3 = y4 - hsb.getPreferredSize().height;
374:
375:
376: if (viewport != null)
377: viewport.setBounds(new Rectangle(x2, y2, x3 - x2, y3 - y2));
378:
379: if (colHead != null)
380: colHead.setBounds(new Rectangle(x2, y1, x3 - x2, y2 - y1));
381:
382: if (rowHead != null)
383: rowHead.setBounds(new Rectangle(x1, y2, x2 - x1, y3 - y2));
384:
385: if (showVsb)
386: {
387: vsb.setVisible(true);
388: vsb.setBounds(new Rectangle(x3, y2, x4 - x3, y3 - y2));
389: }
390: else if (vsb != null)
391: vsb.setVisible(false);
392:
393: if (showHsb)
394: {
395: hsb.setVisible(true);
396: hsb.setBounds(new Rectangle(x2, y3, x3 - x2, y4 - y3));
397: }
398: else if (hsb != null)
399: hsb.setVisible(false);
400:
401: if (upperLeft != null)
402: upperLeft.setBounds(new Rectangle(x1, y1, x2 - x1, y2 - y1));
403:
404: if (upperRight != null)
405: upperRight.setBounds(new Rectangle(x3, y1, x4 - x3, y2 - y1));
406:
407: if (lowerLeft != null)
408: lowerLeft.setBounds(new Rectangle(x1, y3, x2 - x1, y4 - y3));
409:
410: if (lowerRight != null)
411: lowerRight.setBounds(new Rectangle(x3, y3, x4 - x3, y4 - y3));
412: }
413:
414:
423: public Rectangle getViewportBorderBounds(JScrollPane scrollPane) {
424: return null;
425: }
426:
427:
428: }