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