/* File: MenuSupport.java */ package O2PlibSS.gui; import java.util.*; import java.awt.*; import java.awt.event.*; /** * Class MenuSupport contains various Menu support * functions for helping implement and support dynamic submenus * of MenuItem or CheckboxMenuItem types. It tracks both entities * in separate hash tables by either their associated command string * or their MenuItem or CheckboxMenuItem instances. It supports * radio groups using a common command name prefix ending in ':'. * E.g., "Open:Sample:" with "Open:Sample:a1" "Open:Sample:a2" * and "Open:Sample:a2" being in the same radio group. The event * handler is in the parent calling these methods. *
* NOTE: event handlers are in the parent. *
*
* This code is available at the HTMLtools project on SourceForge at * http://htmltools.sourceforge.org/ * under the "Common Public License Version 1.0" * * http://www.opensource.org/licenses/cpl1.0.php.
** It was derived and refactored from the open source * MAExplorer (http://maexplorer.sourceforge.org/), and * Open2Dprot (http://Open2Dprot.sourceforge.net/) Table modules. *
* $Date: 2009/07/03 11:45:56 $ $Revision: 1.28 $
*
* Copyright 2008, 2009 by Peter Lemkin
* E-Mail: lemkin@users.sourceforge.net
* http://lemkingroup.com/
*/
public class MenuSupport
{
/** Initial max number of menu items allowed. */
final public static int
MAX_CMDS= 101; /* was 101, 3001, 1001, 501 */
/** max number of menu checkbox items allowed */
final public static int
MAX_CHKBOX_CMDS= 501;
/** Initial max number of menu items allowed. */
public int
maxMenuItems= MAX_CMDS;
/** initial max number of menu checkbox items allowed */
public int
maxCheckboxMenuItems= MAX_CHKBOX_CMDS;
/** Name of this MenuSupport instance */
public String
name;
/** Hashtable of ALL menu items. The key is the MenuItem.
* The value is the MenuCmdData.
*/
public Hashtable
menuItemDataHT;
/** Hashtable of ALL menu items. The key is the command.
* The value is the MenuCmdData.
*/
public Hashtable
menuItemCmdHT;
/** Hashtable of ALL checkboxItem menu items.
* The key is the CheckboxMenuItem.
* The value is the ChkBoxMenuData intance.
*/
public Hashtable
chkBoxMenuItemDataHT;
/** Hashtable of ALL checkboxItem menu items.
* The key is the command.
* The value is the ChkBoxMenuData intance.
*/
public Hashtable
chkBoxMenuItemCmdHT;
/**
* MenuSupport() - Constructor. use default hash table sizes.
*/
public MenuSupport()
{ /* MenuSupport */
maxMenuItems= MAX_CMDS;
maxCheckboxMenuItems= MAX_CHKBOX_CMDS;
init("none", maxMenuItems, maxCheckboxMenuItems);
} /* MenuSupport */
/**
* MenuSupport() - Constructor. use default hash table sizes.
* @param name of this menu support instance
*/
public MenuSupport(String name)
{ /* MenuSupport */
maxMenuItems= MAX_CMDS;
maxCheckboxMenuItems= MAX_CHKBOX_CMDS;
init(name, maxMenuItems, maxCheckboxMenuItems);
} /* MenuSupport */
/**
* MenuSupport() - Constructor.
* @param name of this menu support instance
* @param maxMenuItems - initial maximum size of Menu Items in menubar
* @param maxCheckboxMenuItems - initial maximum size of Checkbox Menu
* Items in menubar
*/
public MenuSupport(String name, int maxMenuItems, int maxCheckboxMenuItems)
{ /* MenuSupport */
init(name, maxMenuItems, maxCheckboxMenuItems);
} /* MenuSupport */
/**
* init() - create empty hash tables.
* @param name of this menu support instance
* @param maxMenuItems - initial maximum size of Menu Items in menubar
* @param maxCheckboxMenuItems - initial maximum size of Checkbox Menu
* Items in menubar
*/
public void init(String name, int maxMenuItems, int maxCheckboxMenuItems)
{ /* init */
this.name= name;
this.maxMenuItems= maxMenuItems;
this.maxCheckboxMenuItems= maxCheckboxMenuItems;
/* setup menu item and checkbox menu item hash boxes */
menuItemDataHT= new Hashtable(MAX_CMDS);
menuItemCmdHT= new Hashtable(MAX_CMDS);
chkBoxMenuItemDataHT= new Hashtable(MAX_CHKBOX_CMDS);
chkBoxMenuItemCmdHT= new Hashtable(MAX_CHKBOX_CMDS);
} /* init */
/**
* makeMenuItem() - make menuItem entry in menu list.
* Setup action command and listener call back.
* If the command name is null, set the command name to label name.
* if shortCut is <0, then gray-out the item. i.e. not available and
* do not add to event handler.
* @param fm is the frame that contains Action listener for this item
* @param pm is the menu to install new MenuItem
* @param sLabel is the visible label
* @param sCmd is the opt Cmd name (uses sLabel if null)
* @param shortcut is the opt short cut
* @return the menu item
*/
public MenuItem makeMenuItem(Frame fm, Menu pm, String sLabel, String sCmd,
int shortcut )
{ /*makeMenuItem */
MenuItem mi= new MenuItem(sLabel);
if(sCmd==null)
sCmd= sLabel; /* use same string for both */
mi.setActionCommand(sCmd); /* separate string for CMD & LABEL*/
if(shortcut>=0)
{ /* actionListener in this class */
mi.addActionListener((ActionListener)fm);
}
else
mi.setEnabled(false);
/* Put menu item into MenuCmdData hash tables */
menuItemDataHT.put((Object)mi,
(Object)(new MenuCmdData(sLabel,sCmd,mi)));
menuItemCmdHT.put(sCmd,
(Object)(new MenuCmdData(sLabel,sCmd,mi)));
if(shortcut>0)
{
MenuShortcut
msc= new MenuShortcut(shortcut);
mi.setShortcut(msc); /* optional shortcut */
}
pm.add(mi);
return(mi);
} /* makeMenuItem */
/**
* makeSubMenu() - make submenu entry in menu list.
* Setup action command and listener call back.
* If the command name is null, set the command name to label name.
* @param fm is the frame
* @param pm is the menu to install new submenu in
* @param sLabel is the visible label
* @param sCmd is the opt Cmd name (uses sLabel if null)
* @param shortcut is the opt short cut
*/
public Menu makeSubMenu(Frame fm, Menu pm, String sLabel, String sCmd,
int shortcut)
{ /* makeSubMenu*/
Menu mi= new Menu(sLabel);
if(shortcut!=0)
{
MenuShortcut msc= new MenuShortcut(shortcut);
mi.setShortcut(msc); /* optional shortcut */
}
pm.add(mi);
return(mi);
} /* makeSubMenu*/
/**
* makeChkBoxMenuItem() - make CheckboxMenuItem entry in popup menu list.
* Setup action command and listener call back.
* If the command name is null, set the command name to label name.
* if shortCut is <0, then gray-out the item. i.e. not available and
* do not add to event handler.
* @param fm is the frame that contains Item listener for this item
* @param pm is the menu to install new CheckboxMenuItem in
* @param sLabel is the visible label
* @param sCmd is the opt Cmd name (uses sLabel if null)
* @param shortcut is the opt short cut
* @param value is the initial value of the checkbox
*/
public CheckboxMenuItem makeChkBoxMenuItem(Frame fm, Menu pm, String sLabel,
String sCmd, int shortcut,
boolean value )
{ /* makeChkBoxMenuItem */
CheckboxMenuItem cbmi= new CheckboxMenuItem(sLabel,value);
cbmi.setState(value);
if(sCmd==null)
sCmd= sLabel; /* use same string for both */
cbmi.setActionCommand(sCmd); /* separate string for CMD and LABEL */
if(shortcut>=0)
{ /* actionListener in this class */
cbmi.addItemListener((ItemListener)fm);
}
else
cbmi.setEnabled(false);
if(shortcut>0)
{
MenuShortcut msc= new MenuShortcut(shortcut);
cbmi.setShortcut(msc); /* optional shortcut */
}
/* Put menu item into ChkBoxMenuData hash tables */
chkBoxMenuItemDataHT.put((Object)cbmi,
(Object)(new ChkBoxMenuData(sLabel,sCmd,cbmi)));
chkBoxMenuItemCmdHT.put(sCmd,
(Object)(new ChkBoxMenuData(sLabel,sCmd,cbmi)));
pm.add(cbmi);
return(cbmi);
} /* makeChkBoxMenuItem*/
/**
* lookupMenuItem() - lookup MenuItem entry in menu list.
* @param sCmd is the opt Cmd name to search the hash table
* for (key,value)=(cmd,MenuCmdData) and returning the MenuItem
* if found.
* @return the MenuItem instance if found, else null
*/
public MenuItem lookupMenuItem(String sCmd)
{ /* lookupMenuItem */
if(sCmd==null)
return(null);
MenuCmdData mcd= (MenuCmdData)menuItemCmdHT.get(sCmd);
if(mcd==null)
return(null);
MenuItem mi= mcd.mi;
return(mi);
} /* lookupMenuItem */
/**
* lookupMenuItem() - lookup MenuItem entry by MenuItem in menu list.
* @param mi is the MenuItem instance to searching the hash table
* for (key,value)=(MenuItem,MenuCmdData) and returning the command
* if found.
* @param miDataHT is the hash table storing
* @return the menu item command name if found, else null
*/
public String lookupMenuItem(MenuItem mi)
{ /* lookupMenuItem */
if(mi==null)
return(null);
MenuCmdData mcd= (MenuCmdData)menuItemDataHT.get(mi);
if(mcd==null)
return(null);
String cmd= mcd.cmd;
return(cmd);
} /* lookupMenuItem */
/**
* lookupChkboxMenuItem() - lookup CheckboxMenuItem entry in menu list
* by searching (key,value)=(cmd,ChkBoxMenuData) and returning the
* CheckboxMenuItem if found.
* @param sCmd is the opt Cmd name to search for
* @param cbmiCmdHT is the hash table storing
* @return the CheckboxMenuItem item instance if found, else null
*/
public CheckboxMenuItem lookupChkboxMenuItem(String sCmd)
{ /* lookupChkboxMenuItem */
if(sCmd==null)
return(null);
ChkBoxMenuData cbmd= (ChkBoxMenuData)chkBoxMenuItemCmdHT.get(sCmd);
if(cbmd==null)
return(null);
CheckboxMenuItem cbmi= cbmd.cbmi;
return(cbmi);
} /* lookupChkboxMenuItem */
/**
* lookupChkboxMenuItem() - lookup String command entry in
* checkbox menu item list by searching
* (key,value)=(CheckboxMenuItem,ChkBoxMenuData) and returning
* the command if found.
* @param cbmi is the CheckboxMenuItem to search for
* @return the string command name of the checkbox menu item if
* found, else null
*/
public String lookupChkboxMenuItem(CheckboxMenuItem cbmi)
{ /* lookupChkboxMenuItem */
if(cbmi==null)
return(null);
ChkBoxMenuData cbmd= (ChkBoxMenuData)chkBoxMenuItemDataHT.get(cbmi);
if(cbmd==null)
return(null);
String cmd= cbmd.cmd;
return(cmd);
} /* lookupChkboxMenuItem */
/**
* lookupMenuItemLabel() - lookup CheckboxMenuItem entry label
* in menu list by searching (key,value)=(cmd,MenuCmdData) and
* returning the menu label if found.
* @param sCmd is the opt Cmd name to search for
* @return the MenuItem item instance menu label if found, else null
*/
public String lookupMenuItemLabel(String sCmd)
{ /* lookupMenuItemLabel */
if(sCmd==null)
return(null);
MenuCmdData mcd= (MenuCmdData)menuItemCmdHT.get(sCmd);
if(mcd==null)
return(null);
String menuLabel= mcd.menuLabel;
return(menuLabel);
} /* lookupMenuItemLabel */
/**
* lookupChkboxMenuItemLabel() - lookup CheckboxMenuItem entry label
* in menu list by searching (key,value)=(cmd,ChkBoxMenuData) and
* returning the menu label if found.
* @param sCmd is the opt Cmd name to search for
* @return the CheckboxMenuItem item instance menu label if found,
* else null
*/
public String lookupChkboxMenuItemLabel(String sCmd)
{ /* lookupChkboxMenuItemLabel */
if(sCmd==null)
return(null);
ChkBoxMenuData cbmd= (ChkBoxMenuData)chkBoxMenuItemCmdHT.get(sCmd);
if(cbmd==null)
return(null);
String menuLabel= cbmd.menuLabel;
return(menuLabel);
} /* lookupChkboxMenuItemLabel */
/**
* clearSublistChkMenuItemStates() - clear all of the checkbox menu items
* in the radio group. The members of the radio
* group are determined by the command prefix up to the last ':' in the
* command name for the checkbox menu item.
* @param cbmi is the CheckboxMenuItem that is part of a radio group.
* @return true if succeed
*/
public boolean clearSublistChkMenuItemStates(String preface)
{ /* clearSublistChkMenuItemStates */
String entryList[]= getCmdListCBMIbyPreface(preface);
if(entryList==null)
return(false);
for(int i=0; i