1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.ui;
|
---|
16 |
|
---|
17 | import java.io.File;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.logging.Level;
|
---|
21 |
|
---|
22 | import joptsimple.OptionException;
|
---|
23 | import joptsimple.OptionParser;
|
---|
24 | import joptsimple.OptionSet;
|
---|
25 | import joptsimple.OptionSpec;
|
---|
26 | import de.ugoe.cs.autoquest.log4j.Log4JLogger;
|
---|
27 | import de.ugoe.cs.autoquest.plugin.PluginLoader;
|
---|
28 | import de.ugoe.cs.autoquest.plugin.QuestPlugin;
|
---|
29 | import de.ugoe.cs.autoquest.ui.swt.MainWindow;
|
---|
30 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
31 | import de.ugoe.cs.util.console.Console;
|
---|
32 | import de.ugoe.cs.util.console.TextConsole;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * <p>
|
---|
36 | * Start-up class of the application.
|
---|
37 | * </p>
|
---|
38 | * <p>
|
---|
39 | * It sets up and starts the {@link Console}.
|
---|
40 | * </p>
|
---|
41 | *
|
---|
42 | * @author Steffen Herbold
|
---|
43 | * @version 1.0
|
---|
44 | */
|
---|
45 | public class Runner {
|
---|
46 |
|
---|
47 | public enum UITYPE {
|
---|
48 | text, swt
|
---|
49 | };
|
---|
50 |
|
---|
51 | public enum LEVELENUM {
|
---|
52 | OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL;
|
---|
53 |
|
---|
54 | public Level getLevel() {
|
---|
55 | switch (this)
|
---|
56 | {
|
---|
57 | case OFF:
|
---|
58 | return Level.OFF;
|
---|
59 | case SEVERE:
|
---|
60 | return Level.SEVERE;
|
---|
61 | case ALL:
|
---|
62 | return Level.ALL;
|
---|
63 | case CONFIG:
|
---|
64 | return Level.CONFIG;
|
---|
65 | case FINE:
|
---|
66 | return Level.FINE;
|
---|
67 | case FINER:
|
---|
68 | return Level.FINER;
|
---|
69 | case FINEST:
|
---|
70 | return Level.FINEST;
|
---|
71 | case INFO:
|
---|
72 | return Level.INFO;
|
---|
73 | case WARNING:
|
---|
74 | return Level.WARNING;
|
---|
75 | default:
|
---|
76 | throw new AssertionError("reached source code that should be unreachable");
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * <p>
|
---|
83 | * Main method of the application.
|
---|
84 | * </p>
|
---|
85 | *
|
---|
86 | * @param args
|
---|
87 | * if parameters are defined, they are interpreted as commands for the
|
---|
88 | * {@link Console} and executed before the user can use the console; can be used to
|
---|
89 | * perform batch operations
|
---|
90 | */
|
---|
91 | public static void main(String[] args) {
|
---|
92 |
|
---|
93 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.misc");
|
---|
94 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.sequences");
|
---|
95 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.usability");
|
---|
96 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.usage");
|
---|
97 | CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.ui.swt.commands");
|
---|
98 |
|
---|
99 | PluginLoader pluginLoader = new PluginLoader(new File("lib"));
|
---|
100 | pluginLoader.load();
|
---|
101 |
|
---|
102 | for (QuestPlugin plugin : pluginLoader.getPlugins()) {
|
---|
103 | for (String commandPackage : plugin.getCommandPackages()) {
|
---|
104 | CommandExecuter.getInstance().addCommandPackage(commandPackage);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | OptionParser parser = new OptionParser();
|
---|
109 | OptionSpec<LEVELENUM> log4j =
|
---|
110 | parser.accepts("log4j", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
|
---|
111 | .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.INFO);
|
---|
112 | OptionSpec<UITYPE> ui =
|
---|
113 | parser.accepts("ui", "Allowed values: text, swt").withRequiredArg()
|
---|
114 | .ofType(UITYPE.class).defaultsTo(UITYPE.text);
|
---|
115 | OptionSpec<LEVELENUM> trace =
|
---|
116 | parser.accepts("trace", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
|
---|
117 | .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.WARNING);
|
---|
118 | OptionSet options = parser.parse(args);
|
---|
119 |
|
---|
120 | List<String> startupCommands = options.nonOptionArguments();
|
---|
121 | try {
|
---|
122 | if(options.valueOf(log4j)!=LEVELENUM.OFF) {
|
---|
123 | new Log4JLogger(options.valueOf(log4j).getLevel());
|
---|
124 | }
|
---|
125 |
|
---|
126 | switch (options.valueOf(ui))
|
---|
127 | {
|
---|
128 | case text:
|
---|
129 | TextConsole textConsole = new TextConsole(options.valueOf(trace).getLevel());
|
---|
130 | for (String command : startupCommands) {
|
---|
131 | CommandExecuter.getInstance().exec(command);
|
---|
132 | }
|
---|
133 | textConsole.run();
|
---|
134 | break;
|
---|
135 | case swt:
|
---|
136 | MainWindow mainWindow = new MainWindow(startupCommands, options.valueOf(trace).getLevel());
|
---|
137 | mainWindow.open();
|
---|
138 | break;
|
---|
139 | default:
|
---|
140 | throw new AssertionError("reached source code that should be unreachable");
|
---|
141 | }
|
---|
142 | }
|
---|
143 | catch (OptionException e) {
|
---|
144 | System.err.println("Invalid Parameters: " + e.getMessage());
|
---|
145 | try {
|
---|
146 | parser.printHelpOn(System.out);
|
---|
147 | }
|
---|
148 | catch (IOException e1) {
|
---|
149 | // ignore exception.
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | }
|
---|