com.novell.nds.dirxml.util
Class Menu

java.lang.Object
  extended bycom.novell.nds.dirxml.util.Menu

public class Menu
extends Object

publc * Simple menu system for a command-line environment. Also has a few utility prompting methods.


Field Summary
static int RESPONSE_CANCEL
          Value returned from getYesNoResponse() if user enters other than 'y', 'yes', 'n', or 'no' (localized appropriately).
static int RESPONSE_NO
          Value returned from getYesNoResponse() if user answers negatively.
static int RESPONSE_YES
          Value returned from getYesNoResponse() if user answers positively.
 
Constructor Summary
Menu(String label, String header, String exitPrompt, List userItems)
          Constructor.
 
Method Summary
static int getYesNoResponse(String question)
          Pose a yes/no question.
static int[] parseNumberList(String tokenString)
          Parse a whitespace-, comma-, or semicolon-separated list of integers.
static String promptForData(String prompt)
          Utility routine to gather input from System.in.
static String promptForPassword(String prompt)
          Utility routine to gather input from System.in.
 int run()
          Execute this Menu instance.
 void setAllowMultiSelect(boolean allowMultiSelect)
          Enable or disable the selection of multiple menu items at once.
 void setLabel(String label)
          Set the label for this menu.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESPONSE_CANCEL

public static final int RESPONSE_CANCEL
Value returned from getYesNoResponse() if user enters other than 'y', 'yes', 'n', or 'no' (localized appropriately).

See Also:
getYesNoResponse(java.lang.String), Constant Field Values

RESPONSE_NO

public static final int RESPONSE_NO
Value returned from getYesNoResponse() if user answers negatively.

See Also:
getYesNoResponse(java.lang.String), Constant Field Values

RESPONSE_YES

public static final int RESPONSE_YES
Value returned from getYesNoResponse() if user answers positively.

See Also:
getYesNoResponse(java.lang.String), Constant Field Values
Constructor Detail

Menu

public Menu(String label,
            String header,
            String exitPrompt,
            List userItems)
Constructor.

Parameters:
label - text displayed over all menu items.
header - optional text to be displayed over the menu items
exitPrompt - optional text for exit item. If null, "Exit" is used.
userItems - List of MenuItem instances. A default "Exit" item is always provided, so there must not be an "Exit" item in the list.
Method Detail

setLabel

public void setLabel(String label)
Set the label for this menu. Use this if the label needs to be changed after construction.

Parameters:
label - text displayed over all menu items.

setAllowMultiSelect

public void setAllowMultiSelect(boolean allowMultiSelect)
Enable or disable the selection of multiple menu items at once.

Parameters:
allowMultiSelect - true to allow multiple item selection, false otherwise

promptForData

public static String promptForData(String prompt)
                            throws IOException
Utility routine to gather input from System.in.

Parameters:
prompt - text to display informing the user what to input.
Returns:
String containing entered text (possibly empty if user presses "return" without entering any text).
Throws:
IOException - thrown by underlying System.in InputStream.

promptForPassword

public static String promptForPassword(String prompt)
                                throws IOException
Utility routine to gather input from System.in.

This will attempt to hide the input as it is typed.

Parameters:
prompt - text to display informing the user what to input.
Returns:
String containing entered text (possibly empty if user presses "return" without entering any text).
Throws:
IOException - thrown by underlying System.in InputStream.

getYesNoResponse

public static int getYesNoResponse(String question)
Pose a yes/no question. The question is asked in the following form:

<question>? (yes/no):

Parameters:
question - question text (no question mark).
Returns:
RESPONSE_CANCEL, RESPONSE_NO, RESPONSE_YES.

run

public int run()
        throws IOException
Execute this Menu instance.

Returns:
index of MenuItem selected that caused menu to exit. If the built-in "Exit" item is selected then the index will be equal to the size of the List passed to the constructor.
Throws:
IOException - thrown by underlying System.in InputStream.

parseNumberList

public static int[] parseNumberList(String tokenString)
                             throws NumberFormatException
Parse a whitespace-, comma-, or semicolon-separated list of integers.

Parameters:
tokenString - String containing list to parse.
Returns:
array of ints.
Throws:
NumberFormatException - thrown if a token in tokenString isn't a lexically-valid int according to Java rules.