KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > environment > commandline > CommandLineApplicationEnvironment


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.environment.commandline.CommandLineApplicationEnvironment
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.environment.commandline;
20
21 import de.gulden.framework.amoda.environment.commandline.CommandLineArgsParser;
22 import de.gulden.framework.amoda.generic.core.*;
23 import de.gulden.framework.amoda.generic.core.GenericApplicationEnvironment;
24 import de.gulden.framework.amoda.model.data.Value;
25 import de.gulden.framework.amoda.model.interaction.*;
26 import de.gulden.framework.amoda.model.option.*;
27 import java.lang.*;
28 import java.util.*;
29
30 /**
31  * Class CommandLineApplicationEnvironment.
32  *
33  * @author Jens Gulden
34  * @version snapshot-beautyj-1.1
35  */

36 public class CommandLineApplicationEnvironment extends GenericApplicationEnvironment {
37
38     // ------------------------------------------------------------------------
39
// --- constructor ---
40
// ------------------------------------------------------------------------
41

42     public CommandLineApplicationEnvironment() {
43         super();
44     }
45
46
47     // ------------------------------------------------------------------------
48
// --- methods ---
49
// ------------------------------------------------------------------------
50

51     public void doQuestion(Question question) {
52         System.out.println(question.getText());
53         de.gulden.framework.amoda.generic.interaction.GenericQuestion q=(de.gulden.framework.amoda.generic.interaction.GenericQuestion)question;
54         de.gulden.framework.amoda.generic.option.GenericOptionChoice choice=new de.gulden.framework.amoda.generic.option.GenericOptionChoice();
55         choice.setParent(getGenericApplication());
56         choice.addAll(q.getAnswerOptions());
57         askOption(choice);
58         q.setAnswer(choice.getSelectedOption().getId());
59     }
60
61     public void doMessage(Message message) {
62         GenericApplication application = getGenericApplication();
63         if ((application==null) || !application.isQuiet()) {
64             System.out.println(message.getText());
65             if ( (message instanceof de.gulden.framework.amoda.generic.interaction.GenericMessage)
66                  && ((de.gulden.framework.amoda.generic.interaction.GenericMessage)message).getTitle().equals("about") ) {
67                 System.out.println();
68             }
69         }
70     }
71
72     public void doErrorMessage(ErrorMessage errorMessage) {
73         GenericApplication application = getGenericApplication();
74         boolean verbose = (application==null) || application.isVerbose();
75         Throwable JavaDoc cause = errorMessage.getCause();
76         String JavaDoc txt = errorMessage.getText();
77         if ((txt!=null)&&(txt.length()>1)) {
78             txt=txt.substring(0,1).toUpperCase()+txt.substring(1)+(txt.length()>10? (!txt.endsWith(".") ? "." : "") : "" );
79         }
80         if ( ( cause != null ) && ( verbose || (txt == null) ) ) {
81             if (txt == null) {
82                 txt = "";
83             } else {
84                 txt += " ";
85             }
86             txt += de.gulden.util.Toolbox.unqualify(cause.getClass().getName());
87             String JavaDoc causeMsg = cause.getMessage();
88             if (causeMsg != null) {
89                 txt += ": " + causeMsg;
90             }
91             if (!txt.endsWith(".")) {
92                 txt += ".";
93             }
94         }
95         boolean stacktrace = ((txt == null) || verbose);
96         if (txt == null) {
97             txt = "(no message)";
98         }
99         txt = "Error: " + txt + " ('-help' for options)";
100
101         System.out.println(txt);
102
103         if (stacktrace && (cause != null)) {
104             cause.printStackTrace(System.out);
105         }
106         if (errorMessage.exitApplication()) {
107             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))
108
}
109     }
110
111     public void doDialog(Dialog dialog) {
112         // your code here
113
}
114
115     public void doWizard(Wizard wizard) {
116         // your code here
117
}
118
119     public void doExit(GenericApplication application, int code) {
120         System.exit(code);
121     }
122
123     protected void askOption(Option option) {
124         if (option instanceof de.gulden.framework.amoda.model.option.OptionEntry) {
125             de.gulden.framework.amoda.model.option.OptionEntry optionValue=(de.gulden.framework.amoda.model.option.OptionEntry)option;
126             Object JavaDoc value=optionValue.getValue(Option.STATE_DEFAULT).get();
127             String JavaDoc defaultMessage;
128             if (value instanceof Boolean JavaDoc) {
129                 Boolean JavaDoc bool=(Boolean JavaDoc)value;
130                 boolean b=bool.booleanValue();
131                 defaultMessage="["+(b?"_j_/n":"j/_n_")+"]"; // *********** VARIABLEN F?R "J" / "N"
132
} else {
133                 String JavaDoc def=option.toString();
134                 if ((def!=null)&&(def.length()>0)) {
135                     defaultMessage="[default: "+def+"]";
136                 } else {
137                     defaultMessage=null;
138                 }
139             }
140             do {
141                 if (defaultMessage!=null) {
142                     System.out.println(defaultMessage);
143                 }
144                 try {
145                     String JavaDoc r=(new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in))).readLine();
146                     if (!r.equals("")) {
147                         ((de.gulden.framework.amoda.generic.data.GenericValue)optionValue.getValue()).parseString(r);
148                     } // if "", leave default
149
} catch (java.io.IOException JavaDoc ioe) {
150                     System.out.println("ERROR: i/o exception");
151                     // may lead to accepting default value or to retry
152
}
153             } while (!optionValue.isValid());
154         } else if (option instanceof de.gulden.framework.amoda.model.option.OptionsGroup) {
155             // ****** !!!! PROVISORISCH
156
Collection values=((de.gulden.framework.amoda.model.option.OptionsGroup)option).getEntries().values();
157             for (Iterator it=values.iterator();it.hasNext();) {
158                 Option o=(Option)it.next();
159                 askOption(o); // recurse
160
}
161         }
162     }
163
164
165     // ------------------------------------------------------------------------
166
// --- static method ---
167
// ------------------------------------------------------------------------
168

169     public static void invoke(Class JavaDoc applicationClass, String JavaDoc[] args) throws Exception JavaDoc {
170         GenericApplicationEnvironment.invoke(applicationClass, args, CommandLineApplicationEnvironmentFactory.class);
171     }
172
173 } // end CommandLineApplicationEnvironment
174
Popular Tags