KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > ScrollPane


1 /*
2  * @(#)ScrollPane.java 1.95 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.peer.LightweightPeer;
10 import java.awt.peer.ScrollPanePeer;
11 import java.awt.event.*;
12 import javax.accessibility.*;
13 import sun.awt.ScrollPaneWheelScroller;
14 import sun.awt.SunToolkit;
15
16 import java.io.Serializable JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.ObjectOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 /**
22  * A container class which implements automatic horizontal and/or
23  * vertical scrolling for a single child component. The display
24  * policy for the scrollbars can be set to:
25  * <OL>
26  * <LI>as needed: scrollbars created and shown only when needed by scrollpane
27  * <LI>always: scrollbars created and always shown by the scrollpane
28  * <LI>never: scrollbars never created or shown by the scrollpane
29  * </OL>
30  * <P>
31  * The state of the horizontal and vertical scrollbars is represented
32  * by two <code>ScrollPaneAdjustable</code> objects (one for each
33  * dimension) which implement the <code>Adjustable</code> interface.
34  * The API provides methods to access those objects such that the
35  * attributes on the Adjustable object (such as unitIncrement, value,
36  * etc.) can be manipulated.
37  * <P>
38  * Certain adjustable properties (minimum, maximum, blockIncrement,
39  * and visibleAmount) are set internally by the scrollpane in accordance
40  * with the geometry of the scrollpane and its child and these should
41  * not be set by programs using the scrollpane.
42  * <P>
43  * If the scrollbar display policy is defined as "never", then the
44  * scrollpane can still be programmatically scrolled using the
45  * setScrollPosition() method and the scrollpane will move and clip
46  * the child's contents appropriately. This policy is useful if the
47  * program needs to create and manage its own adjustable controls.
48  * <P>
49  * The placement of the scrollbars is controlled by platform-specific
50  * properties set by the user outside of the program.
51  * <P>
52  * The initial size of this container is set to 100x100, but can
53  * be reset using setSize().
54  * <P>
55  * Scrolling with the wheel on a wheel-equipped mouse is enabled by default.
56  * This can be disabled using <code>setWheelScrollingEnabled</code>.
57  * Wheel scrolling can be customized by setting the block and
58  * unit increment of the horizontal and vertical Adjustables.
59  * For information on how mouse wheel events are dispatched, see
60  * the class description for {@link MouseWheelEvent}.
61  * <P>
62  * Insets are used to define any space used by scrollbars and any
63  * borders created by the scroll pane. getInsets() can be used
64  * to get the current value for the insets. If the value of
65  * scrollbarsAlwaysVisible is false, then the value of the insets
66  * will change dynamically depending on whether the scrollbars are
67  * currently visible or not.
68  *
69  * @version 1.95 05/18/04
70  * @author Tom Ball
71  * @author Amy Fowler
72  * @author Tim Prinzing
73  */

