KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.izforge.izpack.uninstaller;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Cursor JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.GraphicsEnvironment JavaDoc;
26 import java.awt.GridBagConstraints JavaDoc;
27 import java.awt.GridBagLayout JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.Point JavaDoc;
30 import java.awt.Window JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.awt.event.KeyAdapter JavaDoc;
34 import java.awt.event.MouseAdapter JavaDoc;
35 import java.awt.event.MouseMotionAdapter JavaDoc;
36 import java.awt.event.WindowAdapter JavaDoc;
37 import java.awt.event.WindowEvent JavaDoc;
38 import java.io.BufferedReader JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41 import java.net.URL JavaDoc;
42
43 import javax.swing.ImageIcon JavaDoc;
44 import javax.swing.JButton JavaDoc;
45 import javax.swing.JCheckBox JavaDoc;
46 import javax.swing.JFrame JavaDoc;
47 import javax.swing.JLabel JavaDoc;
48 import javax.swing.JOptionPane JavaDoc;
49 import javax.swing.JPanel JavaDoc;
50 import javax.swing.JProgressBar JavaDoc;
51 import javax.swing.SwingUtilities JavaDoc;
52 import javax.swing.UIManager JavaDoc;
53
54 import com.izforge.izpack.LocaleDatabase;
55 import com.izforge.izpack.gui.ButtonFactory;
56 import com.izforge.izpack.gui.IconsDatabase;
57 import com.izforge.izpack.util.AbstractUIHandler;
58
59 /**
60  * The uninstaller frame class.
61  *
62  * @author Julien Ponge
63  */

