buoy.widget
Class OverlayContainer

java.lang.Object
  extended by buoy.event.EventSource
      extended by buoy.widget.Widget
          extended by buoy.widget.WidgetContainer
              extended by buoy.widget.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.
 java.util.Collection<Widget> getChildren()
          Get a Collection containing all child Widgets of this container.
 javax.swing.JPanel getComponent()
          Get the java.awt.Component corresponding to this Widget.
 java.awt.Dimension getMinimumSize()
          Get the smallest size at which this Widget can reasonably be drawn.
 java.awt.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(int i)
          Set the i'th child Widget to be visible, and all others to be not visible.
 void setVisibleChild(Widget child)
          Set a particular child Widget to be visible, and all others to be not visible.
 
Methods inherited from class buoy.widget.WidgetContainer
isOpaque, setOpaque
 
Methods inherited from class buoy.widget.Widget
addEventLink, dispatchEvent, getBackground, getBounds, getCursor, getFont, getMaximumSize, getName, getParent, hasFocus, isEnabled, isFocusable, isVisible, repaint, requestFocus, setBackground, setCursor, setEnabled, setFocusable, setFont, setName, setVisible
 
Methods inherited from class buoy.event.EventSource
addEventLink, addEventLink, removeEventLink
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OverlayContainer

public OverlayContainer()
Create a new OverlayContainer.

Method Detail

getComponent

public javax.swing.JPanel getComponent()
Description copied from class: Widget
Get the java.awt.Component corresponding to this Widget.

Overrides:
getComponent in class Widget

getChildCount

public int getChildCount()
Get the number of children in this container.

Specified by:
getChildCount in class WidgetContainer

getChild

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


getChildren

public java.util.Collection<Widget> getChildren()
Get a Collection containing all child Widgets of this container.

Specified by:
getChildren in class 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

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.

Specified by:
layoutChildren in class WidgetContainer

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.

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

remove

public void remove(Widget widget)
Remove a child Widget from this container.

Specified by:
remove in class WidgetContainer
Parameters:
widget - the Widget to remove

removeAll

public void removeAll()
Remove all child Widgets from this container.

Specified by:
removeAll in class WidgetContainer

setVisibleChild

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


setVisibleChild

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


getMinimumSize

public java.awt.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 class Widget

getPreferredSize

public java.awt.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 class Widget


Written by Peter Eastman.