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

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
  • Property svn:mime-type set to text/plain
File size: 4.6 KB
Line 
1package de.ugoe.cs.quest.ui;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.List;
6
7import joptsimple.OptionException;
8import joptsimple.OptionParser;
9import joptsimple.OptionSet;
10import joptsimple.OptionSpec;
11import de.ugoe.cs.quest.log4j.Log4JLogger;
12import de.ugoe.cs.quest.plugin.PluginLoader;
13import de.ugoe.cs.quest.plugin.QuestPlugin;
14import de.ugoe.cs.quest.ui.swt.MainWindow;
15import de.ugoe.cs.util.console.CommandExecuter;
16import de.ugoe.cs.util.console.Console;
17import de.ugoe.cs.util.console.TextConsole;
18
19/**
20 * <p>
21 * Start-up class of the application.
22 * </p>
23 * <p>
24 * It sets up and starts the {@link Console}.
25 * </p>
26 *
27 * @author Steffen Herbold
28 * @version 1.0
29 */
30public class Runner {
31
32    public enum UITYPE {
33        text, swt
34    };
35
36    public enum LOG4JTYPE {
37        enable, disable
38    }
39
40    /**
41     * <p>
42     * Main method of the application.
43     * </p>
44     *
45     * @param args
46     *            if parameters are defined, they are interpreted as commands for the
47     *            {@link Console} and executed before the user can use the console; can be used to
48     *            perform batch operations
49     */
50    public static void main(String[] args) {
51
52        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.commands");
53        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.swt.commands");
54
55        // The following four command packages are added automatically, once the
56        // plugin-mechanism works correctly. Hence, these calls should be
57        // removed then.
58        /*
59         * CommandExecuter.getInstance().addCommandPackage( "de.ugoe.cs.quest.plugin.mfc.commands");
60         * CommandExecuter.getInstance().addCommandPackage( "de.ugoe.cs.quest.plugin.php.commands");
61         * CommandExecuter.getInstance().addCommandPackage(
62         * "de.ugoe.cs.quest.plugin.guitar.commands");
63         * CommandExecuter.getInstance().addCommandPackage( "de.ugoe.cs.quest.plugin.jfc.commands");
64         */
65
66        PluginLoader pluginLoader = new PluginLoader(new File("lib"));
67        pluginLoader.load();
68
69        for (QuestPlugin plugin : pluginLoader.getPlugins()) {
70            for (String commandPackage : plugin.getCommandPackages()) {
71                CommandExecuter.getInstance().addCommandPackage(commandPackage);
72            }
73        }
74
75        OptionParser parser = new OptionParser();
76        OptionSpec<LOG4JTYPE> log4j =
77            parser.accepts("log4j", "Allowed values: enable, disable").withRequiredArg()
78                .ofType(LOG4JTYPE.class).defaultsTo(LOG4JTYPE.enable);
79        OptionSpec<UITYPE> ui =
80            parser.accepts("ui", "Allowed values: text, swt").withRequiredArg()
81                .ofType(UITYPE.class).defaultsTo(UITYPE.text);
82        OptionSpec<LOG4JTYPE> trace =
83            parser.accepts("trace", "Allowed values: enable, disable").withRequiredArg()
84                .ofType(LOG4JTYPE.class).defaultsTo(LOG4JTYPE.enable);
85        OptionSet options = parser.parse(args);
86
87        List<String> startupCommands = options.nonOptionArguments();
88        try {
89            switch (options.valueOf(log4j))
90            {
91                case enable:
92                    new Log4JLogger();
93                    break;
94                case disable:
95                    // do nothing
96                    break;
97                default:
98                    throw new AssertionError("reached source code that should be unreachable");
99            }
100
101            switch (options.valueOf(ui))
102            {
103                case text:
104                    TextConsole textConsole = new TextConsole();
105                    if (options.valueOf(trace) == LOG4JTYPE.disable) {
106                        textConsole.setDebug(false);
107                    }
108                    for (String command : startupCommands) {
109                        CommandExecuter.getInstance().exec(command);
110                    }
111                    textConsole.run(true);
112                    break;
113                case swt:
114                    MainWindow mainWindow = new MainWindow(startupCommands);
115                    mainWindow.open();
116                    break;
117                default:
118                    throw new AssertionError("reached source code that should be unreachable");
119            }
120        }
121        catch (OptionException e) {
122            System.err.println("Invalid Parameters: " + e.getMessage());
123            try {
124                parser.printHelpOn(System.out);
125            }
126            catch (IOException e1) {
127                // ignore exception.
128            }
129        }
130    }
131
132}
Note: See TracBrowser for help on using the repository browser.