1 /* ScrollPane.java -- Scrolling window
2    Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
20 
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.
25 
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. */
37 
38 
39 package java.awt;
40 
41 import java.awt.event.MouseEvent;
42 import java.awt.peer.ComponentPeer;
43 import java.awt.peer.ScrollPanePeer;
44 import javax.accessibility.Accessible;
45 
46 /**
47   * This widget provides a scrollable region that allows a single
48   * subcomponent to be viewed through a smaller window.
49   *
50   * @author Aaron M. Renn (arenn@urbanophile.com)
51   */
52 public class ScrollPane extends Container implements Accessible
53 {
54 
55 /*
56  * Static Variables
57  */
58 
59 /**
60   * Constant indicating that scrollbars are created as needed in this
61   * windows.
62   */
63 public static final int SCROLLBARS_AS_NEEDED = 0;
64 
65 /**
66   * Constant indicating that scrollbars are always displayed in this
67   * window.
68   */
69 public static final int SCROLLBARS_ALWAYS = 1;
70 
71 /**
72   * Constant indicating that scrollbars are never displayed in this window.
73   */
74 public static final int SCROLLBARS_NEVER = 2;
75 
76 // Serialization constant
77 private static final long serialVersionUID = 7956609840827222915L;
78 
79 /*************************************************************************/
80 
81 /*
82  * Instance Variables
83  */
84 
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.
89   */
90 private ScrollPaneAdjustable hAdjustable;
91 
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.
96   */
97 private ScrollPaneAdjustable vAdjustable;
98 
99 /**
100   * @serial Indicates when scrollbars are displayed in this window, will
101   * be one of the constants from this class.
102   */
103 private int scrollbarDisplayPolicy;
104 
105 // Current scroll position
106 private Point scrollPosition = new Point(0, 0);
107 
108 private boolean wheelScrollingEnabled;
109 
110 /*************************************************************************/
111 
112 /*
113  * Constructors
114  */
115 
116 /**
117   * Initializes a new instance of <code>ScrollPane</code> with a default
118   * scrollbar policy of <code>SCROLLBARS_AS_NEEDED</code>.
119   *
120   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
121   */
122 public
ScrollPane()123 ScrollPane()
124 {
125   this(SCROLLBARS_AS_NEEDED);
126 }
127 
128 /*************************************************************************/
129 
130 /**
131   * Initializes a new instance of <code>ScrollPane</code> with the
132   * specified scrollbar policy.
133   *
134   * @param scrollbarDisplayPolicy When to display scrollbars, which must
135   * be one of the constants defined in this class.
136   *
137   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
138   */
139 public
ScrollPane(int scrollbarDisplayPolicy)140 ScrollPane(int scrollbarDisplayPolicy)
141 {
142   if (GraphicsEnvironment.isHeadless ())
143     throw new HeadlessException ();
144 
145   this.scrollbarDisplayPolicy = scrollbarDisplayPolicy;
146 
147   if (scrollbarDisplayPolicy != SCROLLBARS_ALWAYS
148       && scrollbarDisplayPolicy != SCROLLBARS_AS_NEEDED
149       && scrollbarDisplayPolicy != SCROLLBARS_NEVER)
150     throw new IllegalArgumentException("Bad scrollbarDisplayPolicy: " +
151                                        scrollbarDisplayPolicy);
152 
153   if (scrollbarDisplayPolicy != SCROLLBARS_NEVER)
154     {
155       hAdjustable = new ScrollPaneAdjustable (this, Scrollbar.HORIZONTAL);
156       vAdjustable = new ScrollPaneAdjustable (this, Scrollbar.VERTICAL);
157     }
158 
159   wheelScrollingEnabled = true;
160 }
161 
162 /*************************************************************************/
163 
164 /*
165  * Instance Variables
166  */
167 
168 /**
169   * Returns the current scrollbar display policy.
170   *
171   * @return The current scrollbar display policy.
172   */
173 public int
getScrollbarDisplayPolicy()174 getScrollbarDisplayPolicy()
175 {
176   return(scrollbarDisplayPolicy);
177 }
178 
179 /*************************************************************************/
180 
181 /**
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>.
185   *
186   * @return The horizontal scrollbar for this window.
187   */
188 public Adjustable
getHAdjustable()189 getHAdjustable()
190 {
191   return(hAdjustable);
192 }
193 
194 /*************************************************************************/
195 
196 /**
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>.
200   *
201   * @return The horizontal scrollbar for this window.
202   */
203 public Adjustable
getVAdjustable()204 getVAdjustable()
205 {
206   return(vAdjustable);
207 }
208 
209 /*************************************************************************/
210 
211 /**
212   * Returns the current viewport size.  The viewport is the region of
213   * this object's window where the child is actually displayed.
214   *
215   * @return The viewport size.
216   */
getViewportSize()217 public Dimension getViewportSize ()
218 {
219   Dimension viewsize = getSize ();
220   Insets insets = getInsets ();
221 
222   viewsize.width -= (insets.left + insets.right);
223   viewsize.height -= (insets.top + insets.bottom);
224 
225   Component[] list = getComponents();
226   if ((list == null) || (list.length <= 0))
227     return viewsize;
228 
229   Dimension dim = list[0].getPreferredSize();
230 
231   if (dim.width <= 0 && dim.height <= 0)
232     return viewsize;
233 
234   int vScrollbarWidth = getVScrollbarWidth ();
235   int hScrollbarHeight = getHScrollbarHeight ();
236 
237   if (scrollbarDisplayPolicy == SCROLLBARS_ALWAYS)
238     {
239       viewsize.width -= vScrollbarWidth;
240       viewsize.height -= hScrollbarHeight;
241       return viewsize;
242     }
243 
244   if (scrollbarDisplayPolicy == SCROLLBARS_NEVER)
245     return viewsize;
246 
247   // The scroll policy is SCROLLBARS_AS_NEEDED, so we need to see if
248   // either scrollbar is needed.
249 
250   // Assume we don't need either scrollbar.
251   boolean mayNeedVertical = false;
252   boolean mayNeedHorizontal = false;
253 
254   boolean needVertical = false;
255   boolean needHorizontal = false;
256 
257   // Check if we need vertical scrollbars.  If we do, then we need to
258   // subtract the width of the vertical scrollbar from the viewport's
259   // width.
260   if (dim.height > viewsize.height)
261     needVertical = true;
262   else if (dim.height > (viewsize.height - hScrollbarHeight))
263     // This is tricky.  In this case the child is tall enough that its
264     // bottom edge would be covered by a horizontal scrollbar, if one
265     // were present.  This means that if there's a horizontal
266     // scrollbar then we need a vertical scrollbar.
267     mayNeedVertical = true;
268 
269   if (dim.width > viewsize.width)
270     needHorizontal = true;
271   else if (dim.width > (viewsize.width - vScrollbarWidth))
272     mayNeedHorizontal = true;
273 
274   if (needVertical && mayNeedHorizontal)
275     needHorizontal = true;
276 
277   if (needHorizontal && mayNeedVertical)
278     needVertical = true;
279 
280   if (needHorizontal)
281     viewsize.height -= hScrollbarHeight;
282 
283   if (needVertical)
284     viewsize.width -= vScrollbarWidth;
285 
286   return viewsize;
287 }
288 
289 /*************************************************************************/
290 
291 /**
292   * Returns the height of a horizontal scrollbar.
293   *
294   * @return The height of a horizontal scrollbar.
295   */
296 public int
getHScrollbarHeight()297 getHScrollbarHeight()
298 {
299   ScrollPanePeer spp = (ScrollPanePeer)getPeer();
300   if (spp != null)
301     return(spp.getHScrollbarHeight());
302   else
303     return(0); // FIXME: What to do here?
304 }
305 
306 /*************************************************************************/
307 
308 /**
309   * Returns the width of a vertical scrollbar.
310   *
311   * @return The width of a vertical scrollbar.
312   */
313 public int
getVScrollbarWidth()314 getVScrollbarWidth()
315 {
316   ScrollPanePeer spp = (ScrollPanePeer)getPeer();
317   if (spp != null)
318     return(spp.getVScrollbarWidth());
319   else
320     return(0); // FIXME: What to do here?
321 }
322 
323 /*************************************************************************/
324 
325 /**
326   * Returns the current scroll position of the viewport.
327   *
328   * @return The current scroll position of the viewport.
329   */
330 public Point
getScrollPosition()331 getScrollPosition()
332 {
333   int x = 0;
334   int y = 0;
335 
336   Adjustable v = getVAdjustable();
337   Adjustable h = getHAdjustable();
338 
339   if (v != null)
340     y = v.getValue();
341   if (h != null)
342     x = h.getValue();
343 
344   return(new Point(x, y));
345 }
346 
347 /*************************************************************************/
348 
349 /**
350   * Sets the scroll position to the specified value.
351   *
352   * @param scrollPosition The new scrollPosition.
353   *
354   * @exception IllegalArgumentException If the specified value is outside
355   * the legal scrolling range.
356   */
357 public void
setScrollPosition(Point scrollPosition)358 setScrollPosition(Point scrollPosition) throws IllegalArgumentException
359 {
360   setScrollPosition(scrollPosition.x, scrollPosition.y);
361 }
362 
363 /*************************************************************************/
364 
365 /**
366   * Sets the scroll position to the specified value.
367   *
368   * @param x The new X coordinate of the scroll position.
369   * @param y The new Y coordinate of the scroll position.
370   *
371   * @exception IllegalArgumentException If the specified value is outside
372   * the legal scrolling range.
373   */
374 public void
setScrollPosition(int x, int y)375 setScrollPosition(int x, int y)
376 {
377   Adjustable h = getHAdjustable();
378   Adjustable v = getVAdjustable();
379 
380   if (h != null)
381     h.setValue(x);
382   if (v != null)
383     v.setValue(y);
384 
385   ScrollPanePeer spp = (ScrollPanePeer)getPeer();
386   if (spp != null)
387     spp.setScrollPosition(x, y);
388 }
389 
390 /*************************************************************************/
391 
392 /**
393   * Notifies this object that it should create its native peer.
394   */
395 public void
addNotify()396 addNotify()
397 {
398   if (!isDisplayable ())
399     return;
400 
401   setPeer((ComponentPeer)getToolkit().createScrollPane(this));
402   super.addNotify();
403 }
404 
405 /*************************************************************************/
406 
407 /**
408   * Notifies this object that it should destroy its native peers.
409   */
410 public void
removeNotify()411 removeNotify()
412 {
413   super.removeNotify();
414 }
415 
416 /*************************************************************************/
417 
418 /**
419   * Adds the specified child component to this container.  A
420   * <code>ScrollPane</code> can have at most one child, so if a second
421   * one is added, then first one is removed.
422   *
423   * @param component The component to add to this container.
424   * @param constraints A list of layout constraints for this object.
425   * @param index The index at which to add the child, which is ignored
426   * in this implementation.
427   */
addImpl(Component component, Object constraints, int index)428   protected final void addImpl (Component component, Object constraints,
429 				int index)
430 {
431   Component[] list = getComponents();
432   if ((list != null) && (list.length > 0))
433     remove(list[0]);
434 
435   super.addImpl(component, constraints, -1);
436 
437   doLayout();
438 }
439 
440 /*************************************************************************/
441 
442 /**
443   * Lays out this component.  This consists of resizing the sole child
444   * component to its perferred size.
445   */
446 public void
doLayout()447 doLayout()
448 {
449   Component[] list = getComponents();
450   if ((list != null) && (list.length > 0))
451     {
452       Dimension dim = list[0].getPreferredSize();
453       Dimension vp = getViewportSize ();
454 
455       if (dim.width < vp.width)
456 	dim.width = vp.width;
457 
458       if (dim.height < vp.height)
459 	dim.height = vp.height;
460 
461       ScrollPanePeer peer = (ScrollPanePeer) getPeer ();
462       if (peer != null)
463 	peer.childResized (dim.width, dim.height);
464 
465       list[0].resize (dim);
466 
467       Point p = getScrollPosition();
468       if (p.x > dim.width)
469         p.x = dim.width;
470       if (p.y > dim.height)
471         p.y = dim.height;
472 
473       setScrollPosition(p);
474     }
475 }
476 
477 /*************************************************************************/
478 
479 /**
480   * Lays out this component.  This consists of resizing the sole child
481   * component to its perferred size.
482   *
483   * @deprecated This method is deprecated in favor of
484   * <code>doLayout()</code>.
485   */
486 public void
layout()487 layout()
488 {
489   doLayout();
490 }
491 
492 /*************************************************************************/
493 
494 /**
495   * This method overrides its superclass method to ensure no layout
496   * manager is set for this container.  <code>ScrollPane</code>'s do
497   * not have layout managers.
498   *
499   * @param layoutManager Ignored
500   */
501 public final void
setLayout(LayoutManager layoutManager)502 setLayout(LayoutManager layoutManager)
503 {
504   return;
505 }
506 
507 /*************************************************************************/
508 
509 /**
510   * Prints all of the components in this container.
511   *
512   * @param graphics The desired graphics context for printing.
513   */
514 public void
printComponents(Graphics graphics)515 printComponents(Graphics graphics)
516 {
517   super.printComponents(graphics);
518 }
519 
520 /*************************************************************************/
521 
522 /**
523   * Returns a debug string for this object.
524   *
525   * @return A debug string for this object.
526   */
527 public String
paramString()528 paramString()
529 {
530   return(getClass().getName());
531 }
532 
533   /**
534    * Tells whether or not an event is enabled.
535    *
536    * @since 1.4
537    */
eventTypeEnabled(int type)538   protected boolean eventTypeEnabled (int type)
539   {
540     if (type == MouseEvent.MOUSE_WHEEL)
541       return wheelScrollingEnabled;
542 
543     return super.eventTypeEnabled (type);
544   }
545 
546   /**
547    * Tells whether or not wheel scrolling is enabled.
548    *
549    * @since 1.4
550    */
isWheelScrollingEnabled()551   public boolean isWheelScrollingEnabled ()
552   {
553     return wheelScrollingEnabled;
554   }
555 
556   /**
557    * Enables/disables wheel scrolling.
558    *
559    * @since 1.4
560    */
setWheelScrollingEnabled(boolean enable)561   public void setWheelScrollingEnabled (boolean enable)
562   {
563     wheelScrollingEnabled = enable;
564   }
565 } // class ScrollPane
566 
567