|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.awt.MenuComponent
java.awt.MenuItem
java.awt.Menu
com.ccg.awt.MenuChoice
public class MenuChoice
Create a flyout menu of choices from a LookupKeyed.
This class is designed to simplify the task of presenting a list
of choices to a user. The current implementation forces the user to
select exactly one of the choices shown. The developer can check
what is currently selected via the getText() and getNumber() methods.
In addition, if you want to monitor when the user makes a new
selection, you can use the addItemListener(java.awt.event.ItemListener) method.
Also, check out the static create(com.ccg.util.Lookup, java.lang.String) method which lets one
dynamically load the menu from a Lookup object. This
simplifies the process of localizing your menus.
create(com.ccg.util.Lookup, java.lang.String),
Demo.html,
Serialized Form| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class java.awt.Menu |
|---|
Menu.AccessibleAWTMenu |
| Nested classes/interfaces inherited from class java.awt.MenuItem |
|---|
MenuItem.AccessibleAWTMenuItem |
| Nested classes/interfaces inherited from class java.awt.MenuComponent |
|---|
MenuComponent.AccessibleAWTMenuComponent |
| Constructor Summary | |
|---|---|
MenuChoice(String name,
boolean isTearOff,
LookupKeyed cf)
Create the menu object and add a bunch of menu items. |
|
| Method Summary | |
|---|---|
void |
addItemListener(ItemListener l)
Monitor the object for changes in selection. |
static MenuChoice |
create(Lookup lookup,
String tag)
The easy way to create localizable flyout menu choices. |
Number |
getNumber()
Get the Number representation of the current selection. |
Object |
getObject()
Get the associated object with the current selection. |
Object[] |
getSelectedObjects()
Get the selected item. |
String |
getText()
Get the current text which has been selected by the user. |
void |
itemStateChanged(ItemEvent e)
Monitor the user's current choice. |
void |
removeItemListener(ItemListener l)
Stop listening for changes in state. |
void |
setNumberFormat(NumberFormat nf)
Set the parser used to convert strings to numeric values. |
void |
setText(String text)
Set which item is currently selected. |
| Methods inherited from class java.awt.Menu |
|---|
add, add, addNotify, addSeparator, countItems, getAccessibleContext, getItem, getItemCount, insert, insert, insertSeparator, isTearOff, paramString, remove, remove, removeAll, removeNotify |
| Methods inherited from class java.awt.MenuItem |
|---|
addActionListener, deleteShortcut, disable, disableEvents, enable, enable, enableEvents, getActionCommand, getActionListeners, getLabel, getListeners, getShortcut, isEnabled, processActionEvent, processEvent, removeActionListener, setActionCommand, setEnabled, setLabel, setShortcut |
| Methods inherited from class java.awt.MenuComponent |
|---|
dispatchEvent, getFont, getName, getParent, getPeer, getTreeLock, postEvent, setFont, setName, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.awt.MenuContainer |
|---|
getFont, postEvent |
| Constructor Detail |
|---|
public MenuChoice(String name,
boolean isTearOff,
LookupKeyed cf)
Menu object (this
object) and adds a bunch of menu choice items to it based on the
contents of the LookupKeyed object passed. At construction
time, none of the available options will be initially
selected. Use the setText(java.lang.String) method if there is a
particular option you want to initially set (or be lazy and use
the create(com.ccg.util.Lookup, java.lang.String) method and let it do the work for you).
name - Name form the menu (same as Menu).tearOff - Do you want a tear off menu? (typically set to false).cf - The LookupKeyed used to initialize the flyout menu
choices with. The string representation of each key in the
LookupKeyed table is added as an available option.create(com.ccg.util.Lookup, java.lang.String)| Method Detail |
|---|
public void setText(String text)
text - New value to set the selected item to. Ok if null or not one
of the allowed values.getText(),
getNumber()public String getText()
getNumber(),
setText(java.lang.String)public Object getObject()
getNumber(),
setText(java.lang.String)public Number getNumber()
Number representation of the current selection.
This method uses the getObject() method to fetch the
data currently associated with the users current selection. It
then takes the string representation of this value and converts
it to to a Number object using either the
systems default number parser, or the specific number parser last
set via setNumberFormat(java.text.NumberFormat).
Number object associated with the currently
selected item, or null if no item is currently selected or
there was a problem with the conversion to a number.getText(),
getObject(),
setNumberFormat(java.text.NumberFormat)public void setNumberFormat(NumberFormat nf)
getNumber() method uses the system default
number parser for parsing strings to numeric values. This may
work for most cases, however if you want to specify a object to
convert the associated data values (from the getObject()
method) to a Number), you can use this
method.
arg1 - Any object derived from NumberFormat which
is capable of converting strings to Numbers.getNumber()public void itemStateChanged(ItemEvent e)
setText(java.lang.String) method directly instead). It is used internally
by this object to monitor when the user selects one of the items
in the list of choices. It gets the text of the selection made
and then invokes the setText(java.lang.String) method to update this
object and notify any interested listeners.
itemStateChanged in interface ItemListenerie - ItemEvent which is occurring.setText(java.lang.String)
public static MenuChoice create(Lookup lookup,
String tag)
MenuChoice object based on the
contents of a Lookup object. This has two nice
advantages. One, it simplifies the process of localizing your
application. Two, it removes the decision of what should appear
in the menu from the code. Using this method, you can use
standard Java property files to:
Consider the following example resource file:
# Options for the size of the last flash marker (under Setting menu) # It's a choose one option menu. Commenting out either value disables # the menu from appearing in the applet Marker.label=Last Marker Marker.choices=48 1632 2000 Marker.default=Medium
If we were to load the above resource file as a ResourceBundle indirectly using the TagLookup class. For example, if we were working on the "MyProj"
class in the "myproject" package, we could store its resources in
"myproject.MyProjStrings" (don't forget the ".poperties"
extension). The following code fragment demonstrates how this
approach could be used:
void foo(myproject.MyProj ms) {
Lookup _strings = new TagLookup(ms.getClass(),null);
MenuChoice mc = MenuChoice.create(rb,"Marker");
}
That's all there is to it, you would have created the menu
choices with the values "Tiny", "Small", "Medium", "Big" and
"CAD" presented to the user. The default menu item would be
"Medium". In addition you would have associated numbers with each
of the choices (remember the getNumber() method?).
lookup - A Lookup object used to find the values for
your strings. See the LookupCreate class to see how you can create these for a
ResourceBundle. Also, the
TagLookup class has a nice way of loading them as
well.tag - Name of your fly out menu in the resource bundle (it was
"Marker" in the above example).
addItemListener(java.awt.event.ItemListener),
getText(),
getNumber()public Object[] getSelectedObjects()
getText() and getNumber()
methods instead. However, in the future, if this object is
extended to support multiple selections, this method may have
more importance.
getSelectedObjects in interface ItemSelectablegetText(),
getNumber()public void addItemListener(ItemListener l)
setText(java.lang.String) method detects that a new valid
selection has been made (one that is different from the current
selection), it notifies all the registered listeners of the
event. Use this method to register an object as a interested
listener.
addItemListener in interface ItemSelectablelistener - The listener that wants to know when the user changes the
current selection.setText(java.lang.String),
getText(),
getNumber()public void removeItemListener(ItemListener l)
removeItemListener in interface ItemSelectablelistener - Listener which no longer cares when the current selection is changed.addItemListener(java.awt.event.ItemListener)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||