[660] | 1 |
|
---|
[526] | 2 | package de.ugoe.cs.quest.ui;
|
---|
| 3 |
|
---|
[533] | 4 | import java.io.File;
|
---|
[526] | 5 | import java.io.IOException;
|
---|
| 6 | import java.util.List;
|
---|
| 7 |
|
---|
| 8 | import joptsimple.OptionException;
|
---|
| 9 | import joptsimple.OptionParser;
|
---|
| 10 | import joptsimple.OptionSet;
|
---|
| 11 | import joptsimple.OptionSpec;
|
---|
| 12 | import de.ugoe.cs.quest.log4j.Log4JLogger;
|
---|
[533] | 13 | import de.ugoe.cs.quest.plugin.PluginLoader;
|
---|
| 14 | import de.ugoe.cs.quest.plugin.QuestPlugin;
|
---|
[526] | 15 | import de.ugoe.cs.quest.ui.swt.MainWindow;
|
---|
| 16 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
| 17 | import de.ugoe.cs.util.console.Console;
|
---|
| 18 | import de.ugoe.cs.util.console.TextConsole;
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * <p>
|
---|
| 22 | * Start-up class of the application.
|
---|
| 23 | * </p>
|
---|
| 24 | * <p>
|
---|
| 25 | * It sets up and starts the {@link Console}.
|
---|
| 26 | * </p>
|
---|
| 27 | *
|
---|
| 28 | * @author Steffen Herbold
|
---|
| 29 | * @version 1.0
|
---|
| 30 | */
|
---|
| 31 | public class Runner {
|
---|
| 32 |
|
---|
[581] | 33 | public enum UITYPE {
|
---|
| 34 | text, swt
|
---|
| 35 | };
|
---|
[526] | 36 |
|
---|
[581] | 37 | public enum LOG4JTYPE {
|
---|
| 38 | enable, disable
|
---|
| 39 | }
|
---|
[526] | 40 |
|
---|
[581] | 41 | /**
|
---|
| 42 | * <p>
|
---|
| 43 | * Main method of the application.
|
---|
| 44 | * </p>
|
---|
| 45 | *
|
---|
| 46 | * @param args
|
---|
| 47 | * if parameters are defined, they are interpreted as commands for the
|
---|
| 48 | * {@link Console} and executed before the user can use the console; can be used to
|
---|
| 49 | * perform batch operations
|
---|
| 50 | */
|
---|
| 51 | public static void main(String[] args) {
|
---|
[533] | 52 |
|
---|
[581] | 53 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.commands");
|
---|
| 54 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.swt.commands");
|
---|
[533] | 55 |
|
---|
[582] | 56 | PluginLoader pluginLoader = new PluginLoader(new File("lib"));
|
---|
[581] | 57 | pluginLoader.load();
|
---|
[533] | 58 |
|
---|
[581] | 59 | for (QuestPlugin plugin : pluginLoader.getPlugins()) {
|
---|
| 60 | for (String commandPackage : plugin.getCommandPackages()) {
|
---|
| 61 | CommandExecuter.getInstance().addCommandPackage(commandPackage);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
[526] | 64 |
|
---|
[581] | 65 | OptionParser parser = new OptionParser();
|
---|
| 66 | OptionSpec<LOG4JTYPE> log4j =
|
---|
| 67 | parser.accepts("log4j", "Allowed values: enable, disable").withRequiredArg()
|
---|
| 68 | .ofType(LOG4JTYPE.class).defaultsTo(LOG4JTYPE.enable);
|
---|
| 69 | OptionSpec<UITYPE> ui =
|
---|
| 70 | parser.accepts("ui", "Allowed values: text, swt").withRequiredArg()
|
---|
| 71 | .ofType(UITYPE.class).defaultsTo(UITYPE.text);
|
---|
| 72 | OptionSpec<LOG4JTYPE> trace =
|
---|
| 73 | parser.accepts("trace", "Allowed values: enable, disable").withRequiredArg()
|
---|
| 74 | .ofType(LOG4JTYPE.class).defaultsTo(LOG4JTYPE.enable);
|
---|
| 75 | OptionSet options = parser.parse(args);
|
---|
[526] | 76 |
|
---|
[581] | 77 | List<String> startupCommands = options.nonOptionArguments();
|
---|
| 78 | try {
|
---|
| 79 | switch (options.valueOf(log4j))
|
---|
| 80 | {
|
---|
| 81 | case enable:
|
---|
| 82 | new Log4JLogger();
|
---|
| 83 | break;
|
---|
| 84 | case disable:
|
---|
| 85 | // do nothing
|
---|
| 86 | break;
|
---|
| 87 | default:
|
---|
| 88 | throw new AssertionError("reached source code that should be unreachable");
|
---|
| 89 | }
|
---|
[526] | 90 |
|
---|
[581] | 91 | switch (options.valueOf(ui))
|
---|
| 92 | {
|
---|
| 93 | case text:
|
---|
| 94 | TextConsole textConsole = new TextConsole();
|
---|
| 95 | if (options.valueOf(trace) == LOG4JTYPE.disable) {
|
---|
| 96 | textConsole.setDebug(false);
|
---|
| 97 | }
|
---|
| 98 | for (String command : startupCommands) {
|
---|
| 99 | CommandExecuter.getInstance().exec(command);
|
---|
| 100 | }
|
---|
| 101 | textConsole.run(true);
|
---|
| 102 | break;
|
---|
| 103 | case swt:
|
---|
| 104 | MainWindow mainWindow = new MainWindow(startupCommands);
|
---|
| 105 | mainWindow.open();
|
---|
| 106 | break;
|
---|
| 107 | default:
|
---|
| 108 | throw new AssertionError("reached source code that should be unreachable");
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | catch (OptionException e) {
|
---|
| 112 | System.err.println("Invalid Parameters: " + e.getMessage());
|
---|
| 113 | try {
|
---|
| 114 | parser.printHelpOn(System.out);
|
---|
| 115 | }
|
---|
| 116 | catch (IOException e1) {
|
---|
| 117 | // ignore exception.
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[526] | 122 | }
|
---|