74 public class ScrollPane extends Container JavaDoc implements Accessible {
75
76
77     /**
78      * Initialize JNI field and method IDs
79      */

80     private static native void initIDs();
81
82     static {
83         /* ensure that the necessary native libraries are loaded */
84     Toolkit.loadLibraries();
85         if (!GraphicsEnvironment.isHeadless()) {
86             initIDs();
87         }
88     }
89
90     /**
91      * Specifies that horizontal/vertical scrollbar should be shown
92      * only when the size of the child exceeds the size of the scrollpane
93      * in the horizontal/vertical dimension.
94      */

95     public static final int SCROLLBARS_AS_NEEDED = 0;
96
97     /**
98      * Specifies that horizontal/vertical scrollbars should always be
99      * shown regardless of the respective sizes of the scrollpane and child.
100      */

101     public static final int SCROLLBARS_ALWAYS = 1;
102
103     /**
104      * Specifies that horizontal/vertical scrollbars should never be shown
105      * regardless of the respective sizes of the scrollpane and child.
106      */

107     public static final int SCROLLBARS_NEVER = 2;
108
109     /**
110      * There are 3 ways in which a scroll bar can be displayed.
111      * This integer will represent one of these 3 displays -
112      * (SCROLLBARS_ALWAYS, SCROLLBARS_AS_NEEDED, SCROLLBARS_NEVER)
113      *
114      * @serial
115      * @see #getScrollbarDisplayPolicy
116      */

117     private int scrollbarDisplayPolicy;
118
119     /**
120      * An adjustable vertical scrollbar.
121      * It is important to note that you must <em>NOT</em> call 3
122      * <code>Adjustable</code> methods, namely:
123      * <code>setMinimum()</code>, <code>setMaximum()</code>,
124      * <code>setVisibleAmount()</code>.
125      *
126      * @serial
127      * @see #getVAdjustable
128      */

129     private ScrollPaneAdjustable JavaDoc vAdjustable;
130
131     /**
132      * An adjustable horizontal scrollbar.
133      * It is important to note that you must <em>NOT</em> call 3
134      * <code>Adjustable</code> methods, namely:
135      * <code>setMinimum()</code>, <code>setMaximum()</code>,
136      * <code>setVisibleAmount()</code>.
137      *
138      * @serial
139      * @see #getHAdjustable
140      */

141     private ScrollPaneAdjustable JavaDoc hAdjustable;
142
143     private static final String JavaDoc base = "scrollpane";
144     private static int nameCounter = 0;
145
146     private static final boolean defaultWheelScroll = true;
147
148     /**
149      * Indicates whether or not scrolling should take place when a
150      * MouseWheelEvent is received.
151      *
152      * @serial
153      * @since 1.4
154      */

155     private boolean wheelScrollingEnabled = defaultWheelScroll;
156
157     /*
158      * JDK 1.1 serialVersionUID
159      */

160     private static final long serialVersionUID = 7956609840827222915L;
161
162     /**
163      * Create a new scrollpane container with a scrollbar display
164      * policy of "as needed".
165      * @throws HeadlessException if GraphicsEnvironment.isHeadless()
166      * returns true
167      * @see java.awt.GraphicsEnvironment#isHeadless
168      */

169     public ScrollPane() throws HeadlessException JavaDoc {
170     this(SCROLLBARS_AS_NEEDED);
171     }
172
173     /**
174      * Create a new scrollpane container.
175      * @param scrollbarDisplayPolicy policy for when scrollbars should be shown
176      * @throws IllegalArgumentException if the specified scrollbar
177      * display policy is invalid
178      * @throws HeadlessException if GraphicsEnvironment.isHeadless()
179      * returns true
180      * @see java.awt.GraphicsEnvironment#isHeadless
181      */

182     public ScrollPane(int scrollbarDisplayPolicy) throws HeadlessException JavaDoc {
183         GraphicsEnvironment.checkHeadless();
184     this.layoutMgr = null;
185     this.width = 100;
186     this.height = 100;
187     switch (scrollbarDisplayPolicy) {
188         case SCROLLBARS_NEVER:
189         case SCROLLBARS_AS_NEEDED:
190         case SCROLLBARS_ALWAYS:
191         this.scrollbarDisplayPolicy = scrollbarDisplayPolicy;
192         break;
193         default:
194         throw new IllegalArgumentException JavaDoc("illegal scrollbar display policy");
195     }
196
197     vAdjustable = new ScrollPaneAdjustable JavaDoc(this, new PeerFixer(this),
198                            Adjustable.VERTICAL);
199     hAdjustable = new ScrollPaneAdjustable JavaDoc(this, new PeerFixer(this),
200                            Adjustable.HORIZONTAL);
201     setWheelScrollingEnabled(defaultWheelScroll);
202     }
203
204     /**
205      * Construct a name for this component. Called by getName() when the
206      * name is null.
207      */

208     String JavaDoc constructComponentName() {
209         synchronized (getClass()) {
210         return base + nameCounter++;
211     }
212     }
213
214     // The scrollpane won't work with a windowless child... it assumes
215
// it is moving a child window around so the windowless child is
216
// wrapped with a window.
217
private void addToPanel(Component JavaDoc comp, Object JavaDoc constraints, int index) {
218         Panel JavaDoc child = new Panel JavaDoc();
219         child.setLayout(new BorderLayout JavaDoc());
220         child.add(comp);
221         super.addImpl(child, constraints, index);
222         validate();
223     }
224
225     /**
226      * Adds the specified component to this scroll pane container.
227      * If the scroll pane has an existing child component, that
228      * component is removed and the new one is added.
229      * @param comp the component to be added
230      * @param constraints not applicable
231      * @param index position of child component (must be <= 0)
232      */

233     protected final void addImpl(Component JavaDoc comp, Object JavaDoc constraints, int index) {
234         synchronized (getTreeLock()) {
235         if (getComponentCount() > 0) {
236         remove(0);
237         }
238         if (index > 0) {
239         throw new IllegalArgumentException JavaDoc("position greater than 0");
240         }
241
242             if (!SunToolkit.isLightweightOrUnknown(comp)) {
243                 super.addImpl(comp, constraints, index);
244             } else {
245                 addToPanel(comp, constraints, index);
246             }
247     }
248     }
249
250     /**
251      * Returns the display policy for the scrollbars.
252      * @return the display policy for the scrollbars
253      */

254     public int getScrollbarDisplayPolicy() {
255         return scrollbarDisplayPolicy;
256     }
257
258     /**
259      * Returns the current size of the scroll pane's view port.
260      * @return the size of the view port in pixels
261      */

262     public Dimension JavaDoc getViewportSize() {
263     Insets JavaDoc i = getInsets();
264     return new Dimension JavaDoc(width - i.right - i.left,
265                  height - i.top - i.bottom);
266     }
267
268     /**
269      * Returns the height that would be occupied by a horizontal
270      * scrollbar, which is independent of whether it is currently
271      * displayed by the scroll pane or not.
272      * @return the height of a horizontal scrollbar in pixels
273      */

274     public int getHScrollbarHeight() {
275     int h = 0;
276     if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) {
277         ScrollPanePeer peer = (ScrollPanePeer)this.peer;
278         if (peer != null) {
279         h = peer.getHScrollbarHeight();
280         }
281     }
282     return h;
283     }
284
285     /**
286      * Returns the width that would be occupied by a vertical
287      * scrollbar, which is independent of whether it is currently
288      * displayed by the scroll pane or not.
289      * @return the width of a vertical scrollbar in pixels
290      */

