File Source: NicklistListModel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.NicklistListModel__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.components;
    24  
    25  import com.dmdirc.config.IdentityManager;
    26  import com.dmdirc.interfaces.ConfigChangeListener;
    27  import com.dmdirc.parser.irc.ChannelClientInfo;
    28  
    29  import java.util.ArrayList;
    30  import java.util.Collections;
    31  import java.util.List;
    32  
    33  import javax.swing.AbstractListModel;
    34  
    35  /** Stores and provides means to modify nicklist data for a channel. */
         /* 
    P/P   *  Method: Object getElementAt(int)
          * 
          *  Preconditions:
          *    this.nicknames != null
          * 
          *  Postconditions:
          *    init'ed(return_value)
          */
    36  public final class NicklistListModel extends AbstractListModel implements ConfigChangeListener {
    37  
    38      /**
    39       * A version number for this class. It should be changed whenever the class
    40       * structure is changed (or anything else that would prevent serialized
    41       * objects being unserialized with the new class).
    42       */
    43      private static final long serialVersionUID = 1;
    44      /** stores the nicknames to be shown in this list. */
    45      private final List<ChannelClientInfo> nicknames;
    46      /** Sort by mode? */
    47      private boolean sortByMode;
    48      /** Sort by case? */
    49      private boolean sortByCase;
    50  
    51      /** Creates a new empty model. */
    52      public NicklistListModel() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.NicklistListModel()
                  * 
                  *  Presumptions:
                  *    java.util.Collections:synchronizedList(...)@67 != null
                  * 
                  *  Postconditions:
                  *    this.nicknames != null
                  *    init'ed(this.sortByCase)
                  *    init'ed(this.sortByMode)
                  */
    53          this(Collections.synchronizedList(new ArrayList<ChannelClientInfo>()));
    54      }
    55  
    56      /**
    57       * Creates a new model and initiliases it with the data provided.
    58       * @param newNicknames list of nicknames used for initialisation
    59       */
    60      public NicklistListModel(final List<ChannelClientInfo> newNicknames) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.NicklistListModel(List)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@63 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@65 != null
                  *    java.util.Collections:synchronizedList(...)@67 != null
                  * 
                  *  Postconditions:
                  *    this.nicknames != null
                  *    init'ed(this.sortByCase)
                  *    init'ed(this.sortByMode)
                  */
    61          super();
    62          
    63          sortByMode = IdentityManager.getGlobalConfig().
    64                  getOptionBool("nicklist", "sortByMode");
    65          sortByCase = IdentityManager.getGlobalConfig().
    66                  getOptionBool("nicklist", "sortByCase");
    67          nicknames = Collections.synchronizedList(newNicknames);
    68          
    69          sort();
    70      }
    71  
    72      /**
    73       * Returns the size of the current nicklist.
    74       * @return nicklist size
    75       */
    76      @Override
    77      public int getSize() {
                 /* 
    P/P           *  Method: int getSize()
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    78          return nicknames.size();
    79      }
    80  
    81      /**
    82       * Returns the element at the specified place in the nicklist.
    83       * 
    84       * @param index index of nick required
    85       * 
    86       * @return nicklist entry requested
    87       */
    88      @Override
    89      public ChannelClientInfo getElementAt(final int index) {
                 /* 
    P/P           *  Method: ChannelClientInfo getElementAt(int)
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    90          return nicknames.get(index);
    91      }
    92  
    93      /**
    94       * Sorts the nicklist based on settings in the Config.
    95       */
    96      public void sort() {
                 /* 
    P/P           *  Method: void sort()
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  *    init'ed(this.sortByCase)
                  *    init'ed(this.sortByMode)
                  */
    97          synchronized (nicknames) {
    98              Collections.sort(nicknames,
    99                      new NicklistComparator(sortByMode, sortByCase));
   100          }
   101          rerender();
   102      }
   103  
   104      /**
   105       * Replaces the entire nicklist with the arraylist specified.
   106       * 
   107       * @param clients replacement nicklist
   108       * 
   109       * @return boolean success
   110       */
   111      public boolean replace(final List<ChannelClientInfo> clients) {
                 /* 
    P/P           *  Method: bool replace(List)
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  *    init'ed(this.sortByCase)
                  *    init'ed(this.sortByMode)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   112          boolean returnValue = false;
   113  
   114          nicknames.clear();
   115          returnValue = nicknames.addAll(clients);
   116          sort();
   117  
   118          return returnValue;
   119      }
   120  
   121      /**
   122       * Adds the specified client to the nicklist.
   123       * 
   124       * @param client client to add to the nicklist
   125       * 
   126       * @return boolean success
   127       */
   128      public boolean add(final ChannelClientInfo client) {
                 /* 
    P/P           *  Method: bool add(ChannelClientInfo)
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  *    init'ed(this.sortByCase)
                  *    init'ed(this.sortByMode)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   129          boolean returnValue = false;
   130  
   131          returnValue = nicknames.add(client);
   132          sort();
   133  
   134          return returnValue;
   135      }
   136  
   137      /**
   138       * Removes the specified client from the nicklist.
   139       * 
   140       * @param client client to remove
   141       * 
   142       * @return boolean success
   143       */
   144      public boolean remove(final ChannelClientInfo client) {
   145          boolean returnValue;
   146          
                 /* 
    P/P           *  Method: bool remove(ChannelClientInfo)
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   147          returnValue = nicknames.remove(client);
   148          rerender();
   149          
   150          return returnValue;
   151      }
   152  
   153      /**
   154       * Removes the specified index from the nicklist.
   155       * 
   156       * @param index index to remove
   157       * 
   158       * @return ChannelClientInfo client removed
   159       */
   160      public ChannelClientInfo remove(final int index) {
   161          ChannelClientInfo returnValue;
   162          
                 /* 
    P/P           *  Method: ChannelClientInfo remove(int)
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   163          returnValue = nicknames.remove(index);
   164          rerender();
   165          
   166          return returnValue;
   167      }
   168  
   169      /** 
   170       * Fires the model changed event forcing the model to re-render. 
   171       */
   172      public void rerender() {
                 /* 
    P/P           *  Method: void rerender()
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  */
   173          fireContentsChanged(this, 0, nicknames.size());
   174      }
   175  
   176      /** {@inheritDoc} */
   177      @Override
   178      public void configChanged(String domain, String key) {
                 /* 
    P/P           *  Method: void configChanged(String, String)
                  * 
                  *  Preconditions:
                  *    this.nicknames != null
                  *    (soft) init'ed(this.sortByCase)
                  *    (soft) init'ed(this.sortByMode)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@180 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@183 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.sortByCase)
                  *    init'ed(this.sortByMode)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@179: {0}, {1}
                  *    java.lang.String:equals(...)@182: {0}, {1}
                  */
   179          if ("sortByMode".equals(key)) {
   180              sortByMode = IdentityManager.getGlobalConfig().
   181                      getOptionBool("nicklist", "sortByMode");
   182          } else if ("sortByCase".equals(key)) {
   183              sortByCase = IdentityManager.getGlobalConfig().
   184                      getOptionBool("nicklist", "sortByCase");
   185          }
   186          
   187          sort();
   188      }
   189  }








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