1 21 22 package com.izforge.izpack.uninstaller; 23 24 import java.io.BufferedReader ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 28 import com.izforge.izpack.LocaleDatabase; 29 import com.izforge.izpack.util.AbstractUIHandler; 30 31 public class UninstallerConsole 32 { 33 34 35 protected String installPath; 36 37 38 protected static LocaleDatabase langpack; 39 40 public UninstallerConsole() throws Exception 41 { 42 langpack = new LocaleDatabase(UninstallerFrame.class.getResourceAsStream("/langpack.xml")); 44 getInstallPath(); 45 } 46 51 private void getInstallPath() throws Exception 52 { 53 InputStream in = UninstallerFrame.class.getResourceAsStream("/install.log"); 54 InputStreamReader inReader = new InputStreamReader (in); 55 BufferedReader reader = new BufferedReader (inReader); 56 installPath = reader.readLine(); 57 reader.close(); 58 } 59 60 65 public void runUninstall(boolean destroy) 66 { 67 Destroyer destroyer = new Destroyer(installPath, 68 destroy, new DestroyerHandler()); 69 destroyer.start(); 70 } 71 72 78 private final class DestroyerHandler implements 79 com.izforge.izpack.util.AbstractUIProgressHandler 80 { 81 private int AUTO_ANSWER_MODE = -2; 82 83 private void out(String str) 84 { 85 System.out.println(str); 86 } 87 88 private boolean askOKCancel(String question, int defaultchoice) 89 { 90 if(defaultchoice == AUTO_ANSWER_MODE) return true; 91 boolean defaultanswer = defaultchoice == 1 ? true : false; 92 try 93 { 94 System.out.print(question + " (Ok/Cancel) [" + (defaultanswer?"O":"C") + "]:"); 95 String rline = readln(); 96 if(rline.toLowerCase().startsWith("o")) return true; 97 if(rline.toLowerCase().startsWith("c")) return false; 98 } 99 catch(Exception e){} 100 if( defaultchoice == -1 ) return askOKCancel(question, defaultchoice); 101 return defaultanswer; 102 } 103 104 private int askYesNoCancel(String question, int defaultchoice) 105 { 106 if(defaultchoice == AUTO_ANSWER_MODE) return AbstractUIHandler.ANSWER_YES; 107 boolean defaultanswer = defaultchoice == 1 ? true : false; 108 try 109 { 110 System.out.print(question + " (Yes/No/Cancel) [" + (defaultanswer?"Y":"N") + "]:"); 111 String rline = readln(); 112 if(rline.toLowerCase().equals("y")) return AbstractUIHandler.ANSWER_YES; 113 if(rline.toLowerCase().equals("n")) return AbstractUIHandler.ANSWER_NO; 114 if(rline.toLowerCase().equals("c")) return AbstractUIHandler.ANSWER_CANCEL; 115 } 116 catch(Exception e){} 117 if( defaultchoice == -1 ) return askYesNoCancel(question, defaultchoice); 118 return defaultchoice; 119 } 120 121 private int askYesNo(String question, int defaultchoice) 122 { 123 if(defaultchoice == AUTO_ANSWER_MODE) return AbstractUIHandler.ANSWER_YES; 124 boolean defaultanswer = defaultchoice == 1 ? true : false; 125 try 126 { 127 System.out.print(question + " (Yes/No) [" + (defaultanswer?"Y":"N") + "]:"); 128 String rline = readln(); 129 if(rline.toLowerCase().equals("y")) return AbstractUIHandler.ANSWER_YES; 130 if(rline.toLowerCase().equals("n")) return AbstractUIHandler.ANSWER_NO; 131 } 132 catch(Exception e){} 133 if( defaultchoice == -1 ) return askYesNoCancel(question, defaultchoice); 134 return defaultchoice; 135 } 136 137 private String read() throws Exception 138 { 139 byte[] byteArray = {(byte)System.in.read()}; 140 return new String (byteArray); 141 } 142 143 private String readln() throws Exception 144 { 145 String input = read(); 146 int available = System.in.available(); 147 if (available > 0) 148 { 149 byte[] byteArray = new byte[available]; 150 System.in.read(byteArray); 151 input += new String (byteArray); 152 } 153 return input.trim(); 154 } 155 161 public void startAction(final String name, final int max) 162 { 163 out("Processing " + name); 164 } 165 166 167 public void stopAction() 168 { 169 out(langpack.getString("InstallPanel.finished")); 170 } 171 172 178 public void progress(final int pos, final String message) 179 { 180 out(message); 181 } 182 183 public void nextStep(String step_name, int step_no, int no_of_substeps) 184 { 185 } 186 187 194 public void emitNotification(String text) 195 { 196 } 197 198 203 public boolean emitWarning(String title, String text) 204 { 205 return askOKCancel(title+": "+text, AUTO_ANSWER_MODE); 206 } 207 208 213 public void emitError(String title, String error) 214 { 215 out(title+": "+error); 216 } 217 218 229 public int askQuestion(String title, String question, int choices) 230 { 231 return askQuestion(title, question, choices, AUTO_ANSWER_MODE); 232 } 233 234 245 public int askQuestion(String title, String question, int choices, int default_choice) 246 { 247 int choice = 0; 248 249 if (choices == AbstractUIHandler.CHOICES_YES_NO) 250 choice = askYesNo(title+": "+question, default_choice); 251 else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL) 252 choice = askYesNoCancel(title+": "+question, default_choice); 253 254 return choice; 255 256 257 } 258 259 260 } 261 } 262 | Popular Tags |