291     public int getVScrollbarWidth() {
292     int w = 0;
293     if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) {
294         ScrollPanePeer peer = (ScrollPanePeer)this.peer;
295         if (peer != null) {
296         w = peer.getVScrollbarWidth();
297         }
298     }
299     return w;
300     }
301
302     /**
303      * Returns the <code>ScrollPaneAdjustable</code> object which
304      * represents the state of the vertical scrollbar.
305      * The declared return type of this method is
306      * <code>Adjustable</code> to maintain backward compatibility.
307      * @see java.awt.ScrollPaneAdjustable
308      */

309     public Adjustable JavaDoc getVAdjustable() {
310         return vAdjustable;
311     }
312
313     /**
314      * Returns the <code>ScrollPaneAdjustable</code> object which
315      * represents the state of the horizontal scrollbar.
316      * The declared return type of this method is
317      * <code>Adjustable</code> to maintain backward compatibility.
318      * @see java.awt.ScrollPaneAdjustable
319      */

320     public Adjustable JavaDoc getHAdjustable() {
321         return hAdjustable;
322     }
323
324     /**
325      * Scrolls to the specified position within the child component.
326      * A call to this method is only valid if the scroll pane contains
327      * a child. Specifying a position outside of the legal scrolling bounds
328      * of the child will scroll to the closest legal position.
329      * Legal bounds are defined to be the rectangle:
330      * x = 0, y = 0, width = (child width - view port width),
331      * height = (child height - view port height).
332      * This is a convenience method which interfaces with the Adjustable
333      * objects which represent the state of the scrollbars.
334      * @param x the x position to scroll to
335      * @param y the y position to scroll to
336      * @throws NullPointerException if the scrollpane does not contain
337      * a child
338      */

