001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2006 Vladimir Ralev
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.uninstaller;
023:
024: import java.io.BufferedReader;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027:
028: import com.izforge.izpack.LocaleDatabase;
029: import com.izforge.izpack.util.AbstractUIHandler;
030:
031: public class UninstallerConsole {
032:
033: /** The installation path. */
034: protected String installPath;
035:
036: /** The language pack. */
037: protected static LocaleDatabase langpack;
038:
039: public UninstallerConsole() throws Exception {
040: // Initializations
041: langpack = new LocaleDatabase(UninstallerFrame.class
042: .getResourceAsStream("/langpack.xml"));
043: getInstallPath();
044: }
045:
046: /**
047: * Gets the installation path from the log file.
048: *
049: * @exception Exception Description of the Exception
050: */
051: private void getInstallPath() throws Exception {
052: InputStream in = UninstallerFrame.class
053: .getResourceAsStream("/install.log");
054: InputStreamReader inReader = new InputStreamReader(in);
055: BufferedReader reader = new BufferedReader(inReader);
056: installPath = reader.readLine();
057: reader.close();
058: }
059:
060: /**
061: * Runs the cmd line uninstaller.
062: *
063: * @param destroy Equivallen to the destroy option in the GUI.
064: */
065: public void runUninstall(boolean destroy) {
066: Destroyer destroyer = new Destroyer(installPath, destroy,
067: new DestroyerHandler());
068: destroyer.start();
069: }
070:
071: /**
072: * The destroyer handler.
073: *
074: * This class also implements the InstallListener because the FileExecutor needs it. TODO: get
075: * rid of the InstallListener - implement generic Listener
076: */
077: private final class DestroyerHandler implements
078: com.izforge.izpack.util.AbstractUIProgressHandler {
079: private int AUTO_ANSWER_MODE = -2;
080:
081: private void out(String str) {
082: System.out.println(str);
083: }
084:
085: private boolean askOKCancel(String question, int defaultchoice) {
086: if (defaultchoice == AUTO_ANSWER_MODE)
087: return true;
088: boolean defaultanswer = defaultchoice == 1;
089: try {
090: System.out.print(question + " (Ok/Cancel) ["
091: + (defaultanswer ? "O" : "C") + "]:");
092: String rline = readln();
093: if (rline.toLowerCase().startsWith("o"))
094: return true;
095: if (rline.toLowerCase().startsWith("c"))
096: return false;
097: } catch (Exception e) {
098: }
099: if (defaultchoice == -1)
100: return askOKCancel(question, defaultchoice);
101: return defaultanswer;
102: }
103:
104: private int askYesNoCancel(String question, int defaultchoice) {
105: if (defaultchoice == AUTO_ANSWER_MODE)
106: return AbstractUIHandler.ANSWER_YES;
107: boolean defaultanswer = defaultchoice == 1;
108: try {
109: System.out.print(question + " (Yes/No/Cancel) ["
110: + (defaultanswer ? "Y" : "N") + "]:");
111: String rline = readln();
112: if (rline.toLowerCase().equals("y"))
113: return AbstractUIHandler.ANSWER_YES;
114: if (rline.toLowerCase().equals("n"))
115: return AbstractUIHandler.ANSWER_NO;
116: if (rline.toLowerCase().equals("c"))
117: return AbstractUIHandler.ANSWER_CANCEL;
118: } catch (Exception e) {
119: }
120: if (defaultchoice == -1)
121: return askYesNoCancel(question, defaultchoice);
122: return defaultchoice;
123: }
124:
125: private int askYesNo(String question, int defaultchoice) {
126: if (defaultchoice == AUTO_ANSWER_MODE)
127: return AbstractUIHandler.ANSWER_YES;
128: boolean defaultanswer = defaultchoice == 1;
129: try {
130: System.out.print(question + " (Yes/No) ["
131: + (defaultanswer ? "Y" : "N") + "]:");
132: String rline = readln();
133: if (rline.toLowerCase().equals("y"))
134: return AbstractUIHandler.ANSWER_YES;
135: if (rline.toLowerCase().equals("n"))
136: return AbstractUIHandler.ANSWER_NO;
137: } catch (Exception e) {
138: }
139: if (defaultchoice == -1)
140: return askYesNoCancel(question, defaultchoice);
141: return defaultchoice;
142: }
143:
144: private String read() throws Exception {
145: byte[] byteArray = { (byte) System.in.read() };
146: return new String(byteArray);
147: }
148:
149: private String readln() throws Exception {
150: String input = read();
151: int available = System.in.available();
152: if (available > 0) {
153: byte[] byteArray = new byte[available];
154: System.in.read(byteArray);
155: input += new String(byteArray);
156: }
157: return input.trim();
158: }
159:
160: /**
161: * The destroyer starts.
162: *
163: * @param name The name of the overall action. Not used here.
164: * @param max The maximum value of the progress.
165: */
166: public void startAction(final String name, final int max) {
167: out("Processing " + name);
168: }
169:
170: /** The destroyer stops. */
171: public void stopAction() {
172: out(langpack.getString("InstallPanel.finished"));
173: }
174:
175: /**
176: * The destroyer progresses.
177: *
178: * @param pos The actual position.
179: * @param message The message.
180: */
181: public void progress(final int pos, final String message) {
182: out(message);
183: }
184:
185: public void nextStep(String step_name, int step_no,
186: int no_of_substeps) {
187: // not used
188: }
189:
190: public void setSubStepNo(int no_of_substeps) {
191: // not used
192: }
193:
194: /**
195: * Output a notification.
196: *
197: * Does nothing here.
198: *
199: * @param text
200: */
201: public void emitNotification(String text) {
202: }
203:
204: /**
205: * Output a warning.
206: *
207: * @param text
208: */
209: public boolean emitWarning(String title, String text) {
210: return askOKCancel(title + ": " + text, AUTO_ANSWER_MODE);
211: }
212:
213: /**
214: * The destroyer encountered an error.
215: *
216: * @param error The error message.
217: */
218: public void emitError(String title, String error) {
219: out(title + ": " + error);
220: }
221:
222: /**
223: * Ask the user a question.
224: *
225: * @param title Message title.
226: * @param question The question.
227: * @param choices The set of choices to present.
228: *
229: * @return The user's choice.
230: *
231: * @see AbstractUIHandler#askQuestion(String, String, int)
232: */
233: public int askQuestion(String title, String question,
234: int choices) {
235: return askQuestion(title, question, choices,
236: AUTO_ANSWER_MODE);
237: }
238:
239: /**
240: * Ask the user a question.
241: *
242: * @param title Message title.
243: * @param question The question.
244: * @param choices The set of choices to present.
245: * @param default_choice The default choice. (-1 = no default choice)
246: *
247: * @return The user's choice.
248: * @see AbstractUIHandler#askQuestion(String, String, int, int)
249: */
250: public int askQuestion(String title, String question,
251: int choices, int default_choice) {
252: int choice = 0;
253:
254: if (choices == AbstractUIHandler.CHOICES_YES_NO)
255: choice = askYesNo(title + ": " + question,
256: default_choice);
257: else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL)
258: choice = askYesNoCancel(title + ": " + question,
259: default_choice);
260:
261: return choice;
262:
263: }
264:
265: }
266: }
|