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