source: trunk/quest-runner/src/main/java/de/ugoe/cs/quest/ui/Runner.java @ 660

Last change on this file since 660 was 660, checked in by sherbold, 12 years ago
  • code clean-up
  • Property svn:mime-type set to text/plain
File size: 4.0 KB
Line 
1
2package de.ugoe.cs.quest.ui;
3
4import java.io.File;
5import java.io.IOException;
6import java.util.List;
7
8import joptsimple.OptionException;
9import joptsimple.OptionParser;
10import joptsimple.OptionSet;
11import joptsimple.OptionSpec;
12import de.ugoe.cs.quest.log4j.Log4JLogger;
13import de.ugoe.cs.quest.plugin.PluginLoader;
14import de.ugoe.cs.quest.plugin.QuestPlugin;
15import de.ugoe.cs.quest.ui.swt.MainWindow;
16import de.ugoe.cs.util.console.CommandExecuter;
17import de.ugoe.cs.util.console.Console;
18import 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 */
31public class Runner {
32
33    public enum UITYPE {
34        text, swt
35    };
36
37    public enum LOG4JTYPE {
38        enable, disable
39    }
40
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) {
52
53        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.commands");
54        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.swt.commands");
55
56        PluginLoader pluginLoader = new PluginLoader(new File("lib"));
57        pluginLoader.load();
58
59        for (QuestPlugin plugin : pluginLoader.getPlugins()) {
60            for (String commandPackage : plugin.getCommandPackages()) {
61                CommandExecuter.getInstance().addCommandPackage(commandPackage);
62            }
63        }
64
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);
76
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            }
90
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
122}
Note: See TracBrowser for help on using the repository browser.