File Source: ActionTypeModel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__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.actioneditor;
    24  
    25  import com.dmdirc.actions.ActionTypeComparator;
    26  import com.dmdirc.actions.interfaces.ActionType;
    27  import com.dmdirc.util.MapList;
    28  
    29  import java.awt.FontMetrics;
    30  import java.util.Collections;
    31  import java.util.List;
    32  import java.util.Map;
    33  
    34  import javax.swing.DefaultComboBoxModel;
    35  import javax.swing.SwingUtilities;
    36  
    37  /**
    38   * Model for the "trigger" list of the actions editor. Adds type group headers,
    39   * and ensures they can't be selected.
    40   */
    41  public final class ActionTypeModel extends DefaultComboBoxModel {
    42      
    43      /**
    44       * A version number for this class. It should be changed whenever the class
    45       * structure is changed (or anything else that would prevent serialized
    46       * objects being unserialized with the new class).
    47       */
    48      private static final long serialVersionUID = 1;    
    49      /** Max Width. */
    50      private int maxWidth = -1;
    51      /** Font metrics. */
    52      private FontMetrics fm;
    53      
    54      /**
    55       * Creates a new instance of ActionTypeModel.
    56       * 
    57       * @param fm Font metrics
    58       * @param typeGroups The action type groups to use
    59       */
    60      public ActionTypeModel(final FontMetrics fm, final MapList<String, ActionType> typeGroups) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)
                  * 
                  *  Preconditions:
                  *    typeGroups != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.util.MapList:entrySet(...)@65 != null
                  *    java.util.Iterator:next(...)@65 != null
                  *    java.util.Map_Entry:getValue(...)@68 != null
                  * 
                  *  Postconditions:
                  *    this.fm == fm
                  *    init'ed(this.fm)
                  *    init'ed(this.maxWidth)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@65: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@71: {0}, {1}
                  */
    61          super();
    62          
    63          this.fm = fm;
    64          
    65          for (Map.Entry<String, List<ActionType>> entry : typeGroups.entrySet()) {
    66              addElement(entry.getKey());
    67              
    68              final List<ActionType> types = entry.getValue();
    69              Collections.sort(types, new ActionTypeComparator());
    70              
    71              for (ActionType type : types) {
    72                  addElement(type);
    73              }
    74          }
    75      }
    76  
    77      /** 
    78       * {@inheritDoc}
    79       * 
    80       * @param anObject Objerct to add
    81       */
    82      @Override
    83      public void addElement(final Object anObject) {
                 /* 
    P/P           *  Method: void addElement(Object)
                  * 
                  *  Preconditions:
                  *    init'ed(this.maxWidth)
                  *    (soft) init'ed(this.fm)
                  * 
                  *  Postconditions:
                  *    init'ed(this.maxWidth)
                  */
    84          super.addElement(anObject);
    85          int width = maxWidth;
    86          if (anObject instanceof String) {
    87              width = SwingUtilities.computeStringWidth(fm, (String) anObject);
    88          } else if (anObject instanceof ActionType) {
    89              width = SwingUtilities.computeStringWidth(fm, ((ActionType) anObject).getName());
    90          }
    91          maxWidth = Math.max(width, maxWidth);
    92      }
    93  
    94      /** {@inheritDoc} */
    95      @Override
    96      public void setSelectedItem(final Object anObject) {
                 /* 
    P/P           *  Method: void setSelectedItem(Object)
                  */
    97          if (!(anObject instanceof String)) {
    98              super.setSelectedItem(anObject);
    99          }
   100      }
   101      
   102      /**
   103       * Sets the type group for this model.
   104       * 
   105       * @param typeGroup New type group
   106       */
   107      public void setTypeGroup(final MapList<String, ActionType> typeGroup) {
                 /* 
    P/P           *  Method: void setTypeGroup(MapList)
                  * 
                  *  Preconditions:
                  *    typeGroup != null
                  *    (soft) init'ed(this.maxWidth)
                  *    (soft) init'ed(this.fm)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.util.MapList:entrySet(...)@110 != null
                  *    java.util.Iterator:next(...)@110 != null
                  *    java.util.Map_Entry:getValue(...)@113 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.maxWidth)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@110: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@116: {0}, {1}
                  */
   108          removeAllElements();
   109          
   110          for (Map.Entry<String, List<ActionType>> entry : typeGroup.entrySet()) {
   111              addElement(entry.getKey());
   112              
   113              final List<ActionType> types = entry.getValue();
   114              Collections.sort(types, new ActionTypeComparator());
   115              
   116              for (ActionType type : types) {
   117                  addElement(type);
   118              }
   119          }
   120      }
   121      
   122      /**
   123       * Returns the maximum width of a string in this model.
   124       * 
   125       * @return String max width
   126       */
   127      public int getMaxWidth() {
                 /* 
    P/P           *  Method: int getMaxWidth()
                  * 
                  *  Preconditions:
                  *    init'ed(this.maxWidth)
                  * 
                  *  Postconditions:
                  *    return_value == this.maxWidth
                  *    init'ed(return_value)
                  */
   128          return maxWidth;
   129      }
   130  }








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