package net.xoetrope.awt;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import net.xoetrope.xui.XAppender;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.XStartupObject;
/**
* A wrapper for menus
* <p>Copyright: Copyright (c) Xoetrope Ltd., 2002-2003</p>
* @version $Revision: 2.5 $
*/
public class XMenuBar extends MenuBar implements XAppender
{
private XProject currentProject;
/**
* Create a new menu bar
*/
public XMenuBar()
{
currentProject = XProjectManager.getCurrentProject();
}
/**
* Set an Action for the menu bar - does nothing
* @param action the action object
*/
public void setAction( Object action )
{
}
/**
* Do any final setup of the menu needed. If there is an existing menu bar in
* the application the menus of this menu bar are attached to the existing
* menu bar, unless the menu bar name matches that of the exitsing menu bar,
* in which case the old menu bar is replaced by this one. The menu bar is
* normally named by the name of the page within which it was declared.
*/
public void setup()
{
XStartupObject so = currentProject.getStartupObject();
XMenuBar menuBar = (XMenuBar)so.getApplicationMenuBar();
if (( menuBar == null ) || menuBar.getName().equals( getName())) {
so.setApplicationMenuBar( this );
currentProject.setObject( "MenuBar", this );
}
else {
int numMenus = this.getMenuCount();
for ( int i = 0; i < numMenus; i++ ) {
XMenu menu = (XMenu)getMenu( i );
menuBar.append( menu, menu.getName());
// Decrement the counters as an item has been removed from the menu
numMenus--;
i--;
}
}
}
/**
* Appends the object o to this item
* @param o the appended item
* @param name the caption
*/
public void append( Object o, String name )
{
add( (Menu)o );
}
/**
* Implemented from the XAppender interface. No separator needed at this level.
*/
public void addSeparator(){}
/**
* Get a child object by name
* @param name the name of the menu bar
* @return the child
*/
public Object getObject( String name )
{
int menuCount = getMenuCount();
for ( int i = 0; i < menuCount; i++ ) {
Menu menu = getMenu( i );
Object obj = getObject( menu, name );
if ( obj != null )
return obj;
}
return null;
}
/**
* Get a child object by name
* @param meu the menu or submenu to be searched
* @param name the name of the menu bar
* @return the child
*/
public Object getObject( Object menu, String name )
{
Menu jmenu = (Menu)menu;
int itemCount = jmenu.getItemCount();
for ( int j = 0; j < itemCount; j++ ) {
Object item = jmenu.getItem( j );
if ( item instanceof Menu ) {
Object so = getObject( item, name );
if ( so != null )
return so;
}
else if ( item instanceof MenuItem ) {
String itemName = (( MenuItem )item ).getName();
if ( ( itemName != null ) && itemName.equals( name ))
return item;
}
}
return null;
}
}