1 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 36 public class CommandLineApplicationEnvironment extends GenericApplicationEnvironment { 37 38 42 public CommandLineApplicationEnvironment() { 43 super(); 44 } 45 46 47 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 cause = errorMessage.getCause(); 76 String 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 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); } 109 } 110 111 public void doDialog(Dialog dialog) { 112 } 114 115 public void doWizard(Wizard wizard) { 116 } 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 value=optionValue.getValue(Option.STATE_DEFAULT).get(); 127 String defaultMessage; 128 if (value instanceof Boolean ) { 129 Boolean bool=(Boolean )value; 130 boolean b=bool.booleanValue(); 131 defaultMessage="["+(b?"_j_/n":"j/_n_")+"]"; } else { 133 String 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 r=(new java.io.BufferedReader (new java.io.InputStreamReader (System.in))).readLine(); 146 if (!r.equals("")) { 147 ((de.gulden.framework.amoda.generic.data.GenericValue)optionValue.getValue()).parseString(r); 148 } } catch (java.io.IOException ioe) { 150 System.out.println("ERROR: i/o exception"); 151 } 153 } while (!optionValue.isValid()); 154 } else if (option instanceof de.gulden.framework.amoda.model.option.OptionsGroup) { 155 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); } 161 } 162 } 163 164 165 169 public static void invoke(Class applicationClass, String [] args) throws Exception { 170 GenericApplicationEnvironment.invoke(applicationClass, args, CommandLineApplicationEnvironmentFactory.class); 171 } 172 173 } | Popular Tags |