File Source: AddOptionPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel__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.addons.ui_swing.components.renderers.AddOptionCellRenderer;
    26  import com.dmdirc.addons.ui_swing.components.ColourChooser;
    27  import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel.OptionType;
    28  import com.dmdirc.addons.ui_swing.UIUtilities;
    29  
    30  import java.awt.Component;
    31  import java.awt.event.ActionEvent;
    32  import java.awt.event.ActionListener;
    33  
    34  import javax.swing.AbstractAction;
    35  import javax.swing.DefaultComboBoxModel;
    36  import javax.swing.JButton;
    37  import javax.swing.JCheckBox;
    38  import javax.swing.JComboBox;
    39  import javax.swing.JLabel;
    40  import javax.swing.JPanel;
    41  import javax.swing.JSpinner;
    42  import javax.swing.JTextField;
    43  import javax.swing.KeyStroke;
    44  import javax.swing.SpinnerNumberModel;
    45  
    46  import net.miginfocom.swing.MigLayout;
    47  
    48  /**
    49   * Add option panel.
    50   */
         /* 
    P/P   *  Method: JButton access$000(AddOptionPanel)
          * 
          *  Preconditions:
          *    x0 != null
          *    init'ed(x0.addOptionButton)
          * 
          *  Postconditions:
          *    return_value == x0.addOptionButton
          *    init'ed(return_value)
          */
    51  public final class AddOptionPanel extends JPanel implements ActionListener {
    52      
    53      /**
    54       * A version number for this class. It should be changed whenever the class
    55       * structure is changed (or anything else that would prevent serialized
    56       * objects being unserialized with the new class).
    57       */
    58      private static final long serialVersionUID = 2;
    59      
    60      /** Parent settings panel. */
    61      private final SettingsPanel parent;
    62      
    63      /** Add options combobox. */
    64      private JComboBox addOptionComboBox;
    65      /** Add option button. */
    66      private JButton addOptionButton;
    67      /** Current add option input. */
    68      private Component addInputCurrent;
    69      /** Add option colour chooser. */
    70      private ColourChooser addInputColourChooser;
    71      /** Add option textfield. */
    72      private JTextField addInputText;
    73      /** Add option checkbox. */
    74      private JCheckBox addInputCheckbox;
    75      /** Add option checkbox. */
    76      private JSpinner addInputSpinner;
    77      /** Add option checkbox. */
    78      private JLabel addInputNone;
    79      
    80      /**
    81       * Creates a new instance of AddOptionPanel.
    82       *
    83       * @param parent Parent settings panel.
    84       */
    85      protected AddOptionPanel(final SettingsPanel parent) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel(SettingsPanel)
                  * 
                  *  Postconditions:
                  *    this.addInputCheckbox == &new JCheckBox(initComponents#7)
                  *    this.addInputColourChooser == &new ColourChooser(initComponents#5)
                  *    this.addInputCurrent == &new JLabel(initComponents#10)
                  *    this.addInputNone == &new JLabel(initComponents#10)
                  *    this.addInputSpinner == &new JSpinner(initComponents#8)
                  *    this.addInputText == &new JTextField(initComponents#6)
                  *    this.addOptionButton == &new JButton(initComponents#3)
                  *    this.addOptionComboBox == &new JComboBox(initComponents#1)
                  *    this.parent == parent
                  *    init'ed(this.parent)
                  *    ...
                  */
    86          super();
    87          
    88          this.parent = parent;
    89          
    90          this.setOpaque(UIUtilities.getTabbedPaneOpaque());
    91          initComponents();
    92          initListeners();
    93          setLayout(new MigLayout("ins 0"));
    94          layoutComponents();
    95      }
    96      
    97      /** Initialises the components. */
    98      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Postconditions:
                  *    this.addInputCheckbox == &new JCheckBox(initComponents#7)
                  *    this.addInputColourChooser == &new ColourChooser(initComponents#5)
                  *    this.addInputCurrent == &new JLabel(initComponents#10)
                  *    this.addInputNone == &new JLabel(initComponents#10)
                  *    this.addInputSpinner == &new JSpinner(initComponents#8)
                  *    this.addInputText == &new JTextField(initComponents#6)
                  *    this.addOptionButton == &new JButton(initComponents#3)
                  *    this.addOptionComboBox == &new JComboBox(initComponents#1)
                  *    new ColourChooser(initComponents#5) num objects == 1
                  *    this.addInputColourChooser.showHex == 1
                  *    ...
                  */
    99          addOptionComboBox = new JComboBox(new DefaultComboBoxModel());
   100          addOptionButton = new JButton("Add");
   101          
   102          addOptionComboBox.setRenderer(new AddOptionCellRenderer(parent));
   103          
   104          addInputColourChooser = new ColourChooser();
   105          addInputText = new JTextField();
   106          addInputCheckbox = new JCheckBox();
   107          addInputCheckbox.setOpaque(UIUtilities.getTabbedPaneOpaque());
   108          addInputSpinner = new JSpinner(new SpinnerNumberModel());
   109          addInputNone = new JLabel("");
   110          
   111          addInputCurrent = addInputNone;
   112          
   113          addOptionComboBox.setEnabled(false);
   114          addOptionButton.setEnabled(false);
   115      }
   116      
   117      /** Initialises listeners. */
   118      private void initListeners() {
   119          //Only fire events on selection not on highlight
                 /* 
    P/P           *  Method: void initListeners()
                  * 
                  *  Preconditions:
                  *    this.addInputText != null
                  *    this.addOptionButton != null
                  *    this.addOptionComboBox != null
                  * 
                  *  Presumptions:
                  *    init'ed(java.lang.Boolean.TRUE)
                  *    javax.swing.JTextField:getActionMap(...)@124 != null
                  *    javax.swing.JTextField:getInputMap(...)@134 != null
                  */
   120          addOptionComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
   121          addOptionComboBox.addActionListener(this);
   122          addOptionButton.addActionListener(this);
   123  
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel$1(AddOptionPanel)
                  */
   124          addInputText.getActionMap().put("enter", new AbstractAction() {
   125              
   126              private static final long serialVersionUID = 2;
   127  
   128              /** {@inheritDoc} */
   129              @Override
   130              public void actionPerformed(final ActionEvent e) {
                         /* 
    P/P                   *  Method: void actionPerformed(ActionEvent)
                          * 
                          *  Preconditions:
                          *    this.addOptionButton != null
                          */
   131                  addOptionButton.doClick();
   132              }
   133          });
   134          addInputText.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enter");
   135      }
   136      
   137      /** Lays out the components. */
   138      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.addInputCurrent)
                  *    init'ed(this.addOptionButton)
                  *    init'ed(this.addOptionComboBox)
                  */
   139          setVisible(false);
   140          
   141          removeAll();
   142          
   143          add(addOptionComboBox, "left, aligny top");
   144          add(addInputCurrent, "growx, pushx, aligny top");
   145          add(addOptionButton, "right, aligny top");
   146          
   147          setVisible(true);
   148      }
   149      
   150      /**
   151       * Adds an addable option.
   152       *
   153       * @param optionName Option name
   154       */
   155      protected void addOption(final String optionName) {
                 /* 
    P/P           *  Method: void addOption(String)
                  * 
                  *  Preconditions:
                  *    this.addOptionButton != null
                  *    this.addOptionComboBox != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JComboBox:getModel(...)@156 != null
                  */
   156          ((DefaultComboBoxModel) addOptionComboBox.getModel()).addElement(
   157                  optionName);
   158          addOptionButton.setEnabled(true);
   159          addOptionComboBox.setEnabled(true);
   160      }
   161      
   162      /**
   163       * Removes an addable option.
   164       *
   165       * @param optionName Option name
   166       */
   167      protected void delOption(final String optionName) {
                 /* 
    P/P           *  Method: void delOption(String)
                  * 
                  *  Preconditions:
                  *    this.addOptionComboBox != null
                  *    (soft) this.addOptionButton != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JComboBox:getModel(...)@168 != null
                  *    javax.swing.JComboBox:getModel(...)@170 != null
                  * 
                  *  Test Vectors:
                  *    javax.swing.ComboBoxModel:getSize(...)@170: {-231..-1, 1..232-1}, {0}
                  */
   168          ((DefaultComboBoxModel) addOptionComboBox.getModel()).removeElement(
   169                  optionName);
   170          if (addOptionComboBox.getModel().getSize() == 0) {
   171              addOptionComboBox.setEnabled(false);
   172              addOptionButton.setEnabled(false);
   173          }
   174      }
   175      
   176      /** Clears the options. */
   177      protected void clearOptions() {
                 /* 
    P/P           *  Method: void clearOptions()
                  * 
                  *  Preconditions:
                  *    init'ed(this.addInputNone)
                  *    init'ed(this.addOptionButton)
                  *    this.addOptionComboBox != null
                  * 
                  *  Postconditions:
                  *    this.addInputCurrent == this.addInputNone
                  *    init'ed(this.addInputCurrent)
                  */
   178          addOptionComboBox.removeAllItems();
   179          addInputCurrent = addInputNone;
   180          layoutComponents();
   181      }
   182      
   183      /**
   184       * Swaps the input field type to the appropriate type.
   185       *
   186       * @param type Option type
   187       */
   188      private void switchInputField(final OptionType type) {
                 /* 
    P/P           *  Method: void switchInputField(SettingsPanel$OptionType)
                  * 
                  *  Preconditions:
                  *    init'ed(this.addOptionButton)
                  *    this.addOptionComboBox != null
                  *    (soft) init'ed(com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel$2__static_init.new int[](AddOptionPanel$2__static_init#1)[...])
                  *    (soft) this.addInputCheckbox != null
                  *    (soft) this.addInputColourChooser != null
                  *    (soft) this.addInputColourChooser.previewPanel != null
                  *    (soft) init'ed(this.addInputNone)
                  *    (soft) this.addInputSpinner != null
                  *    (soft) this.addInputText != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@192 in {0..3}
                  * 
                  *  Postconditions:
                  *    this.addInputColourChooser.value == One-of{old this.addInputColourChooser.value, &"ffffff"}
                  *    this.addInputCurrent == One-of{this.addInputNone, this.addInputText, this.addInputCheckbox, this.addInputColourChooser, this.addInputSpinner}
                  *    init'ed(this.addInputCurrent)
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel$2__static_init.new int[](AddOptionPanel$2__static_init#1)[...]: {1}, {2}, {3}, {4}, {-231..0, 5..232-1}
                  *    type: Inverse{null}, Addr_Set{null}
                  */
   189          if (type == null) {
   190              addInputCurrent = addInputNone;
   191          } else {
                     /* 
    P/P               *  Method: com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel$2__static_init
                      * 
                      *  Preconditions:
                      *    (soft) init'ed(com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel$OptionType__static_init.new SettingsPanel$OptionType[](SettingsPanel$OptionType__static_init#5)[...])
                      * 
                      *  Presumptions:
                      *    com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@192 in {0..3}
                      *    com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@192 - values(...).length in range
                      * 
                      *  Postconditions:
                      *    new int[](AddOptionPanel$2__static_init#1) num objects == 1
                      */
   192              switch (type) {
   193                  case TEXTFIELD:
   194                      addInputText.setText("");
   195                      addInputCurrent = addInputText;
   196                      break;
   197                  case CHECKBOX:
   198                      addInputCheckbox.setSelected(false);
   199                      addInputCurrent = addInputCheckbox;
   200                      break;
   201                  case COLOUR:
   202                      addInputColourChooser.clearColour();
   203                      addInputCurrent = addInputColourChooser;
   204                      break;
   205                  case SPINNER:
   206                      addInputSpinner.setValue(0);
   207                      addInputCurrent = addInputSpinner;
   208                      break;
   209                  default:
   210                      addInputCurrent = addInputNone;
   211                      break;
   212              }
   213          }
   214          
   215          layoutComponents();
   216          
   217          addOptionComboBox.requestFocusInWindow();
   218      }
   219      
   220      /** {@inheritDoc} */
   221      @Override
   222      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    (soft) init'ed(com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel$2__static_init.new int[](AddOptionPanel$2__static_init#1)[...])
                  *    (soft) com.dmdirc.addons.ui_swing.components.expandingsettings.CurrentOptionsPanel$1__static_init.new int[](CurrentOptionsPanel$1__static_init#1)[...] in {1..4}
                  *    (soft) init'ed(this.addInputColourChooser.value)
                  *    (soft) this.addInputCheckbox != null
                  *    (soft) this.addInputColourChooser != null
                  *    (soft) this.addInputColourChooser.previewPanel != null
                  *    (soft) init'ed(this.addInputNone)
                  *    (soft) this.addInputSpinner != null
                  *    (soft) this.addInputText != null
                  *    ...
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel_OptionType:ordinal(...)@234 in {0..3}
                  *    java.util.Map:get(...)@242 != null
                  *    javax.swing.JSpinner:getValue(...)@254 != null
                  * 
                  *  Postconditions:
                  *    this.addInputColourChooser.value == One-of{old this.addInputColourChooser.value, &"ffffff"}
                  *    init'ed(this.addInputColourChooser.value)
                  *    this.addInputCurrent == One-of{this.addInputNone, this.addInputText, this.addInputCheckbox, this.addInputColourChooser, this.addInputSpinner, old this.addInputCurrent}
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.ui_swing.components.expandingsettings.AddOptionPanel$2__static_init.new int[](AddOptionPanel$2__static_init#1)[...]: {1}, {2}, {3}, {4}, {-231..0, 5..232-1}
                  *    javax.swing.JComboBox:getSelectedItem(...)@224: Inverse{null}, Addr_Set{null}
                  */
   223          if (e.getSource() == addOptionComboBox) {
   224              if (addOptionComboBox.getSelectedItem() == null) {
   225                  addOptionComboBox.setEnabled(false);
   226                  addOptionButton.setEnabled(false);
   227              }
   228              switchInputField(parent.getOptionType(
   229                      (String) addOptionComboBox.getSelectedItem()));
   230          } else if (e.getSource() == addOptionButton) {
   231              final OptionType type = parent.getOptionType(
   232                      (String) addOptionComboBox.getSelectedItem());
   233              
   234              switch (type) {
   235                  case TEXTFIELD:
   236                      parent.addCurrentOption(
   237                              (String) addOptionComboBox.getSelectedItem(),
   238                              type,
   239                              addInputText.getText());
   240                      break;
   241                  case CHECKBOX:
   242                      parent.addCurrentOption(
   243                              (String) addOptionComboBox.getSelectedItem(),
   244                              type,
   245                              String.valueOf(addInputCheckbox.isSelected()));
   246                      break;
   247                  case COLOUR:
   248                      parent.addCurrentOption(
   249                              (String) addOptionComboBox.getSelectedItem(),
   250                              type,
   251                              addInputColourChooser.getColour());
   252                      break;
   253                  case SPINNER:
   254                      parent.addCurrentOption(
   255                              (String) addOptionComboBox.getSelectedItem(),
   256                              type,
   257                              addInputSpinner.getValue().toString());
   258                      break;
   259                  default:
   260                      break;
   261              }
   262              
   263              delOption((String) addOptionComboBox.getSelectedItem());
   264          }
   265      }
   266      
   267  }








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