summaryrefslogtreecommitdiff
path: root/libjava/java/awt/ScrollPane.java
blob: dd657ac19968ea1eb37cd17d0df158e497b5bf7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* Copyright (C) 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.awt;

import java.awt.event.AdjustmentListener;
import java.awt.peer.ScrollPanePeer;

/** A ScrollPane is a component that has vertical and horizontal
 * scrollbars as well as a single child which is scrolled by them.
 * @author Tom Tromey <tromey@redhat.com>
 * @date December 31, 2000
 */
public class ScrollPane extends Container
{
  /** This indicates that scrollbars should only be displayed when
   * needed.  */
  public static final int SCROLLBARS_AS_NEEDED = 0;
  /** This indicates that scrollbars should always be displayed.  */
  public static final int SCROLLBARS_ALWAYS = 1;
  /** This indicates that scrollbars should never be displayed.  */
  public static final int SCROLLBARS_NEVER = 2;

  /** Create a new ScrollPane object using the indicated scrollbar
   * display policy.  If the policy is not specified it defaults to
   * SCROLLBARS_AS_NEEDED.  The default size of this component is
   * 100x100.
   * @param policy The scrollbar display policy
   */
  public ScrollPane ()
  {
    this (SCROLLBARS_AS_NEEDED);
  }

  public ScrollPane (int policy)
  {
    if (policy != SCROLLBARS_AS_NEEDED
	&& policy != SCROLLBARS_ALWAYS
	&& policy != SCROLLBARS_NEVER)
      throw new IllegalArgumentException ("invalid value for policy");

    this.policy = policy;
    setSize (100, 100);
  }

  /** Add a component to this ScrollPane.
   * @param comp The component to add
   * @param constraints Constraints.  This is ignored.
   * @param pos Position.  This must be <= 0, but is otherwise ignored.
   */
  protected final void addImpl (Component comp, Object constraints,
				int pos)
  {
    if (pos > 0)
      throw new IllegalArgumentException ("pos must be <= 0");

    if (ncomponents > 0)
      remove (component[0]);

    if (comp.isLightweight ())
      {
	Panel p = new Panel ();
	p.add (comp);
	comp = p;
      }

    super.addImpl (comp, constraints, pos);
  }

  /** This creates the component's peer.  */
  public void addNotify ()
  {
    if (peer == null)
      peer = getToolkit ().createScrollPane (this);
    super.addNotify ();
  }

  /** Lays out the components in this container.  */
  public void doLayout ()
  {
    ScrollPanePeer spp = (ScrollPanePeer) peer;
    Dimension c = component[0].getPreferredSize ();
    component[0].setSize (c.width, c.height);
    spp.childResized (c.width, c.height);
    // Update the scrollbar position to the closest valid value.
    setScrollPosition (hscroll.getValue (), vscroll.getValue ());
  }

  /** Returns an Adjustable representing the horizontal scrollbar.
   * The methods setMaximum, setMinimum, and setVisibleAmount should
   * not be called on this Adjustable.  They will throw AWTError if
   * called.
   */
  public Adjustable getHAdjustable ()
  {
    return hscroll;
  }

  /** Returns the height of the horizontal scrollbar.  */
  public int getHScrollbarHeight ()
  {
    if (peer == null)
      return 0;
    ScrollPanePeer spp = (ScrollPanePeer) peer;
    return spp.getHScrollbarHeight ();
  }

  /** Returns the scrollbar display policy.  */
  public int getScrollbarDisplayPolicy ()
  {
    return policy;
  }

  /** Returns the viewport's scroll position.  */
  public Point getScrollPosition ()
  {
    return new Point (hscroll.getValue (), vscroll.getValue ());
  }

  /** Returns an Adjustable representing the vertical scrollbar.
   * The methods setMaximum, setMinimum, and setVisibleAmount should
   * not be called on this Adjustable.  They will throw AWTError if
   * called.
   */
  public Adjustable getVAdjustable ()
  {
    return vscroll;
  }

  /** Returns the size of the viewport.  */
  public Dimension getViewportSize ()
  {
    // Note: according to the online docs, the Insets are
    // automatically updated by the peer to include the scrollbar
    // sizes.
    Insets ins = getInsets ();
    int myw = width - ins.left - ins.right;
    int myh = height - ins.top - ins.bottom;

    Dimension cs;
    if (ncomponents > 0)
      cs = component[0].getPreferredSize ();
    else
      cs = new Dimension (myw, myh);

    // A little optimization -- reuse the Dimension.
    cs.setSize (myw, myh);
    return cs;
  }

