| 1 19 20 package com.izforge.izpack.uninstaller; 21 22 import java.awt.Color ; 23 import java.awt.Cursor ; 24 import java.awt.Dimension ; 25 import java.awt.GraphicsEnvironment ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.awt.Insets ; 29 import java.awt.Point ; 30 import java.awt.Window ; 31 import java.awt.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 import java.awt.event.KeyAdapter ; 34 import java.awt.event.MouseAdapter ; 35 import java.awt.event.MouseMotionAdapter ; 36 import java.awt.event.WindowAdapter ; 37 import java.awt.event.WindowEvent ; 38 import java.io.BufferedReader ; 39 import java.io.InputStream ; 40 import java.io.InputStreamReader ; 41 import java.net.URL ; 42 43 import javax.swing.ImageIcon ; 44 import javax.swing.JButton ; 45 import javax.swing.JCheckBox ; 46 import javax.swing.JFrame ; 47 import javax.swing.JLabel ; 48 import javax.swing.JOptionPane ; 49 import javax.swing.JPanel ; 50 import javax.swing.JProgressBar ; 51 import javax.swing.SwingUtilities ; 52 import javax.swing.UIManager ; 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 64 public class UninstallerFrame extends JFrame  65 { 66 67 70 private static final long serialVersionUID = 3257281444152684850L; 71 72 73 private IconsDatabase icons; 74 75 76 protected static LocaleDatabase langpack; 77 78 79 protected JCheckBox targetDestroyCheckbox; 80 81 82 protected JProgressBar progressBar; 83 84 85 protected JButton destroyButton; 86 87 88 protected JButton quitButton; 89 90 91 private Color buttonsHColor = new Color (230, 230, 230); 92 93 94 protected String installPath; 95 96 101 public UninstallerFrame() throws Exception  102 { 103 super("IzPack - Uninstaller"); 104 105 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 setIconImage(icons.getImageIcon("JFrameIcon").getImage()); 116 117 buildGUI(); 119 addWindowListener(new WindowHandler()); 120 pack(); 121 centerFrame(this); 122 setResizable(false); 123 setVisible(true); 124 } 125 126 127 private void buildGUI() 128 { 129 JPanel contentPane = (JPanel ) getContentPane(); 131 GridBagLayout layout = new GridBagLayout (); 132 contentPane.setLayout(layout); 133 GridBagConstraints gbConstraints = new GridBagConstraints (); 134 gbConstraints.insets = new Insets (5, 5, 5, 5); 135 136 ActionsHandler handler = new ActionsHandler(); 138 139 JPanel glassPane = (JPanel ) getGlassPane(); 141 glassPane.addMouseListener(new MouseAdapter () {}); 142 glassPane.addMouseMotionListener(new MouseMotionAdapter () {}); 143 glassPane.addKeyListener(new KeyAdapter () {}); 144 145 ButtonFactory.useButtonIcons(); 147 ButtonFactory.useHighlightButtons(); 148 149 151 JLabel warningLabel = new JLabel (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 (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 (); 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 197 private void centerFrame(Window frame) 198 { 199 Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); 200 Dimension frameSize = frame.getSize(); 201 frame.setLocation(center.x - frameSize.width / 2, 202 center.y - frameSize.height / 2 - 10); 203 } 204 205 216 private void buildConstraints(GridBagConstraints 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 232 private void getInstallPath() throws Exception  233 { 234 InputStream in = UninstallerFrame.class.getResourceAsStream("/install.log"); 235 InputStreamReader inReader = new InputStreamReader (in); 236 BufferedReader reader = new BufferedReader (inReader); 237 installPath = reader.readLine(); 238 reader.close(); 239 } 240 241 246 private void loadIcons() throws Exception  247 { 248 icons = new IconsDatabase(); 250 URL url; 251 ImageIcon img; 252 253 url = UninstallerFrame.class.getResource("/img/trash.png"); 255 img = new ImageIcon (url); 256 icons.put("delete", img); 257 258 url = UninstallerFrame.class.getResource("/img/stop.png"); 259 img = new ImageIcon (url); 260 icons.put("stop", img); 261 262 url = UninstallerFrame.class.getResource("/img/flag.png"); 263 img = new ImageIcon (url); 264 icons.put("warning", img); 265 266 url = UninstallerFrame.class.getResource("/img/JFrameIcon.png"); 267 img = new ImageIcon (url); 268 icons.put("JFrameIcon", img); 269 } 270 271 272 public void blockGUI() 273 { 274 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 275 getGlassPane().setVisible(true); 276 getGlassPane().setEnabled(true); 277 } 278 279 280 public void releaseGUI() 281 { 282 getGlassPane().setEnabled(false); 283 getGlassPane().setVisible(false); 284 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 285 } 286 287 292 private final class WindowHandler extends WindowAdapter  293 { 294 295 300 public void windowClosing(WindowEvent e) 301 { 302 System.exit(0); 303 } 304 } 305 306 315 private final class DestroyerHandler implements 316 com.izforge.izpack.util.AbstractUIProgressHandler 317 { 318 319 325 public void startAction(final String name, final int max) 326 { 327 SwingUtilities.invokeLater(new Runnable () { 328 public void run() 329 { 330 progressBar.setMinimum(0); 331 progressBar.setMaximum(max); 332 blockGUI(); 333 } 334 }); 335 } 336 337 338 public void stopAction() 339 { 340 SwingUtilities.invokeLater(new Runnable () { 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 357 public void progress(final int pos, final String message) 358 { 359 SwingUtilities.invokeLater(new Runnable () { 360 public void run() 361 { 362 progressBar.setValue(pos); 363 progressBar.setString(message); 364 } 365 }); 366 } 367 368 public void nextStep(String step_name, int step_no, int no_of_substeps) 369 { 370 } 371 372 379 public void emitNotification(String text) 380 { 381 } 382 383 388 public boolean emitWarning(String title, String text) 389 { 390 return (JOptionPane.showConfirmDialog(null, text, title, JOptionPane.OK_CANCEL_OPTION, 391 JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION); 392 } 393 394 399 public void emitError(String title, String error) 400 { 401 progressBar.setString(error); 402 JOptionPane.showMessageDialog(null, error, title, JOptionPane.OK_CANCEL_OPTION); 403 } 404 405 416 public int askQuestion(String title, String question, int choices) 417 { 418 return askQuestion(title, question, choices, -1); 419 } 420 421 432 public int askQuestion(String title, String 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 ) 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 460 class ActionsHandler implements ActionListener  461 { 462 463 468 public void actionPerformed(ActionEvent e) 469 { 470 Object 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 488 public static LocaleDatabase getLangpack() 489 { 490 return langpack; 491 } 492 493 } 494 | Popular Tags |