package java.awt;
import java.awt.event.AdjustmentListener;
import java.awt.peer.ScrollPanePeer;
public class ScrollPane extends Container
{
public static final int SCROLLBARS_AS_NEEDED = 0;
public static final int SCROLLBARS_ALWAYS = 1;
public static final int SCROLLBARS_NEVER = 2;
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);
}
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);
}
public void addNotify ()
{
if (peer == null)
peer = getToolkit ().createScrollPane (this);
super.addNotify ();
}
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);
setScrollPosition (hscroll.getValue (), vscroll.getValue ());
}
public Adjustable getHAdjustable ()
{
return hscroll;
}
public int getHScrollbarHeight ()
{
if (peer == null)
return 0;
ScrollPanePeer spp = (ScrollPanePeer) peer;
return spp.getHScrollbarHeight ();
}
public int getScrollbarDisplayPolicy ()
{
return policy;
}
public Point getScrollPosition ()
{
return new Point (hscroll.getValue (), vscroll.getValue ());
}
public Adjustable getVAdjustable ()
{
return vscroll;
}
public Dimension getViewportSize ()
{
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);
cs.setSize (myw, myh);
return cs;
}
public int getVScrollbarWidth ()
{
if (peer == null)
return 0;
ScrollPanePeer spp = (ScrollPanePeer) peer;
return spp.getVScrollbarWidth ();
}
public String paramString ()
{
return ("[" + getClass ().getName ()
+ ": " + ((ncomponents > 0) ? component[0].paramString () : "")
+ "]");
}
public final void setLayout (LayoutManager m)
{
}
public void setScrollPosition (int x, int y)
{
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);
}
public void setScrollPosition (Point p)
{
setScrollPosition (p.x, p.y);
}
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;
}