64 public class UninstallerFrame extends JFrame JavaDoc
65 {
66
67     /**
68      *
69      */

70     private static final long serialVersionUID = 3257281444152684850L;
71
72     /** The icons database. */
73     private IconsDatabase icons;
74
75     /** The language pack. */
76     protected static LocaleDatabase langpack;
77
78     /** The target destroy checkbox. */
79     protected JCheckBox JavaDoc targetDestroyCheckbox;
80
81     /** The progress bar. */
82     protected JProgressBar JavaDoc progressBar;
83
84     /** The destroy button. */
85     protected JButton JavaDoc destroyButton;
86
87     /** The quit button. */
88     protected JButton JavaDoc quitButton;
89
90     /** The buttons hover color. */
91     private Color JavaDoc buttonsHColor = new Color JavaDoc(230, 230, 230);
92
93     /** The installation path. */
94     protected String JavaDoc installPath;
95
96     /**
97      * The constructor.
98      *
99      * @exception Exception Description of the Exception
100      */

101     public UninstallerFrame() throws Exception JavaDoc
102     {
103         super("IzPack - Uninstaller");
104
105         // Initializations
106
langpack = new LocaleDatabase(UninstallerFrame.class.getResourceAsStream("/langpack.xml"));
107         getInstallPath();
108         icons = new IconsDatabase();
109         loadIcons();
110         UIManager.put("OptionPane.yesButtonText", langpack.getString("installer.yes"));
111         UIManager.put("OptionPane.noButtonText", langpack.getString("installer.no"));
112         UIManager.put("OptionPane.cancelButtonText", langpack.getString("installer.cancel"));
113
114         // Sets the frame icon
115
setIconImage(icons.getImageIcon("JFrameIcon").getImage());
116
117         // We build the GUI & show it
118
buildGUI();
119         addWindowListener(new WindowHandler());
120         pack();
121         centerFrame(this);
122         setResizable(false);
123         setVisible(true);
124     }
125
126     /** Builds the GUI. */
127     private void buildGUI()
128     {
129         // We initialize our layout
130
JPanel JavaDoc contentPane = (JPanel JavaDoc) getContentPane();
131         GridBagLayout JavaDoc layout = new GridBagLayout JavaDoc();
132         contentPane.setLayout(layout);
133         GridBagConstraints JavaDoc gbConstraints = new GridBagConstraints JavaDoc();
134         gbConstraints.insets = new Insets JavaDoc(5, 5, 5, 5);
135
136         // We prepare our action handler
137
ActionsHandler handler = new ActionsHandler();
138
139         // Prepares the glass pane to block gui interaction when needed
140
JPanel JavaDoc glassPane = (JPanel JavaDoc) getGlassPane();
141         glassPane.addMouseListener(new MouseAdapter JavaDoc() {});
142         glassPane.addMouseMotionListener(new MouseMotionAdapter JavaDoc() {});
143         glassPane.addKeyListener(new KeyAdapter JavaDoc() {});
144
145         // We set-up the buttons factory
146
ButtonFactory.useButtonIcons();
147         ButtonFactory.useHighlightButtons();
148
149         // We put our components
150

151         JLabel JavaDoc warningLabel = new JLabel JavaDoc(langpack.getString("uninstaller.warning"), icons
152                 .getImageIcon("warning"), JLabel.TRAILING);
153         buildConstraints(gbConstraints, 0, 0, 2, 1, 1.0, 0.0);
154         gbConstraints.anchor = GridBagConstraints.WEST;
155         gbConstraints.fill = GridBagConstraints.NONE;
156         layout.addLayoutComponent(warningLabel, gbConstraints);
157         contentPane.add(warningLabel);
158
159         targetDestroyCheckbox = new JCheckBox JavaDoc(langpack.getString("uninstaller.destroytarget")
160                 + installPath, false);
161         buildConstraints(gbConstraints, 0, 1, 2, 1, 1.0, 0.0);
162         layout.addLayoutComponent(targetDestroyCheckbox, gbConstraints);
163         contentPane.add(targetDestroyCheckbox);
164         gbConstraints.fill = GridBagConstraints.HORIZONTAL;
165
166         progressBar = new JProgressBar JavaDoc();
167         progressBar.setStringPainted(true);
168         progressBar.setString(langpack.getString("InstallPanel.begin"));
169         buildConstraints(gbConstraints, 0, 2, 2, 1, 1.0, 0.0);
170         layout.addLayoutComponent(progressBar, gbConstraints);
171         contentPane.add(progressBar);
172
173         destroyButton = ButtonFactory.createButton(langpack.getString("uninstaller.uninstall"),
174                 icons.getImageIcon("delete"), buttonsHColor);
175         destroyButton.addActionListener(handler);
176         buildConstraints(gbConstraints, 0, 3, 1, 1, 0.5, 0.0);
177         gbConstraints.fill = GridBagConstraints.NONE;
178         gbConstraints.anchor = GridBagConstraints.WEST;
179         layout.addLayoutComponent(destroyButton, gbConstraints);
180         contentPane.add(destroyButton);
181
182         quitButton = ButtonFactory.createButton(langpack.getString("installer.quit"), icons
183                 .getImageIcon("stop"), buttonsHColor);
184         quitButton.addActionListener(handler);
185         buildConstraints(gbConstraints, 1, 3, 1, 1, 0.5, 0.0);
186         gbConstraints.anchor = GridBagConstraints.EAST;
187         layout.addLayoutComponent(quitButton, gbConstraints);
188         contentPane.add(quitButton);
189
190     }
191
192     /**
193      * Centers a window on screen.
194      *
195      * @param frame The window to center.
196      */

197     private void centerFrame(Window JavaDoc frame)
198     {
199         Point JavaDoc center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
200         Dimension JavaDoc frameSize = frame.getSize();
201         frame.setLocation(center.x - frameSize.width / 2,
202                 center.y - frameSize.height / 2 - 10);
203     }
204
205     /**
206      * Sets the parameters of a GridBagConstraints object.
207      *
208      * @param gbc The constraints object.
209      * @param gx The x coordinates.
210      * @param gy The y coordinates.
211      * @param gw The width.
212      * @param wx The x wheight.
213      * @param wy The y wheight.
214      * @param gh Description of the Parameter
215      */

216     private void buildConstraints(GridBagConstraints JavaDoc gbc, int gx, int gy, int gw, int gh,
217             double wx, double wy)
218     {
219         gbc.gridx = gx;
220         gbc.gridy = gy;
221         gbc.gridwidth = gw;
222         gbc.gridheight = gh;
223         gbc.weightx = wx;
224         gbc.weighty = wy;
225     }
226
227     /**
228      * Gets the installation path from the log file.
229      *
230      * @exception Exception Description of the Exception
231      */

232     private void getInstallPath() throws Exception JavaDoc
233     {
234         InputStream JavaDoc in = UninstallerFrame.class.getResourceAsStream("/install.log");
235         InputStreamReader JavaDoc inReader = new InputStreamReader JavaDoc(in);
236         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(inReader);
237         installPath = reader.readLine();
238         reader.close();
239     }
240
241     /**
242      * Loads the icons.
243      *
244      * @exception Exception Description of the Exception
245      */

246     private void loadIcons() throws Exception JavaDoc
247     {
248         // Initialisations
249
icons = new IconsDatabase();
250         URL JavaDoc url;
251         ImageIcon JavaDoc img;
252
253         // We load it
254
url = UninstallerFrame.class.getResource("/img/trash.png");
255         img = new ImageIcon JavaDoc(url);
256         icons.put("delete", img);
257
258         url = UninstallerFrame.class.getResource("/img/stop.png");
259         img = new ImageIcon JavaDoc(url);
260         icons.put("stop", img);
261
262         url = UninstallerFrame.class.getResource("/img/flag.png");
263         img = new ImageIcon JavaDoc(url);
264         icons.put("warning", img);
265
266         url = UninstallerFrame.class.getResource("/img/JFrameIcon.png");
267         img = new ImageIcon JavaDoc(url);
268         icons.put("JFrameIcon", img);
269     }
270
271     /** Blocks GUI interaction. */
272     public void blockGUI()
273     {
274         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
275         getGlassPane().setVisible(true);
276         getGlassPane().setEnabled(true);
277     }
278
279     /** Releases GUI interaction. */
280     public void releaseGUI()
281     {
282         getGlassPane().setEnabled(false);
283         getGlassPane().setVisible(false);
284         setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
285     }
286
287     /**
288      * The window events handler.
289      *
290      * @author Julien Ponge
291      */

292     private final class WindowHandler extends WindowAdapter JavaDoc
293     {
294
295         /**
296          * We can't avoid the exit here, so don't call exit elsewhere.
297          *
298          * @param e The event.
299          */

300         public void windowClosing(WindowEvent JavaDoc e)
301         {
302             System.exit(0);
303         }
304     }
305
306     /**
307      * The destroyer handler.
308      *
309      * This class also implements the InstallListener because the FileExecutor needs it. TODO: get
310      * rid of the InstallListener - implement generic Listener
311      *
312      * @author Julien Ponge
313      * @author Tino Schwarze
314      */

315     private final class DestroyerHandler implements
316             com.izforge.izpack.util.AbstractUIProgressHandler
317     {
318
319         /**
320          * The destroyer starts.
321          *
322          * @param name The name of the overall action. Not used here.
323          * @param max The maximum value of the progress.
324          */

325         public void startAction(final String JavaDoc name, final int max)
326         {
327             SwingUtilities.invokeLater(new Runnable JavaDoc() {
328                 public void run()
329                 {
330                     progressBar.setMinimum(0);
331                     progressBar.setMaximum(max);
332                     blockGUI();
333                 }
334             });
335         }
336
337         /** The destroyer stops. */
338         public void stopAction()
339         {
340             SwingUtilities.invokeLater(new Runnable JavaDoc() {
341                 public void run()
342                 {
343                     progressBar.setString(langpack.getString("InstallPanel.finished"));
344                     targetDestroyCheckbox.setEnabled(false);
345                     destroyButton.setEnabled(false);
346                     releaseGUI();
347                 }
348             });
349         }
350
351         /**
352          * The destroyer progresses.
353          *
354          * @param pos The actual position.
355          * @param message The message.
356          */

357         public void progress(final int pos, final String JavaDoc message)
358         {
359             SwingUtilities.invokeLater(new Runnable JavaDoc() {
360                 public void run()
361                 {
362                     progressBar.setValue(pos);
363                     progressBar.setString(message);
364                 }
365             });
366         }
367
368         public void nextStep(String JavaDoc step_name, int step_no, int no_of_substeps)
369         {
370         }
371
372         /**
373          * Output a notification.
374          *
375          * Does nothing here.
376          *
377          * @param text
378          */

379         public void emitNotification(String JavaDoc text)
380         {
381         }
382
383         /**
384          * Output a warning.
385          *
386          * @param text
387          */

388         public boolean emitWarning(String JavaDoc title, String JavaDoc text)
389         {
390             return (JOptionPane.showConfirmDialog(null, text, title, JOptionPane.OK_CANCEL_OPTION,
391                     JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION);
392         }
393
394         /**
395          * The destroyer encountered an error.
396          *
397          * @param error The error message.
398          */

399         public void emitError(String JavaDoc title, String JavaDoc error)
400         {
401             progressBar.setString(error);
402             JOptionPane.showMessageDialog(null, error, title, JOptionPane.OK_CANCEL_OPTION);
403         }
404
405         /**
406          * Ask the user a question.
407          *
408          * @param title Message title.
409          * @param question The question.
410          * @param choices The set of choices to present.
411          *
412          * @return The user's choice.
413          *
414          * @see AbstractUIHandler#askQuestion(String, String, int)
415          */

416         public int askQuestion(String JavaDoc title, String JavaDoc question, int choices)
417         {
418             return askQuestion(title, question, choices, -1);
419         }
420
421         /**
422          * Ask the user a question.
423          *
424          * @param title Message title.
425          * @param question The question.
426          * @param choices The set of choices to present.
427          * @param default_choice The default choice. (-1 = no default choice)
428          *
429          * @return The user's choice.
430          * @see AbstractUIHandler#askQuestion(String, String, int, int)
431          */

432         public int askQuestion(String JavaDoc title, String JavaDoc question, int choices, int default_choice)
433         {
434             int jo_choices = 0;
435
436             if (choices == AbstractUIHandler.CHOICES_YES_NO)
437                 jo_choices = JOptionPane.YES_NO_OPTION;
438             else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL)
439                 jo_choices = JOptionPane.YES_NO_CANCEL_OPTION;
440
441             int user_choice = JOptionPane.showConfirmDialog(null, (Object JavaDoc) question, title,
442                     jo_choices, JOptionPane.QUESTION_MESSAGE);
443
444             if (user_choice == JOptionPane.CANCEL_OPTION) return AbstractUIHandler.ANSWER_CANCEL;
445
446             if (user_choice == JOptionPane.YES_OPTION) return AbstractUIHandler.ANSWER_YES;
447
448             if (user_choice == JOptionPane.NO_OPTION) return AbstractUIHandler.ANSWER_NO;
449
450             return default_choice;
451         }
452
453     }
454
455     /**
456      * The actions events handler.
457      *
458      * @author Julien Ponge
459      */

460     class ActionsHandler implements ActionListener JavaDoc
461     {
462
463         /**
464          * Action handling method.
465          *
466          * @param e The event.
467          */

468         public void actionPerformed(ActionEvent JavaDoc e)
469         {
470             Object JavaDoc src = e.getSource();
471             if (src == quitButton)
472                 System.exit(0);
473             else if (src == destroyButton)
474             {
475                 destroyButton.setEnabled(false);
476                 Destroyer destroyer = new Destroyer(installPath,
477                         targetDestroyCheckbox.isSelected(), new DestroyerHandler());
478                 destroyer.start();
479             }
480         }
481     }
482
483     /**
484      * Returns the langpack.
485      *
486      * @return Returns the langpack.
487      */

488     public static LocaleDatabase getLangpack()
489     {
490         return langpack;
491     }
492
493 }
494
Popular Tags