File Source: UpdateTableModel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.prefs.UpdateTableModel__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.prefs;
    24  
    25  import com.dmdirc.updater.UpdateChecker;
    26  import com.dmdirc.updater.UpdateComponent;
    27  
    28  import java.util.ArrayList;
    29  import java.util.HashMap;
    30  import java.util.List;
    31  import java.util.Map;
    32  
    33  import javax.swing.table.AbstractTableModel;
    34  
    35  /**
    36   * Update component table model
    37   */
    38  public class UpdateTableModel extends AbstractTableModel {
    39  
    40      /**
    41       * A version number for this class. It should be changed whenever the class
    42       * structure is changed (or anything else that would prevent serialized
    43       * objects being unserialized with the new class).
    44       */
    45      private static final long serialVersionUID = 3;
    46      /** Update component list. */
    47      private final List<UpdateComponent> updates;
    48      /** Enabled list. */
    49      private Map<UpdateComponent, Boolean> enabled;
    50  
    51      /**
    52       * Instantiates a new table model.
    53       */
    54      public UpdateTableModel() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.UpdateTableModel()
                  * 
                  *  Postconditions:
                  *    this.enabled == &amp;new HashMap(UpdateTableModel#2)
                  *    this.updates == &amp;new ArrayList(UpdateTableModel#1)
                  *    new ArrayList(UpdateTableModel#1) num objects == 1
                  *    new HashMap(UpdateTableModel#2) num objects == 1
                  */
    55          this(new ArrayList<UpdateComponent>());
    56      }
    57  
    58      /**
    59       * Instantiates a new table model.
    60       * 
    61       * @param updates Update components to show
    62       */
             /* 
    P/P       *  Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.UpdateTableModel(List)
              * 
              *  Postconditions:
              *    this.enabled == &amp;new HashMap(UpdateTableModel#2)
              *    this.updates == &amp;new ArrayList(UpdateTableModel#1)
              *    new ArrayList(UpdateTableModel#1) num objects == 1
              *    new HashMap(UpdateTableModel#2) num objects == 1
              * 
              *  Test Vectors:
              *    java.util.Iterator:hasNext(...)@67: {0}, {1}
              */
    63      public UpdateTableModel(final List<UpdateComponent> updates) {
    64          this.updates = new ArrayList<UpdateComponent>(updates);
    65          this.enabled = new HashMap<UpdateComponent, Boolean>();
    66  
    67          for (UpdateComponent update : this.updates) {
    68              enabled.put(update, UpdateChecker.isEnabled(update));
    69          }
    70      }
    71  
    72      /** {@inheritDoc} */
    73      @Override
    74      public int getRowCount() {
                 /* 
    P/P           *  Method: int getRowCount()
                  * 
                  *  Preconditions:
                  *    this.updates != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    75          synchronized (updates) {
    76              return updates.size();
    77          }
    78      }
    79  
    80      /** {@inheritDoc} */
    81      @Override
    82      public int getColumnCount() {
                 /* 
    P/P           *  Method: int getColumnCount()
                  * 
                  *  Postconditions:
                  *    return_value == 3
                  */
    83          return 3;
    84      }
    85  
    86      /** {@inheritDoc} */
    87      @Override
    88      public String getColumnName(final int columnIndex) {
                 /* 
    P/P           *  Method: String getColumnName(int)
                  * 
                  *  Preconditions:
                  *    columnIndex in {0..2}
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&amp;"Update Component",&amp;"Enabled?",&amp;"Version"}
                  * 
                  *  Test Vectors:
                  *    columnIndex: {0}, {1}, {2}
                  */
    89          switch (columnIndex) {
    90              case 0:
    91                  return "Update Component";
    92              case 1:
    93                  return "Enabled?";
    94              case 2:
    95                  return "Version";
    96              default:
    97                  throw new IllegalArgumentException("Unknown column: " +
    98                          columnIndex);
    99          }
   100      }
   101  
   102      /** {@inheritDoc} */
   103      @Override
   104      public Class<?> getColumnClass(final int columnIndex) {
                 /* 
    P/P           *  Method: Class getColumnClass(int)
                  * 
                  *  Preconditions:
                  *    columnIndex in {0..2}
                  * 
                  *  Test Vectors:
                  *    columnIndex: {0}, {1}, {2}
                  */
   105          switch (columnIndex) {
   106              case 0:
   107                  return String.class;
   108              case 1:
   109                  return Boolean.class;
   110              case 2:
   111                  return Integer.class;
   112              default:
   113                  throw new IllegalArgumentException("Unknown column: " +
   114                          columnIndex);
   115          }
   116      }
   117  
   118      /** {@inheritDoc} */
   119      @Override
   120      public boolean isCellEditable(final int rowIndex, final int columnIndex) {
                 /* 
    P/P           *  Method: bool isCellEditable(int, int)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   121          return columnIndex == 1;
   122      }
   123  
   124      /** {@inheritDoc} */
   125      @Override
   126      public Object getValueAt(int rowIndex, int columnIndex) {
                 /* 
    P/P           *  Method: Object getValueAt(int, int)
                  * 
                  *  Preconditions:
                  *    columnIndex in {0..2}
                  *    rowIndex in {0..232-2}
                  *    this.updates != null
                  *    (soft) this.enabled != null
                  * 
                  *  Presumptions:
                  *    java.util.List:get(...)@137 != null
                  *    java.util.List:get(...)@141 != null
                  *    java.util.List:size(...)@128 >= 1
                  *    java.util.List:size(...)@128 - rowIndex in {1..232-1}
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    columnIndex: {0}, {1}, {2}
                  */
   127          synchronized (updates) {
   128              if (updates.size() <= rowIndex) {
   129                  throw new IndexOutOfBoundsException(rowIndex + " >= " +
   130                          updates.size());
   131              }
   132              if (rowIndex < 0) {
   133                  throw new IllegalArgumentException("Must specify a positive integer");
   134              }
   135              switch (columnIndex) {
   136                  case 0:
   137                      return updates.get(rowIndex).getFriendlyName();
   138                  case 1:
   139                      return enabled.get(updates.get(rowIndex));
   140                  case 2:
   141                      return updates.get(rowIndex).getFriendlyVersion();
   142                  default:
   143                      throw new IllegalArgumentException("Unknown column: " +
   144                              columnIndex);
   145              }
   146          }
   147      }
   148  
   149      /** {@inheritDoc} */
   150      @Override
   151      public void setValueAt(final Object aValue, final int rowIndex,
   152              final int columnIndex) {
                 /* 
    P/P           *  Method: void setValueAt(Object, int, int)
                  * 
                  *  Preconditions:
                  *    columnIndex == 1
                  *    rowIndex in {0..232-2}
                  *    this.enabled != null
                  *    this.updates != null
                  * 
                  *  Presumptions:
                  *    java.util.List:size(...)@154 >= 1
                  *    java.util.List:size(...)@154 - rowIndex in {1..232-1}
                  */
   153          synchronized (updates) {
   154              if (updates.size() <= rowIndex) {
   155                  throw new IndexOutOfBoundsException(rowIndex + " >= " +
   156                          updates.size());
   157              }
   158              if (rowIndex < 0) {
   159                  throw new IllegalArgumentException("Must specify a positive integer");
   160              }
   161              switch (columnIndex) {
   162                  case 1:
   163                      enabled.put(updates.get(rowIndex), (Boolean) aValue);
   164                      break;
   165                  default:
   166                      throw new IllegalArgumentException("Unknown column: " +
   167                              columnIndex);
   168              }
   169              fireTableCellUpdated(rowIndex, columnIndex);
   170          }
   171      }
   172  
   173      /**
   174       * Adds a update component to the model.
   175       * 
   176       * @param component update component to add
   177       */
   178      public void add(final UpdateComponent component) {
                 /* 
    P/P           *  Method: void add(UpdateComponent)
                  * 
                  *  Preconditions:
                  *    this.updates != null
                  * 
                  *  Presumptions:
                  *    java.util.List:size(...)@181 >= -231+1
                  */
   179          synchronized (updates) {
   180              updates.add(component);
   181              fireTableRowsInserted(updates.size() - 1, updates.size() - 1);
   182          }
   183      }
   184  
   185      /**
   186       * Removes a update component to the model.
   187       * 
   188       * @param component update component to remove
   189       */
   190      public void remove(final UpdateComponent component) {
                 /* 
    P/P           *  Method: void remove(UpdateComponent)
                  * 
                  *  Preconditions:
                  *    this.updates != null
                  */
   191          synchronized (updates) {
   192              remove(updates.indexOf(component));
   193          }
   194      }
   195  
   196      /**
   197       * Removes a update component to the model.
   198       * 
   199       * @param index Index of the update component to remove
   200       */
   201      public void remove(final int index) {
                 /* 
    P/P           *  Method: void remove(int)
                  * 
                  *  Preconditions:
                  *    (soft) this.updates != null
                  */
   202          synchronized (updates) {
   203              if (index != -1) {
   204                  updates.remove(index);
   205                  fireTableRowsDeleted(index, index);
   206              }
   207          }
   208      }
   209  }








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