1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
80: public class JScrollPane
81: extends JComponent
82: implements Accessible, ScrollPaneConstants
83: {
84: private static final long serialVersionUID = 5203525440012340014L;
85:
86: protected JViewport columnHeader;
87: protected JViewport rowHeader;
88:
89: protected Component lowerLeft;
90: protected Component lowerRight;
91: protected Component upperLeft;
92: protected Component upperRight;
93:
94: protected JScrollBar horizontalScrollBar;
95: protected int horizontalScrollBarPolicy;
96: protected JScrollBar verticalScrollBar;
97: protected int verticalScrollBarPolicy;
98:
99: protected JViewport viewport;
100:
101: Border viewportBorder;
102: boolean wheelScrollingEnabled;
103: ChangeListener scrollListener;
104:
105: public JViewport getColumnHeader()
106: {
107: return columnHeader;
108: }
109:
110: public Component getCorner(String key) {
111: if (getComponentOrientation()
112: == ComponentOrientation.LEFT_TO_RIGHT)
113: {
114: if (key == LOWER_LEADING_CORNER)
115: key = LOWER_LEFT_CORNER;
116: else if (key == LOWER_TRAILING_CORNER)
117: key = LOWER_RIGHT_CORNER;
118: else if (key == UPPER_LEADING_CORNER)
119: key = UPPER_LEFT_CORNER;
120: else if (key == UPPER_TRAILING_CORNER)
121: key = UPPER_RIGHT_CORNER;
122: }
123: else if (getComponentOrientation()
124: == ComponentOrientation.RIGHT_TO_LEFT)
125: {
126: if (key == LOWER_LEADING_CORNER)
127: key = LOWER_RIGHT_CORNER;
128: else if (key == LOWER_TRAILING_CORNER)
129: key = LOWER_LEFT_CORNER;
130: else if (key == UPPER_LEADING_CORNER)
131: key = UPPER_RIGHT_CORNER;
132: else if (key == UPPER_TRAILING_CORNER)
133: key = UPPER_LEFT_CORNER;
134: }
135:
136: if (key == LOWER_RIGHT_CORNER)
137: return lowerRight;
138: else if (key == UPPER_RIGHT_CORNER)
139: return upperRight;
140: else if (key == LOWER_LEFT_CORNER)
141: return lowerLeft;
142: else if (key == UPPER_LEFT_CORNER)
143: return upperLeft;
144: return null;
145: }
146:
147: public JScrollBar getHorizontalScrollBar()
148: {
149: return horizontalScrollBar;
150: }
151:
152: public int getHorizontalScrollBarPolicy()
153: {
154: return horizontalScrollBarPolicy;
155: }
156:
157: public JViewport getRowHeader()
158: {
159: return rowHeader;
160: }
161:
162: public JScrollBar getVerticalScrollBar()
163: {
164: return verticalScrollBar;
165: }
166:
167: public int getVerticalScrollBarPolicy()
168: {
169: return verticalScrollBarPolicy;
170: }
171:
172: public JViewport getViewport()
173: {
174: return viewport;
175: }
176:
177: public Border getViewportBorder()
178: {
179: return viewportBorder;
180: }
181:
182: public Rectangle getViewportBorderBounds()
183: {
184: if (viewportBorder == null)
185: {
186: if (getViewport() == null)
187: return new Rectangle(0,0,0,0);
188: else
189: return getViewport().getBounds();
190: }
191: else
192: {
193: Insets i = viewportBorder.getBorderInsets(getViewport());
194: if (getViewport() == null)
195: return new Rectangle(0,0,
196: i.left+i.right, i.top+i.bottom);
197: else
198: {
199: Rectangle b = getViewport().getBounds();
200: return new Rectangle(b.x - i.left,
201: b.y - i.top,
202: b.width + i.left + i.right,
203: b.height + i.top + i.bottom);
204: }
205: }
206: }
207:
208: public boolean isWheelScrollingEnabled()
209: {
210: return wheelScrollingEnabled;
211: }
212:
213:
214:
215: private void sync()
216: {
217: LayoutManager m = super.getLayout();
218: if (m != null && m instanceof ScrollPaneLayout)
219: {
220: ScrollPaneLayout sl = (ScrollPaneLayout) m;
221: sl.syncWithScrollPane(this);
222: }
223: }
224:
225: private void removeNonNull(Component c)
226: {
227: if (c != null)
228: remove(c);
229: }
230:
231: private void addNonNull(Component c)
232: {
233: if (c != null)
234: add(c);
235: }
236:
237: public void setComponentOrientation(ComponentOrientation co)
238: {
239: ComponentOrientation old = super.getComponentOrientation();
240: super.setComponentOrientation(co);
241: firePropertyChange("componentOrientation", old, co);
242: sync();
243: }
244:
245: public void setColumnHeader(JViewport h)
246: {
247: if (columnHeader == h)
248: return;
249:
250: JViewport old = columnHeader;
251: removeNonNull(old);
252: columnHeader = h;
253: addNonNull(h);
254: firePropertyChange("columnHeader", old, h);
255: sync();
256: }
257:
258: public void setColumnHeaderView(Component c)
259: {
260: if (columnHeader == null)
261: setColumnHeader(createViewport());
262: columnHeader.setView(c);
263: sync();
264: }
265:
266: public void setCorner(String key, Component c)
267: {
268: if (getComponentOrientation()
269: == ComponentOrientation.LEFT_TO_RIGHT)
270: {
271: if (key == LOWER_LEADING_CORNER)
272: key = LOWER_LEFT_CORNER;
273: else if (key == LOWER_TRAILING_CORNER)
274: key = LOWER_RIGHT_CORNER;
275: else if (key == UPPER_LEADING_CORNER)
276: key = UPPER_LEFT_CORNER;
277: else if (key == UPPER_TRAILING_CORNER)
278: key = UPPER_RIGHT_CORNER;
279: }
280: else if (getComponentOrientation()
281: == ComponentOrientation.RIGHT_TO_LEFT)
282: {
283: if (key == LOWER_LEADING_CORNER)
284: key = LOWER_RIGHT_CORNER;
285: else if (key == LOWER_TRAILING_CORNER)
286: key = LOWER_LEFT_CORNER;
287: else if (key == UPPER_LEADING_CORNER)
288: key = UPPER_RIGHT_CORNER;
289: else if (key == UPPER_TRAILING_CORNER)
290: key = UPPER_LEFT_CORNER;
291: }
292:
293: if (key == LOWER_RIGHT_CORNER)
294: {
295: removeNonNull(lowerRight);
296: lowerRight = c;
297: addNonNull(c);
298: }
299: else if (key == UPPER_RIGHT_CORNER)
300: {
301: removeNonNull(upperRight);
302: upperRight = c;
303: addNonNull(c);
304: }
305: else if (key == LOWER_LEFT_CORNER)
306: {
307: removeNonNull(lowerLeft);
308: lowerLeft = c;
309: addNonNull(c);
310: }
311: else if (key == UPPER_LEFT_CORNER)
312: {
313: removeNonNull(upperLeft);
314: upperLeft = c;
315: addNonNull(c);
316: }
317: else
318: throw new IllegalArgumentException("unknown corner " + key);
319: sync();
320: }
321:
322: public void setHorizontalScrollBar(JScrollBar h)
323: {
324: if (horizontalScrollBar == h)
325: return;
326:
327: JScrollBar old = horizontalScrollBar;
328: removeNonNull(old);
329: horizontalScrollBar = h;
330: addNonNull(h);
331: firePropertyChange("horizontalScrollBar", old, h);
332: sync();
333:
334: if (old != null)
335: {
336: BoundedRangeModel model = old.getModel();
337: if (model != null)
338: model.removeChangeListener(scrollListener);
339: }
340: if (h != null)
341: {
342: BoundedRangeModel model = h.getModel();
343: if (model != null)
344: model.addChangeListener(scrollListener);
345: }
346: }
347:
348: public void setHorizontalScrollBarPolicy(int h)
349: {
350: if (horizontalScrollBarPolicy == h)
351: return;
352:
353: if (h != HORIZONTAL_SCROLLBAR_AS_NEEDED
354: && h != HORIZONTAL_SCROLLBAR_NEVER
355: && h != HORIZONTAL_SCROLLBAR_ALWAYS)
356: throw new IllegalArgumentException("unknown horizontal scrollbar policy");
357:
358: int old = horizontalScrollBarPolicy;
359: horizontalScrollBarPolicy = h;
360: firePropertyChange("horizontalScrollBarPolicy", old, h);
361: sync();
362: }
363:
364: public void setLayout(LayoutManager l)
365: {
366: LayoutManager old = super.getLayout();
367: ScrollPaneLayout tmp = (ScrollPaneLayout) l;
368: super.setLayout(l);
369: tmp.syncWithScrollPane(this);
370: firePropertyChange("layout", old, l);
371: sync();
372: }
373:
374: public void setRowHeader(JViewport v)
375: {
376: if (rowHeader == v)
377: return;
378:
379: JViewport old = rowHeader;
380: removeNonNull(old);
381: rowHeader = v;
382: addNonNull(v);
383: firePropertyChange("rowHeader", old, v);
384: sync();
385: }
386:
387: public void setRowHeaderView(Component c)
388: {
389: if (rowHeader == null)
390: setRowHeader(createViewport());
391: rowHeader.setView(c);
392: sync();
393: }
394:
395: public void setVerticalScrollBar(JScrollBar v)
396: {
397: if (verticalScrollBar == v)
398: return;
399:
400: JScrollBar old = verticalScrollBar;
401: removeNonNull(old);
402: verticalScrollBar = v;
403: addNonNull(v);
404: firePropertyChange("verticalScrollBar", old, v);
405: sync();
406:
407: if (old != null)
408: {
409: BoundedRangeModel model = old.getModel();
410: if (model != null)
411: model.removeChangeListener(scrollListener);
412: }
413: if (v != null)
414: {
415: BoundedRangeModel model = v.getModel();
416: if (model != null)
417: model.addChangeListener(scrollListener);
418: }
419: }
420:
421: public void setVerticalScrollBarPolicy(int v)
422: {
423: if (verticalScrollBarPolicy == v)
424: return;
425:
426: if (v != VERTICAL_SCROLLBAR_AS_NEEDED
427: && v != VERTICAL_SCROLLBAR_NEVER
428: && v != VERTICAL_SCROLLBAR_ALWAYS)
429: throw new IllegalArgumentException("unknown vertical scrollbar policy");
430:
431: int old = verticalScrollBarPolicy;
432: verticalScrollBarPolicy = v;
433: firePropertyChange("verticalScrollBarPolicy", old, v);
434: sync();
435: }
436:
437: public void setWheelScrollingEnabled(boolean b)
438: {
439: if (wheelScrollingEnabled == b)
440: return;
441:
442: boolean old = wheelScrollingEnabled;
443: wheelScrollingEnabled = b;
444: firePropertyChange("wheelScrollingEnabled", old, b);
445: sync();
446: }
447:
448: public void setViewport(JViewport v)
449: {
450: if (viewport == v)
451: return;
452:
453: JViewport old = viewport;
454: removeNonNull(old);
455: if (old != null)
456: old.removeChangeListener(scrollListener);
457: viewport = v;
458: if (v != null)
459: v.addChangeListener(scrollListener);
460: addNonNull(v);
461: revalidate();
462: repaint();
463: firePropertyChange("viewport", old, v);
464: sync();
465: }
466:
467: public void setViewportBorder(Border b)
468: {
469: if (viewportBorder == b)
470: return;
471:
472: Border old = viewportBorder;
473: viewportBorder = b;
474: firePropertyChange("viewportBorder", old, b);
475: sync();
476: }
477:
478: public void setViewportView(Component view)
479: {
480: if (getViewport() == null)
481: {
482: setViewport(createViewport());
483: }
484:
485: if (view != null)
486: {
487: getViewport().setView(view);
488: }
489: sync();
490: }
491:
492: public boolean isValidateRoot()
493: {
494: return true;
495: }
496:
497: ChangeListener createScrollListener()
498: {
499: return new ChangeListener()
500: {
501:
502: public void stateChanged(ChangeEvent event)
503: {
504: JScrollBar vsb = JScrollPane.this.getVerticalScrollBar();
505: JScrollBar hsb = JScrollPane.this.getHorizontalScrollBar();
506: JViewport vp = JScrollPane.this.getViewport();
507:
508: if (vp != null && event.getSource() == vp)
509: {
510:
511:
512:
513: Rectangle vr = vp.getViewRect();
514: Dimension vs = vp.getViewSize();
515: if (vsb != null
516: && (vsb.getMinimum() != 0
517: || vsb.getMaximum() != vs.height
518: || vsb.getValue() != vr.y
519: || vsb.getVisibleAmount() != vr.height))
520: vsb.setValues(vr.y, vr.height, 0, vs.height);
521:
522: if (hsb != null
523: && (hsb.getMinimum() != 0
524: || hsb.getMaximum() != vs.width
525: || hsb.getValue() != vr.width
526: || hsb.getVisibleAmount() != vr.height))
527: hsb.setValues(vr.x, vr.width, 0, vs.width);
528: }
529: else
530: {
531:
532:
533:
534:
535:
536: int xpos = 0;
537: int ypos = 0;
538:
539: if (vsb != null)
540: ypos = vsb.getValue();
541:
542: if (hsb != null)
543: xpos = hsb.getValue();
544:
545: Point pt = new Point(xpos, ypos);
546:
547: if (vp != null
548: && vp.getViewPosition() != pt)
549: vp.setViewPosition(pt);
550:
551: pt.x = 0;
552:
553: if (rowHeader != null
554: && rowHeader.getViewPosition() != pt)
555: rowHeader.setViewPosition(pt);
556:
557: pt.x = xpos;
558: pt.y = 0;
559:
560: if (columnHeader != null
561: && columnHeader.getViewPosition() != pt)
562: columnHeader.setViewPosition(pt);
563:
564: }
565: }
566: };
567: }
568:
569:
570:
577: public JScrollPane()
578: {
579: this(null);
580: }
581:
582:
589: public JScrollPane(Component view)
590: {
591: this(view,
592: VERTICAL_SCROLLBAR_AS_NEEDED,
593: HORIZONTAL_SCROLLBAR_AS_NEEDED);
594: }
595:
596:
610: public JScrollPane(int vsbPolicy, int hsbPolicy)
611: {
612: this(null, vsbPolicy, hsbPolicy);
613: }
614:
615:
630: public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
631: {
632: scrollListener = createScrollListener();
633: setVerticalScrollBarPolicy(vsbPolicy);
634: setVerticalScrollBar(createVerticalScrollBar());
635: setHorizontalScrollBarPolicy(hsbPolicy);
636: setHorizontalScrollBar(createHorizontalScrollBar());
637: viewport = createViewport();
638: if (view != null)
639: getViewport().setView(view);
640: viewport.addChangeListener(scrollListener);
641: add(viewport,0);
642: setLayout(new ScrollPaneLayout());
643: setOpaque(false);
644: updateUI();
645: }
646:
647:
648: public JScrollBar createHorizontalScrollBar()
649: {
650: return new ScrollBar(SwingConstants.HORIZONTAL);
651: }
652:
653: public JScrollBar createVerticalScrollBar()
654: {
655: return new ScrollBar(SwingConstants.VERTICAL);
656: }
657:
658: protected JViewport createViewport()
659: {
660: return new JViewport();
661: }
662:
663: public String getUIClassID()
664: {
665: return "ScrollPaneUI";
666: }
667:
668: public void updateUI()
669: {
670: ScrollPaneUI b = (ScrollPaneUI)UIManager.getUI(this);
671: setUI(b);
672: }
673:
674:
679: public ScrollPaneUI getUI()
680: {
681: return (ScrollPaneUI) ui;
682: }
683:
684:
689: public void setUI(ScrollPaneUI ui)
690: {
691: super.setUI(ui);
692: }
693:
694: protected class ScrollBar
695: extends JScrollBar
696: implements UIResource
697: {
698: private static final long serialVersionUID = -42032395320987283L;
699:
700: public ScrollBar(int orientation)
701: {
702: super(orientation);
703: }
704:
705: public int getBlockIncrement(int direction)
706: {
707: Component view = JScrollPane.this.getViewport().getView();
708: if (view == null || (! (view instanceof Scrollable)))
709: return super.getBlockIncrement(direction);
710: else
711: {
712: Scrollable s = (Scrollable) view;
713: return s.getScrollableBlockIncrement(JScrollPane.this.getViewport().getViewRect(),
714: this.getOrientation(),
715: direction);
716: }
717: }
718:
719: public int getUnitIncrement(int direction)
720: {
721: Component view = JScrollPane.this.getViewport().getView();
722: if (view == null || (! (view instanceof Scrollable)))
723: return super.getUnitIncrement(direction);
724: else
725: {
726: Scrollable s = (Scrollable) view;
727: return s.getScrollableUnitIncrement(JScrollPane.this.getViewport().getViewRect(),
728: this.getOrientation(),
729: direction);
730: }
731: }
732: }
733: }