File Source: CurrentOptionsPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel__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.expandingsettings;
    24  
    25  import com.dmdirc.ui.IconManager;
    26  import com.dmdirc.addons.ui_swing.components.ColourChooser;
    27  import com.dmdirc.addons.ui_swing.components.ImageButton;
    28  import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel.OptionType;
    29  import com.dmdirc.addons.ui_swing.UIUtilities;
    30  
    31  import java.awt.event.ActionEvent;
    32  import java.awt.event.ActionListener;
    33  import java.util.HashMap;
    34  import java.util.Map;
    35  import java.util.Map.Entry;
    36  import javax.swing.JCheckBox;
    37  import javax.swing.JComponent;
    38  import javax.swing.JLabel;
    39  import javax.swing.JPanel;
    40  import javax.swing.JSpinner;
    41  import javax.swing.JTextField;
    42  import javax.swing.SpinnerNumberModel;
    43  
    44  import net.miginfocom.swing.MigLayout;
    45  
    46  /**
    47   * Current options panel.
    48   */
    49  public final class CurrentOptionsPanel extends JPanel implements ActionListener {
    50      
    51      /**
    52       * A version number for this class. It should be changed whenever the class
    53       * structure is changed (or anything else that would prevent serialized
    54       * objects being unserialized with the new class).
    55       */
    56      private static final long serialVersionUID = 2;
    57      
    58      /** Parent settings panel. */
    59      private final SettingsPanel parent;
    60      
    61      /** config option -> text fields. */
    62      private Map<String, JTextField> textFields;
    63      /** config option -> checkboxes. */
    64      private Map<String, JCheckBox> checkBoxes;
    65      /** config option -> colours. */
    66      private Map<String, ColourChooser> colours;
    67      /** config option -> spinners. */
    68      private Map<String, JSpinner> spinners;
    69      
    70      /**
    71       * Creates a new instance of CurrentOptionsPanel.
    72       *
    73       * @param parent Parent settings panel.
    74       */
    75      protected CurrentOptionsPanel(final SettingsPanel parent) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel(SettingsPanel)
                  * 
                  *  Postconditions:
                  *    this.checkBoxes == &amp;new HashMap(initComponents#2)
                  *    this.colours == &amp;new HashMap(initComponents#3)
                  *    this.parent == parent
                  *    init'ed(this.parent)
                  *    this.spinners == &amp;new HashMap(initComponents#4)
                  *    this.textFields == &amp;new HashMap(initComponents#1)
                  *    new HashMap(initComponents#1) num objects == 1
                  *    new HashMap(initComponents#2) num objects == 1
                  *    new HashMap(initComponents#3) num objects == 1
                  *    new HashMap(initComponents#4) num objects == 1
                  */
    76          super();
    77          
    78          this.parent = parent;
    79          
    80          this.setOpaque(UIUtilities.getTabbedPaneOpaque());
    81          initComponents();
    82      }
    83      
    84      /** Initialises the components. */
    85      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Postconditions:
                  *    this.checkBoxes == &amp;new HashMap(initComponents#2)
                  *    this.colours == &amp;new HashMap(initComponents#3)
                  *    this.spinners == &amp;new HashMap(initComponents#4)
                  *    this.textFields == &amp;new HashMap(initComponents#1)
                  *    new HashMap(initComponents#1) num objects == 1
                  *    new HashMap(initComponents#2) num objects == 1
                  *    new HashMap(initComponents#3) num objects == 1
                  *    new HashMap(initComponents#4) num objects == 1
                  */
    86          textFields = new HashMap<String, JTextField>();
    87          checkBoxes = new HashMap<String, JCheckBox>();
    88          colours = new HashMap<String, ColourChooser>();
    89          spinners = new HashMap<String, JSpinner>();
    90      }
    91      
    92      /** Clears all the current options. */
    93      protected void clearOptions() {
                 /* 
    P/P           *  Method: void clearOptions()
                  * 
                  *  Preconditions:
                  *    this.checkBoxes != null
                  *    this.colours != null
                  *    this.spinners != null
                  *    this.textFields != null
                  *    (soft) this.parent != null
                  *    (soft) this.parent.names != null
                  */
    94          textFields.clear();
    95          checkBoxes.clear();
    96          colours.clear();
    97          spinners.clear();
    98          populateCurrentSettings();
    99      }
   100      
   101      /**
   102       * Adds a current option.
   103       *
   104       * @param optionName option to add
   105       * @param type Option type
   106       * @param value Option value
   107       */
   108      protected void addOption(final String optionName,
   109              final OptionType type, final String value) {
                 /* 
    P/P           *  Method: void addOption(String, SettingsPanel$OptionType, String)
                  * 
                  *  Preconditions:
                  *    this.checkBoxes != null
                  *    this.colours != null
                  *    this.spinners != null
                  *    this.textFields != null
                  *    type != null
                  *    (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
                  *    (soft) this.parent != null
                  *    (soft) this.parent.names != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@110 in {0..3}
                  *    java.util.Map:get(...)@123 != null
                  *    values(...).length - com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@110 in range
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...]: {1}, {2}, {3}, {4}
                  */
   110          switch (type) {
   111              case TEXTFIELD:
   112                  textFields.put(optionName, new JTextField(value));
   113                  break;
   114              case CHECKBOX:
   115                  checkBoxes.put(optionName, new JCheckBox("",
   116                          Boolean.parseBoolean(value)));
   117                  break;
   118              case COLOUR:
   119                  colours.put(optionName, new ColourChooser(value, true, true));
   120                  break;
   121              case SPINNER:
   122                  spinners.put(optionName, new JSpinner(new SpinnerNumberModel()));
   123                  spinners.get(optionName).setValue(Integer.parseInt(value));
   124                  break;
   125              default:
   126                  throw new IllegalArgumentException("Illegal Type: " + type);
   127          }
   128          
   129          populateCurrentSettings();
   130      }
   131      
   132      /**
   133       * Deletes a current option.
   134       *
   135       * @param optionName Option to delete
   136       * @param type Option type
   137       */
   138      protected void delOption(final String optionName,
   139              final OptionType type) {
                 /* 
    P/P           *  Method: void delOption(String, SettingsPanel$OptionType)
                  * 
                  *  Preconditions:
                  *    this.checkBoxes != null
                  *    this.colours != null
                  *    this.spinners != null
                  *    this.textFields != null
                  *    type != null
                  *    (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
                  *    (soft) this.parent != null
                  *    (soft) this.parent.names != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@140 in {0..3}
                  *    values(...).length - com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@140 in range
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...]: {1}, {2}, {3}, {4}
                  */
   140          switch (type) {
   141              case TEXTFIELD:
   142                  textFields.remove(optionName);
   143                  break;
   144              case CHECKBOX:
   145                  checkBoxes.remove(optionName);
   146                  break;
   147              case COLOUR:
   148                  colours.remove(optionName);
   149                  break;
   150              case SPINNER:
   151                  spinners.remove(optionName);
   152                  break;
   153              default:
   154                  throw new IllegalArgumentException("Illegal Type: " + type);
   155          }
   156          
   157          populateCurrentSettings();
   158      }
   159      
   160      /**
   161       * Retrives an options value.
   162       *
   163       * @param optionName Option to delete
   164       * @param type Option type
   165       *
   166       * @return Option value or a blank string
   167       */
   168      public String getOption(final String optionName, final OptionType type) {
                 /* 
    P/P           *  Method: String getOption(String, SettingsPanel$OptionType)
                  * 
                  *  Preconditions:
                  *    type != null
                  *    (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
                  *    (soft) this.checkBoxes != null
                  *    (soft) this.colours != null
                  *    (soft) this.spinners != null
                  *    (soft) this.textFields != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@170 in {0..3}
                  *    java.util.Map:get(...)@173 != null
                  *    java.util.Map:get(...)@178 != null
                  *    java.util.Map:get(...)@187 != null
                  *    java.util.Map:get(...)@192 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    java.lang.Object:toString(...)._tainted == 0
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...]: {1}, {2}, {3}, {4}
                  *    java.util.Map:containsKey(...)@172: {0}, {1}
                  *    java.util.Map:containsKey(...)@177: {0}, {1}
                  *    java.util.Map:containsKey(...)@186: {0}, {1}
                  *    java.util.Map:containsKey(...)@191: {0}, {1}
                  *    javax.swing.JCheckBox:isSelected(...)@178: {0}, {1}
                  */
   169          String returnValue = null;
   170          switch (type) {
   171              case TEXTFIELD:
   172                  if (textFields.containsKey(optionName)) {
   173                      returnValue =  textFields.get(optionName).getText();
   174                  }
   175                  break;
   176              case CHECKBOX:
   177                  if (checkBoxes.containsKey(optionName)) {
   178                      if (checkBoxes.get(optionName).isSelected()) {
   179                          returnValue = "true";
   180                      } else {
   181                          returnValue = "false";
   182                      }
   183                  }
   184                  break;
   185              case COLOUR:
   186                  if (colours.containsKey(optionName)) {
   187                      returnValue = colours.get(optionName).getColour();
   188                  }
   189                  break;
   190              case SPINNER:
   191                  if (spinners.containsKey(optionName)) {
   192                      returnValue = spinners.get(optionName).getValue().toString();
   193                  }
   194                  break;
   195              default:
   196                  throw new IllegalArgumentException("Illegal Type: " + type);
   197          }
   198          return returnValue;
   199      }
   200      
   201      /**
   202       * Adds an option to the current options pane.
   203       * @param configName config option name
   204       * @param displayName config option display name
   205       * @param panel parent panel
   206       * @param component Option component to add
   207       */
   208      private void addCurrentOption(final String configName, final String displayName,
   209              final JPanel panel, final JComponent component) {
                 /* 
    P/P           *  Method: void addCurrentOption(String, String, JPanel, JComponent)
                  * 
                  *  Preconditions:
                  *    panel != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.ui.IconManager:getIconManager(...)@211 != null
                  */
   210          final JLabel label = new JLabel();
   211          final ImageButton button = new ImageButton(configName,
   212                  IconManager.getIconManager().getIcon("close-inactive"),
   213                  IconManager.getIconManager().getIcon("close-active"));
   214          
   215          label.setText(displayName + ": ");
   216          label.setLabelFor(component);
   217          
   218          button.addActionListener(this);
   219          
   220          panel.add(label);
   221          panel.add(component, "growx, pushx");
   222          panel.add(button, "wrap");
   223      }
   224      
   225      
   226      /** Populates the current settings. */
   227      protected void populateCurrentSettings() {
                 /* 
    P/P           *  Method: void populateCurrentSettings()
                  * 
                  *  Preconditions:
                  *    this.checkBoxes != null
                  *    this.colours != null
                  *    this.spinners != null
                  *    this.textFields != null
                  *    (soft) this.parent != null
                  *    (soft) this.parent.names != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@234 != null
                  *    java.util.Iterator:next(...)@240 != null
                  *    java.util.Iterator:next(...)@246 != null
                  *    java.util.Iterator:next(...)@252 != null
                  *    java.util.Map:entrySet(...)@234 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@234: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@240: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@246: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@252: {0}, {1}
                  */
   228          setVisible(false);
   229          
   230          setLayout(new MigLayout("fillx, aligny top"));
   231          
   232          removeAll();
   233          
   234          for (Entry<String, JTextField> entry : textFields.entrySet()) {
   235              addCurrentOption(entry.getKey(),
   236                      parent.getOptionName(entry.getKey()),
   237                      this, entry.getValue());
   238          }
   239          
   240          for (Entry<String, JCheckBox> entry : checkBoxes.entrySet()) {
   241              addCurrentOption(entry.getKey(),
   242                      parent.getOptionName(entry.getKey()),
   243                      this, entry.getValue());
   244          }
   245          
   246          for (Entry<String, ColourChooser> entry : colours.entrySet()) {
   247              addCurrentOption(entry.getKey(),
   248                      parent.getOptionName(entry.getKey()),
   249                      this, entry.getValue());
   250          }
   251          
   252          for (Entry<String, JSpinner> entry : spinners.entrySet()) {
   253              addCurrentOption(entry.getKey(),
   254                      parent.getOptionName(entry.getKey()),
   255                      this, entry.getValue());
   256          }
   257          
   258          setVisible(true);
   259      }
   260      
   261      /** 
   262       * {@inheritDoc}
   263       * 
   264       * @param e Action performed
   265       */
   266      @Override
   267      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    this.checkBoxes != null
                  *    this.colours != null
                  *    this.parent != null
                  *    this.parent.addOptionPanel != null
                  *    this.parent.addOptionPanel.addOptionButton != null
                  *    this.parent.addOptionPanel.addOptionComboBox != null
                  *    this.parent.types != null
                  *    this.spinners != null
                  *    this.textFields != null
                  *    ...
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@242 != null
                  */
   268          delOption(e.getActionCommand(), parent.getOptionType(e.getActionCommand()));
   269          parent.addAddableOption(e.getActionCommand());
   270      }
   271      
   272  }








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