source: trunk/gcc/libjava/java/awt/ScrollPane.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.2 KB
Line 
1/* ScrollPane.java -- Scrolling window
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.ScrollPanePeer;
42import java.awt.peer.ContainerPeer;
43import java.awt.peer.ComponentPeer;
44import java.io.Serializable;
45import javax.accessibility.Accessible;
46
47/**
48 * This widget provides a scrollable region that allows a single
49 * subcomponent to be viewed through a smaller window.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 */
53public class ScrollPane extends Container implements Accessible, Serializable
54{
55
56/*
57 * Static Variables
58 */
59
60/**
61 * Constant indicating that scrollbars are created as needed in this
62 * windows.
63 */
64public static final int SCROLLBARS_AS_NEEDED = 0;
65
66/**
67 * Constant indicating that scrollbars are always displayed in this
68 * window.
69 */
70public static final int SCROLLBARS_ALWAYS = 1;
71
72/**
73 * Constant indicating that scrollbars are never displayed in this window.
74 */
75public static final int SCROLLBARS_NEVER = 2;
76
77// Serialization constant
78private static final long serialVersionUID = 7956609840827222915L;
79
80/*************************************************************************/
81
82/*
83 * Instance Variables
84 */
85
86/**
87 * @serial The horizontal scrollbar for this window. The methods
88 * <code>setMinimum()</code>, <code>setMaximum</code>, and
89 * <code>setVisibleAmount</code> must not be called on this scrollbar.
90 */
91private ScrollPaneAdjustable hAdjustable;
92
93/**
94 * @serial The vertical scrollbar for this window. The methods
95 * <code>setMinimum()</code>, <code>setMaximum</code>, and
96 * <code>setVisibleAmount</code> must not be called on this scrollbar.
97 */
98private ScrollPaneAdjustable vAdjustable;
99
100/**
101 * @serial Indicates when scrollbars are displayed in this window, will
102 * be one of the constants from this class.
103 */
104private int scrollbarDisplayPolicy;
105
106// Current scroll position
107private Point scrollPosition = new Point(0, 0);
108
109/*************************************************************************/
110
111/*
112 * Constructors
113 */
114
115/**
116 * Initializes a new instance of <code>ScrollPane</code> with a default
117 * scrollbar policy of <code>SCROLLBARS_AS_NEEDED</code>.
118 *
119 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
120 */
121public
122ScrollPane()
123{
124 this(SCROLLBARS_AS_NEEDED);
125}
126
127/*************************************************************************/
128
129/**
130 * Initializes a new instance of <code>ScrollPane</code> with the
131 * specified scrollbar policy.
132 *
133 * @param scrollbarDisplayPolicy When to display scrollbars, which must
134 * be one of the constants defined in this class.
135 *
136 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
137 */
138public
139ScrollPane(int scrollbarDisplayPolicy)
140{
141 if (GraphicsEnvironment.isHeadless ())
142 throw new HeadlessException ();
143
144 this.scrollbarDisplayPolicy = scrollbarDisplayPolicy;
145
146 if (scrollbarDisplayPolicy != SCROLLBARS_ALWAYS
147 && scrollbarDisplayPolicy != SCROLLBARS_AS_NEEDED
148 && scrollbarDisplayPolicy != SCROLLBARS_NEVER)
149 throw new IllegalArgumentException("Bad scrollbarDisplayPolicy: " +
150 scrollbarDisplayPolicy);
151
152 if (scrollbarDisplayPolicy != SCROLLBARS_NEVER)
153 {
154 hAdjustable = new ScrollPaneAdjustable(Scrollbar.HORIZONTAL);
155 vAdjustable = new ScrollPaneAdjustable(Scrollbar.VERTICAL);
156 }
157}
158
159/*************************************************************************/
160
161/*
162 * Instance Variables
163 */
164
165/**
166 * Returns the current scrollbar display policy.
167 *
168 * @return The current scrollbar display policy.
169 */
170public int
171getScrollbarDisplayPolicy()
172{
173 return(scrollbarDisplayPolicy);
174}
175
176/*************************************************************************/
177
178/**
179 * Returns the horizontal scrollbar for this object. If the scrollbar
180 * display policy is set to <code>SCROLLBARS_NEVER</code> then this
181 * will be <code>null</code>.
182 *
183 * @return The horizontal scrollbar for this window.
184 */
185public Adjustable
186getHAdjustable()
187{
188 return(hAdjustable);
189}
190
191/*************************************************************************/
192
193/**
194 * Returns the vertical scrollbar for this object. If the scrollbar
195 * display policy is set to <code>SCROLLBARS_NEVER</code> then this
196 * will be <code>null</code>.
197 *
198 * @return The horizontal scrollbar for this window.
199 */
200public Adjustable
201getVAdjustable()
202{
203 return(vAdjustable);
204}
205
206/*************************************************************************/
207
208/**
209 * Returns the current viewport size. The viewport is the region of
210 * this object's window where the child is actually displayed.
211 *
212 * @return The viewport size.
213 */
214public Dimension
215getViewportSize()
216{
217 Dimension viewsize = getSize();
218 Insets insets = getInsets();
219 viewsize.width = viewsize.width - (insets.left + insets.right);
220 viewsize.height = viewsize.height - (insets.top + insets.bottom);
221
222 ScrollPaneAdjustable v = (ScrollPaneAdjustable)getVAdjustable();
223 ScrollPaneAdjustable h = (ScrollPaneAdjustable)getHAdjustable();
224
225 if ((v != null) && v.isVisible())
226 viewsize.width = viewsize.width - v.getSize().width;
227 if ((h != null) && h.isVisible())
228 viewsize.height = viewsize.height - v.getSize().height;
229
230 return(viewsize);
231}
232
233/*************************************************************************/
234
235/**
236 * Returns the height of a horizontal scrollbar.
237 *
238 * @return The height of a horizontal scrollbar.
239 */
240public int
241getHScrollbarHeight()
242{
243 ScrollPanePeer spp = (ScrollPanePeer)getPeer();
244 if (spp != null)
245 return(spp.getHScrollbarHeight());
246 else
247 return(0); // FIXME: What to do here?
248}
249
250/*************************************************************************/
251
252/**
253 * Returns the width of a vertical scrollbar.
254 *
255 * @return The width of a vertical scrollbar.
256 */
257public int
258getVScrollbarWidth()
259{
260 ScrollPanePeer spp = (ScrollPanePeer)getPeer();
261 if (spp != null)
262 return(spp.getVScrollbarWidth());
263 else
264 return(0); // FIXME: What to do here?
265}
266
267/*************************************************************************/
268
269/**
270 * Returns the current scroll position of the viewport.
271 *
272 * @return The current scroll position of the viewport.
273 */
274public Point
275getScrollPosition()
276{
277 int x = 0;
278 int y = 0;
279
280 Adjustable v = getVAdjustable();
281 Adjustable h = getHAdjustable();
282
283 if (v != null)
284 y = v.getValue();
285 if (h != null)
286 x = h.getValue();
287
288 return(new Point(x, y));
289}
290
291/*************************************************************************/
292
293/**
294 * Sets the scroll position to the specified value.
295 *
296 * @param scrollPosition The new scrollPosition.
297 *
298 * @exception IllegalArgumentException If the specified value is outside
299 * the legal scrolling range.
300 */
301public void
302setScrollPosition(Point scrollPosition) throws IllegalArgumentException
303{
304 setScrollPosition(scrollPosition.x, scrollPosition.y);
305}
306
307/*************************************************************************/
308
309/**
310 * Sets the scroll position to the specified value.
311 *
312 * @param x The new X coordinate of the scroll position.
313 * @param y The new Y coordinate of the scroll position.
314 *
315 * @exception IllegalArgumentException If the specified value is outside
316 * the legal scrolling range.
317 */
318public void
319setScrollPosition(int x, int y)
320{
321 Adjustable h = getHAdjustable();
322 Adjustable v = getVAdjustable();
323
324 if (h != null)
325 h.setValue(x);
326 if (v != null)
327 v.setValue(y);
328
329 ScrollPanePeer spp = (ScrollPanePeer)getPeer();
330 if (spp != null)
331 spp.setScrollPosition(x, y);
332}
333
334/*************************************************************************/
335
336/**
337 * Notifies this object that it should create its native peer.
338 */
339public void
340addNotify()
341{
342 if (getPeer() == null)
343 return;
344
345 setPeer((ComponentPeer)getToolkit().createScrollPane(this));
346
347 if (hAdjustable != null)
348 hAdjustable.addNotify();
349 if (vAdjustable != null)
350 vAdjustable.removeNotify();
351}
352
353/*************************************************************************/
354
355/**
356 * Notifies this object that it should destroy its native peers.
357 */
358public void
359removeNotify()
360{
361 if (hAdjustable != null)
362 hAdjustable.removeNotify();
363 if (vAdjustable != null)
364 vAdjustable.removeNotify();
365
366 super.removeNotify();
367}
368
369/*************************************************************************/
370
371/**
372 * Adds the specified child component to this container. A
373 * <code>ScrollPane</code> can have at most one child, so if a second
374 * one is added, then first one is removed.
375 *
376 * @param component The component to add to this container.
377 * @param constraints A list of layout constraints for this object.
378 * @param index The index at which to add the child, which is ignored
379 * in this implementation.
380 */
381public final void
382addImpl(Component component, Object constraints, int index)
383{
384 Component[] list = getComponents();
385 if ((list != null) && (list.length > 0))
386 remove(list[0]);
387
388 super.addImpl(component, constraints, -1);
389
390 doLayout();
391}
392
393/*************************************************************************/
394
395/**
396 * Lays out this component. This consists of resizing the sole child
397 * component to its perferred size.
398 */
399public void
400doLayout()
401{
402 Component[] list = getComponents();
403 if ((list != null) && (list.length > 0))
404 {
405 Dimension dim = list[0].getPreferredSize();
406 list[0].resize(dim);
407
408 Point p = getScrollPosition();
409 if (p.x > dim.width)
410 p.x = dim.width;
411 if (p.y > dim.height)
412 p.y = dim.height;
413
414 setScrollPosition(p);
415 }
416}
417
418/*************************************************************************/
419
420/**
421 * Lays out this component. This consists of resizing the sole child
422 * component to its perferred size.
423 *
424 * @deprecated This method is deprecated in favor of
425 * <code>doLayout()</code>.
426 */
427public void
428layout()
429{
430 doLayout();
431}
432
433/*************************************************************************/
434
435/**
436 * This method overrides its superclass method to ensure no layout
437 * manager is set for this container. <code>ScrollPane</code>'s do
438 * not have layout managers.
439 *
440 * @param layoutManager Ignored
441 */
442public final void
443setLayout(LayoutManager layoutManager)
444{
445 return;
446}
447
448/*************************************************************************/
449
450/**
451 * Prints all of the components in this container.
452 *
453 * @param graphics The desired graphics context for printing.
454 */
455public void
456printComponents(Graphics graphics)
457{
458 super.printComponents(graphics);
459}
460
461/*************************************************************************/
462
463/**
464 * Returns a debug string for this object.
465 *
466 * @return A debug string for this object.
467 */
468public String
469paramString()
470{
471 return(getClass().getName());
472}
473
474} // class ScrollPane
475
Note: See TracBrowser for help on using the repository browser.