339     public void setScrollPosition(int x, int y) {
340         synchronized (getTreeLock()) {
341         if (ncomponents <= 0) {
342         throw new NullPointerException JavaDoc("child is null");
343         }
344         hAdjustable.setValue(x);
345         vAdjustable.setValue(y);
346     }
347     }
348
349     /**
350      * Scrolls to the specified position within the child component.
351      * A call to this method is only valid if the scroll pane contains
352      * a child and the specified position is within legal scrolling bounds
353      * of the child. Specifying a position outside of the legal scrolling
354      * bounds of the child will scroll to the closest legal position.
355      * Legal bounds are defined to be the rectangle:
356      * x = 0, y = 0, width = (child width - view port width),
357      * height = (child height - view port height).
358      * This is a convenience method which interfaces with the Adjustable
359      * objects which represent the state of the scrollbars.
360      * @param p the Point representing the position to scroll to
361      */

362     public void setScrollPosition(Point JavaDoc p) {
363         setScrollPosition(p.x, p.y);
364     }
365
366     /**
367      * Returns the current x,y position within the child which is displayed
368      * at the 0,0 location of the scrolled panel's view port.
369      * This is a convenience method which interfaces with the adjustable
370      * objects which represent the state of the scrollbars.
371      * @return the coordinate position for the current scroll position
372      * @throws NullPointerException if the scrollpane does not contain
373      * a child
374      */

375     public Point JavaDoc getScrollPosition() {
376     if (ncomponents <= 0) {
377         throw new NullPointerException JavaDoc("child is null");
378     }
379     return new Point JavaDoc(hAdjustable.getValue(), vAdjustable.getValue());
380     }
381
382     /**
383      * Sets the layout manager for this container. This method is
384      * overridden to prevent the layout mgr from being set.
385      * @param mgr the specified layout manager
386      */

387     public final void setLayout(LayoutManager JavaDoc mgr) {
388     throw new AWTError JavaDoc("ScrollPane controls layout");
389     }
390
391     /**
392      * Lays out this container by resizing its child to its preferred size.
393      * If the new preferred size of the child causes the current scroll
394      * position to be invalid, the scroll position is set to the closest
395      * valid position.
396      *
397      * @see Component#validate
398      */

399     public void doLayout() {
400     layout();
401     }
402
403     /**
404      * Determine the size to allocate the child component.
405      * If the viewport area is bigger than the childs
406      * preferred size then the child is allocated enough
407      * to fill the viewport, otherwise the child is given
408      * it's preferred size.
409      */

410     Dimension JavaDoc calculateChildSize() {
411     //
412
// calculate the view size, accounting for border but not scrollbars
413
// - don't use right/bottom insets since they vary depending
414
// on whether or not scrollbars were displayed on last resize
415
//
416
Dimension JavaDoc size = getSize();
417     Insets JavaDoc insets = getInsets();
418     int viewWidth = size.width - insets.left*2;
419     int viewHeight = size.height - insets.top*2;
420
421     //
422
// determine whether or not horz or vert scrollbars will be displayed
423
//
424
boolean vbarOn;
425     boolean hbarOn;
426     Component JavaDoc child = getComponent(0);
427     Dimension JavaDoc childSize = new Dimension JavaDoc(child.getPreferredSize());
428
429     if (scrollbarDisplayPolicy == SCROLLBARS_AS_NEEDED) {
430         vbarOn = childSize.height > viewHeight;
431         hbarOn = childSize.width > viewWidth;
432     } else if (scrollbarDisplayPolicy == SCROLLBARS_ALWAYS) {
433         vbarOn = hbarOn = true;
434     } else { // SCROLLBARS_NEVER
435
vbarOn = hbarOn = false;
436     }
437     
438     //
439
// adjust predicted view size to account for scrollbars
440
//
441
int vbarWidth = getVScrollbarWidth();
442     int hbarHeight = getHScrollbarHeight();
443     if (vbarOn) {
444         viewWidth -= vbarWidth;
445     }
446     if(hbarOn) {
447         viewHeight -= hbarHeight;
448     }
449
450     //
451
// if child is smaller than view, size it up
452
//
453
if (childSize.width < viewWidth) {
454         childSize.width = viewWidth;
455     }
456     if (childSize.height < viewHeight) {
457         childSize.height = viewHeight;
458     }
459
460     return childSize;
461     }
462
463     /**
464      * @deprecated As of JDK version 1.1,
465      * replaced by <code>doLayout()</code>.
466      */