  /** Returns the width of the vertical scrollbar.  */
  public int getVScrollbarWidth ()
  {
    if (peer == null)
      return 0;
    ScrollPanePeer spp = (ScrollPanePeer) peer;
    return spp.getVScrollbarWidth ();
  }

  /** Generates a String representation of this ScrollPane's state.  */
  public String paramString ()
  {
    return ("[" + getClass ().getName ()
	    + ": " + ((ncomponents > 0) ? component[0].paramString () : "")
	    + "]");
  }

  /** Set the layout manager for this component.  ScrollPane has its
   * own layout manager and overrides this method so that the layout
   * manager cannot be changed.
   * @param m The new layout manager (ignored)
   */
  public final void setLayout (LayoutManager m)
  {
    // Nothing.
  }

  /** Sets the scroll position for this ScrollPane.  If the point if
   * out of range it is silently moved within range.
   * @param x The x coordinate
   * @param y The y coordinate
   */
  public void setScrollPosition (int x, int y)
  {
    // According to the JCL we throw a NullPointerException if there
    // is no child.
    if (ncomponents == 0)
      throw new NullPointerException ("no child in ScrollPane");

    Dimension child_d = component[0].getPreferredSize ();
    Dimension our_d = getViewportSize ();

    int xmax = Math.max (0, child_d.width - our_d.width);
    int ymax = Math.max (0, child_d.height - our_d.height);

    if (x < 0)
      x = 0;
    else if (x > xmax)
      x = xmax;
    if (y < 0)
      y = 0;
    else if (y > ymax)
      y = ymax;

    ScrollPanePeer spp = (ScrollPanePeer) peer;
    spp.setScrollPosition (x, y);
  }

  /** Sets the scroll position for this ScrollPane.  If the point if
   * out of range it is silently moved within range.
   * @param p The new point
   */
  public void setScrollPosition (Point p)
  {
    setScrollPosition (p.x, p.y);
  }

  // This implements the Adjustable for each scrollbar.  The
  // expectation is that the peer will look at these objects directly
  // and modify the values in them when the user manipulates the
  // scrollbars.  This has to be done from CNI to bypass Java
  // protection rules.  The peer should also take care of calling the
  // adjustment listeners.
  class ScrollPaneAdjustable implements Adjustable
  {
    AdjustmentListener listeners;
    int orient;
    int unit;
    int block;
    int value;

    public ScrollPaneAdjustable (int orient)
    {
      this.orient = orient;
    }

    public void addAdjustmentListener (AdjustmentListener l)
    {
      listeners = AWTEventMulticaster.add (listeners, l);
    }

    public int getBlockIncrement ()
    {
      return block;
    }

    public int getMaximum ()
    {
      Dimension child_d = component[0].getPreferredSize ();
      Dimension our_d = getViewportSize ();

      int xmax = Math.max (0, child_d.width - our_d.width);
      int ymax = Math.max (0, child_d.height - our_d.height);

      return (orient == Adjustable.HORIZONTAL) ? xmax : ymax;
    }

    public int getMinimum ()
    {
      return 0;
    }

    public int getOrientation ()
    {
      return orient;
    }

    public int getUnitIncrement ()
    {
      return unit;
    }

    public int getValue ()
    {
      return value;
    }

    public int getVisibleAmount ()
    {
      Dimension d = getViewportSize ();
      return (orient == Adjustable.HORIZONTAL) ? d.width : d.height;
    }

    public void removeAdjustmentListener (AdjustmentListener l)
    {
      listeners = AWTEventMulticaster.remove (listeners, l);
    }

    public void setBlockIncrement (int b)
    {
      throw new AWTError ("can't use setBlockIncrement on this Adjustable");
    }

    public void setMaximum (int max)
    {
      throw new AWTError ("can't use setMaximum on this Adjustable");
    }

    public void setMinimum (int min)
    {
      throw new AWTError ("can't use setMinimum on this Adjustable");
    }

    public void setUnitIncrement (int u)
    {
      unit = u;
      if (peer != null)
	{
	  ScrollPanePeer spp = (ScrollPanePeer) peer;
	  spp.setUnitIncrement (this, u);
	}
    }

    public void setValue (int v)
    {
      value = v;
      if (peer != null)
	{
	  ScrollPanePeer spp = (ScrollPanePeer) peer;
	  spp.setValue (this, v);
	}
    }

    public void setVisibleAmount (int v)
    {
      throw new AWTError ("can't use setVisibleAmount on this Adjustable");
    }
  }

  ScrollPaneAdjustable hscroll
    = new ScrollPaneAdjustable (Adjustable.HORIZONTAL);
  ScrollPaneAdjustable vscroll
    = new ScrollPaneAdjustable (Adjustable.VERTICAL);
  int policy;
}