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

Last change on this file since 922 was 922, checked in by sherbold, 12 years ago
  • renaming of packages from de.ugoe.cs.quest to de.ugoe.cs.autoquest
  • Property svn:mime-type set to text/plain
File size: 5.0 KB
Line 
1
2package de.ugoe.cs.autoquest.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.autoquest.log4j.Log4JLogger;
14import de.ugoe.cs.autoquest.plugin.PluginLoader;
15import de.ugoe.cs.autoquest.plugin.QuestPlugin;
16import de.ugoe.cs.autoquest.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 LEVELENUM {
39        OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL;
40       
41        public Level getLevel() {
42            switch (this)
43            {
44                case OFF:
45                    return Level.OFF;
46                case SEVERE:
47                    return Level.SEVERE;
48                case ALL:
49                    return Level.ALL;
50                case CONFIG:
51                    return Level.CONFIG;
52                case FINE:
53                    return Level.FINE;
54                case FINER:
55                    return Level.FINER;
56                case FINEST:
57                    return Level.FINEST;
58                case INFO:
59                    return Level.INFO;
60                case WARNING:
61                    return Level.WARNING;
62                default:
63                    throw new AssertionError("reached source code that should be unreachable");
64            }
65        }
66    }
67
68    /**
69     * <p>
70     * Main method of the application.
71     * </p>
72     *
73     * @param args
74     *            if parameters are defined, they are interpreted as commands for the
75     *            {@link Console} and executed before the user can use the console; can be used to
76     *            perform batch operations
77     */
78    public static void main(String[] args) {
79       
80        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.misc");
81        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.sequences");
82        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.usability");
83        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.commands.usage");
84        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.autoquest.ui.swt.commands");
85
86        PluginLoader pluginLoader = new PluginLoader(new File("lib"));
87        pluginLoader.load();
88
89        for (QuestPlugin plugin : pluginLoader.getPlugins()) {
90            for (String commandPackage : plugin.getCommandPackages()) {
91                CommandExecuter.getInstance().addCommandPackage(commandPackage);
92            }
93        }
94
95        OptionParser parser = new OptionParser();
96        OptionSpec<LEVELENUM> log4j =
97            parser.accepts("log4j", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
98                .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.INFO);
99        OptionSpec<UITYPE> ui =
100            parser.accepts("ui", "Allowed values: text, swt").withRequiredArg()
101                .ofType(UITYPE.class).defaultsTo(UITYPE.text);
102        OptionSpec<LEVELENUM> trace =
103            parser.accepts("trace", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
104                .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.WARNING);
105        OptionSet options = parser.parse(args);
106
107        List<String> startupCommands = options.nonOptionArguments();
108        try {
109            if(options.valueOf(log4j)!=LEVELENUM.OFF) {
110                new Log4JLogger(options.valueOf(log4j).getLevel());
111            }
112
113            switch (options.valueOf(ui))
114            {
115                case text:
116                    TextConsole textConsole = new TextConsole(options.valueOf(trace).getLevel());
117                    for (String command : startupCommands) {
118                        CommandExecuter.getInstance().exec(command);
119                    }
120                    textConsole.run();
121                    break;
122                case swt:
123                    MainWindow mainWindow = new MainWindow(startupCommands, options.valueOf(trace).getLevel());
124                    mainWindow.open();
125                    break;
126                default:
127                    throw new AssertionError("reached source code that should be unreachable");
128            }
129        }
130        catch (OptionException e) {
131            System.err.println("Invalid Parameters: " + e.getMessage());
132            try {
133                parser.printHelpOn(System.out);
134            }
135            catch (IOException e1) {
136                // ignore exception.
137            }
138        }
139    }
140
141}
Note: See TracBrowser for help on using the repository browser.