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

Last change on this file since 677 was 677, checked in by sherbold, 12 years ago
  • fixed problem with new startup-parameters
  • Property svn:mime-type set to text/plain
File size: 4.7 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 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.quest.ui.commands");
81        CommandExecuter.getInstance().addCommandPackage("de.ugoe.cs.quest.ui.swt.commands");
82
83        PluginLoader pluginLoader = new PluginLoader(new File("lib"));
84        pluginLoader.load();
85
86        for (QuestPlugin plugin : pluginLoader.getPlugins()) {
87            for (String commandPackage : plugin.getCommandPackages()) {
88                CommandExecuter.getInstance().addCommandPackage(commandPackage);
89            }
90        }
91
92        OptionParser parser = new OptionParser();
93        OptionSpec<LEVELENUM> log4j =
94            parser.accepts("log4j", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
95                .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.INFO);
96        OptionSpec<UITYPE> ui =
97            parser.accepts("ui", "Allowed values: text, swt").withRequiredArg()
98                .ofType(UITYPE.class).defaultsTo(UITYPE.text);
99        OptionSpec<LEVELENUM> trace =
100            parser.accepts("trace", "Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL").withRequiredArg()
101                .ofType(LEVELENUM.class).defaultsTo(LEVELENUM.WARNING);
102        OptionSet options = parser.parse(args);
103
104        List<String> startupCommands = options.nonOptionArguments();
105        try {
106            if(options.valueOf(log4j)!=LEVELENUM.OFF) {
107                new Log4JLogger(options.valueOf(log4j).getLevel());
108            }
109
110            switch (options.valueOf(ui))
111            {
112                case text:
113                    TextConsole textConsole = new TextConsole(options.valueOf(trace).getLevel());
114                    for (String command : startupCommands) {
115                        CommandExecuter.getInstance().exec(command);
116                    }
117                    textConsole.run();
118                    break;
119                case swt:
120                    MainWindow mainWindow = new MainWindow(startupCommands, options.valueOf(trace).getLevel());
121                    mainWindow.open();
122                    break;
123                default:
124                    throw new AssertionError("reached source code that should be unreachable");
125            }
126        }
127        catch (OptionException e) {
128            System.err.println("Invalid Parameters: " + e.getMessage());
129            try {
130                parser.printHelpOn(System.out);
131            }
132            catch (IOException e1) {
133                // ignore exception.
134            }
135        }
136    }
137
138}
Note: See TracBrowser for help on using the repository browser.