001: /*
002: * Project: AMODA - Abstract Modeled Application
003: * Class: de.gulden.framework.amoda.environment.commandline.CommandLineApplicationEnvironment
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the AMODA 0.2 development branch,
009: * it is not released as a seperate version.
010: * For AMODA, see http://amoda.berlios.de/.
011: *
012: * This is licensed under the GNU Lesser General Public License (LGPL)
013: * and comes with NO WARRANTY.
014: *
015: * Author: Jens Gulden
016: * Email: amoda@jensgulden.de
017: */
018:
019: package de.gulden.framework.amoda.environment.commandline;
020:
021: import de.gulden.framework.amoda.environment.commandline.CommandLineArgsParser;
022: import de.gulden.framework.amoda.generic.core.*;
023: import de.gulden.framework.amoda.generic.core.GenericApplicationEnvironment;
024: import de.gulden.framework.amoda.model.data.Value;
025: import de.gulden.framework.amoda.model.interaction.*;
026: import de.gulden.framework.amoda.model.option.*;
027: import java.lang.*;
028: import java.util.*;
029:
030: /**
031: * Class CommandLineApplicationEnvironment.
032: *
033: * @author Jens Gulden
034: * @version snapshot-beautyj-1.1
035: */
036: public class CommandLineApplicationEnvironment extends
037: GenericApplicationEnvironment {
038:
039: // ------------------------------------------------------------------------
040: // --- constructor ---
041: // ------------------------------------------------------------------------
042:
043: public CommandLineApplicationEnvironment() {
044: super ();
045: }
046:
047: // ------------------------------------------------------------------------
048: // --- methods ---
049: // ------------------------------------------------------------------------
050:
051: public void doQuestion(Question question) {
052: System.out.println(question.getText());
053: de.gulden.framework.amoda.generic.interaction.GenericQuestion q = (de.gulden.framework.amoda.generic.interaction.GenericQuestion) question;
054: de.gulden.framework.amoda.generic.option.GenericOptionChoice choice = new de.gulden.framework.amoda.generic.option.GenericOptionChoice();
055: choice.setParent(getGenericApplication());
056: choice.addAll(q.getAnswerOptions());
057: askOption(choice);
058: q.setAnswer(choice.getSelectedOption().getId());
059: }
060:
061: public void doMessage(Message message) {
062: GenericApplication application = getGenericApplication();
063: if ((application == null) || !application.isQuiet()) {
064: System.out.println(message.getText());
065: if ((message instanceof de.gulden.framework.amoda.generic.interaction.GenericMessage)
066: && ((de.gulden.framework.amoda.generic.interaction.GenericMessage) message)
067: .getTitle().equals("about")) {
068: System.out.println();
069: }
070: }
071: }
072:
073: public void doErrorMessage(ErrorMessage errorMessage) {
074: GenericApplication application = getGenericApplication();
075: boolean verbose = (application == null)
076: || application.isVerbose();
077: Throwable cause = errorMessage.getCause();
078: String txt = errorMessage.getText();
079: if ((txt != null) && (txt.length() > 1)) {
080: txt = txt.substring(0, 1).toUpperCase()
081: + txt.substring(1)
082: + (txt.length() > 10 ? (!txt.endsWith(".") ? "."
083: : "") : "");
084: }
085: if ((cause != null) && (verbose || (txt == null))) {
086: if (txt == null) {
087: txt = "";
088: } else {
089: txt += " ";
090: }
091: txt += de.gulden.util.Toolbox.unqualify(cause.getClass()
092: .getName());
093: String causeMsg = cause.getMessage();
094: if (causeMsg != null) {
095: txt += ": " + causeMsg;
096: }
097: if (!txt.endsWith(".")) {
098: txt += ".";
099: }
100: }
101: boolean stacktrace = ((txt == null) || verbose);
102: if (txt == null) {
103: txt = "(no message)";
104: }
105: txt = "Error: " + txt + " ('-help' for options)";
106:
107: System.out.println(txt);
108:
109: if (stacktrace && (cause != null)) {
110: cause.printStackTrace(System.out);
111: }
112: if (errorMessage.exitApplication()) {
113: System.exit(1); // (from OS point of view, 1 is a good value for returning from any Java program on error (no higher values, because OS is usually unaffected))
114: }
115: }
116:
117: public void doDialog(Dialog dialog) {
118: // your code here
119: }
120:
121: public void doWizard(Wizard wizard) {
122: // your code here
123: }
124:
125: public void doExit(GenericApplication application, int code) {
126: System.exit(code);
127: }
128:
129: protected void askOption(Option option) {
130: if (option instanceof de.gulden.framework.amoda.model.option.OptionEntry) {
131: de.gulden.framework.amoda.model.option.OptionEntry optionValue = (de.gulden.framework.amoda.model.option.OptionEntry) option;
132: Object value = optionValue.getValue(Option.STATE_DEFAULT)
133: .get();
134: String defaultMessage;
135: if (value instanceof Boolean) {
136: Boolean bool = (Boolean) value;
137: boolean b = bool.booleanValue();
138: defaultMessage = "[" + (b ? "_j_/n" : "j/_n_") + "]"; // *********** VARIABLEN F?R "J" / "N"
139: } else {
140: String def = option.toString();
141: if ((def != null) && (def.length() > 0)) {
142: defaultMessage = "[default: " + def + "]";
143: } else {
144: defaultMessage = null;
145: }
146: }
147: do {
148: if (defaultMessage != null) {
149: System.out.println(defaultMessage);
150: }
151: try {
152: String r = (new java.io.BufferedReader(
153: new java.io.InputStreamReader(System.in)))
154: .readLine();
155: if (!r.equals("")) {
156: ((de.gulden.framework.amoda.generic.data.GenericValue) optionValue
157: .getValue()).parseString(r);
158: } // if "", leave default
159: } catch (java.io.IOException ioe) {
160: System.out.println("ERROR: i/o exception");
161: // may lead to accepting default value or to retry
162: }
163: } while (!optionValue.isValid());
164: } else if (option instanceof de.gulden.framework.amoda.model.option.OptionsGroup) {
165: // ****** !!!! PROVISORISCH
166: Collection values = ((de.gulden.framework.amoda.model.option.OptionsGroup) option)
167: .getEntries().values();
168: for (Iterator it = values.iterator(); it.hasNext();) {
169: Option o = (Option) it.next();
170: askOption(o); // recurse
171: }
172: }
173: }
174:
175: // ------------------------------------------------------------------------
176: // --- static method ---
177: // ------------------------------------------------------------------------
178:
179: public static void invoke(Class applicationClass, String[] args)
180: throws Exception {
181: GenericApplicationEnvironment.invoke(applicationClass, args,
182: CommandLineApplicationEnvironmentFactory.class);
183: }
184:
185: } // end CommandLineApplicationEnvironment
|