OSDN Git Service

7bacb132e5aca7f49d08ab8aa639a0b1fc96f663
[redminele/redminele.git] / installer / IzPack / src / lib / com / izforge / izpack / uninstaller / UninstallerFrame.java
1 /*
2  * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
3  * 
4  * http://izpack.org/
5  * http://izpack.codehaus.org/
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 com.izforge.izpack.LocaleDatabase;
23 import com.izforge.izpack.gui.ButtonFactory;
24 import com.izforge.izpack.gui.IconsDatabase;
25 import com.izforge.izpack.util.AbstractUIHandler;
26 import com.izforge.izpack.util.Housekeeper;
27
28 import javax.swing.*;
29
30 import java.awt.*;
31 import java.awt.event.*;
32 import java.io.BufferedReader;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.net.URL;
36
37 /**
38  * The uninstaller frame class.
39  *
40  * @author Julien Ponge
41  */
42 public class UninstallerFrame extends JFrame
43 {
44
45     /**
46      *
47      */
48     private static final long serialVersionUID = 3257281444152684850L;
49
50     /**
51      * The icons database.
52      */
53     private IconsDatabase icons;
54
55     /**
56      * The language pack.
57      */
58     protected static LocaleDatabase langpack;
59
60     /**
61      * The target destroy checkbox.
62      */
63     protected JCheckBox targetDestroyCheckbox;
64
65     /**
66      * The progress bar.
67      */
68     protected JProgressBar progressBar;
69
70     /**
71      * The destroy button.
72      */
73     protected JButton destroyButton;
74
75     /**
76      * The quit button.
77      */
78     protected JButton quitButton;
79
80     /**
81      * The buttons hover color.
82      */
83     private Color buttonsHColor = new Color(230, 230, 230);
84
85     /**
86      * The installation path.
87      */
88     protected String installPath;
89
90     /**
91      * The constructor.
92      *
93      * @param displayForceOption If true, display to the user the option permitting to force
94      *                           all files deletion.
95      * @param forceOptionState   If true, force deletion is activated.
96      * @throws Exception Description of the Exception
97      */
98     public UninstallerFrame(boolean displayForceOption, boolean forceOptionState) throws Exception
99     {
100         super("IzPack - Uninstaller");
101
102         // Initializations
103         langpack = new LocaleDatabase(UninstallerFrame.class.getResourceAsStream("/langpack.xml"));
104         getInstallPath();
105         icons = new IconsDatabase();
106         loadIcons();
107         UIManager.put("OptionPane.yesButtonText", langpack.getString("installer.yes"));
108         UIManager.put("OptionPane.noButtonText", langpack.getString("installer.no"));
109         UIManager.put("OptionPane.cancelButtonText", langpack.getString("installer.cancel"));
110
111         // Sets the frame icon
112         setIconImage(icons.getImageIcon("JFrameIcon").getImage());
113
114         // We build the GUI & show it
115         buildGUI(displayForceOption, forceOptionState);
116         addWindowListener(new WindowHandler());
117         pack();
118         centerFrame(this);
119         setResizable(false);
120         setVisible(true);
121     }
122
123     /**
124      * Builds the GUI.
125      *
126      * @param displayForceOption If true, display to the user the option permitting to force
127      *                           all files deletion.
128      * @param forceOptionState   If true, force deletion is activated.
129      */
130     private void buildGUI(boolean displayForceOption, boolean forceOptionState)
131     {
132         // We initialize our layout
133         JPanel contentPane = (JPanel) getContentPane();
134         GridBagLayout layout = new GridBagLayout();
135         contentPane.setLayout(layout);
136         GridBagConstraints gbConstraints = new GridBagConstraints();
137         gbConstraints.insets = new Insets(5, 5, 5, 5);
138
139         // We prepare our action handler
140         ActionsHandler handler = new ActionsHandler();
141
142         // Prepares the glass pane to block gui interaction when needed
143         JPanel glassPane = (JPanel) getGlassPane();
144         glassPane.addMouseListener(new MouseAdapter()
145         {
146         });
147         glassPane.addMouseMotionListener(new MouseMotionAdapter()
148         {
149         });
150         glassPane.addKeyListener(new KeyAdapter()
151         {
152         });
153
154         // We set-up the buttons factory
155         ButtonFactory.useButtonIcons();
156         ButtonFactory.useHighlightButtons();
157
158         // We put our components
159
160         JLabel warningLabel = new JLabel(langpack.getString("uninstaller.warning"), icons
161                 .getImageIcon("warning"), JLabel.TRAILING);
162         buildConstraints(gbConstraints, 0, 0, 2, 1, 1.0, 0.0);
163         gbConstraints.anchor = GridBagConstraints.WEST;
164         gbConstraints.fill = GridBagConstraints.NONE;
165         layout.addLayoutComponent(warningLabel, gbConstraints);
166         contentPane.add(warningLabel);
167
168         targetDestroyCheckbox = new JCheckBox(langpack.getString("uninstaller.destroytarget")
169                 + installPath, forceOptionState);
170         buildConstraints(gbConstraints, 0, 1, 2, 1, 1.0, 0.0);
171         layout.addLayoutComponent(targetDestroyCheckbox, gbConstraints);
172         if (displayForceOption)
173         {
174             contentPane.add(targetDestroyCheckbox);
175         }
176         gbConstraints.fill = GridBagConstraints.HORIZONTAL;
177
178         progressBar = new JProgressBar();
179         progressBar.setStringPainted(true);
180         progressBar.setString(langpack.getString("InstallPanel.begin"));
181         buildConstraints(gbConstraints, 0, 2, 2, 1, 1.0, 0.0);
182         layout.addLayoutComponent(progressBar, gbConstraints);
183         contentPane.add(progressBar);
184
185         destroyButton = ButtonFactory.createButton(langpack.getString("uninstaller.uninstall"),
186                 icons.getImageIcon("delete"), buttonsHColor);
187         destroyButton.addActionListener(handler);
188         buildConstraints(gbConstraints, 0, 3, 1, 1, 0.5, 0.0);
189         gbConstraints.fill = GridBagConstraints.NONE;
190         gbConstraints.anchor = GridBagConstraints.WEST;
191         layout.addLayoutComponent(destroyButton, gbConstraints);
192         contentPane.add(destroyButton);
193
194         quitButton = ButtonFactory.createButton(langpack.getString("installer.quit"), icons
195                 .getImageIcon("stop"), buttonsHColor);
196         quitButton.addActionListener(handler);
197         buildConstraints(gbConstraints, 1, 3, 1, 1, 0.5, 0.0);
198         gbConstraints.anchor = GridBagConstraints.EAST;
199         layout.addLayoutComponent(quitButton, gbConstraints);
200         contentPane.add(quitButton);
201
202     }
203
204     /**
205      * Centers a window on screen.
206      *
207      * @param frame The window to center.
208      */
209     private void centerFrame(Window frame)
210     {
211         Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
212         Dimension frameSize = frame.getSize();
213         frame.setLocation(center.x - frameSize.width / 2,
214                 center.y - frameSize.height / 2 - 10);
215     }
216
217     /**
218      * Sets the parameters of a GridBagConstraints object.
219      *
220      * @param gbc The constraints object.
221      * @param gx  The x coordinates.
222      * @param gy  The y coordinates.
223      * @param gw  The width.
224      * @param wx  The x wheight.
225      * @param wy  The y wheight.
226      * @param gh  Description of the Parameter
227      */
228     private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh,
229                                   double wx, double wy)
230     {
231         gbc.gridx = gx;
232         gbc.gridy = gy;
233         gbc.gridwidth = gw;
234         gbc.gridheight = gh;
235         gbc.weightx = wx;
236         gbc.weighty = wy;
237     }
238
239     /**
240      * Gets the installation path from the log file.
241      *
242      * @throws Exception Description of the Exception
243      */
244     private void getInstallPath() throws Exception
245     {
246         InputStream in = UninstallerFrame.class.getResourceAsStream("/install.log");
247         InputStreamReader inReader = new InputStreamReader(in);
248         BufferedReader reader = new BufferedReader(inReader);
249         installPath = reader.readLine();
250         reader.close();
251     }
252
253     /**
254      * Loads the icons.
255      *
256      * @throws Exception Description of the Exception
257      */
258     private void loadIcons() throws Exception
259     {
260         // Initialisations
261         icons = new IconsDatabase();
262         URL url;
263         ImageIcon img;
264
265         // We load it
266         url = UninstallerFrame.class.getResource("/img/trash.png");
267         img = new ImageIcon(url);
268         icons.put("delete", img);
269
270         url = UninstallerFrame.class.getResource("/img/stop.png");
271         img = new ImageIcon(url);
272         icons.put("stop", img);
273
274         url = UninstallerFrame.class.getResource("/img/flag.png");
275         img = new ImageIcon(url);
276         icons.put("warning", img);
277
278         url = UninstallerFrame.class.getResource("/img/JFrameIcon.png");
279         img = new ImageIcon(url);
280         icons.put("JFrameIcon", img);
281     }
282
283     /**
284      * Blocks GUI interaction.
285      */
286     public void blockGUI()
287     {
288         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
289         getGlassPane().setVisible(true);
290         getGlassPane().setEnabled(true);
291     }
292
293     /**
294      * Releases GUI interaction.
295      */
296     public void releaseGUI()
297     {
298         getGlassPane().setEnabled(false);
299         getGlassPane().setVisible(false);
300         setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
301     }
302
303     /**
304      * The window events handler.
305      *
306      * @author Julien Ponge
307      */
308     private final class WindowHandler extends WindowAdapter
309     {
310
311         /**
312          * We can't avoid the exit here, so don't call exit elsewhere.
313          *
314          * @param e The event.
315          */
316         public void windowClosing(WindowEvent e)
317         {
318             Housekeeper.getInstance().shutDown(0);
319         }
320     }
321
322     /**
323      * The destroyer handler.
324      * <p/>
325      * This class also implements the InstallListener because the FileExecutor needs it. TODO: get
326      * rid of the InstallListener - implement generic Listener
327      *
328      * @author Julien Ponge
329      * @author Tino Schwarze
330      */
331     private final class DestroyerHandler implements
332             com.izforge.izpack.util.AbstractUIProgressHandler
333     {
334
335         /**
336          * The destroyer starts.
337          *
338          * @param name The name of the overall action. Not used here.
339          * @param max  The maximum value of the progress.
340          */
341         public void startAction(final String name, final int max)
342         {
343             SwingUtilities.invokeLater(new Runnable()
344             {
345                 public void run()
346                 {
347                     progressBar.setMinimum(0);
348                     progressBar.setMaximum(max);
349                     blockGUI();
350                 }
351             });
352         }
353
354         /**
355          * The destroyer stops.
356          */
357         public void stopAction()
358         {
359             SwingUtilities.invokeLater(new Runnable()
360             {
361                 public void run()
362                 {
363                     progressBar.setString(langpack.getString("InstallPanel.finished"));
364                     targetDestroyCheckbox.setEnabled(false);
365                     destroyButton.setEnabled(false);
366                     releaseGUI();
367                 }
368             });
369         }
370
371         /**
372          * The destroyer progresses.
373          *
374          * @param pos     The actual position.
375          * @param message The message.
376          */
377         public void progress(final int pos, final String message)
378         {
379             SwingUtilities.invokeLater(new Runnable()
380             {
381                 public void run()
382                 {
383                     progressBar.setValue(pos);
384                     progressBar.setString(message);
385                 }
386             });
387         }
388
389         /**
390          * {@inheritDoc}
391          */
392         public void nextStep(String step_name, int step_no, int no_of_substeps)
393         {
394             // not used
395         }
396
397         /**
398          * {@inheritDoc}
399          */
400         public void setSubStepNo(int no_of_substeps)
401         {
402             // not used
403         }
404
405         /**
406          * Output a notification.
407          * <p/>
408          * Does nothing here.
409          *
410          * @param text
411          */
412         public void emitNotification(String text)
413         {
414         }
415
416         /**
417          * Output a warning.
418          *
419          * @param text
420          */
421         public boolean emitWarning(String title, String text)
422         {
423             return (JOptionPane.showConfirmDialog(null, text, title, JOptionPane.OK_CANCEL_OPTION,
424                     JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION);
425         }
426
427         /**
428          * The destroyer encountered an error.
429          *
430          * @param error The error message.
431          */
432         public void emitError(String title, String error)
433         {
434             progressBar.setString(error);
435             JOptionPane.showMessageDialog(null, error, title, JOptionPane.OK_CANCEL_OPTION);
436         }
437         
438         /**
439          * The destroyer encountered an error.
440          * 
441          * @param error The error message.
442          */
443         public void emitErrorAndBlockNext(String title, String error)
444         {
445             emitError(title, error);
446         }
447
448         /**
449          * Ask the user a question.
450          *
451          * @param title    Message title.
452          * @param question The question.
453          * @param choices  The set of choices to present.
454          * @return The user's choice.
455          * @see AbstractUIHandler#askQuestion(String, String, int)
456          */
457         public int askQuestion(String title, String question, int choices)
458         {
459             return askQuestion(title, question, choices, -1);
460         }
461
462         /**
463          * Ask the user a question.
464          *
465          * @param title          Message title.
466          * @param question       The question.
467          * @param choices        The set of choices to present.
468          * @param default_choice The default choice. (-1 = no default choice)
469          * @return The user's choice.
470          * @see AbstractUIHandler#askQuestion(String, String, int, int)
471          */
472         public int askQuestion(String title, String question, int choices, int default_choice)
473         {
474             int jo_choices = 0;
475
476             if (choices == AbstractUIHandler.CHOICES_YES_NO)
477             {
478                 jo_choices = JOptionPane.YES_NO_OPTION;
479             }
480             else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL)
481             {
482                 jo_choices = JOptionPane.YES_NO_CANCEL_OPTION;
483             }
484
485             int user_choice = JOptionPane.showConfirmDialog(null, question, title,
486                     jo_choices, JOptionPane.QUESTION_MESSAGE);
487
488             if (user_choice == JOptionPane.CANCEL_OPTION)
489             {
490                 return AbstractUIHandler.ANSWER_CANCEL;
491             }
492
493             if (user_choice == JOptionPane.YES_OPTION)
494             {
495                 return AbstractUIHandler.ANSWER_YES;
496             }
497
498             if (user_choice == JOptionPane.NO_OPTION)
499             {
500                 return AbstractUIHandler.ANSWER_NO;
501             }
502
503             return default_choice;
504         }
505
506     }
507
508     /**
509      * The actions events handler.
510      *
511      * @author Julien Ponge
512      */
513     class ActionsHandler implements ActionListener
514     {
515
516         /**
517          * Action handling method.
518          *
519          * @param e The event.
520          */
521         public void actionPerformed(ActionEvent e)
522         {
523             Object src = e.getSource();
524             if (src == quitButton)
525             {
526                 Housekeeper.getInstance().shutDown(0);
527             }
528             else if (src == destroyButton)
529             {
530                 destroyButton.setEnabled(false);
531                 Destroyer destroyer = new Destroyer(installPath,
532                         targetDestroyCheckbox.isSelected(), new DestroyerHandler());
533                 destroyer.start();
534             }
535         }
536     }
537
538     /**
539      * Returns the langpack.
540      *
541      * @return Returns the langpack.
542      */
543     public static LocaleDatabase getLangpack()
544     {
545         return langpack;
546     }
547
548 }