KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > uninstaller > UninstallerConsole


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2006 Vladimir Ralev
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.uninstaller;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27
28 import com.izforge.izpack.LocaleDatabase;
29 import com.izforge.izpack.util.AbstractUIHandler;
30
31 public class UninstallerConsole
32 {
33
34    /** The installation path. */
35    protected String JavaDoc installPath;
36
37    /** The language pack. */
38    protected static LocaleDatabase langpack;
39
40    public UninstallerConsole() throws Exception JavaDoc
41    {
42       // Initializations
43
langpack = new LocaleDatabase(UninstallerFrame.class.getResourceAsStream("/langpack.xml"));
44       getInstallPath();
45    }
46    /**
47     * Gets the installation path from the log file.
48     *
49     * @exception Exception Description of the Exception
50     */

51    private void getInstallPath() throws Exception JavaDoc
52    {
53        InputStream JavaDoc in = UninstallerFrame.class.getResourceAsStream("/install.log");
54        InputStreamReader JavaDoc inReader = new InputStreamReader JavaDoc(in);
55        BufferedReader JavaDoc reader = new BufferedReader JavaDoc(inReader);
56        installPath = reader.readLine();
57        reader.close();
58    }
59
60    /**
61     * Runs the cmd line uninstaller.
62     *
63     * @param destroy Equivallen to the destroy option in the GUI.
64     */

65    public void runUninstall(boolean destroy)
66    {
67       Destroyer destroyer = new Destroyer(installPath,
68             destroy, new DestroyerHandler());
69       destroyer.start();
70    }
71
72    /**
73     * The destroyer handler.
74     *
75     * This class also implements the InstallListener because the FileExecutor needs it. TODO: get
76     * rid of the InstallListener - implement generic Listener
77     */

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 JavaDoc str)
84        {
85           System.out.println(str);
86        }
87
88        private boolean askOKCancel(String JavaDoc 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 JavaDoc rline = readln();
96              if(rline.toLowerCase().startsWith("o")) return true;
97              if(rline.toLowerCase().startsWith("c")) return false;
98           }
99           catch(Exception JavaDoc e){}
100           if( defaultchoice == -1 ) return askOKCancel(question, defaultchoice);
101           return defaultanswer;
102        }
103
104        private int askYesNoCancel(String JavaDoc 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 JavaDoc 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 JavaDoc e){}
117           if( defaultchoice == -1 ) return askYesNoCancel(question, defaultchoice);
118           return defaultchoice;
119        }
120
121        private int askYesNo(String JavaDoc 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 JavaDoc 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 JavaDoc e){}
133           if( defaultchoice == -1 ) return askYesNoCancel(question, defaultchoice);
134           return defaultchoice;
135        }
136
137        private String JavaDoc read() throws Exception JavaDoc
138        {
139          byte[] byteArray = {(byte)System.in.read()};
140          return new String JavaDoc(byteArray);
141        }
142
143        private String JavaDoc readln() throws Exception JavaDoc
144        {
145          String JavaDoc 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 JavaDoc(byteArray);
152          }
153          return input.trim();
154        }
155        /**
156         * The destroyer starts.
157         *
158         * @param name The name of the overall action. Not used here.
159         * @param max The maximum value of the progress.
160         */

161        public void startAction(final String JavaDoc name, final int max)
162        {
163            out("Processing " + name);
164        }
165
166        /** The destroyer stops. */
167        public void stopAction()
168        {
169            out(langpack.getString("InstallPanel.finished"));
170        }
171
172        /**
173         * The destroyer progresses.
174         *
175         * @param pos The actual position.
176         * @param message The message.
177         */

178        public void progress(final int pos, final String JavaDoc message)
179        {
180           out(message);
181        }
182
183        public void nextStep(String JavaDoc step_name, int step_no, int no_of_substeps)
184        {
185        }
186
187        /**
188         * Output a notification.
189         *
190         * Does nothing here.
191         *
192         * @param text
193         */

194        public void emitNotification(String JavaDoc text)
195        {
196        }
197
198        /**
199         * Output a warning.
200         *
201         * @param text
202         */

203        public boolean emitWarning(String JavaDoc title, String JavaDoc text)
204        {
205            return askOKCancel(title+": "+text, AUTO_ANSWER_MODE);
206        }
207
208        /**
209         * The destroyer encountered an error.
210         *
211         * @param error The error message.
212         */

213        public void emitError(String JavaDoc title, String JavaDoc error)
214        {
215            out(title+": "+error);
216        }
217
218        /**
219         * Ask the user a question.
220         *
221         * @param title Message title.
222         * @param question The question.
223         * @param choices The set of choices to present.
224         *
225         * @return The user's choice.
226         *
227         * @see AbstractUIHandler#askQuestion(String, String, int)
228         */

229        public int askQuestion(String JavaDoc title, String JavaDoc question, int choices)
230        {
231            return askQuestion(title, question, choices, AUTO_ANSWER_MODE);
232        }
233
234        /**
235         * Ask the user a question.
236         *
237         * @param title Message title.
238         * @param question The question.
239         * @param choices The set of choices to present.
240         * @param default_choice The default choice. (-1 = no default choice)
241         *
242         * @return The user's choice.
243         * @see AbstractUIHandler#askQuestion(String, String, int, int)
244         */

245        public int askQuestion(String JavaDoc title, String JavaDoc 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