1 /* ScrollPane.java -- Scrolling window
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.event
.MouseEvent
;
42 import java
.awt
.peer
.ComponentPeer
;
43 import java
.awt
.peer
.ScrollPanePeer
;
44 import javax
.accessibility
.Accessible
;
47 * This widget provides a scrollable region that allows a single
48 * subcomponent to be viewed through a smaller window.
50 * @author Aaron M. Renn (arenn@urbanophile.com)
52 public class ScrollPane
extends Container
implements Accessible
60 * Constant indicating that scrollbars are created as needed in this
63 public static final int SCROLLBARS_AS_NEEDED
= 0;
66 * Constant indicating that scrollbars are always displayed in this
69 public static final int SCROLLBARS_ALWAYS
= 1;
72 * Constant indicating that scrollbars are never displayed in this window.
74 public static final int SCROLLBARS_NEVER
= 2;
76 // Serialization constant
77 private static final long serialVersionUID
= 7956609840827222915L;
79 /*************************************************************************/
86 * @serial The horizontal scrollbar for this window. The methods
87 * <code>setMinimum()</code>, <code>setMaximum</code>, and
88 * <code>setVisibleAmount</code> must not be called on this scrollbar.
90 private ScrollPaneAdjustable hAdjustable
;
93 * @serial The vertical scrollbar for this window. The methods
94 * <code>setMinimum()</code>, <code>setMaximum</code>, and
95 * <code>setVisibleAmount</code> must not be called on this scrollbar.
97 private ScrollPaneAdjustable vAdjustable
;
100 * @serial Indicates when scrollbars are displayed in this window, will
101 * be one of the constants from this class.
103 private int scrollbarDisplayPolicy
;
105 // Current scroll position
106 private Point scrollPosition
= new Point(0, 0);
108 private boolean wheelScrollingEnabled
;
110 /*************************************************************************/
117 * Initializes a new instance of <code>ScrollPane</code> with a default
118 * scrollbar policy of <code>SCROLLBARS_AS_NEEDED</code>.
120 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
125 this(SCROLLBARS_AS_NEEDED
);
128 /*************************************************************************/
131 * Initializes a new instance of <code>ScrollPane</code> with the
132 * specified scrollbar policy.
134 * @param scrollbarDisplayPolicy When to display scrollbars, which must
135 * be one of the constants defined in this class.
137 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
140 ScrollPane(int scrollbarDisplayPolicy
)
142 if (GraphicsEnvironment
.isHeadless ())
143 throw new HeadlessException ();
145 this.scrollbarDisplayPolicy
= scrollbarDisplayPolicy
;
147 if (scrollbarDisplayPolicy
!= SCROLLBARS_ALWAYS
148 && scrollbarDisplayPolicy
!= SCROLLBARS_AS_NEEDED
149 && scrollbarDisplayPolicy
!= SCROLLBARS_NEVER
)
150 throw new IllegalArgumentException("Bad scrollbarDisplayPolicy: " +
151 scrollbarDisplayPolicy
);
153 if (scrollbarDisplayPolicy
!= SCROLLBARS_NEVER
)
155 hAdjustable
= new ScrollPaneAdjustable (this, Scrollbar
.HORIZONTAL
);
156 vAdjustable
= new ScrollPaneAdjustable (this, Scrollbar
.VERTICAL
);
159 wheelScrollingEnabled
= true;
162 /*************************************************************************/
169 * Returns the current scrollbar display policy.
171 * @return The current scrollbar display policy.
174 getScrollbarDisplayPolicy()
176 return(scrollbarDisplayPolicy
);
179 /*************************************************************************/
182 * Returns the horizontal scrollbar for this object. If the scrollbar
183 * display policy is set to <code>SCROLLBARS_NEVER</code> then this
184 * will be <code>null</code>.
186 * @return The horizontal scrollbar for this window.
194 /*************************************************************************/
197 * Returns the vertical scrollbar for this object. If the scrollbar
198 * display policy is set to <code>SCROLLBARS_NEVER</code> then this
199 * will be <code>null</code>.
201 * @return The horizontal scrollbar for this window.
209 /*************************************************************************/
212 * Returns the current viewport size. The viewport is the region of
213 * this object's window where the child is actually displayed.
215 * @return The viewport size.
217 public Dimension
getViewportSize ()
219 Dimension viewsize
= getSize ();
220 Insets insets
= getInsets ();
221 viewsize
.width
= (viewsize
.width
222 - (insets
.left
+ insets
.right
)
223 - getVScrollbarWidth ());
224 viewsize
.height
= (viewsize
.height
225 - (insets
.top
+ insets
.bottom
)
226 - getHScrollbarHeight ());
230 /*************************************************************************/
233 * Returns the height of a horizontal scrollbar.
235 * @return The height of a horizontal scrollbar.
238 getHScrollbarHeight()
240 ScrollPanePeer spp
= (ScrollPanePeer
)getPeer();
242 return(spp
.getHScrollbarHeight());
244 return(0); // FIXME: What to do here?
247 /*************************************************************************/
250 * Returns the width of a vertical scrollbar.
252 * @return The width of a vertical scrollbar.
257 ScrollPanePeer spp
= (ScrollPanePeer
)getPeer();
259 return(spp
.getVScrollbarWidth());
261 return(0); // FIXME: What to do here?
264 /*************************************************************************/
267 * Returns the current scroll position of the viewport.
269 * @return The current scroll position of the viewport.
277 Adjustable v
= getVAdjustable();
278 Adjustable h
= getHAdjustable();
285 return(new Point(x
, y
));
288 /*************************************************************************/
291 * Sets the scroll position to the specified value.
293 * @param scrollPosition The new scrollPosition.
295 * @exception IllegalArgumentException If the specified value is outside
296 * the legal scrolling range.
299 setScrollPosition(Point scrollPosition
) throws IllegalArgumentException
301 setScrollPosition(scrollPosition
.x
, scrollPosition
.y
);
304 /*************************************************************************/
307 * Sets the scroll position to the specified value.
309 * @param x The new X coordinate of the scroll position.
310 * @param y The new Y coordinate of the scroll position.
312 * @exception IllegalArgumentException If the specified value is outside
313 * the legal scrolling range.
316 setScrollPosition(int x
, int y
)
318 Adjustable h
= getHAdjustable();
319 Adjustable v
= getVAdjustable();
326 ScrollPanePeer spp
= (ScrollPanePeer
)getPeer();
328 spp
.setScrollPosition(x
, y
);
331 /*************************************************************************/
334 * Notifies this object that it should create its native peer.
339 if (!isDisplayable ())
342 setPeer((ComponentPeer
)getToolkit().createScrollPane(this));
346 /*************************************************************************/
349 * Notifies this object that it should destroy its native peers.
354 super.removeNotify();
357 /*************************************************************************/
360 * Adds the specified child component to this container. A
361 * <code>ScrollPane</code> can have at most one child, so if a second
362 * one is added, then first one is removed.
364 * @param component The component to add to this container.
365 * @param constraints A list of layout constraints for this object.
366 * @param index The index at which to add the child, which is ignored
367 * in this implementation.
369 protected final void addImpl (Component component
, Object constraints
,
372 Component
[] list
= getComponents();
373 if ((list
!= null) && (list
.length
> 0))
376 super.addImpl(component
, constraints
, -1);
381 /*************************************************************************/
384 * Lays out this component. This consists of resizing the sole child
385 * component to its perferred size.
390 Component
[] list
= getComponents();
391 if ((list
!= null) && (list
.length
> 0))
393 Dimension dim
= list
[0].getPreferredSize();
396 Point p
= getScrollPosition();
399 if (p
.y
> dim
.height
)
402 setScrollPosition(p
);
406 /*************************************************************************/
409 * Lays out this component. This consists of resizing the sole child
410 * component to its perferred size.
412 * @deprecated This method is deprecated in favor of
413 * <code>doLayout()</code>.
421 /*************************************************************************/
424 * This method overrides its superclass method to ensure no layout
425 * manager is set for this container. <code>ScrollPane</code>'s do
426 * not have layout managers.
428 * @param layoutManager Ignored
431 setLayout(LayoutManager layoutManager
)
436 /*************************************************************************/
439 * Prints all of the components in this container.
441 * @param graphics The desired graphics context for printing.
444 printComponents(Graphics graphics
)
446 super.printComponents(graphics
);
449 /*************************************************************************/
452 * Returns a debug string for this object.
454 * @return A debug string for this object.
459 return(getClass().getName());
463 * Tells whether or not an event is enabled.
467 protected boolean eventTypeEnabled (int type
)
469 if (type
== MouseEvent
.MOUSE_WHEEL
)
470 return wheelScrollingEnabled
;
472 return super.eventTypeEnabled (type
);
476 * Tells whether or not wheel scrolling is enabled.
480 public boolean isWheelScrollingEnabled ()
482 return wheelScrollingEnabled
;
486 * Enables/disables wheel scrolling.
490 public void setWheelScrollingEnabled (boolean enable
)
492 wheelScrollingEnabled
= enable
;
494 } // class ScrollPane