467     @Deprecated JavaDoc
468     public void layout() {
469     if (ncomponents > 0) {
470         Component JavaDoc c = getComponent(0);
471         Point JavaDoc p = getScrollPosition();
472         Dimension JavaDoc cs = calculateChildSize();
473         Dimension JavaDoc vs = getViewportSize();
474         Insets JavaDoc i = getInsets();
475
476         c.reshape(i.left - p.x, i.top - p.y, cs.width, cs.height);
477         ScrollPanePeer peer = (ScrollPanePeer)this.peer;
478         if (peer != null) {
479             peer.childResized(cs.width, cs.height);
480         }
481
482         // update adjustables... the viewport size may have changed
483
// with the scrollbars coming or going so the viewport size
484
// is updated before the adjustables.
485
vs = getViewportSize();
486         hAdjustable.setSpan(0, cs.width, vs.width);
487         vAdjustable.setSpan(0, cs.height, vs.height);
488     }
489     }
490
491     /**
492      * Prints the component in this scroll pane.
493      * @param g the specified Graphics window
494      * @see Component#print
495      * @see Component#printAll
496      */

497     public void printComponents(Graphics JavaDoc g) {
498     if (ncomponents > 0) {
499         Component JavaDoc c = component[0];
500         Point JavaDoc p = c.getLocation();
501         Dimension JavaDoc vs = getViewportSize();
502         Insets JavaDoc i = getInsets();
503
504         Graphics JavaDoc cg = g.create();
505         try {
506             cg.clipRect(i.left, i.top, vs.width, vs.height);
507             cg.translate(p.x, p.y);
508         c.printAll(cg);
509         } finally {
510         cg.dispose();
511         }
512     }
513     }
514
515     /**
516      * Creates the scroll pane's peer.
517      */

518     public void addNotify() {
519         synchronized (getTreeLock()) {
520
521             int vAdjustableValue = 0;
522             int hAdjustableValue = 0;
523
524             // Bug 4124460. Save the current adjustable values,
525
// so they can be restored after addnotify. Set the
526
// adjustables to 0, to prevent crashes for possible
527
// negative values.
528
if (getComponentCount() > 0) {
529                 vAdjustableValue = vAdjustable.getValue();
530                 hAdjustableValue = hAdjustable.getValue();
531                 vAdjustable.setValue(0);
532                 hAdjustable.setValue(0);
533             }
534
535         if (peer == null)
536             peer = getToolkit().createScrollPane(this);
537         super.addNotify();
538
539             // Bug 4124460. Restore the adjustable values.
540
if (getComponentCount() > 0) {
541                 vAdjustable.setValue(vAdjustableValue);
542                 hAdjustable.setValue(hAdjustableValue);
543             }
544     }
545     }
546
547     /**
548      * Returns a string representing the state of this
549      * <code>ScrollPane</code>. This
550      * method is intended to be used only for debugging purposes, and the
551      * content and format of the returned string may vary between
552      * implementations. The returned string may be empty but may not be
553      * <code>null</code>.
554      *
555      * @return the parameter string of this scroll pane
556      */

557     public String JavaDoc paramString() {
558     String JavaDoc sdpStr;
559     switch (scrollbarDisplayPolicy) {
560         case SCROLLBARS_AS_NEEDED:
561         sdpStr = "as-needed";
562         break;
563         case SCROLLBARS_ALWAYS:
564         sdpStr = "always";
565         break;
566         case SCROLLBARS_NEVER:
567         sdpStr = "never";
568         break;
569         default:
570         sdpStr = "invalid display policy";
571     }
572     Point JavaDoc p = ncomponents > 0? getScrollPosition() : new Point JavaDoc(0,0);
573     Insets JavaDoc i = getInsets();
574     return super.paramString()+",ScrollPosition=("+p.x+","+p.y+")"+
575         ",Insets=("+i.top+","+i.left+","+i.bottom+","+i.right+")"+
576         ",ScrollbarDisplayPolicy="+sdpStr+
577         ",wheelScrollingEnabled="+isWheelScrollingEnabled();
578     }
579
580     void autoProcessMouseWheel(MouseWheelEvent e) {
581         processMouseWheelEvent(e);
582     }
583
584     /**
585      * Process mouse wheel events that are delivered to this
586      * <code>ScrollPane</code> by scrolling an appropriate amount.
587      * <p>Note that if the event parameter is <code>null</code>
588      * the behavior is unspecified and may result in an
589      * exception.
590      *
591      * @param e the mouse wheel event
592      * @since 1.4
593      */

