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: {
87: viewport = scrollPane.getViewport();
88: rowHead = scrollPane.getRowHeader();
89: colHead = scrollPane.getColumnHeader();
90: vsb = scrollPane.getVerticalScrollBar();
91: hsb = scrollPane.getHorizontalScrollBar();
92: vsbPolicy = scrollPane.getVerticalScrollBarPolicy();
93: hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();
94: lowerLeft = scrollPane.getCorner(LOWER_LEFT_CORNER);
95: lowerRight = scrollPane.getCorner(LOWER_RIGHT_CORNER);
96: upperLeft = scrollPane.getCorner(UPPER_LEFT_CORNER);
97: upperRight = scrollPane.getCorner(UPPER_RIGHT_CORNER);
98: }
99:
100:
108: protected Component addSingletonComponent(Component oldComponent,
109: Component newComponent)
110: {
111: if (oldComponent != null && oldComponent != newComponent)
112: oldComponent.getParent().remove(oldComponent);
113: return newComponent;
114: }
115:
116:
125: public void addLayoutComponent(String key, Component component)
126: {
127: if (key == VIEWPORT)
128: viewport = (JViewport) component;
129: else if (key == VERTICAL_SCROLLBAR)
130: vsb = (JScrollBar) component;
131: else if (key == HORIZONTAL_SCROLLBAR)
132: hsb = (JScrollBar) component;
133: else if (key == ROW_HEADER)
134: rowHead = (JViewport) component;
135: else if (key == COLUMN_HEADER)
136: colHead = (JViewport) component;
137: else if (key == LOWER_RIGHT_CORNER)
138: lowerRight = component;
139: else if (key == UPPER_RIGHT_CORNER)
140: upperRight = component;
141: else if (key == LOWER_LEFT_CORNER)
142: lowerLeft = component;
143: else if (key == UPPER_LEFT_CORNER)
144: upperLeft = component;
145: else
146: throw new IllegalArgumentException();
147: }
148:
149: public void removeLayoutComponent(Component component)
150: {
151: if (component == viewport)
152: viewport = null;
153: else if (component == vsb)
154: vsb = null;
155: else if (component == hsb)
156: hsb = null;
157: else if (component == rowHead)
158: rowHead = null;
159: else if (component == colHead)
160: colHead = null;
161: else if (component == lowerRight)
162: lowerRight = null;
163: else if (component == upperRight)
164: upperRight = null;
165: else if (component == lowerLeft)
166: lowerLeft = null;
167: else if (component == upperLeft)
168: upperLeft = null;
169: }
170:
171: public int getVerticalScrollBarPolicy()
172: {
173: return vsbPolicy;
174: }
175:
176:
183: public void setVerticalScrollBarPolicy(int policy)
184: {
185: if (policy != VERTICAL_SCROLLBAR_AS_NEEDED &&
186: policy != VERTICAL_SCROLLBAR_NEVER &&
187: policy != VERTICAL_SCROLLBAR_ALWAYS)
188: throw new IllegalArgumentException("Illegal Scrollbar Policy");
189: vsbPolicy = policy;
190: }
191:
192: public int getHorizontalScrollBarPolicy()
193: {
194: return hsbPolicy;
195: }
196:
197:
204: public void setHorizontalScrollBarPolicy(int policy)
205: {
206: if (policy != HORIZONTAL_SCROLLBAR_AS_NEEDED &&
207: policy != HORIZONTAL_SCROLLBAR_NEVER &&
208: policy != HORIZONTAL_SCROLLBAR_ALWAYS)
209: throw new IllegalArgumentException("Illegal Scrollbar Policy");
210: hsbPolicy = policy;
211: }
212:
213: public JViewport getViewport()
214: {
215: return viewport;
216: }
217:
218: public JScrollBar getHorizontalScrollBar()
219: {
220: return hsb;
221: }
222:
223: public JScrollBar getVerticalScrollBar()
224: {
225: return vsb;
226: }
227:
228: public JViewport getRowHeader()
229: {
230: return rowHead;
231: }
232:
233: public JViewport getColumnHeader()
234: {
235: return colHead;
236: }
237:
238:
244: public Component getCorner(String key)
245: {
246: if (key == LOWER_RIGHT_CORNER)
247: return lowerRight;
248: else if (key == UPPER_RIGHT_CORNER)
249: return upperRight;
250: else if (key == LOWER_LEFT_CORNER)
251: return lowerLeft;
252: else if (key == UPPER_LEFT_CORNER)
253: return upperLeft;
254: return null;
255: }
256:
257: public Dimension preferredLayoutSize(Container parent)
258: {
259:
260:
261: JScrollPane sc = (JScrollPane) parent;
262: Dimension viewportSize = viewport.getPreferredSize();
263: Dimension viewSize = viewport.getViewSize();
264: int width = viewportSize.width;
265: int height = viewportSize.height;
266:
267:
268:
269: if (hsb != null && viewSize.width > viewportSize.width)
270: height += hsb.getPreferredSize().height;
271:
272:
273:
274: if (vsb != null && viewSize.height > viewportSize.height)
275: width += vsb.getPreferredSize().width;
276: if (rowHead != null && rowHead.isVisible())
277: width += rowHead.getPreferredSize().width;
278: if (colHead != null && colHead.isVisible())
279: height += colHead.getPreferredSize().height;
280: Insets i = sc.getInsets();
281: return new Dimension(width + i.left + i.right,
282: height + i.left + i.right);
283: }
284:
285: public Dimension minimumLayoutSize(Container parent)
286: {
287:
288:
289: JScrollPane sc = (JScrollPane) parent;
290: Insets i = sc.getInsets();
291: Dimension viewportMinSize = sc.getViewport().getMinimumSize();
292:
293: int width = i.left + i.right + viewportMinSize.width;
294: if (sc.getVerticalScrollBarPolicy()
295: != JScrollPane.VERTICAL_SCROLLBAR_NEVER)
296: width += sc.getVerticalScrollBar().getMinimumSize().width;
297:
298: int height = i.top + i.bottom + viewportMinSize.height;
299: if (sc.getHorizontalScrollBarPolicy()
300: != JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)
301: height += sc.getHorizontalScrollBar().getMinimumSize().height;
302:
303: return new Dimension(width, height);
304: }
305:
306:
327: public void layoutContainer(Container parent)
328: {
329:
330:
331: JScrollPane sc = (JScrollPane) parent;
332: JViewport viewport = sc.getViewport();
333: Component view = viewport.getView();
334:
335:
336: if (view == null)
337: return;
338:
339: Dimension viewSize = viewport.getView().getPreferredSize();
340:
341: int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
342: int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
343: Rectangle scrollPaneBounds = SwingUtilities.calculateInnerArea(sc, null);
344:
345: x1 = scrollPaneBounds.x;
346: y1 = scrollPaneBounds.y;
347: x4 = scrollPaneBounds.x + scrollPaneBounds.width;
348: y4 = scrollPaneBounds.y + scrollPaneBounds.height;
349: if (colHead != null)
350: y2 = y1 + colHead.getPreferredSize().height;
351: else
352: y2 = y1;
353:
354: if (rowHead != null)
355: x2 = x1 + rowHead.getPreferredSize().width;
356: else
357: x2 = x1;
358:
359: int vsbPolicy = sc.getVerticalScrollBarPolicy();
360: int hsbPolicy = sc.getHorizontalScrollBarPolicy();
361:
362: int vsWidth = 0;
363: int hsHeight = 0;
364:
365: boolean showVsb =
366: (vsb != null)
367: && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
368: || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
369: && viewSize.height > (y4 - y2)));
370:
371: if (showVsb)
372: vsWidth = vsb.getPreferredSize().width;
373:
374:
375:
376:
377: boolean showHsb =
378: (hsb != null)
379: && ((hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS)
380: || (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED
381: && viewSize.width > (x4 - x2 - vsWidth)));
382:
383: if (showHsb)
384: hsHeight = hsb.getPreferredSize().height;
385:
386:
387:
388:
389:
390: if (!showVsb)
391: {
392: showVsb =
393: (vsb != null)
394: && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
395: || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
396: && viewSize.height > (y4 - y2)));
397:
398: if (showVsb)
399: vsWidth = vsb.getPreferredSize().width;
400: }
401:
402: x3 = x4 - vsWidth;
403: y3 = y4 - hsHeight;
404:
405:
406: if (viewport != null)
407: viewport.setBounds(new Rectangle(x2, y2, x3 - x2, y3 - y2));
408:
409: if (colHead != null)
410: colHead.setBounds(new Rectangle(x2, y1, x3 - x2, y2 - y1));
411:
412: if (rowHead != null)
413: rowHead.setBounds(new Rectangle(x1, y2, x2 - x1, y3 - y2));
414:
415: if (showVsb)
416: {
417: vsb.setVisible(true);
418: vsb.setBounds(new Rectangle(x3, y2, x4 - x3, y3 - y2));
419: }
420: else if (vsb != null)
421: vsb.setVisible(false);
422:
423: if (showHsb)
424: {
425: hsb.setVisible(true);
426: hsb.setBounds(new Rectangle(x2, y3, x3 - x2, y4 - y3));
427: }
428: else if (hsb != null)
429: hsb.setVisible(false);
430:
431: if (upperLeft != null)
432: upperLeft.setBounds(new Rectangle(x1, y1, x2 - x1, y2 - y1));
433:
434: if (upperRight != null)
435: upperRight.setBounds(new Rectangle(x3, y1, x4 - x3, y2 - y1));
436:
437: if (lowerLeft != null)
438: lowerLeft.setBounds(new Rectangle(x1, y3, x2 - x1, y4 - y3));
439:
440: if (lowerRight != null)
441: lowerRight.setBounds(new Rectangle(x3, y3, x4 - x3, y4 - y3));
442: }
443:
444:
453: public Rectangle getViewportBorderBounds(JScrollPane scrollPane)
454: {
455: return null;
456: }
457:
458:
459: }