Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / uninstaller / UninstallerFrame.java @ 15280

History | View | Annotate | Download (14.4 KB)

1
/*
2
 *  $Id: UninstallerFrame.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 *  IzPack
4
 *  Copyright (C) 2001-2004 Julien Ponge
5
 *
6
 *  File :               UninstallerFrame.java
7
 *  Description :        The uninstaller frame class.
8
 *  Author's email :     julien@izforge.com
9
 *  Author's Website :   http://www.izforge.com
10
 *
11
 *  This program is free software; you can redistribute it and/or
12
 *  modify it under the terms of the GNU General Public License
13
 *  as published by the Free Software Foundation; either version 2
14
 *  of the License, or any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program; if not, write to the Free Software
23
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
 */
25
package com.izforge.izpack.uninstaller;
26

    
27
import java.awt.Color;
28
import java.awt.Cursor;
29
import java.awt.Dimension;
30
import java.awt.GridBagConstraints;
31
import java.awt.GridBagLayout;
32
import java.awt.Insets;
33
import java.awt.Toolkit;
34
import java.awt.Window;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionListener;
37
import java.awt.event.KeyAdapter;
38
import java.awt.event.MouseAdapter;
39
import java.awt.event.MouseMotionAdapter;
40
import java.awt.event.WindowAdapter;
41
import java.awt.event.WindowEvent;
42
import java.io.BufferedReader;
43
import java.io.InputStream;
44
import java.io.InputStreamReader;
45
import java.net.URL;
46

    
47
import javax.swing.ImageIcon;
48
import javax.swing.JButton;
49
import javax.swing.JCheckBox;
50
import javax.swing.JFrame;
51
import javax.swing.JLabel;
52
import javax.swing.JOptionPane;
53
import javax.swing.JPanel;
54
import javax.swing.JProgressBar;
55
import javax.swing.UIManager;
56

    
57
import com.izforge.izpack.LocaleDatabase;
58
import com.izforge.izpack.gui.ButtonFactory;
59
import com.izforge.izpack.gui.IconsDatabase;
60
import com.izforge.izpack.util.AbstractUIHandler;
61

    
62
/**
63
 *  The uninstaller frame class.
64
 *
65
 * @author     Julien Ponge
66
 */
