1 /* Copyright (C) 2000 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 import java.awt.event.AdjustmentListener;
12 import java.awt.peer.ScrollPanePeer;
14 /** A ScrollPane is a component that has vertical and horizontal
15 * scrollbars as well as a single child which is scrolled by them.
16 * @author Tom Tromey <tromey@redhat.com>
17 * @date December 31, 2000
19 public class ScrollPane extends Container
21 /** This indicates that scrollbars should only be displayed when
23 public static final int SCROLLBARS_AS_NEEDED = 0;
24 /** This indicates that scrollbars should always be displayed. */
25 public static final int SCROLLBARS_ALWAYS = 1;
26 /** This indicates that scrollbars should never be displayed. */
27 public static final int SCROLLBARS_NEVER = 2;
29 /** Create a new ScrollPane object using the indicated scrollbar
30 * display policy. If the policy is not specified it defaults to
31 * SCROLLBARS_AS_NEEDED. The default size of this component is
33 * @param policy The scrollbar display policy
37 this (SCROLLBARS_AS_NEEDED);
40 public ScrollPane (int policy)
42 if (policy != SCROLLBARS_AS_NEEDED
43 && policy != SCROLLBARS_ALWAYS
44 && policy != SCROLLBARS_NEVER)
45 throw new IllegalArgumentException ("invalid value for policy");
51 /** Add a component to this ScrollPane.
52 * @param comp The component to add
53 * @param constraints Constraints. This is ignored.
54 * @param pos Position. This must be <= 0, but is otherwise ignored.
56 protected final void addImpl (Component comp, Object constraints,
60 throw new IllegalArgumentException ("pos must be <= 0");
63 remove (component[0]);
65 if (comp.isLightweight ())
67 Panel p = new Panel ();
72 super.addImpl (comp, constraints, pos);
75 /** This creates the component's peer. */
76 public void addNotify ()
79 peer = getToolkit ().createScrollPane (this);
83 /** Lays out the components in this container. */
84 public void doLayout ()
86 ScrollPanePeer spp = (ScrollPanePeer) peer;
87 Dimension c = component[0].getPreferredSize ();
88 component[0].setSize (c.width, c.height);
89 spp.childResized (c.width, c.height);
90 // Update the scrollbar position to the closest valid value.
91 setScrollPosition (hscroll.getValue (), vscroll.getValue ());
94 /** Returns an Adjustable representing the horizontal scrollbar.
95 * The methods setMaximum, setMinimum, and setVisibleAmount should
96 * not be called on this Adjustable. They will throw AWTError if
99 public Adjustable getHAdjustable ()
104 /** Returns the height of the horizontal scrollbar. */
105 public int getHScrollbarHeight ()
109 ScrollPanePeer spp = (ScrollPanePeer) peer;
110 return spp.getHScrollbarHeight ();
113 /** Returns the scrollbar display policy. */
114 public int getScrollbarDisplayPolicy ()
119 /** Returns the viewport's scroll position. */
120 public Point getScrollPosition ()
122 return new Point (hscroll.getValue (), vscroll.getValue ());
125 /** Returns an Adjustable representing the vertical scrollbar.
126 * The methods setMaximum, setMinimum, and setVisibleAmount should
127 * not be called on this Adjustable. They will throw AWTError if
130 public Adjustable getVAdjustable ()
135 /** Returns the size of the viewport. */
136 public Dimension getViewportSize ()
138 // Note: according to the online docs, the Insets are
139 // automatically updated by the peer to include the scrollbar
141 Insets ins = getInsets ();
142 int myw = width - ins.left - ins.right;
143 int myh = height - ins.top - ins.bottom;
147 cs = component[0].getPreferredSize ();
149 cs = new Dimension (myw, myh);
151 // A little optimization -- reuse the Dimension.
152 cs.setSize (myw, myh);
156 /** Returns the width of the vertical scrollbar. */
157 public int getVScrollbarWidth ()
161 ScrollPanePeer spp = (ScrollPanePeer) peer;
162 return spp.getVScrollbarWidth ();
165 /** Generates a String representation of this ScrollPane's state. */
166 public String paramString ()
168 return ("[" + getClass ().getName ()
169 + ": " + ((ncomponents > 0) ? component[0].paramString () : "")
173 /** Set the layout manager for this component. ScrollPane has its
174 * own layout manager and overrides this method so that the layout
175 * manager cannot be changed.
176 * @param m The new layout manager (ignored)
178 public final void setLayout (LayoutManager m)
183 /** Sets the scroll position for this ScrollPane. If the point if
184 * out of range it is silently moved within range.
185 * @param x The x coordinate
186 * @param y The y coordinate
188 public void setScrollPosition (int x, int y)
190 // According to the JCL we throw a NullPointerException if there
192 if (ncomponents == 0)
193 throw new NullPointerException ("no child in ScrollPane");
195 Dimension child_d = component[0].getPreferredSize ();
196 Dimension our_d = getViewportSize ();
198 int xmax = Math.max (0, child_d.width - our_d.width);
199 int ymax = Math.max (0, child_d.height - our_d.height);
210 ScrollPanePeer spp = (ScrollPanePeer) peer;
211 spp.setScrollPosition (x, y);
214 /** Sets the scroll position for this ScrollPane. If the point if
215 * out of range it is silently moved within range.
216 * @param p The new point
218 public void setScrollPosition (Point p)
220 setScrollPosition (p.x, p.y);
223 // This implements the Adjustable for each scrollbar. The
224 // expectation is that the peer will look at these objects directly
225 // and modify the values in them when the user manipulates the
226 // scrollbars. This has to be done from CNI to bypass Java
227 // protection rules. The peer should also take care of calling the
228 // adjustment listeners.
229 class ScrollPaneAdjustable implements Adjustable
231 AdjustmentListener listeners;
237 public ScrollPaneAdjustable (int orient)
239 this.orient = orient;
242 public void addAdjustmentListener (AdjustmentListener l)
244 listeners = AWTEventMulticaster.add (listeners, l);
247 public int getBlockIncrement ()
252 public int getMaximum ()
254 Dimension child_d = component[0].getPreferredSize ();
255 Dimension our_d = getViewportSize ();
257 int xmax = Math.max (0, child_d.width - our_d.width);
258 int ymax = Math.max (0, child_d.height - our_d.height);
260 return (orient == Adjustable.HORIZONTAL) ? xmax : ymax;
263 public int getMinimum ()
268 public int getOrientation ()
273 public int getUnitIncrement ()
278 public int getValue ()
283 public int getVisibleAmount ()
285 Dimension d = getViewportSize ();
286 return (orient == Adjustable.HORIZONTAL) ? d.width : d.height;
289 public void removeAdjustmentListener (AdjustmentListener l)
291 listeners = AWTEventMulticaster.remove (listeners, l);
294 public void setBlockIncrement (int b)
296 throw new AWTError ("can't use setBlockIncrement on this Adjustable");
299 public void setMaximum (int max)
301 throw new AWTError ("can't use setMaximum on this Adjustable");
304 public void setMinimum (int min)
306 throw new AWTError ("can't use setMinimum on this Adjustable");
309 public void setUnitIncrement (int u)
314 ScrollPanePeer spp = (ScrollPanePeer) peer;
315 spp.setUnitIncrement (this, u);
319 public void setValue (int v)
324 ScrollPanePeer spp = (ScrollPanePeer) peer;
325 spp.setValue (this, v);
329 public void setVisibleAmount (int v)
331 throw new AWTError ("can't use setVisibleAmount on this Adjustable");
335 ScrollPaneAdjustable hscroll
336 = new ScrollPaneAdjustable (Adjustable.HORIZONTAL);
337 ScrollPaneAdjustable vscroll
338 = new ScrollPaneAdjustable (Adjustable.VERTICAL);