/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2007-2008 Sun Microsystems, Inc.
*/
/**
* An interface for incrementally building a command-line menu.
*
* @param <T>
* The type of value returned by the call-backs. Use
* <code>Void</code> if the call-backs do not return a
* value.
*/
public final class MenuBuilder<T> {
/**
* A simple menu option call-back which is a composite of zero or
* more underlying call-backs.
*
* @param <T>
* The type of value returned by the call-back.
*/
// The list of underlying call-backs.
/**
* Creates a new composite call-back with the specified set of
* call-backs.
*
* @param callbacks
* The set of call-backs.
*/
}
/**
* {@inheritDoc}
*/
// Throw away all the other results.
return result;
} else {
}
}
}
}
/**
* Underlying menu implementation generated by this menu builder.
*
* @param <T>
* The type of value returned by the call-backs. Use
* <code>Void</code> if the call-backs do not return a
* value.
*/
// Indicates whether the menu will allow selection of multiple
// numeric options.
private final boolean allowMultiSelect;
// The application console.
// The call-back lookup table.
// The char options table builder.
// The call-back for the optional default action.
// The description of the optional default action.
// The numeric options table builder.
// The table printer.
// The menu prompt.
// The menu title.
// The maximum number of times we display the menu if the user provides
// bad input (-1 for unlimited).
private int nMaxTries;
// Private constructor.
int nMaxTries) {
this.allowMultiSelect = allowMultiSelect;
this.defaultCallback = defaultCallback;
this.defaultDescription = defaultDescription;
}
/**
* {@inheritDoc}
*/
// The validation call-back which will be used to determine the
// action call-back.
new ValidationCallback<MenuCallback<T>>() {
if (defaultCallback != null) {
return defaultCallback;
} else if (allowMultiSelect) {
return null;
} else {
return null;
}
} else if (allowMultiSelect) {
// Use a composite call-back to collect all the results.
// Make sure that there are no duplicates.
return null;
return null;
} else {
}
}
return new CompositeCallback<T>(cl);
return null;
} else {
}
}
};
// Determine the correct choice prompt.
if (allowMultiSelect) {
if (defaultDescription != null) {
} else {
}
} else {
if (defaultDescription != null) {
} else {
}
}
// If the user selects help then we need to loop around and
// display the menu again.
while (true) {
// Display the menu.
}
}
}
cprinter.setDisplayHeadings(false);
}
// Get the user's choice.
MenuCallback<T> choice;
if (nMaxTries != -1)
{
}
else
{
}
// Invoke the user's selected choice.
// Determine if the help needs to be displayed, display it and
// start again.
return result;
} else {
}
}
}
}
/**
* A simple menu option call-back which does nothing but return the
* provided menu result.
*
* @param <T>
* The type of result returned by the call-back.
*/
// The result to be returned by this call-back.
// Private constructor.
}
/**
* {@inheritDoc}
*/
return result;
}
}
// The multiple column display threshold.
// Indicates whether the menu will allow selection of multiple
// numeric options.
private boolean allowMultiSelect = false;
// The application console.
// The char option call-backs.
new ArrayList<MenuCallback<T>>();
// The char option keys (must be single-character messages).
// The synopsis of char options.
// Optional column headings.
// Optional column widths.
// The call-back for the optional default action.
// The description of the optional default action.
// The numeric option call-backs.
new ArrayList<MenuCallback<T>>();
// The numeric option fields.
// The menu title.
// The menu prompt.
// The maximum number of times that we allow the user to provide an invalid
// answer (-1 if unlimited).
/**
* Creates a new menu.
*
* @param app
* The application console.
*/
}
/**
* Creates a "back" menu option. When invoked, this option will
* return a {@code MenuResult.cancel()} result.
*
* @param isDefault
* Indicates whether this option should be made the menu
* default.
*/
MenuResult.<T> cancel());
if (isDefault) {
}
}
/**
* Creates a "cancel" menu option. When invoked, this option will
* return a {@code MenuResult.cancel()} result.
*
* @param isDefault
* Indicates whether this option should be made the menu
* default.
*/
if (isDefault) {
}
}
/**
* Adds a menu choice to the menu which will have a single letter as
* its key.
*
* @param c
* The single-letter message which will be used as the key
* for this option.
* @param description
* The menu option description.
* @param callback
* The call-back associated with this option.
*/
MenuCallback<T> callback) {
}
/**
* Adds a menu choice to the menu which will have a single letter as
* its key and which returns the provided result.
*
* @param c
* The single-letter message which will be used as the key
* for this option.
* @param description
* The menu option description.
* @param result
* The menu result which should be returned by this menu
* choice.
*/
MenuResult<T> result) {
}
/**
* Creates a "help" menu option which will use the provided help
* call-back to display help relating to the other menu options.
* When the help menu option is selected help will be displayed and
* then the user will be shown the menu again and prompted to enter
* a choice.
*
* @param callback
* The help call-back.
*/
return MenuResult.again();
}
};
wrapper);
}
/**
* Adds a menu choice to the menu which will have a numeric key.
*
* @param description
* The menu option description.
* @param callback
* The call-back associated with this option.
* @param extraFields
* Any additional fields associated with this menu option.
* @return Returns the number associated with menu choice.
*/
Message... extraFields) {
if (extraFields != null) {
}
return numericCallbacks.size();
}
/**
* Adds a menu choice to the menu which will have a numeric key and
* which returns the provided result.
*
* @param description
* The menu option description.
* @param result
* The menu result which should be returned by this menu
* choice.
* @param extraFields
* Any additional fields associated with this menu option.
* @return Returns the number associated with menu choice.
*/
Message... extraFields) {
}
/**
* Creates a "quit" menu option. When invoked, this option will
* return a {@code MenuResult.quit()} result.
*/
public void addQuitOption() {
MenuResult.<T> quit());
}
/**
* Sets the flag which indicates whether or not the menu will permit
* multiple numeric options to be selected at once. Users specify
* multiple choices by separating them with a comma. The default is
* <code>false</code>.
*
* @param allowMultiSelect
* Indicates whether or not the menu will permit multiple
* numeric options to be selected at once.
*/
this.allowMultiSelect = allowMultiSelect;
}
/**
* Sets the optional column headings. The column headings will be
* displayed above the menu options.
*
* @param headings
* The optional column headings.
*/
this.columnHeadings.clear();
}
}
/**
* Sets the optional column widths. A value of zero indicates that
* the column should be expandable, a value of <code>null</code>
* indicates that the column should use its default width.
*
* @param widths
* The optional column widths.
*/
this.columnWidths.clear();
}
}
/**
* Sets the optional default action for this menu. The default
* action call-back will be invoked if the user does not specify an
* option and just presses enter.
*
* @param description
* A short description of the default action.
* @param callback
* The call-back associated with the default action.
*/
}
/**
* Sets the optional default action for this menu. The default
* action call-back will be invoked if the user does not specify an
* option and just presses enter.
*
* @param description
* A short description of the default action.
* @param result
* The menu result which should be returned by default.
*/
}
/**
* Sets the number of numeric options required to trigger
* multiple-column display. A negative value (the default) indicates
* that the numeric options will always be displayed in a single
* column. A value of 0 indicates that numeric options will always
* be displayed in multiple columns.
*
* @param threshold
* The number of numeric options required to trigger
* multiple-column display.
*/
}
/**
* Sets the optional menu prompt. The prompt will be displayed above
* the menu. Menus do not have a prompt by default.
*
* @param prompt
* The menu prompt, or <code>null</code> if there is not
* prompt.
*/
}
/**
* Sets the optional menu title. The title will be displayed above
* the menu prompt. Menus do not have a title by default.
*
* @param title
* The menu title, or <code>null</code> if there is not
* title.
*/
}
/**
* Creates a menu from this menu builder.
*
* @return Returns the new menu.
*/
// Determine whether multiple columns should be used for numeric
// options.
boolean useMultipleColumns = false;
useMultipleColumns = true;
}
// Create optional column headers.
if (!columnHeadings.isEmpty()) {
} else {
}
}
if (useMultipleColumns) {
} else {
}
}
}
}
// Add the numeric options first.
if (useMultipleColumns) {
// Display in two columns the first column should contain half
// the options. If there are an odd number of columns then the
// first column should contain an additional option (e.g. if
// there are 23 options, the first column should contain 12
// options and the second column 11 options).
rows /= 2;
}
} else {
}
}
// Second column.
if (useMultipleColumns && (j < sz)) {
} else {
}
}
}
}
// Add the char options last.
}
// Configure the table printer.
if (columnHeadings.isEmpty()) {
printer.setDisplayHeadings(false);
} else {
printer.setDisplayHeadings(true);
}
if (columnWidths.isEmpty()) {
if (useMultipleColumns) {
}
} else {
if (j != null) {
// Skip the option key column.
if (useMultipleColumns) {
}
}
}
}
}
/**
* Sets the maximum number of tries that the user can provide an invalid
* value in the menu. -1 for unlimited tries (the default). If this limit is
* reached a CLIException will be thrown.
* @param nTries the maximum number of tries.
*/
{
}
}