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

Last change on this file since 582 was 582, checked in by sherbold, 12 years ago
  • plugin mechanism now working correctly in assembly
  • Property svn:mime-type set to text/plain
File size: 4.6 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        // The following four command packages are added automatically, once the
57        // plugin-mechanism works correctly. Hence, these calls should be
58        // removed then.
59        /*
60         * CommandExecuter.getInstance().addCommandPackage( "de.ugoe.cs.quest.plugin.mfc.commands");
61         * CommandExecuter.getInstance().addCommandPackage( "de.ugoe.cs.quest.plugin.php.commands");
62         * CommandExecuter.getInstance().addCommandPackage(
63         * "de.ugoe.cs.quest.plugin.guitar.commands");
64         * CommandExecuter.getInstance().addCommandPackage( "de.ugoe.cs.quest.plugin.jfc.commands");
65         */
66
67        PluginLoader pluginLoader = new PluginLoader(new File("lib"));
68        pluginLoader.load();
69
70        for (QuestPlugin plugin : pluginLoader.getPlugins()) {
71            for (String commandPackage : plugin.getCommandPackages()) {
72                CommandExecuter.getInstance().addCommandPackage(commandPackage);
73            }
74        }
75
76        OptionParser parser = new OptionParser();
77        OptionSpec<LOG4JTYPE> log4j =
78            parser.accepts("log4j", "Allowed values: enable, disable").withRequiredArg()
79                .ofType(LOG4JTYPE.class).defaultsTo(LOG4JTYPE.enable);
80        OptionSpec<UITYPE> ui =
81            parser.accepts("ui", "Allowed values: text, swt").withRequiredArg()
82                .ofType(UITYPE.class).defaultsTo(UITYPE.text);
83        OptionSpec<LOG4JTYPE> trace =
84            parser.accepts("trace", "Allowed values: enable, disable").withRequiredArg()
85                .ofType(LOG4JTYPE.class).defaultsTo(LOG4JTYPE.enable);
86        OptionSet options = parser.parse(args);
87
88        List<String> startupCommands = options.nonOptionArguments();
89        try {
90            switch (options.valueOf(log4j))
91            {
92                case enable:
93                    new Log4JLogger();
94                    break;
95                case disable:
96                    // do nothing
97                    break;
98                default:
99                    throw new AssertionError("reached source code that should be unreachable");
100            }
101
102            switch (options.valueOf(ui))
103            {
104                case text:
105                    TextConsole textConsole = new TextConsole();
106                    if (options.valueOf(trace) == LOG4JTYPE.disable) {
107                        textConsole.setDebug(false);
108                    }
109                    for (String command : startupCommands) {
110                        CommandExecuter.getInstance().exec(command);
111                    }
112                    textConsole.run(true);
113                    break;
114                case swt:
115                    MainWindow mainWindow = new MainWindow(startupCommands);
116                    mainWindow.open();
117                    break;
118                default:
119                    throw new AssertionError("reached source code that should be unreachable");
120            }
121        }
122        catch (OptionException e) {
123            System.err.println("Invalid Parameters: " + e.getMessage());
124            try {
125                parser.printHelpOn(System.out);
126            }
127            catch (IOException e1) {
128                // ignore exception.
129            }
130        }
131    }
132
133}
Note: See TracBrowser for help on using the repository browser.