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

Last change on this file since 674 was 674, checked in by sherbold, 12 years ago
  • modified startup-parameter trace of runner from binary into java.util.Logger.Level to define the tracing granularity; defaults to WARNING)
  • modified TextConsole? and SWTConsole to trace according to trace-parameter
  • 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;
7import java.util.logging.Level;
8
9import joptsimple.OptionException;
10import joptsimple.OptionParser;
11import joptsimple.OptionSet;
12import joptsimple.OptionSpec;
13import de.ugoe.cs.quest.log4j.Log4JLogger;
14import de.ugoe.cs.quest.plugin.PluginLoader;
15import de.ugoe.cs.quest.plugin.QuestPlugin;
16import de.ugoe.cs.quest.ui.swt.MainWindow;
17import de.ugoe.cs.util.console.CommandExecuter;
18import de.ugoe.cs.util.console.Console;
19import 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 */
32public class Runner {
33
34    public enum UITYPE {
35        text, swt
36    };
37
38    public enum LOG4JTYPE {
39        enable, disable
40    }
41
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) {
53
54        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.commands");
55        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.swt.commands");
56
57        PluginLoader pluginLoader = new PluginLoader(new File("lib"));
58        pluginLoader.load();
59
60        for (QuestPlugin plugin : pluginLoader.getPlugins()) {
61            for (String commandPackage : plugin.getCommandPackages()) {
62                CommandExecuter.getInstance().addCommandPackage(commandPackage);
63            }
64        }
65
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);
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);
76        OptionSet options = parser.parse(args);
77
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            }
91
92            switch (options.valueOf(ui))
93            {
94                case text:
95                    TextConsole textConsole = new TextConsole(options.valueOf(trace));
96                    for (String command : startupCommands) {
97                        CommandExecuter.getInstance().exec(command);
98                    }
99                    textConsole.run();
100                    break;
101                case swt:
102                    MainWindow mainWindow = new MainWindow(startupCommands, options.valueOf(trace));
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
120}
Note: See TracBrowser for help on using the repository browser.