File Source: SwingUpdaterDialog.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.addons.ui_swing.dialogs.updater;
    24  
    25  import com.dmdirc.addons.ui_swing.MainFrame;
    26  import com.dmdirc.updater.UpdateCheckerListener;
    27  import com.dmdirc.ui.interfaces.UpdaterDialog;
    28  import com.dmdirc.addons.ui_swing.components.text.TextLabel;
    29  import com.dmdirc.addons.ui_swing.components.PackingTable;
    30  import com.dmdirc.addons.ui_swing.components.StandardDialog;
    31  import com.dmdirc.addons.ui_swing.components.renderers.UpdateComponentTableCellRenderer;
    32  import com.dmdirc.addons.ui_swing.components.renderers.UpdateStatusTableCellRenderer;
    33  import com.dmdirc.updater.Update;
    34  import com.dmdirc.updater.UpdateChecker;
    35  import com.dmdirc.updater.UpdateChecker.STATE;
    36  
    37  import java.awt.Dimension;
    38  import java.awt.event.ActionEvent;
    39  import java.awt.event.ActionListener;
    40  import java.util.List;
    41  
    42  import javax.swing.JButton;
    43  import javax.swing.JScrollPane;
    44  import javax.swing.JTable;
    45  import javax.swing.ListSelectionModel;
    46  import javax.swing.WindowConstants;
    47  import javax.swing.table.TableCellRenderer;
    48  
    49  import net.miginfocom.swing.MigLayout;
    50  
    51  /**
    52   * The updater dialog informs the user of the new update that is available,
    53   * and walks them through the process of downloading the update.
    54   */
         /* 
    P/P   *  Method: UpdateStatusTableCellRenderer access$100(SwingUpdaterDialog)
          * 
          *  Preconditions:
          *    x0 != null
          *    init'ed(x0.updateStatusRenderer)
          * 
          *  Postconditions:
          *    return_value == x0.updateStatusRenderer
          *    init'ed(return_value)
          */
    55  public final class SwingUpdaterDialog extends StandardDialog implements
    56          ActionListener, UpdaterDialog, UpdateCheckerListener {
    57  
    58      /**
    59       * A version number for this class. It should be changed whenever the class
    60       * structure is changed (or anything else that would prevent serialized
    61       * objects being unserialized with the new class).
    62       */
    63      private static final long serialVersionUID = 3;
    64      /** Previously created instance of SwingUpdaterDialog. */
    65      private static volatile SwingUpdaterDialog me;
    66      /** Update table. */
    67      private JTable table;
    68      /** Table scrollpane. */
    69      private JScrollPane scrollPane;
    70      /** The label we use for the dialog header. */
    71      private TextLabel header;
    72      /** UpdateComponent renderer. */
    73      private UpdateComponentTableCellRenderer updateComponentRenderer;
    74      /** Update.Status renderer. */
    75      private UpdateStatusTableCellRenderer updateStatusRenderer;
    76      /** Swing controller. */
    77      private MainFrame mainFrame;
    78  
    79      /**
    80       * Creates a new instance of the updater dialog.
    81       * 
    82       * @param updates A list of updates that are available.
    83       * @param mainFrame Main frame
    84       */
    85      private SwingUpdaterDialog(final List<Update> updates, final MainFrame mainFrame) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog(List, MainFrame)
                  * 
                  *  Preconditions:
                  *    updates != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getCancelButton(...)@96 != null
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getOkButton(...)@95 != null
                  *    init'ed(java.awt.Dialog$ModalityType.MODELESS)
                  * 
                  *  Postconditions:
                  *    this.header == &amp;new TextLabel(initComponents#3)
                  *    this.mainFrame == mainFrame
                  *    init'ed(this.mainFrame)
                  *    this.scrollPane == &amp;new JScrollPane(initComponents#4)
                  *    this.table == &amp;new SwingUpdaterDialog$1(initComponents#5)
                  *    this.updateComponentRenderer == &amp;new UpdateComponentTableCellRenderer(initComponents#2)
                  *    this.updateStatusRenderer == &amp;new UpdateStatusTableCellRenderer(initComponents#1)
                  *    new JScrollPane(initComponents#4) num objects == 1
                  *    new SwingUpdaterDialog$1(initComponents#5) num objects == 1
                  *    new TextLabel(initComponents#3) num objects == 1
                  *    ...
                  */
    86          super(mainFrame, ModalityType.MODELESS);
    87          
    88          this.mainFrame = mainFrame;
    89  
    90          initComponents(updates);
    91          layoutComponents();
    92  
    93          UpdateChecker.addListener(this);
    94  
    95          getOkButton().addActionListener(this);
    96          getCancelButton().addActionListener(this);
    97  
    98          setTitle("Update available");
    99          setSize(new Dimension(450, 400));
   100      }
   101  
   102      /**
   103       * Creates the dialog if one doesn't exist, and displays it.
   104       * 
   105       * @param updates The updates that are available
   106       * @param mainFrame Main frame
   107       */
   108      public static void showSwingUpdaterDialog(
   109              final List<Update> updates, final MainFrame mainFrame) {
                 /* 
    P/P           *  Method: void showSwingUpdaterDialog(List, MainFrame)
                  * 
                  *  Preconditions:
                  *    init'ed(me)
                  *    updates != null
                  * 
                  *  Postconditions:
                  *    me == One-of{&amp;new SwingUpdaterDialog(getSwingUpdaterDialog#1), old me}
                  *    me != null
                  *    new ArrayList(setUpdates#1) num objects <= 1
                  *    new HashMap(setUpdates#2) num objects == new ArrayList(setUpdates#1) num objects
                  *    new JScrollPane(initComponents#4) num objects <= 1
                  *    new SwingUpdaterDialog$1(initComponents#5) num objects == new JScrollPane(initComponents#4) num objects
                  *    new SwingUpdaterDialog(getSwingUpdaterDialog#1) num objects == new JScrollPane(initComponents#4) num objects
                  *    new TextLabel(initComponents#3) num objects == new JScrollPane(initComponents#4) num objects
                  *    new UpdateComponentTableCellRenderer(initComponents#2) num objects == new JScrollPane(initComponents#4) num objects
                  *    new UpdateStatusTableCellRenderer(initComponents#1) num objects == new JScrollPane(initComponents#4) num objects
                  *    ...
                  */
   110          me = getSwingUpdaterDialog(updates, mainFrame);
   111          me.display();
   112      }
   113  
   114      /**
   115       * Gets the dialog if one doesn't exist.
   116       * 
   117       * @param updates The updates that are available
   118       * @param mainFrame Main frame
   119       * @return SwingUpdaterDialog instance
   120       */
   121      public static SwingUpdaterDialog getSwingUpdaterDialog(
   122              final List<Update> updates, final MainFrame mainFrame) {
                 /* 
    P/P           *  Method: SwingUpdaterDialog getSwingUpdaterDialog(List, MainFrame)
                  * 
                  *  Preconditions:
                  *    init'ed(me)
                  *    updates != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTable:getModel(...)@127 != null
                  *    me.table != null
                  * 
                  *  Postconditions:
                  *    me == One-of{&amp;new SwingUpdaterDialog(getSwingUpdaterDialog#1), old me}
                  *    me != null
                  *    return_value == One-of{&amp;new SwingUpdaterDialog(getSwingUpdaterDialog#1), old me}
                  *    return_value != null
                  *    new ArrayList(setUpdates#1) num objects <= 1
                  *    new HashMap(setUpdates#2) num objects <= 1
                  *    new JScrollPane(initComponents#4) num objects <= 1
                  *    new SwingUpdaterDialog$1(initComponents#5) num objects <= 1
                  *    new SwingUpdaterDialog(getSwingUpdaterDialog#1) num objects <= 1
                  *    init'ed(new SwingUpdaterDialog(getSwingUpdaterDialog#1).header)
                  *    ...
                  * 
                  *  Test Vectors:
                  *    me: Inverse{null}, Addr_Set{null}
                  */
   123          synchronized (SwingUpdaterDialog.class) {
   124              if (me == null) {
   125                  me = new SwingUpdaterDialog(updates, mainFrame);
   126              } else {
   127                  ((UpdateTableModel) me.table.getModel()).setUpdates(updates);
   128              }
   129          }
   130  
   131          return me;
   132      }
   133  
   134      /** 
   135       * Initialises the components.
   136       * 
   137       * @param updates The updates that are available
   138       */
   139      private void initComponents(final List<Update> updates) {
                 /* 
    P/P           *  Method: void initComponents(List)
                  * 
                  *  Preconditions:
                  *    updates != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTable:getRowSorter(...)@174 != null
                  * 
                  *  Postconditions:
                  *    this.header == &amp;new TextLabel(initComponents#3)
                  *    this.scrollPane == &amp;new JScrollPane(initComponents#4)
                  *    this.table == &amp;new SwingUpdaterDialog$1(initComponents#5)
                  *    this.updateComponentRenderer == &amp;new UpdateComponentTableCellRenderer(initComponents#2)
                  *    this.updateStatusRenderer == &amp;new UpdateStatusTableCellRenderer(initComponents#1)
                  *    new JScrollPane(initComponents#4) num objects == 1
                  *    new SwingUpdaterDialog$1(initComponents#5) num objects == 1
                  *    new TextLabel(initComponents#3) num objects == 1
                  *    new UpdateComponentTableCellRenderer(initComponents#2) num objects == 1
                  *    new UpdateStatusTableCellRenderer(initComponents#1) num objects == 1
                  */
   140          setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   141          updateStatusRenderer = new UpdateStatusTableCellRenderer();
   142          updateComponentRenderer = new UpdateComponentTableCellRenderer();
   143  
   144          header = new TextLabel("An update is available for one or more " +
   145                  "components of DMDirc:");
   146  
   147          scrollPane = new JScrollPane();
   148          table = new PackingTable(new UpdateTableModel(updates), false,
                         /* 
    P/P                   *  Method: void com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog$1(SwingUpdaterDialog, TableModel, bool, JScrollPane)
                          */
   149                  scrollPane) {
   150  
   151              private static final long serialVersionUID = 1;
   152  
   153              @Override
   154              public TableCellRenderer getCellRenderer(final int row,
   155                      final int column) {
                         /* 
    P/P                   *  Method: TableCellRenderer getCellRenderer(int, int)
                          * 
                          *  Preconditions:
                          *    (soft) init'ed(this.updateComponentRenderer)
                          *    (soft) init'ed(this.updateStatusRenderer)
                          * 
                          *  Postconditions:
                          *    init'ed(return_value)
                          * 
                          *  Test Vectors:
                          *    column: {1}, {3}, {-231..0, 2, 4..232-1}
                          */
   156                  switch (column) {
   157                      case 1:
   158                          return updateComponentRenderer;
   159                      case 3:
   160                          return updateStatusRenderer;
   161                      default:
   162                          return super.getCellRenderer(row, column);
   163                  }
   164              }
   165          };
   166  
   167          table.setAutoCreateRowSorter(true);
   168          table.setAutoCreateColumnsFromModel(true);
   169          table.setColumnSelectionAllowed(false);
   170          table.setCellSelectionEnabled(false);
   171          table.setFillsViewportHeight(false);
   172          table.setRowSelectionAllowed(true);
   173          table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   174          table.getRowSorter().toggleSortOrder(0);
   175  
   176          scrollPane.setViewportView(table);
   177  
   178          orderButtons(new JButton(), new JButton());
   179      }
   180  
   181      /**
   182       * Lays out the components.
   183       */
   184      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.header)
                  *    init'ed(this.scrollPane)
                  */
   185          setLayout(new MigLayout("fill"));
   186  
   187          add(header, "wrap 1.5*unrel, growx, pushx");
   188          add(scrollPane, "grow, push, wrap");
   189          add(getLeftButton(), "split 2, right");
   190          add(getRightButton(), "right");
   191      }
   192  
   193      /** {@inheritDoc} */
   194      @Override
   195      public void display() {
                 /* 
    P/P           *  Method: void display()
                  * 
                  *  Preconditions:
                  *    init'ed(this.mainFrame)
                  */
   196          setLocationRelativeTo(mainFrame);
   197          setVisible(true);
   198          requestFocusInWindow();
   199      }
   200  
   201      /** 
   202       * {@inheritDoc}
   203       * 
   204       * @param e Action event
   205       */
   206      @Override
   207      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    (soft) init'ed(me)
                  *    (soft) this.header != null
                  *    (soft) init'ed(this.mainFrame)
                  *    (soft) this.table != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getCancelButton(...)@210 != null
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getOkButton(...)@209 != null
                  *    init'ed(com.dmdirc.updater.UpdateChecker$STATE.RESTART_REQUIRED)
                  *    init'ed(com.dmdirc.updater.UpdateChecker$STATE.UPDATING)
                  *    init'ed(java.awt.Dialog$ModalityType.MODELESS)
                  *    ...
                  * 
                  *  Postconditions:
                  *    me == One-of{old me, null}
                  *    init'ed(me)
                  * 
                  *  Test Vectors:
                  *    java.lang.Boolean:booleanValue(...)@226: {1}, {0}
                  *    java.lang.Object:equals(...)@208: {0}, {1}
                  *    java.lang.Object:equals(...)@228: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@214: {0}, {1}
                  */
   208          if (e.getSource().equals(getOkButton())) {
   209              getOkButton().setEnabled(false);
   210              getCancelButton().setVisible(false);
   211  
   212              header.setText("DMDirc is updating the following components:");
   213  
   214              for (Update update : ((UpdateTableModel) table.getModel()).getUpdates()) {
   215                  if (!((UpdateTableModel) table.getModel()).isEnabled(update)) {
   216                      UpdateChecker.removeUpdate(update);
   217                  }
   218              }
   219  
   220              UpdateChecker.applyUpdates();
   221  
   222              if (UpdateChecker.getStatus() != STATE.UPDATING) {
   223                  dispose();
   224                  if (UpdateChecker.getStatus() == STATE.RESTART_REQUIRED) {
   225                      new SwingRestartDialog(mainFrame, ModalityType.MODELESS).setVisible(true);
   226                  }
   227              }
   228          } else if (e.getSource().equals(getCancelButton())) {
   229              dispose();
   230          }
   231      }
   232  
   233      /** {@inheritDoc} */
   234      @Override
   235      public void statusChanged(final STATE newStatus) {
                 /* 
    P/P           *  Method: void statusChanged(UpdateChecker$STATE)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getCancelButton(...)@242 != null
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getCancelButton(...)@244 != null
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getOkButton(...)@237 != null
                  *    com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog:getOkButton(...)@239 != null
                  *    init'ed(com.dmdirc.updater.UpdateChecker$STATE.RESTART_REQUIRED)
                  *    ...
                  */
   236          if (newStatus == STATE.UPDATING) {
   237              getOkButton().setEnabled(false);
   238          } else {
   239              getOkButton().setEnabled(true);
   240          }
   241          if (newStatus == STATE.RESTART_REQUIRED) {
   242              getCancelButton().setVisible(false);
   243          } else {
   244              getCancelButton().setVisible(true);
   245          }
   246      }
   247      
   248      /** {@inheritDoc} */
   249      @Override
   250      public void dispose() {
                 /* 
    P/P           *  Method: void dispose()
                  * 
                  *  Preconditions:
                  *    init'ed(me)
                  * 
                  *  Postconditions:
                  *    me == null
                  * 
                  *  Test Vectors:
                  *    me: Inverse{null}, Addr_Set{null}
                  */
   251          if (me == null) {
   252              return;
   253          }
   254          synchronized (me) {
   255              super.dispose();
   256              me = null;
   257          }
   258      }
   259  }








SofCheck Inspector Build Version : 2.17854
SwingUpdaterDialog.java 2009-Jun-25 01:54:24
SwingUpdaterDialog.class 2009-Sep-02 17:04:17
SwingUpdaterDialog$1.class 2009-Sep-02 17:04:17