67
public class UninstallerFrame extends JFrame
68
{
69
  /**  The icons database. */
70
  private IconsDatabase icons;
71

    
72
  /**  The language pack. */
73
  protected LocaleDatabase langpack;
74

    
75
  /**  The warning label. */
76
  private JLabel warningLabel;
77

    
78
  /**  The target destroy checkbox. */
79
  protected JCheckBox targetDestroyCheckbox;
80

    
81
  /**  The progress bar. */
82
  protected JProgressBar progressBar;
83

    
84
  /**  The destroy button. */
85
  protected JButton destroyButton;
86

    
87
  /**  The quit button. */
88
  protected JButton quitButton;
89

    
90
  /**  The layout. */
91
  private GridBagLayout layout;
92

    
93
  /**  the layout constraints. */
94
  private GridBagConstraints gbConstraints;
95

    
96
  /**  The buttons hover color. */
97
  private Color buttonsHColor = new Color(230, 230, 230);
98

    
99
  /**  The installation path. */
100
  protected String installPath;
101

    
102
  /**
103
   *  The constructor.
104
   *
105
   * @exception  Exception  Description of the Exception
106
   */
107
  public UninstallerFrame() throws Exception
108
  {
109
    super("IzPack - Uninstaller");
110

    
111
    // Initializations
112
    langpack =
113
      new LocaleDatabase(UninstallerFrame.class.getResourceAsStream("/langpack.xml"));
114
    getInstallPath();
115
    icons = new IconsDatabase();
116
    loadIcons();
117
    UIManager.put(
118
      "OptionPane.yesButtonText",
119
      langpack.getString("installer.yes"));
120
    UIManager.put(
121
      "OptionPane.noButtonText",
122
      langpack.getString("installer.no"));
123
    UIManager.put(
124
      "OptionPane.cancelButtonText",
125
      langpack.getString("installer.cancel"));
126

    
127
    // Sets the frame icon
128
    setIconImage(icons.getImageIcon("JFrameIcon").getImage());
129

    
130
    // We build the GUI & show it
131
    buildGUI();
132
    addWindowListener(new WindowHandler());
133
    pack();
134
    centerFrame(this);
135
    setResizable(false);
136
    setVisible(true);
137
  }
138

    
139
  /**  Builds the GUI.  */
140
  private void buildGUI()
141
  {
142
    // We initialize our layout
143
    JPanel contentPane = (JPanel) getContentPane();
144
    layout = new GridBagLayout();
145
    contentPane.setLayout(layout);
146
    gbConstraints = new GridBagConstraints();
147
    gbConstraints.insets = new Insets(5, 5, 5, 5);
148

    
149
    // We prepare our action handler
150
    ActionsHandler handler = new ActionsHandler();
151

    
152
    // Prepares the glass pane to block gui interaction when needed
153
    JPanel glassPane = (JPanel) getGlassPane();
154
    glassPane.addMouseListener(new MouseAdapter()
155
    {
156
    });
157
    glassPane.addMouseMotionListener(new MouseMotionAdapter()
158
    {
159
    });
160
    glassPane.addKeyListener(new KeyAdapter()
161
    {
162
    });
163

    
164
    // We set-up the buttons factory
165
    ButtonFactory.useButtonIcons();
166
    ButtonFactory.useHighlightButtons();
167

    
168
    // We put our components
169

    
170
    warningLabel =
171
      new JLabel(
172
        langpack.getString("uninstaller.warning"),
173
        icons.getImageIcon("warning"),
174
        JLabel.TRAILING);
175
    buildConstraints(gbConstraints, 0, 0, 2, 1, 1.0, 0.0);
176
    gbConstraints.anchor = GridBagConstraints.WEST;
177
    gbConstraints.fill = GridBagConstraints.NONE;
178
    layout.addLayoutComponent(warningLabel, gbConstraints);
179
    contentPane.add(warningLabel);
180

    
181
    targetDestroyCheckbox =
182
      new JCheckBox(
183
        langpack.getString("uninstaller.destroytarget") + installPath,
184
        false);
185
    buildConstraints(gbConstraints, 0, 1, 2, 1, 1.0, 0.0);
186
    layout.addLayoutComponent(targetDestroyCheckbox, gbConstraints);
187
    contentPane.add(targetDestroyCheckbox);
188
    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
189

    
190
    progressBar = new JProgressBar();
191
    progressBar.setStringPainted(true);
192
    progressBar.setString(langpack.getString("InstallPanel.begin"));
193
    buildConstraints(gbConstraints, 0, 2, 2, 1, 1.0, 0.0);
194
    layout.addLayoutComponent(progressBar, gbConstraints);
195
    contentPane.add(progressBar);
196

    
197
    destroyButton =
198
      ButtonFactory.createButton(
199
        langpack.getString("uninstaller.uninstall"),
200
        icons.getImageIcon("delete"),
201
        buttonsHColor);
202
    destroyButton.addActionListener(handler);
203
    buildConstraints(gbConstraints, 0, 3, 1, 1, 0.5, 0.0);
204
    gbConstraints.fill = GridBagConstraints.NONE;
205
    gbConstraints.anchor = GridBagConstraints.WEST;
206
    layout.addLayoutComponent(destroyButton, gbConstraints);
207
    contentPane.add(destroyButton);
208

    
209
    quitButton =
210
      ButtonFactory.createButton(
211
        langpack.getString("installer.quit"),
212
        icons.getImageIcon("stop"),
213
        buttonsHColor);
214
    quitButton.addActionListener(handler);
215
    buildConstraints(gbConstraints, 1, 3, 1, 1, 0.5, 0.0);
216
    gbConstraints.anchor = GridBagConstraints.EAST;
217
    layout.addLayoutComponent(quitButton, gbConstraints);
218
    contentPane.add(quitButton);
219

    
220
  }
221

    
222
  /**
223
   *  Centers a window on screen.
224
   *
225
   * @param  frame  The window to center.
226
   */
227
  private void centerFrame(Window frame)
228
  {
229
    Dimension frameSize = frame.getSize();
230
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
231
    frame.setLocation(
232
      (screenSize.width - frameSize.width) / 2,
233
      (screenSize.height - frameSize.height) / 2 - 10);
234
  }
235

    
236
  /**
237
   *  Sets the parameters of a GridBagConstraints object.
238
   *
239
   * @param  gbc  The constraints object.
240
   * @param  gx   The x coordinates.
241
   * @param  gy   The y coordinates.
242
   * @param  gw   The width.
243
   * @param  wx   The x wheight.
244
   * @param  wy   The y wheight.
245
   * @param  gh   Description of the Parameter
246
   */
247
  private void buildConstraints(
248
    GridBagConstraints gbc,
249
    int gx,
250
    int gy,
251
    int gw,
252
    int gh,
253
    double wx,
254
    double wy)
255
  {
256
    gbc.gridx = gx;
257
    gbc.gridy = gy;
258
    gbc.gridwidth = gw;
259
    gbc.gridheight = gh;
260
    gbc.weightx = wx;
261
    gbc.weighty = wy;
262
  }
263

    
264
  /**
265
   *  Gets the installation path from the log file.
266
   *
267
   * @exception  Exception  Description of the Exception
268
   */
269
  private void getInstallPath() throws Exception
270
  {
271
    InputStream in = UninstallerFrame.class.getResourceAsStream("/install.log");
272
    InputStreamReader inReader = new InputStreamReader(in);
273
    BufferedReader reader = new BufferedReader(inReader);
274
    installPath = reader.readLine();
275
    reader.close();
276
  }
277

    
278
  /**
279
   *  Loads the icons.
280
   *
281
   * @exception  Exception  Description of the Exception
282
   */
283
  private void loadIcons() throws Exception
284
  {
285
    // Initialisations
286
    icons = new IconsDatabase();
287
    URL url;
288
    ImageIcon img;
289

    
290
    // We load it
291
    url = UninstallerFrame.class.getResource("/img/trash.png");
292
    img = new ImageIcon(url);
293
    icons.put("delete", img);
294

    
295
    url = UninstallerFrame.class.getResource("/img/stop.png");
296
    img = new ImageIcon(url);
297
    icons.put("stop", img);
298

    
299
    url = UninstallerFrame.class.getResource("/img/flag.png");
300
    img = new ImageIcon(url);
301
    icons.put("warning", img);
302

    
303
    url = UninstallerFrame.class.getResource("/img/JFrameIcon.png");
304
    img = new ImageIcon(url);
305
    icons.put("JFrameIcon", img);
306
  }
307

    
308
  /**  Blocks GUI interaction.  */
309
  public void blockGUI()
310
  {
311
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
312
    getGlassPane().setVisible(true);
313
    getGlassPane().setEnabled(true);
314
  }
315

    
316
  /**  Releases GUI interaction.  */
317
  public void releaseGUI()
318
  {
319
    getGlassPane().setEnabled(false);
320
    getGlassPane().setVisible(false);
321
    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
322
  }
323

    
324
  /**
325
   *  The window events handler.
326
   *
327
   * @author     Julien Ponge
328
   */
329
  private final class WindowHandler extends WindowAdapter
330
  {
331
    /**
332
     *  We can't avoid the exit here, so don't call exit elsewhere.
333
     *
334
     * @param  e  The event.
335
     */
336
    public void windowClosing(WindowEvent e)
337
    {
338
      System.exit(0);
339
    }
340
  }
341

    
342
  /**
343
   * The destroyer handler.
344
   *
345
   * This class also implements the InstallListener because the FileExecutor needs it.
346
   * TODO: get rid of the InstallListener - implement generic Listener
347
   * 
348
   * @author     Julien Ponge
349
   * @author     Tino Schwarze
350
   */
351
  private final class DestroyerHandler
352
    implements com.izforge.izpack.util.AbstractUIProgressHandler
353
  {
354
    /**
355
     *  The destroyer starts.
356
     *
357
     * @param name The name of the overall action. Not used here. 
358
     * @param max  The maximum value of the progress.
359
     */
360
    public void startAction(String name, int max)
361
    {
362
      progressBar.setMinimum(0);
363
      progressBar.setMaximum(max);
364
      blockGUI();
365
    }
366

    
367
    /**  The destroyer stops.  */
368
    public void stopAction()
369
    {
370
      progressBar.setString(langpack.getString("InstallPanel.finished"));
371
      targetDestroyCheckbox.setEnabled(false);
372
      destroyButton.setEnabled(false);
373
      releaseGUI();
374
    }
375

    
376
    /**
377
     *  The destroyer progresses.
378
     *
379
     * @param  pos      The actual position.
380
     * @param  message  The message.
381
     */
382
    public void progress(int pos, String message)
383
    {
384
      progressBar.setValue(pos);
385
      progressBar.setString(message);
386
    }
387

    
388
    public void nextStep(String step_name, int step_no, int no_of_substeps)
389
    {
390
    }
391

    
392
    /**
393
     *  Output a notification.
394
     * 
395
     * Does nothing here.
396
     * 
397
     * @param text
398
     */
399
    public void emitNotification(String text)
400
    {
401
    }
402

    
403
    /**
404
     *  Output a warning.
405
     * 
406
     * @param text
407
     */
408
    public boolean emitWarning(String title, String text)
409
    {
410
      return (
411
        JOptionPane.showConfirmDialog(
412
          null,
413
          text,
414
          title,
415
          JOptionPane.OK_CANCEL_OPTION,
416
          JOptionPane.WARNING_MESSAGE)
417
          == JOptionPane.OK_OPTION);
418
    }
419

    
420
    /**
421
     *  The destroyer encountered an error.
422
     *
423
     * @param  error  The error message.
424
     */
425
    public void emitError(String title, String error)
426
    {
427
      progressBar.setString(error);
428
      JOptionPane.showMessageDialog(
429
        null,
430
        error,
431
        title,
432
        JOptionPane.OK_CANCEL_OPTION);
433
    }
434

    
435
    /**
436
    * Ask the user a question.
437
    * 
438
    * @param title Message title.
439
    * @param question The question.
440
    * @param choices The set of choices to present.
441
    * 
442
    * @return The user's choice.
443
    * 
444
    * @see AbstractUIHandler#askQuestion(String, String, int)
445
    */
446
    public int askQuestion(String title, String question, int choices)
447
    {
448
      return askQuestion(title, question, choices, -1);
449
    }
450

    
451
    /**
452
     * Ask the user a question.
453
     * 
454
     * @param title Message title.
455
     * @param question The question.
456
     * @param choices The set of choices to present.
457
     * @param default_choice The default choice. (-1 = no default choice)
458
     * 
459
     * @return The user's choice.
460
     * @see AbstractUIHandler#askQuestion(String, String, int, int)
461
     */
462
    public int askQuestion(
463
      String title,
464
      String question,
465
      int choices,
466
      int default_choice)
467
    {
468
      int jo_choices = 0;
469

    
470
      if (choices == AbstractUIHandler.CHOICES_YES_NO)
471
        jo_choices = JOptionPane.YES_NO_OPTION;
472
      else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL)
473
        jo_choices = JOptionPane.YES_NO_CANCEL_OPTION;
474

    
475
      int user_choice =
476
        JOptionPane.showConfirmDialog(
477
          null,
478
          (Object) question,
479
          title,
480
          jo_choices,
481
          JOptionPane.QUESTION_MESSAGE);
482

    
483
      if (user_choice == JOptionPane.CANCEL_OPTION)
484
        return AbstractUIHandler.ANSWER_CANCEL;
485

    
486
      if (user_choice == JOptionPane.YES_OPTION)
487
        return AbstractUIHandler.ANSWER_YES;
488

    
489
      if (user_choice == JOptionPane.NO_OPTION)
490
        return AbstractUIHandler.ANSWER_NO;
491

    
492
      return default_choice;
493
    }
494

    
495
  }
496

    
497
  /**
498
   *  The actions events handler.
499
   *
500
   * @author     Julien Ponge
501
   */
502
  class ActionsHandler implements ActionListener
503
  {
504
    /**
505
     *  Action handling method.
506
     *
507
     * @param  e  The event.
508
     */
509
    public void actionPerformed(ActionEvent e)
510
    {
511
      Object src = e.getSource();
512
      if (src == quitButton)
513
        System.exit(0);
514
      else if (src == destroyButton)
515
      {
516
        Destroyer destroyer =
517
          new Destroyer(
518
            installPath,
519
            targetDestroyCheckbox.isSelected(),
520
            new DestroyerHandler());
521
        destroyer.start();
522
      }
523
    }
524
  }
525
}