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: import ;
56:
57:
81: public class JScrollPane extends JComponent
82: implements Accessible, ScrollPaneConstants
83: {
84:
89: protected class AccessibleJScrollPane extends AccessibleJComponent
90: implements ChangeListener, PropertyChangeListener
91: {
92:
93:
96: protected JViewport viewPort;
97:
98:
104: public AccessibleJScrollPane()
105: {
106: viewPort = getViewport();
107: viewPort.addChangeListener(this);
108: viewPort.addPropertyChangeListener(this);
109: }
110:
111:
116: public void stateChanged(ChangeEvent event)
117: {
118:
119: }
120:
121:
126: public void propertyChange(PropertyChangeEvent e)
127: {
128:
129: }
130:
131:
138: public void resetViewPort()
139: {
140: viewPort.removeChangeListener(this);
141: viewPort.removePropertyChangeListener(this);
142: viewPort = getViewport();
143: viewPort.addChangeListener(this);
144: viewPort.addPropertyChangeListener(this);
145: }
146: }
147:
148: private static final long serialVersionUID = 5203525440012340014L;
149:
150: protected JViewport columnHeader;
151: protected JViewport rowHeader;
152:
153: protected Component lowerLeft;
154: protected Component lowerRight;
155: protected Component upperLeft;
156: protected Component upperRight;
157:
158: protected JScrollBar horizontalScrollBar;
159: protected int horizontalScrollBarPolicy;
160: protected JScrollBar verticalScrollBar;
161: protected int verticalScrollBarPolicy;
162:
163: protected JViewport viewport;
164:
165: private Border viewportBorder;
166:
167: private boolean wheelScrollingEnabled;
168:
169: public JViewport getColumnHeader()
170: {
171: return columnHeader;
172: }
173:
174: public Component getCorner(String key)
175: {
176: if (getComponentOrientation()
177: == ComponentOrientation.LEFT_TO_RIGHT)
178: {
179: if (key == LOWER_LEADING_CORNER)
180: key = LOWER_LEFT_CORNER;
181: else if (key == LOWER_TRAILING_CORNER)
182: key = LOWER_RIGHT_CORNER;
183: else if (key == UPPER_LEADING_CORNER)
184: key = UPPER_LEFT_CORNER;
185: else if (key == UPPER_TRAILING_CORNER)
186: key = UPPER_RIGHT_CORNER;
187: }
188: else if (getComponentOrientation()
189: == ComponentOrientation.RIGHT_TO_LEFT)
190: {
191: if (key == LOWER_LEADING_CORNER)
192: key = LOWER_RIGHT_CORNER;
193: else if (key == LOWER_TRAILING_CORNER)
194: key = LOWER_LEFT_CORNER;
195: else if (key == UPPER_LEADING_CORNER)
196: key = UPPER_RIGHT_CORNER;
197: else if (key == UPPER_TRAILING_CORNER)
198: key = UPPER_LEFT_CORNER;
199: }
200:
201: if (key == LOWER_RIGHT_CORNER)
202: return lowerRight;
203: else if (key == UPPER_RIGHT_CORNER)
204: return upperRight;
205: else if (key == LOWER_LEFT_CORNER)
206: return lowerLeft;
207: else if (key == UPPER_LEFT_CORNER)
208: return upperLeft;
209: return null;
210: }
211:
212: public JScrollBar getHorizontalScrollBar()
213: {
214: return horizontalScrollBar;
215: }
216:
217: public int getHorizontalScrollBarPolicy()
218: {
219: return horizontalScrollBarPolicy;
220: }
221:
222: public JViewport getRowHeader()
223: {
224: return rowHeader;
225: }
226:
227: public JScrollBar getVerticalScrollBar()
228: {
229: return verticalScrollBar;
230: }
231:
232: public int getVerticalScrollBarPolicy()
233: {
234: return verticalScrollBarPolicy;
235: }
236:
237: public JViewport getViewport()
238: {
239: return viewport;
240: }
241:
242: public Border getViewportBorder()
243: {
244: return viewportBorder;
245: }
246:
247: public Rectangle getViewportBorderBounds()
248: {
249: if (viewportBorder == null)
250: {
251: if (getViewport() == null)
252: return new Rectangle(0, 0, 0, 0);
253: else
254: return getViewport().getBounds();
255: }
256: else
257: {
258: Insets i = viewportBorder.getBorderInsets(getViewport());
259: if (getViewport() == null)
260: return new Rectangle(0, 0, i.left + i.right, i.top + i.bottom);
261: else
262: {
263: Rectangle b = getViewport().getBounds();
264: return new Rectangle(b.x - i.left,
265: b.y - i.top,
266: b.width + i.left + i.right,
267: b.height + i.top + i.bottom);
268: }
269: }
270: }
271:
272: public boolean isWheelScrollingEnabled()
273: {
274: return wheelScrollingEnabled;
275: }
276:
277:
278:
279: private void sync()
280: {
281: LayoutManager m = super.getLayout();
282: if (m != null && m instanceof ScrollPaneLayout)
283: {
284: ScrollPaneLayout sl = (ScrollPaneLayout) m;
285: sl.syncWithScrollPane(this);
286: }
287: }
288:
289: private void removeNonNull(Component c)
290: {
291: if (c != null)
292: remove(c);
293: }
294:
295: private void addNonNull(Component c, Object constraints)
296: {
297: if (c != null)
298: add(c, constraints);
299: }
300:
301: public void setComponentOrientation(ComponentOrientation co)
302: {
303: ComponentOrientation old = super.getComponentOrientation();
304: super.setComponentOrientation(co);
305: firePropertyChange("componentOrientation", old, co);
306: sync();
307: }
308:
309: public void setColumnHeader(JViewport h)
310: {
311: if (columnHeader == h)
312: return;
313:
314: JViewport old = columnHeader;
315: removeNonNull(old);
316: columnHeader = h;
317: addNonNull(h, JScrollPane.COLUMN_HEADER);
318: firePropertyChange("columnHeader", old, h);
319: sync();
320: }
321:
322: public void setColumnHeaderView(Component c)
323: {
324: if (columnHeader == null)
325: setColumnHeader(createViewport());
326: columnHeader.setView(c);
327: sync();
328: }
329:
330: public void setCorner(String key, Component c)
331: {
332: if (getComponentOrientation()
333: == ComponentOrientation.LEFT_TO_RIGHT)
334: {
335: if (key == LOWER_LEADING_CORNER)
336: key = LOWER_LEFT_CORNER;
337: else if (key == LOWER_TRAILING_CORNER)
338: key = LOWER_RIGHT_CORNER;
339: else if (key == UPPER_LEADING_CORNER)
340: key = UPPER_LEFT_CORNER;
341: else if (key == UPPER_TRAILING_CORNER)
342: key = UPPER_RIGHT_CORNER;
343: }
344: else if (getComponentOrientation()
345: == ComponentOrientation.RIGHT_TO_LEFT)
346: {
347: if (key == LOWER_LEADING_CORNER)
348: key = LOWER_RIGHT_CORNER;
349: else if (key == LOWER_TRAILING_CORNER)
350: key = LOWER_LEFT_CORNER;
351: else if (key == UPPER_LEADING_CORNER)
352: key = UPPER_RIGHT_CORNER;
353: else if (key == UPPER_TRAILING_CORNER)
354: key = UPPER_LEFT_CORNER;
355: }
356:
357: if (key == LOWER_RIGHT_CORNER)
358: {
359: removeNonNull(lowerRight);
360: lowerRight = c;
361: addNonNull(c, JScrollPane.LOWER_RIGHT_CORNER);
362: }
363: else if (key == UPPER_RIGHT_CORNER)
364: {
365: removeNonNull(upperRight);
366: upperRight = c;
367: addNonNull(c, JScrollPane.UPPER_RIGHT_CORNER);
368: }
369: else if (key == LOWER_LEFT_CORNER)
370: {
371: removeNonNull(lowerLeft);
372: lowerLeft = c;
373: addNonNull(c, JScrollPane.LOWER_LEFT_CORNER);
374: }
375: else if (key == UPPER_LEFT_CORNER)
376: {
377: removeNonNull(upperLeft);
378: upperLeft = c;
379: addNonNull(c, JScrollPane.UPPER_LEFT_CORNER);
380: }
381: else
382: throw new IllegalArgumentException("unknown corner " + key);
383: sync();
384: }
385:
386: public void setHorizontalScrollBar(JScrollBar h)
387: {
388: if (horizontalScrollBar == h)
389: return;
390:
391: JScrollBar old = horizontalScrollBar;
392: removeNonNull(old);
393: horizontalScrollBar = h;
394: addNonNull(h, JScrollPane.HORIZONTAL_SCROLLBAR);
395: firePropertyChange("horizontalScrollBar", old, h);
396: sync();
397:
398: }
399:
400: public void setHorizontalScrollBarPolicy(int h)
401: {
402: if (horizontalScrollBarPolicy == h)
403: return;
404:
405: if (h != HORIZONTAL_SCROLLBAR_AS_NEEDED
406: && h != HORIZONTAL_SCROLLBAR_NEVER
407: && h != HORIZONTAL_SCROLLBAR_ALWAYS)
408: throw new IllegalArgumentException("unknown horizontal scrollbar policy");
409:
410: int old = horizontalScrollBarPolicy;
411: horizontalScrollBarPolicy = h;
412: firePropertyChange("horizontalScrollBarPolicy", old, h);
413: sync();
414: revalidate();
415: }
416:
417: public void setLayout(LayoutManager l)
418: {
419: LayoutManager old = super.getLayout();
420: ScrollPaneLayout tmp = (ScrollPaneLayout) l;
421: super.setLayout(l);
422: tmp.syncWithScrollPane(this);
423: firePropertyChange("layout", old, l);
424: sync();
425: }
426:
427: public void setRowHeader(JViewport v)
428: {
429: if (rowHeader == v)
430: return;
431:
432: JViewport old = rowHeader;
433: removeNonNull(old);
434: rowHeader = v;
435: addNonNull(v, JScrollPane.ROW_HEADER);
436: firePropertyChange("rowHeader", old, v);
437: sync();
438: }
439:
440: public void setRowHeaderView(Component c)
441: {
442: if (rowHeader == null)
443: setRowHeader(createViewport());
444: rowHeader.setView(c);
445: sync();
446: }
447:
448: public void setVerticalScrollBar(JScrollBar v)
449: {
450: if (verticalScrollBar == v)
451: return;
452:
453: JScrollBar old = verticalScrollBar;
454: removeNonNull(old);
455: verticalScrollBar = v;
456: addNonNull(v, JScrollPane.VERTICAL_SCROLLBAR);
457: firePropertyChange("verticalScrollBar", old, v);
458: sync();
459: }
460:
461: public void setVerticalScrollBarPolicy(int v)
462: {
463: if (verticalScrollBarPolicy == v)
464: return;
465:
466: if (v != VERTICAL_SCROLLBAR_AS_NEEDED
467: && v != VERTICAL_SCROLLBAR_NEVER
468: && v != VERTICAL_SCROLLBAR_ALWAYS)
469: throw new IllegalArgumentException("unknown vertical scrollbar policy");
470:
471: int old = verticalScrollBarPolicy;
472: verticalScrollBarPolicy = v;
473: firePropertyChange("verticalScrollBarPolicy", old, v);
474: sync();
475: revalidate();
476: }
477:
478: public void setWheelScrollingEnabled(boolean b)
479: {
480: if (wheelScrollingEnabled == b)
481: return;
482:
483: boolean old = wheelScrollingEnabled;
484: wheelScrollingEnabled = b;
485: firePropertyChange("wheelScrollingEnabled", old, b);
486: sync();
487: }
488:
489: public void setViewport(JViewport v)
490: {
491: if (viewport == v)
492: return;
493:
494: JViewport old = viewport;
495: removeNonNull(old);
496: viewport = v;
497: addNonNull(v, JScrollPane.VIEWPORT);
498: revalidate();
499: repaint();
500: firePropertyChange("viewport", old, v);
501: sync();
502: if (accessibleContext != null)
503: {
504: AccessibleJScrollPane asp = (AccessibleJScrollPane) accessibleContext;
505: asp.resetViewPort();
506: }
507: }
508:
509: public void setViewportBorder(Border b)
510: {
511: if (viewportBorder == b)
512: return;
513:
514: Border old = viewportBorder;
515: viewportBorder = b;
516: firePropertyChange("viewportBorder", old, b);
517: sync();
518: }
519:
520: public void setViewportView(Component view)
521: {
522: if (getViewport() == null)
523: {
524: setViewport(createViewport());
525: }
526:
527: if (view != null)
528: {
529: getViewport().setView(view);
530: }
531: sync();
532: }
533:
534: public boolean isValidateRoot()
535: {
536: return true;
537: }
538:
539:
544: public JScrollPane()
545: {
546: this(null);
547: }
548:
549:
556: public JScrollPane(Component view)
557: {
558: this(view,
559: VERTICAL_SCROLLBAR_AS_NEEDED,
560: HORIZONTAL_SCROLLBAR_AS_NEEDED);
561: }
562:
563:
577: public JScrollPane(int vsbPolicy, int hsbPolicy)
578: {
579: this(null, vsbPolicy, hsbPolicy);
580: }
581:
582:
597: public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
598: {
599: wheelScrollingEnabled = true;
600: setVerticalScrollBarPolicy(vsbPolicy);
601: setVerticalScrollBar(createVerticalScrollBar());
602: setHorizontalScrollBarPolicy(hsbPolicy);
603: setHorizontalScrollBar(createHorizontalScrollBar());
604: viewport = createViewport();
605: if (view != null)
606: getViewport().setView(view);
607: add(viewport,0);
608: setLayout(new ScrollPaneLayout());
609: setOpaque(false);
610: updateUI();
611: }
612:
613:
614: public JScrollBar createHorizontalScrollBar()
615: {
616: return new ScrollBar(SwingConstants.HORIZONTAL);
617: }
618:
619: public JScrollBar createVerticalScrollBar()
620: {
621: return new ScrollBar(SwingConstants.VERTICAL);
622: }
623:
624: protected JViewport createViewport()
625: {
626: return new JViewport();
627: }
628:
629: public String getUIClassID()
630: {
631: return "ScrollPaneUI";
632: }
633:
634: public void updateUI()
635: {
636: setUI((ScrollPaneUI) UIManager.getUI(this));
637: }
638:
639:
644: public ScrollPaneUI getUI()
645: {
646: return (ScrollPaneUI) ui;
647: }
648:
649:
654: public void setUI(ScrollPaneUI ui)
655: {
656: super.setUI(ui);
657: }
658:
659: protected class ScrollBar
660: extends JScrollBar
661: implements UIResource
662: {
663: private static final long serialVersionUID = -42032395320987283L;
664:
665: public ScrollBar(int orientation)
666: {
667: super(orientation);
668: }
669:
670: public int getBlockIncrement(int direction)
671: {
672: Component view = JScrollPane.this.getViewport().getView();
673: if (view == null || (! (view instanceof Scrollable)))
674: return super.getBlockIncrement(direction);
675: else
676: {
677: Scrollable s = (Scrollable) view;
678: return s.getScrollableBlockIncrement(JScrollPane.this.getViewport().getViewRect(),
679: this.getOrientation(),
680: direction);
681: }
682: }
683:
684: public int getUnitIncrement(int direction)
685: {
686: Component view = JScrollPane.this.getViewport().getView();
687: if (view == null || (! (view instanceof Scrollable)))
688: return super.getUnitIncrement(direction);
689: else
690: {
691: Scrollable s = (Scrollable) view;
692: return s.getScrollableUnitIncrement(JScrollPane.this.getViewport().getViewRect(),
693: this.getOrientation(),
694: direction);
695: }
696: }
697: }
698:
699:
706: public AccessibleContext getAccessibleContext()
707: {
708: if (accessibleContext == null)
709: accessibleContext = new AccessibleJScrollPane();
710: return accessibleContext;
711: }
712: }