1 |
|
---|
2 | package de.ugoe.cs.quest.ui;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.logging.Level;
|
---|
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;
|
---|
14 | import de.ugoe.cs.quest.plugin.PluginLoader;
|
---|
15 | import de.ugoe.cs.quest.plugin.QuestPlugin;
|
---|
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 |
|
---|
34 | public enum UITYPE {
|
---|
35 | text, swt
|
---|
36 | };
|
---|
37 |
|
---|
38 | public enum LEVELENUM {
|
---|
39 | OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL;
|
---|
40 |
|
---|
41 | public Level getLevel() {
|
---|
42 | switch (this)
|
---|
43 | {
|
---|
44 | case OFF:
|
---|
45 | return Level.OFF;
|
---|
46 | case SEVERE:
|
---|
47 | return Level.SEVERE;
|
---|
48 | case ALL:
|
---|
49 | return Level.ALL;
|
---|
50 | case CONFIG:
|
---|
51 | return Level.CONFIG;
|
---|
52 | case FINE:
|
---|
53 | return Level.FINE;
|
---|
54 | case FINER:
|
---|
55 | return Level.FINER;
|
---|
56 | case FINEST:
|
---|
57 | return Level.FINEST;
|
---|
58 | case INFO:
|
---|
59 | return Level.INFO;
|
---|
60 | case WARNING:
|
---|
61 | return Level.WARNING;
|
---|
62 | default:
|
---|
63 | throw new AssertionError("reached source code that should be unreachable");
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * <p>
|
---|
70 | * Main method of the application.
|
---|
71 | * </p>
|
---|
72 | *
|
---|
73 | * @param args
|
---|
74 | * if parameters are defined, they are interpreted as commands for the
|
---|
75 | * {@link Console} and executed before the user can use the console; can be used to
|
---|
76 | * perform batch operations
|
---|
77 | */
|
---|
78 | public static void main(String[] args) {
|
---|
79 |
|
---|
80 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.commands.misc");
|
---|
81 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.commands.sequences");
|
---|
82 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.commands.usability");
|
---|
83 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.commands.usage");
|
---|
84 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.swt.commands");
|
---|
85 |
|
---|
86 | PluginLoader pluginLoader = new PluginLoader(new File("lib"));
|
---|
87 | pluginLoader.load();
|
---|
88 |
|
---|
89 | for (QuestPlugin plugin : pluginLoader.getPlugins()) {
|
---|
90 | for (String commandPackage : plugin.getCommandPackages()) {
|
---|
91 | CommandExecuter.getInstance().addCommandPackage(commandPackage);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | OptionParser parser = new OptionParser();
|
---|
96 | OptionSpec<LEVELENUM> log4j =
|
---|
97 | parser.accepts("log4j", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
|
---|
98 | .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.INFO);
|
---|
99 | OptionSpec<UITYPE> ui =
|
---|
100 | parser.accepts("ui", "Allowed values: text, swt").withRequiredArg()
|
---|
101 | .ofType(UITYPE.class).defaultsTo(UITYPE.text);
|
---|
102 | OptionSpec<LEVELENUM> trace =
|
---|
103 | parser.accepts("trace", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
|
---|
104 | .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.WARNING);
|
---|
105 | OptionSet options = parser.parse(args);
|
---|
106 |
|
---|
107 | List<String> startupCommands = options.nonOptionArguments();
|
---|
108 | try {
|
---|
109 | if(options.valueOf(log4j)!=LEVELENUM.OFF) {
|
---|
110 | new Log4JLogger(options.valueOf(log4j).getLevel());
|
---|
111 | }
|
---|
112 |
|
---|
113 | switch (options.valueOf(ui))
|
---|
114 | {
|
---|
115 | case text:
|
---|
116 | TextConsole textConsole = new TextConsole(options.valueOf(trace).getLevel());
|
---|
117 | for (String command : startupCommands) {
|
---|
118 | CommandExecuter.getInstance().exec(command);
|
---|
119 | }
|
---|
120 | textConsole.run();
|
---|
121 | break;
|
---|
122 | case swt:
|
---|
123 | MainWindow mainWindow = new MainWindow(startupCommands, options.valueOf(trace).getLevel());
|
---|
124 | mainWindow.open();
|
---|
125 | break;
|
---|
126 | default:
|
---|
127 | throw new AssertionError("reached source code that should be unreachable");
|
---|
128 | }
|
---|
129 | }
|
---|
130 | catch (OptionException e) {
|
---|
131 | System.err.println("Invalid Parameters: " + e.getMessage());
|
---|
132 | try {
|
---|
133 | parser.printHelpOn(System.out);
|
---|
134 | }
|
---|
135 | catch (IOException e1) {
|
---|
136 | // ignore exception.
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|