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: Insets i = sc.getInsets();
289: Dimension viewportMinSize = sc.getViewport().getMinimumSize();
290:
291: int width = i.left + i.right + viewportMinSize.width;
292: if (sc.getVerticalScrollBarPolicy()
293: != JScrollPane.VERTICAL_SCROLLBAR_NEVER)
294: width += sc.getVerticalScrollBar().getMinimumSize().width;
295:
296: int height = i.top + i.bottom + viewportMinSize.height;
297: if (sc.getHorizontalScrollBarPolicy()
298: != JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)
299: height += sc.getHorizontalScrollBar().getMinimumSize().height;
300:
301: return new Dimension(width, height);
302: }
303:
304:
325: public void layoutContainer(Container parent)
326: {
327:
328:
329: JScrollPane sc = (JScrollPane) parent;
330: JViewport viewport = sc.getViewport();
331: Component view = viewport.getView();
332:
333:
334: if (view == null)
335: return;
336:
337: Dimension viewSize = viewport.getView().getPreferredSize();
338:
339: int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
340: int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
341: Rectangle scrollPaneBounds = SwingUtilities.calculateInnerArea(sc, null);
342:
343: x1 = scrollPaneBounds.x;
344: y1 = scrollPaneBounds.y;
345: x4 = scrollPaneBounds.x + scrollPaneBounds.width;
346: y4 = scrollPaneBounds.y + scrollPaneBounds.height;
347: if (colHead != null)
348: y2 = y1 + colHead.getPreferredSize().height;
349: else
350: y2 = y1;
351:
352: if (rowHead != null)
353: x2 = x1 + rowHead.getPreferredSize().width;
354: else
355: x2 = x1;
356:
357: int vsbPolicy = sc.getVerticalScrollBarPolicy();
358: int hsbPolicy = sc.getHorizontalScrollBarPolicy();
359:
360: int vsWidth = 0;
361: int hsHeight = 0;
362:
363: boolean showVsb =
364: (vsb != null)
365: && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
366: || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
367: && viewSize.height > (y4 - y2)));
368:
369: if (showVsb)
370: vsWidth = vsb.getPreferredSize().width;
371:
372:
373:
374:
375: boolean showHsb =
376: (hsb != null)
377: && ((hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS)
378: || (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED
379: && viewSize.width > (x4 - x2 - vsWidth)));
380:
381: if (showHsb)
382: hsHeight = hsb.getPreferredSize().height;
383:
384:
385:
386:
387:
388: if (!showVsb)
389: {
390: showVsb =
391: (vsb != null)
392: && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
393: || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED
394: && viewSize.height > (y4 - y2)));
395:
396: if (showVsb)
397: vsWidth = vsb.getPreferredSize().width;
398: }
399:
400: x3 = x4 - vsWidth;
401: y3 = y4 - hsHeight;
402:
403:
404: if (viewport != null)
405: viewport.setBounds(new Rectangle(x2, y2, x3 - x2, y3 - y2));
406:
407: if (colHead != null)
408: colHead.setBounds(new Rectangle(x2, y1, x3 - x2, y2 - y1));
409:
410: if (rowHead != null)
411: rowHead.setBounds(new Rectangle(x1, y2, x2 - x1, y3 - y2));
412:
413: if (showVsb)
414: {
415: vsb.setVisible(true);
416: vsb.setBounds(new Rectangle(x3, y2, x4 - x3, y3 - y2));
417: }
418: else if (vsb != null)
419: vsb.setVisible(false);
420:
421: if (showHsb)
422: {
423: hsb.setVisible(true);
424: hsb.setBounds(new Rectangle(x2, y3, x3 - x2, y4 - y3));
425: }
426: else if (hsb != null)
427: hsb.setVisible(false);
428:
429: if (upperLeft != null)
430: upperLeft.setBounds(new Rectangle(x1, y1, x2 - x1, y2 - y1));
431:
432: if (upperRight != null)
433: upperRight.setBounds(new Rectangle(x3, y1, x4 - x3, y2 - y1));
434:
435: if (lowerLeft != null)
436: lowerLeft.setBounds(new Rectangle(x1, y3, x2 - x1, y4 - y3));
437:
438: if (lowerRight != null)
439: lowerRight.setBounds(new Rectangle(x3, y3, x4 - x3, y4 - y3));
440: }
441:
442:
451: public Rectangle getViewportBorderBounds(JScrollPane scrollPane) {
452: return null;
453: }
454:
455:
456: }