594     protected void processMouseWheelEvent(MouseWheelEvent e) {
595         if (isWheelScrollingEnabled()) {
596             ScrollPaneWheelScroller.handleWheelScrolling(this, e);
597             e.consume();
598         }
599         super.processMouseWheelEvent(e);
600     }
601
602     /**
603      * If wheel scrolling is enabled, we return true for MouseWheelEvents
604      * @since 1.4
605      */

606     protected boolean eventTypeEnabled(int type) {
607         if (type == MouseEvent.MOUSE_WHEEL && isWheelScrollingEnabled()) {
608             return true;
609         }
610         else {
611             return super.eventTypeEnabled(type);
612         }
613     }
614
615     /**
616      * Enables/disables scrolling in response to movement of the mouse wheel.
617      * Wheel scrolling is enabled by default.
618      *
619      * @param handleWheel <code>true</code> if scrolling should be done
620      * automatically for a MouseWheelEvent,
621      * <code>false</code> otherwise.
622      * @see #isWheelScrollingEnabled
623      * @see java.awt.event.MouseWheelEvent
624      * @see java.awt.event.MouseWheelListener
625      * @since 1.4
626      */

627     public void setWheelScrollingEnabled(boolean handleWheel) {
628         wheelScrollingEnabled = handleWheel;
629     }
630
631     /**
632      * Indicates whether or not scrolling will take place in response to
633      * the mouse wheel. Wheel scrolling is enabled by default.
634      *
635      * @see #setWheelScrollingEnabled(boolean)
636      * @since 1.4
637      */

638     public boolean isWheelScrollingEnabled() {
639         return wheelScrollingEnabled;
640     }
641
642
643     /**
644      * Writes default serializable fields to stream.
645      */

646     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
647     // 4352819: We only need this degenerate writeObject to make
648
// it safe for future versions of this class to write optional
649
// data to the stream.
650
s.defaultWriteObject();
651     }
652
653     /**
654      * Reads default serializable fields to stream.
655      * @exception HeadlessException if
656      * <code>GraphicsEnvironment.isHeadless()</code> returns
657      * <code>true</code>
658      * @see java.awt.GraphicsEnvironment#isHeadless
659      */

660     private void readObject(ObjectInputStream JavaDoc s)
661     throws ClassNotFoundException JavaDoc, IOException JavaDoc, HeadlessException JavaDoc
662     {
663         GraphicsEnvironment.checkHeadless();
664     // 4352819: Gotcha! Cannot use s.defaultReadObject here and
665
// then continue with reading optional data. Use GetField instead.
666
ObjectInputStream.GetField JavaDoc f = s.readFields();
667
668     // Old fields
669
scrollbarDisplayPolicy = f.get("scrollbarDisplayPolicy",
670                        SCROLLBARS_AS_NEEDED);
671     hAdjustable = (ScrollPaneAdjustable JavaDoc)f.get("hAdjustable", null);
672     vAdjustable = (ScrollPaneAdjustable JavaDoc)f.get("vAdjustable", null);
673
674     // Since 1.4
675
wheelScrollingEnabled = f.get("wheelScrollingEnabled",
676                       defaultWheelScroll);
677
678 // // Note to future maintainers
679
// if (f.defaulted("wheelScrollingEnabled")) {
680
// // We are reading pre-1.4 stream that doesn't have
681
// // optional data, not even the TC_ENDBLOCKDATA marker.
682
// // Reading anything after this point is unsafe as we will
683
// // read unrelated objects further down the stream (4352819).
684
// }
685
// else {
686
// // Reading data from 1.4 or later, it's ok to try to read
687
// // optional data as OptionalDataException with eof == true
688
// // will be correctly reported
689
// }
690
}
691
692     class PeerFixer implements AdjustmentListener, java.io.Serializable JavaDoc
693     {
694         private static final long serialVersionUID = 1043664721353696630L;
695
696     PeerFixer(ScrollPane JavaDoc scroller) {
697         this.scroller = scroller;
698     }
699
700     /**
701      * Invoked when the value of the adjustable has changed.
702      */

703         public void adjustmentValueChanged(AdjustmentEvent e) {
704         Adjustable JavaDoc adj = e.getAdjustable();
705         int value = e.getValue();
706         ScrollPanePeer peer = (ScrollPanePeer) scroller.peer;
707         if (peer != null) {
708         peer.setValue(adj, value);
709         }
710
711         Component JavaDoc c = scroller.getComponent(0);
712         switch(adj.getOrientation()) {
713         case Adjustable.VERTICAL:
714         c.move(c.getLocation().x, -(value));
715         break;
716         case Adjustable.HORIZONTAL:
717         c.move(-(value), c.getLocation().y);
718         break;
719         default:
720         throw new IllegalArgumentException JavaDoc("Illegal adjustable orientation");
721         }
722     }
723
724         private ScrollPane JavaDoc scroller;
725     }
726
727
728 /////////////////
729
// Accessibility support
730
////////////////
731

