Adding swt and socket frontends
This commit is contained in:
@@ -53,7 +53,7 @@
|
||||
<parent>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
<artifactId>ebigeon-config</artifactId>
|
||||
<version>1.3</version>
|
||||
<version>1.6.0</version>
|
||||
</parent>
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -42,11 +42,31 @@ import fr.bigeon.gclc.ConsoleManager;
|
||||
|
||||
/** <p>
|
||||
* A command to execute. It is mandatory that it has a name and that name cannot
|
||||
* start with minus character or contain space
|
||||
*
|
||||
* start with minus character or contain space.
|
||||
* <p>
|
||||
* A command can be executed, with parameters that will be provided as an array
|
||||
* of strings.
|
||||
* <p>
|
||||
* The help mechanism can be overwritten, but is by default doing the following:
|
||||
* <ul>
|
||||
* <li>Print the command name
|
||||
* <li>Print the {@link #brief()} message
|
||||
* <li>Print a blank line
|
||||
* <li>Print "Usage:"
|
||||
* <li>Print the usage pattern
|
||||
* <li>Print the usage details
|
||||
* </ul>
|
||||
* <p>
|
||||
* The default behavior for the brief message is to print the tip preceeded by a
|
||||
* couple of spaces.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public abstract class Command {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final String EOL_LINUX = "\n"; //$NON-NLS-1$
|
||||
/** The name of the command */
|
||||
protected final String name;
|
||||
|
||||
@@ -64,7 +84,19 @@ public abstract class Command {
|
||||
/** @param args the arguments of the command (some expect an empty array) */
|
||||
public abstract void execute(String... args);
|
||||
|
||||
/** This prints the help associated to this command
|
||||
/** This prints the help associated to this command.
|
||||
* <p>
|
||||
* The default behavior is to print:
|
||||
*
|
||||
* <pre>
|
||||
* [Command name]
|
||||
* [brief message]
|
||||
*
|
||||
* Usage:
|
||||
* [Usage pattern]
|
||||
*
|
||||
* [Usage details]
|
||||
* </pre>
|
||||
*
|
||||
* @param manager the manager to print the data
|
||||
* @param args the arguments called with the help */
|
||||
@@ -75,16 +107,30 @@ public abstract class Command {
|
||||
manager.println("Usage:"); //$NON-NLS-1$
|
||||
manager.println(usagePattern());
|
||||
manager.println();
|
||||
manager.print(usageDetail());
|
||||
String details = usageDetail();
|
||||
if (details != null && !details.isEmpty()) {
|
||||
manager.print(details);
|
||||
if (!(details.endsWith(EOL_LINUX) || details.endsWith(System.lineSeparator()))) {
|
||||
manager.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the detailed help (should end with end of line or be empty) */
|
||||
/** <p>
|
||||
* This method return the detail of the help. It immediatly follows the
|
||||
* {@link #usagePattern() usage pattern}.
|
||||
*
|
||||
* @return the detailed help (should end with end of line or be empty) */
|
||||
@SuppressWarnings("static-method")
|
||||
protected String usageDetail() {
|
||||
return new String();
|
||||
}
|
||||
|
||||
/** @return the usage pattern */
|
||||
/** <p>
|
||||
* This prints the usage pattern for the command. It follows the brief
|
||||
* introduction on the command ({@link #brief()})
|
||||
*
|
||||
* @return the usage pattern */
|
||||
protected String usagePattern() {
|
||||
return getCommandName();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,6 @@
|
||||
package fr.bigeon.gclc.command;
|
||||
|
||||
import fr.bigeon.gclc.ConsoleManager;
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
import fr.bigeon.gclc.prompt.CLIPrompterMessages;
|
||||
|
||||
/** <p>
|
||||
@@ -55,15 +54,11 @@ public class HelpExecutor extends Command {
|
||||
|
||||
/** @param cmdName the command name
|
||||
* @param consoleManager the manager for the console
|
||||
* @param cmd the command to execute the help of
|
||||
* @throws InvalidCommandName if the name is invalid */
|
||||
public HelpExecutor(String cmdName, ConsoleManager consoleManager,
|
||||
Command cmd) throws InvalidCommandName {
|
||||
* @param cmd the command to execute the help of */
|
||||
public HelpExecutor(String cmdName, ConsoleManager consoleManager, Command cmd) {
|
||||
super(cmdName);
|
||||
this.cmd = cmd;
|
||||
if (consoleManager == null)
|
||||
throw new NullPointerException(
|
||||
"Argument cannot be null: ConsoleManager"); //$NON-NLS-1$
|
||||
if (consoleManager == null) throw new NullPointerException("Argument cannot be null: ConsoleManager"); //$NON-NLS-1$
|
||||
this.consoleManager = consoleManager;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,8 @@ public class CLIPrompter {
|
||||
private static final String LIST_DISP_KEY = "promptlist.exit.dispkey"; //$NON-NLS-1$
|
||||
@SuppressWarnings("javadoc")
|
||||
private static final String PROMPT = "prompt.lineprompt"; //$NON-NLS-1$
|
||||
@SuppressWarnings("javadoc")
|
||||
private static final String LIST_CHOICE_SEP = "promptlist.multi.sepkey"; //$NON-NLS-1$
|
||||
|
||||
/** @param manager the manager
|
||||
* @param prompt the prompting message
|
||||
@@ -268,10 +270,95 @@ public class CLIPrompter {
|
||||
return choices.get(index);
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param <U> The choices labels type
|
||||
* @param <T> The real choices objects
|
||||
* @param choices the list of labels (in order to be displayed)
|
||||
* @param choicesMap the map of label to actual objects
|
||||
* @param message the prompting message
|
||||
* @return the chosen objects (or an empty list) */
|
||||
public static <U, T> List<T> promptMultiChoice(ConsoleManager manager, List<U> choices, Map<U, T> choicesMap,
|
||||
String message) {
|
||||
List<Integer> chs = promptMultiChoice(manager, choices, message);
|
||||
List<T> userChoices = new ArrayList<>();
|
||||
for (Integer integer : chs) {
|
||||
userChoices.add(choicesMap.get(integer));
|
||||
}
|
||||
return userChoices;
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param <U> The choices labels type
|
||||
* @param <T> The real choices objects
|
||||
* @param choicesMap the map of label to actual objects
|
||||
* @param message the prompting message
|
||||
* @return the chosen objects */
|
||||
public static <U, T> List<T> promptMultiChoice(ConsoleManager manager, Map<U, T> choicesMap, String message) {
|
||||
return promptMultiChoice(manager, new ArrayList<>(choicesMap.keySet()), choicesMap, message);
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param <U> the type of choices
|
||||
* @param choices the list of choices
|
||||
* @param message the prompting message
|
||||
* @return the indices of the choices */
|
||||
@SuppressWarnings("boxing")
|
||||
public static <U> List<Integer> promptMultiChoice(ConsoleManager manager, List<U> choices, String message) {
|
||||
manager.println(message);
|
||||
int index = listChoices(manager, choices, null);
|
||||
String result = ""; //$NON-NLS-1$
|
||||
boolean keepOn = true;
|
||||
List<Integer> chs = new ArrayList<>();
|
||||
while (keepOn) {
|
||||
keepOn = false;
|
||||
result = manager.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
String[] vals = result.split(CLIPrompterMessages.getString(LIST_CHOICE_SEP));
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
if (vals[i].isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
int r = Integer.parseInt(vals[i]);
|
||||
if (r >= 0 && r < index) {
|
||||
chs.add(r);
|
||||
} else {
|
||||
manager.println(CLIPrompterMessages.getString("promptchoice.outofbounds", 0, index)); //$NON-NLS-1$
|
||||
listChoices(manager, choices, null);
|
||||
keepOn = true;
|
||||
break;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
keepOn = true;
|
||||
manager.println(CLIPrompterMessages.getString("promptchoice.formaterr", 0, index)); //$NON-NLS-1$
|
||||
listChoices(manager, choices, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return chs;
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param keys the keys to be printed
|
||||
* @param choices the real choices
|
||||
* @param message the message
|
||||
* @param <U> the type of elements
|
||||
* @return the choice */
|
||||
public static <U> List<U> promptMultiChoice(ConsoleManager manager, List<String> keys, List<U> choices,
|
||||
String message) {
|
||||
List<Integer> indices = promptMultiChoice(manager, keys, message);
|
||||
List<U> userChoices = new ArrayList<>();
|
||||
for (Integer integer : indices) {
|
||||
userChoices.add(choices.get(integer.intValue()));
|
||||
}
|
||||
return userChoices;
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param choices the choices
|
||||
* @param cancel the cancel option if it exists
|
||||
* @return the number of choices plus one */
|
||||
* @return the number of choices plus one (or the number of choices if there
|
||||
* is a cancel) */
|
||||
private static <U> int listChoices(ConsoleManager manager, List<U> choices,
|
||||
String cancel) {
|
||||
int index = 0;
|
||||
@@ -279,9 +366,9 @@ public class CLIPrompter {
|
||||
manager.println((index++) + ") " + u); //$NON-NLS-1$
|
||||
}
|
||||
if (cancel != null) {
|
||||
manager.println((index++) + ") " + cancel); //$NON-NLS-1$
|
||||
manager.println((index) + ") " + cancel); //$NON-NLS-1$
|
||||
}
|
||||
return --index;
|
||||
return index;
|
||||
}
|
||||
|
||||
/** This methods prompt the user for a list of elements
|
||||
|
||||
Reference in New Issue
Block a user