buoy.widget

Class OverlayContainer


public class OverlayContainer
extends WidgetContainer

OverlayContainer is a WidgetContainer which overlays its children on top of each other. Every child Widget is sized to fill the entire container, and the preferred size of the container is equal to the largest preferred size of any of its children.

OverlayContainers are generally used to switch between several different Widgets which all appear in the same position. When used this way, only one of the Widgets will be visible at any time. The setVisibleChild() method provides an easy way to switch between the child Widgets by showing one and hiding all the others.

Another use of OverlayContainer is to place a partially transparent Widget in front of another one. This allows an "overlay" to be displayed at a particular position, independent of the positions of other Widgets in the window. An OverlayContainer can also be used to block mouse events from reaching a Widget. Mouse events are always delivered to the topmost visible Widget that has requested to receive them. Suppose that mainWidget is a Widget (possibly a WidgetContainer with many children). The following code will block all mouse events from reaching it and its children:

 CustomWidget blocker = new CustomWidget();
 blocker.setOpaque(false);
 blocker.addEventLink(WidgetMouseEvent.class, new Object() {
   void processEvent()
   {
     // Ignore the event.
   }
 });
 OverlayContainer overlay = new OverlayContainer();
 overlay.add(mainWidget);
 overlay.add(blocker);
 

You can then turn event blocking on or off by calling blocker.setVisible(true) or blocker.setVisible(false). Note that we have added an event link to blocker which listens for mouse events. If you do not do this, it will ignore mouse events and let them pass through to the Widget underneath.

In addition to the event types generated by all Widgets, OverlayContainers generate the following event types:

Author:
Peter Eastman

Constructor Summary

OverlayContainer()
Create a new OverlayContainer.

Method Summary

void
add(Widget widget)
Add a Widget to this container.
void
add(Widget widget, int index)
Add a Widget to this container.
Widget
getChild(int i)
Get the i'th child of this container.
int
getChildCount()
Get the number of children in this container.
int
getChildIndex(Widget widget)
Get the index of a particular Widget.
Iterator
getChildren()
Get an Iterator listing all child Widgets.
Dimension
getMinimumSize()
Get the smallest size at which this Widget can reasonably be drawn.
Dimension
getPreferredSize()
Get the preferred size at which this Widget will look best.
void
layoutChildren()
Layout the child Widgets.
void
remove(Widget widget)
Remove a child Widget from this container.
void
removeAll()
Remove all child Widgets from this container.
void
setVisibleChild(Widget child)
Set a particular child Widget to be visible, and all others to be not visible.
void
setVisibleChild(int i)
Set the i'th child Widget to be visible, and all others to be not visible.

Methods inherited from class buoy.widget.WidgetContainer

getChildCount, getChildren, isOpaque, layoutChildren, remove, removeAll, setOpaque

Methods inherited from class buoy.widget.Widget

addEventLink, dispatchEvent, getBackground, getBounds, getComponent, getCursor, getFont, getMaximumSize, getMinimumSize, getName, getParent, getPreferredSize, hasFocus, isEnabled, isFocusable, isVisible, repaint, requestFocus, setBackground, setCursor, setEnabled, setFocusable, setFont, setName, setVisible

Methods inherited from class buoy.event.EventSource

addEventLink, addEventLink, addEventLink, dispatchEvent, removeEventLink

Constructor Details

OverlayContainer

public OverlayContainer()
Create a new OverlayContainer.

Method Details

add

public void add(Widget widget)
Add a Widget to this container. The new Widget will be placed in front of all other Widgets in the container (at index 0).
Parameters:
widget - the Widget to add

add

public void add(Widget widget,
                int index)
Add a Widget to this container.
Parameters:
widget - the Widget to add
index - the index of the Widget within the container. Widgets with lower indices are displayed in front of ones with higher indices.

getChild

public Widget getChild(int i)
Get the i'th child of this container.

getChildCount

public int getChildCount()
Get the number of children in this container.
Overrides:
getChildCount in interface WidgetContainer

getChildIndex

public int getChildIndex(Widget widget)
Get the index of a particular Widget.
Parameters:
widget - the Widget to locate
Returns:
the index of the Widget within this container

getChildren

public Iterator getChildren()
Get an Iterator listing all child Widgets.
Overrides:
getChildren in interface WidgetContainer

getMinimumSize

public Dimension getMinimumSize()
Get the smallest size at which this Widget can reasonably be drawn. When a WidgetContainer lays out its contents, it will attempt never to make this Widget smaller than its minimum size.
Overrides:
getMinimumSize in interface Widget

getPreferredSize

public Dimension getPreferredSize()
Get the preferred size at which this Widget will look best. When a WidgetContainer lays out its contents, it will attempt to make this Widget as close as possible to its preferred size.
Overrides:
getPreferredSize in interface Widget

layoutChildren

public void layoutChildren()
Layout the child Widgets. This may be invoked whenever something has changed (the size of this WidgetContainer, the preferred size of one of its children, etc.) that causes the layout to no longer be correct. If a child is itself a WidgetContainer, its layoutChildren() method will be called in turn.
Overrides:
layoutChildren in interface WidgetContainer

remove

public void remove(Widget widget)
Remove a child Widget from this container.
Overrides:
remove in interface WidgetContainer
Parameters:
widget - the Widget to remove

removeAll

public void removeAll()
Remove all child Widgets from this container.
Overrides:
removeAll in interface WidgetContainer

setVisibleChild

public void setVisibleChild(Widget child)
Set a particular child Widget to be visible, and all others to be not visible.

setVisibleChild

public void setVisibleChild(int i)
Set the i'th child Widget to be visible, and all others to be not visible.

Written by Peter Eastman.