732     /**
733      * Gets the AccessibleContext associated with this ScrollPane.
734      * For scroll panes, the AccessibleContext takes the form of an
735      * AccessibleAWTScrollPane.
736      * A new AccessibleAWTScrollPane instance is created if necessary.
737      *
738      * @return an AccessibleAWTScrollPane that serves as the
739      * AccessibleContext of this ScrollPane
740      */

741     public AccessibleContext getAccessibleContext() {
742         if (accessibleContext == null) {
743             accessibleContext = new AccessibleAWTScrollPane();
744         }
745         return accessibleContext;
746     }
747
748     /**
749      * This class implements accessibility support for the
750      * <code>ScrollPane</code> class. It provides an implementation of the
751      * Java Accessibility API appropriate to scroll pane user-interface
752      * elements.
753      */

754     protected class AccessibleAWTScrollPane extends AccessibleAWTContainer
755     {
756         /*
757          * JDK 1.3 serialVersionUID
758          */

759         private static final long serialVersionUID = 6100703663886637L;
760
761         /**
762          * Get the role of this object.
763          *
764          * @return an instance of AccessibleRole describing the role of the
765          * object
766          * @see AccessibleRole
767          */

768         public AccessibleRole getAccessibleRole() {
769             return AccessibleRole.SCROLL_PANE;
770         }
771
772     } // class AccessibleAWTScrollPane
773

774 }
775
776 /*
777  * In JDK 1.1.1, the pkg private class java.awt.PeerFixer was moved to
778  * become an inner class of ScrollPane, which broke serialization
779  * for ScrollPane objects using JDK 1.1.
780  * Instead of moving it back out here, which would break all JDK 1.1.x
781  * releases, we keep PeerFixer in both places. Because of the scoping rules,
782  * the PeerFixer that is used in ScrollPane will be the one that is the
783  * inner class. This pkg private PeerFixer class below will only be used
784  * if the Java 2 platform is used to deserialize ScrollPane objects that were serialized
785  * using JDK1.1
786  */

787 class PeerFixer implements AdjustmentListener, java.io.Serializable JavaDoc {
788
789     PeerFixer(ScrollPane JavaDoc scroller) {
790     this.scroller = scroller;
791     }
792
793     /**
794      * Invoked when the value of the adjustable has changed.
795      */

796     public void adjustmentValueChanged(AdjustmentEvent e) {
797     Adjustable JavaDoc adj = e.getAdjustable();
798     int value = e.getValue();
799     ScrollPanePeer peer = (ScrollPanePeer) scroller.peer;
800     if (peer != null) {
801         peer.setValue(adj, value);
802     }
803
804     Component JavaDoc c = scroller.getComponent(0);
805         switch(adj.getOrientation()) {
806     case Adjustable.VERTICAL:
807         c.move(c.getLocation().x, -(value));
808         break;
809     case Adjustable.HORIZONTAL:
810         c.move(-(value), c.getLocation().y);
811         break;
812     default:
813         throw new IllegalArgumentException JavaDoc("Illegal adjustable orientation");
814     }
815     }
816
817     private ScrollPane JavaDoc scroller;
818 }
819
Popular Tags