2 * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
5 * http://izpack.codehaus.org/
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package com.izforge.izpack.uninstaller;
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;
31 import java.awt.event.*;
32 import java.io.BufferedReader;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
38 * The uninstaller frame class.
40 * @author Julien Ponge
42 public class UninstallerFrame extends JFrame
48 private static final long serialVersionUID = 3257281444152684850L;
53 private IconsDatabase icons;
58 protected static LocaleDatabase langpack;
61 * The target destroy checkbox.
63 protected JCheckBox targetDestroyCheckbox;
68 protected JProgressBar progressBar;
73 protected JButton destroyButton;
78 protected JButton quitButton;
81 * The buttons hover color.
83 private Color buttonsHColor = new Color(230, 230, 230);
86 * The installation path.
88 protected String installPath;
93 * @param displayForceOption If true, display to the user the option permitting to force
95 * @param forceOptionState If true, force deletion is activated.
96 * @throws Exception Description of the Exception
98 public UninstallerFrame(boolean displayForceOption, boolean forceOptionState) throws Exception
100 super("IzPack - Uninstaller");
103 langpack = new LocaleDatabase(UninstallerFrame.class.getResourceAsStream("/langpack.xml"));
105 icons = new IconsDatabase();
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"));
111 // Sets the frame icon
112 setIconImage(icons.getImageIcon("JFrameIcon").getImage());
114 // We build the GUI & show it
115 buildGUI(displayForceOption, forceOptionState);
116 addWindowListener(new WindowHandler());
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.
130 private void buildGUI(boolean displayForceOption, boolean forceOptionState)
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);
139 // We prepare our action handler
140 ActionsHandler handler = new ActionsHandler();
142 // Prepares the glass pane to block gui interaction when needed
143 JPanel glassPane = (JPanel) getGlassPane();
144 glassPane.addMouseListener(new MouseAdapter()
147 glassPane.addMouseMotionListener(new MouseMotionAdapter()
150 glassPane.addKeyListener(new KeyAdapter()
154 // We set-up the buttons factory
155 ButtonFactory.useButtonIcons();
156 ButtonFactory.useHighlightButtons();
158 // We put our components
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);
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)
174 contentPane.add(targetDestroyCheckbox);
176 gbConstraints.fill = GridBagConstraints.HORIZONTAL;
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);
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);
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);
205 * Centers a window on screen.
207 * @param frame The window to center.
209 private void centerFrame(Window frame)
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);
218 * Sets the parameters of a GridBagConstraints object.
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
228 private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh,
229 double wx, double wy)
240 * Gets the installation path from the log file.
242 * @throws Exception Description of the Exception
244 private void getInstallPath() throws Exception
246 InputStream in = UninstallerFrame.class.getResourceAsStream("/install.log");
247 InputStreamReader inReader = new InputStreamReader(in);
248 BufferedReader reader = new BufferedReader(inReader);
249 installPath = reader.readLine();
256 * @throws Exception Description of the Exception
258 private void loadIcons() throws Exception
261 icons = new IconsDatabase();
266 url = UninstallerFrame.class.getResource("/img/trash.png");
267 img = new ImageIcon(url);
268 icons.put("delete", img);
270 url = UninstallerFrame.class.getResource("/img/stop.png");
271 img = new ImageIcon(url);
272 icons.put("stop", img);
274 url = UninstallerFrame.class.getResource("/img/flag.png");
275 img = new ImageIcon(url);
276 icons.put("warning", img);
278 url = UninstallerFrame.class.getResource("/img/JFrameIcon.png");
279 img = new ImageIcon(url);
280 icons.put("JFrameIcon", img);
284 * Blocks GUI interaction.
286 public void blockGUI()
288 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
289 getGlassPane().setVisible(true);
290 getGlassPane().setEnabled(true);
294 * Releases GUI interaction.
296 public void releaseGUI()
298 getGlassPane().setEnabled(false);
299 getGlassPane().setVisible(false);
300 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
304 * The window events handler.
306 * @author Julien Ponge
308 private final class WindowHandler extends WindowAdapter
312 * We can't avoid the exit here, so don't call exit elsewhere.
314 * @param e The event.
316 public void windowClosing(WindowEvent e)
318 Housekeeper.getInstance().shutDown(0);
323 * The destroyer handler.
325 * This class also implements the InstallListener because the FileExecutor needs it. TODO: get
326 * rid of the InstallListener - implement generic Listener
328 * @author Julien Ponge
329 * @author Tino Schwarze
331 private final class DestroyerHandler implements
332 com.izforge.izpack.util.AbstractUIProgressHandler
336 * The destroyer starts.
338 * @param name The name of the overall action. Not used here.
339 * @param max The maximum value of the progress.
341 public void startAction(final String name, final int max)
343 SwingUtilities.invokeLater(new Runnable()
347 progressBar.setMinimum(0);
348 progressBar.setMaximum(max);
355 * The destroyer stops.
357 public void stopAction()
359 SwingUtilities.invokeLater(new Runnable()
363 progressBar.setString(langpack.getString("InstallPanel.finished"));
364 targetDestroyCheckbox.setEnabled(false);
365 destroyButton.setEnabled(false);
372 * The destroyer progresses.
374 * @param pos The actual position.
375 * @param message The message.
377 public void progress(final int pos, final String message)
379 SwingUtilities.invokeLater(new Runnable()
383 progressBar.setValue(pos);
384 progressBar.setString(message);
392 public void nextStep(String step_name, int step_no, int no_of_substeps)
400 public void setSubStepNo(int no_of_substeps)
406 * Output a notification.
412 public void emitNotification(String text)
421 public boolean emitWarning(String title, String text)
423 return (JOptionPane.showConfirmDialog(null, text, title, JOptionPane.OK_CANCEL_OPTION,
424 JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION);
428 * The destroyer encountered an error.
430 * @param error The error message.
432 public void emitError(String title, String error)
434 progressBar.setString(error);
435 JOptionPane.showMessageDialog(null, error, title, JOptionPane.OK_CANCEL_OPTION);
439 * The destroyer encountered an error.
441 * @param error The error message.
443 public void emitErrorAndBlockNext(String title, String error)
445 emitError(title, error);
449 * Ask the user a question.
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)
457 public int askQuestion(String title, String question, int choices)
459 return askQuestion(title, question, choices, -1);
463 * Ask the user a question.
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)
472 public int askQuestion(String title, String question, int choices, int default_choice)
476 if (choices == AbstractUIHandler.CHOICES_YES_NO)
478 jo_choices = JOptionPane.YES_NO_OPTION;
480 else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL)
482 jo_choices = JOptionPane.YES_NO_CANCEL_OPTION;
485 int user_choice = JOptionPane.showConfirmDialog(null, question, title,
486 jo_choices, JOptionPane.QUESTION_MESSAGE);
488 if (user_choice == JOptionPane.CANCEL_OPTION)
490 return AbstractUIHandler.ANSWER_CANCEL;
493 if (user_choice == JOptionPane.YES_OPTION)
495 return AbstractUIHandler.ANSWER_YES;
498 if (user_choice == JOptionPane.NO_OPTION)
500 return AbstractUIHandler.ANSWER_NO;
503 return default_choice;
509 * The actions events handler.
511 * @author Julien Ponge
513 class ActionsHandler implements ActionListener
517 * Action handling method.
519 * @param e The event.
521 public void actionPerformed(ActionEvent e)
523 Object src = e.getSource();
524 if (src == quitButton)
526 Housekeeper.getInstance().shutDown(0);
528 else if (src == destroyButton)
530 destroyButton.setEnabled(false);
531 Destroyer destroyer = new Destroyer(installPath,
532 targetDestroyCheckbox.isSelected(), new DestroyerHandler());
539 * Returns the langpack.
541 * @return Returns the langpack.
543 public static LocaleDatabase getLangpack()