2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / ScrollPane.java
blobceb45680514d01e72c868b2fccda81f9778937a3
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package java.awt;
41 import java.awt.event.MouseEvent;
42 import java.awt.peer.ComponentPeer;
43 import java.awt.peer.ScrollPanePeer;
44 import javax.accessibility.Accessible;
46 /**
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
56 * Static Variables
59 /**
60 * Constant indicating that scrollbars are created as needed in this
61 * windows.
63 public static final int SCROLLBARS_AS_NEEDED = 0;
65 /**
66 * Constant indicating that scrollbars are always displayed in this
67 * window.
69 public static final int SCROLLBARS_ALWAYS = 1;
71 /**
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 /*************************************************************************/
82 * Instance Variables
85 /**
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;
92 /**
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;
99 /**
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 /*************************************************************************/
113 * Constructors
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.
122 public
123 ScrollPane()
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.
139 public
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 /*************************************************************************/
165 * Instance Variables
169 * Returns the current scrollbar display policy.
171 * @return The current scrollbar display policy.
173 public int
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.
188 public Adjustable
189 getHAdjustable()
191 return(hAdjustable);
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.
203 public Adjustable
204 getVAdjustable()
206 return(vAdjustable);
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 ());
227 return viewsize;
230 /*************************************************************************/
233 * Returns the height of a horizontal scrollbar.
235 * @return The height of a horizontal scrollbar.
237 public int
238 getHScrollbarHeight()
240 ScrollPanePeer spp = (ScrollPanePeer)getPeer();
241 if (spp != null)
242 return(spp.getHScrollbarHeight());
243 else
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.
254 public int
255 getVScrollbarWidth()
257 ScrollPanePeer spp = (ScrollPanePeer)getPeer();
258 if (spp != null)
259 return(spp.getVScrollbarWidth());
260 else
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.
271 public Point
272 getScrollPosition()
274 int x = 0;
275 int y = 0;
277 Adjustable v = getVAdjustable();
278 Adjustable h = getHAdjustable();
280 if (v != null)
281 y = v.getValue();
282 if (h != null)
283 x = h.getValue();
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.
298 public void
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.
315 public void
316 setScrollPosition(int x, int y)
318 Adjustable h = getHAdjustable();
319 Adjustable v = getVAdjustable();
321 if (h != null)
322 h.setValue(x);
323 if (v != null)
324 v.setValue(y);
326 ScrollPanePeer spp = (ScrollPanePeer)getPeer();
327 if (spp != null)
328 spp.setScrollPosition(x, y);
331 /*************************************************************************/
334 * Notifies this object that it should create its native peer.
336 public void
337 addNotify()
339 if (!isDisplayable ())
340 return;
342 setPeer((ComponentPeer)getToolkit().createScrollPane(this));
343 super.addNotify();
346 /*************************************************************************/
349 * Notifies this object that it should destroy its native peers.
351 public void
352 removeNotify()
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,
370 int index)
372 Component[] list = getComponents();
373 if ((list != null) && (list.length > 0))
374 remove(list[0]);
376 super.addImpl(component, constraints, -1);
378 doLayout();
381 /*************************************************************************/
384 * Lays out this component. This consists of resizing the sole child
385 * component to its perferred size.
387 public void
388 doLayout()
390 Component[] list = getComponents();
391 if ((list != null) && (list.length > 0))
393 Dimension dim = list[0].getPreferredSize();
394 list[0].resize(dim);
396 Point p = getScrollPosition();
397 if (p.x > dim.width)
398 p.x = dim.width;
399 if (p.y > dim.height)
400 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>.
415 public void
416 layout()
418 doLayout();
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
430 public final void
431 setLayout(LayoutManager layoutManager)
433 return;
436 /*************************************************************************/
439 * Prints all of the components in this container.
441 * @param graphics The desired graphics context for printing.
443 public void
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.
456 public String
457 paramString()
459 return(getClass().getName());
463 * Tells whether or not an event is enabled.
465 * @since 1.4
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.
478 * @since 1.4
480 public boolean isWheelScrollingEnabled ()
482 return wheelScrollingEnabled;
486 * Enables/disables wheel scrolling.
488 * @since 1.4
490 public void setWheelScrollingEnabled (boolean enable)
492 wheelScrollingEnabled = enable;
494 } // class ScrollPane