File Source: ActionConditionEditorPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionEditorPanel__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.ActionCondition;
    26  import com.dmdirc.actions.ActionManager;
    27  import com.dmdirc.actions.interfaces.ActionComparison;
    28  import com.dmdirc.actions.interfaces.ActionComponent;
    29  import com.dmdirc.actions.interfaces.ActionType;
    30  import com.dmdirc.addons.ui_swing.components.renderers.ActionCellRenderer;
    31  
    32  import java.awt.event.ActionEvent;
    33  import java.awt.event.ActionListener;
    34  import java.beans.PropertyChangeEvent;
    35  import java.beans.PropertyChangeListener;
    36  
    37  import javax.swing.DefaultComboBoxModel;
    38  import javax.swing.JComboBox;
    39  import javax.swing.JLabel;
    40  import javax.swing.JPanel;
    41  import javax.swing.JTextField;
    42  import javax.swing.event.DocumentEvent;
    43  import javax.swing.event.DocumentListener;
    44  
    45  import net.miginfocom.swing.MigLayout;
    46  
    47  /**
    48   * Action conditioneditor panel.
    49   */
         /* 
    P/P   *  Method: JTextField access$000(ActionConditionEditorPanel)
          * 
          *  Preconditions:
          *    x0 != null
          *    init'ed(x0.target)
          * 
          *  Postconditions:
          *    return_value == x0.target
          *    init'ed(return_value)
          */
    50  public class ActionConditionEditorPanel extends JPanel implements ActionListener,
    51          DocumentListener, PropertyChangeListener {
    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 = 1;
    59      /** Condition. */
    60      private ActionCondition condition;
    61      /** Trigger. */
    62      private ActionType trigger;
    63      /** Argument. */
    64      private JComboBox arguments;
    65      /** Component. */
    66      private JComboBox components;
    67      /** Comparison. */
    68      private JComboBox comparisons;
    69      /** Target. */
    70      private JTextField target;
    71  
    72      /** 
    73       * Instantiates the panel.
    74       * 
    75       * @param condition Action condition
    76       * @param trigger Action trigger
    77       */
    78      public ActionConditionEditorPanel(final ActionCondition condition,
    79              final ActionType trigger) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionEditorPanel(ActionCondition, ActionType)
                  * 
                  *  Preconditions:
                  *    (soft) condition != null
                  * 
                  *  Postconditions:
                  *    this.arguments == &new JComboBox(initComponents#1)
                  *    this.comparisons == &new JComboBox(initComponents#5)
                  *    this.components == &new JComboBox(initComponents#3)
                  *    this.condition == condition
                  *    this.condition != null
                  *    this.target == &new ActionConditionEditorPanel$1(initComponents#7)
                  *    this.trigger == trigger
                  *    init'ed(this.trigger)
                  *    new ActionConditionEditorPanel$1(initComponents#7) num objects == 1
                  *    new JComboBox(initComponents#1) num objects == 1
                  *    ...
                  * 
                  *  Test Vectors:
                  *    trigger: Inverse{null}, Addr_Set{null}
                  */
    80          super();
    81  
    82          this.condition = condition;
    83          this.trigger = trigger;
    84  
    85          initComponents();
    86  
    87          if (trigger == null) {
    88              setEnabled(false);
    89          } else {
    90              populateArguments();
    91              populateComponents();
    92              populateComparisons();
    93              populateTarget();
    94          }
    95  
    96          firePropertyChange("edit", null, null);
    97  
    98          addListeners();
    99          layoutComponents();
   100      }
   101  
   102      /** Initialises the components. */
   103      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Presumptions:
                  *    init'ed(java.lang.Boolean.TRUE)
                  * 
                  *  Postconditions:
                  *    this.arguments == &new JComboBox(initComponents#1)
                  *    this.comparisons == &new JComboBox(initComponents#5)
                  *    this.components == &new JComboBox(initComponents#3)
                  *    this.target == &new ActionConditionEditorPanel$1(initComponents#7)
                  *    new ActionConditionEditorPanel$1(initComponents#7) num objects == 1
                  *    new JComboBox(initComponents#1) num objects == 1
                  *    new JComboBox(initComponents#3) num objects == 1
                  *    new JComboBox(initComponents#5) num objects == 1
                  */
   104          arguments = new JComboBox(new DefaultComboBoxModel());
   105          arguments.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
   106          arguments.setName("argument");
   107          components = new JComboBox(new DefaultComboBoxModel());
   108          components.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
   109          components.setName("component");
   110          comparisons = new JComboBox(new DefaultComboBoxModel());
   111          comparisons.putClientProperty("JComboBox.isTableCellEditor",
   112                  Boolean.TRUE);
   113          comparisons.setName("comparison");
   114  
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionEditorPanel$1(ActionConditionEditorPanel)
                  */
   115          target = new JTextField() {
   116  
   117              /** Serial version UID. */
   118              private static final long serialVersionUID = 1;
   119  
   120              /** {@inheritDoc} */
   121              @Override
   122              public void setEnabled(final boolean enabled) {
                         /* 
    P/P                   *  Method: void setEnabled(bool)
                          * 
                          *  Preconditions:
                          *    this.target != null
                          */
   123                  firePropertyChange("validationResult", target.isEnabled(),
   124                          enabled);
   125                  super.setEnabled(enabled);
   126              }
   127          };
   128  
   129          arguments.setRenderer(new ActionCellRenderer());
   130          components.setRenderer(new ActionCellRenderer());
   131          comparisons.setRenderer(new ActionCellRenderer());
   132  
   133          components.setEnabled(false);
   134          comparisons.setEnabled(false);
   135          target.setEnabled(false);
   136      }
   137  
   138      /** Populates the arguments combo box. */
   139      private void populateArguments() {
                 /* 
    P/P           *  Method: void populateArguments()
                  * 
                  *  Preconditions:
                  *    this.arguments != null
                  *    this.condition != null
                  *    this.trigger != null
                  * 
                  *  Presumptions:
                  *    arr$.length@142 <= 232-1
                  *    com.dmdirc.actions.interfaces.ActionMetaType:getArgNames(...)@142 != null
                  *    com.dmdirc.actions.interfaces.ActionType:getType(...)@142 != null
                  *    javax.swing.JComboBox:getModel(...)@140 != null
                  *    javax.swing.JComboBox:getModel(...)@143 != null
                  */
   140          ((DefaultComboBoxModel) arguments.getModel()).removeAllElements();
   141  
   142          for (String arg : trigger.getType().getArgNames()) {
   143              ((DefaultComboBoxModel) arguments.getModel()).addElement(arg);
   144          }
   145          arguments.setSelectedIndex(condition.getArg());
   146      }
   147  
   148      /** Populates the components combo box. */
   149      private void populateComponents() {
                 /* 
    P/P           *  Method: void populateComponents()
                  * 
                  *  Preconditions:
                  *    this.components != null
                  *    this.condition != null
                  *    (soft) this.trigger != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.ActionCondition:getArg(...)@153 >= 0
                  *    com.dmdirc.actions.ActionCondition:getArg(...)@153 < com.dmdirc.actions.interfaces.ActionMetaType:getArgTypes(...).length@153
                  *    com.dmdirc.actions.ActionManager:getCompatibleComponents(...)@153 != null
                  *    com.dmdirc.actions.interfaces.ActionMetaType:getArgTypes(...).length@153 >= 1
                  *    com.dmdirc.actions.interfaces.ActionMetaType:getArgTypes(...)@153 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.actions.ActionCondition:getArg(...)@152: {-1}, {-231..-2, 0..232-1}
                  *    java.util.Iterator:hasNext(...)@153: {0}, {1}
                  */
   150          ((DefaultComboBoxModel) components.getModel()).removeAllElements();
   151  
   152          if (condition.getArg() != -1) {
   153              for (ActionComponent comp : ActionManager.getCompatibleComponents(
   154                      trigger.getType().getArgTypes()[condition.getArg()])) {
   155                  ((DefaultComboBoxModel) components.getModel()).addElement(comp);
   156              }
   157          }
   158          components.setSelectedItem(condition.getComponent());
   159      }
   160  
   161      /** Populates the comparisons combo box. */
   162      private void populateComparisons() {
                 /* 
    P/P           *  Method: void populateComparisons()
                  * 
                  *  Preconditions:
                  *    this.comparisons != null
                  *    this.condition != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.ActionCondition:getComponent(...)@166 != null
                  *    com.dmdirc.actions.ActionManager:getCompatibleComparisons(...)@166 != null
                  *    javax.swing.JComboBox:getModel(...)@163 != null
                  *    javax.swing.JComboBox:getModel(...)@168 != null
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.actions.ActionCondition:getComponent(...)@165: Addr_Set{null}, Inverse{null}
                  *    java.util.Iterator:hasNext(...)@166: {0}, {1}
                  */
   163          ((DefaultComboBoxModel) comparisons.getModel()).removeAllElements();
   164  
   165          if (condition.getComponent() != null) {
   166              for (ActionComparison comp : ActionManager.getCompatibleComparisons(
   167                      condition.getComponent().getType())) {
   168                  ((DefaultComboBoxModel) comparisons.getModel()).addElement(comp);
   169              }
   170          }
   171          comparisons.setSelectedItem(condition.getComparison());
   172      }
   173  
   174      /** Populates the target textfield. */
   175      private void populateTarget() {
                 /* 
    P/P           *  Method: void populateTarget()
                  * 
                  *  Preconditions:
                  *    this.condition != null
                  *    this.target != null
                  */
   176          target.setText(condition.getTarget());
   177      }
   178  
   179      /** Handles the argument changing. */
   180      private void handleArgumentChange() {
                 /* 
    P/P           *  Method: void handleArgumentChange()
                  * 
                  *  Preconditions:
                  *    this.arguments != null
                  *    this.comparisons != null
                  *    this.components != null
                  *    this.condition != null
                  *    this.target != null
                  *    (soft) this.trigger != null
                  */
   181          condition.setArg(arguments.getSelectedIndex());
   182          populateComponents();
   183          components.setEnabled(true);
   184          components.setSelectedItem(null);
   185          comparisons.setSelectedItem(null);
   186          comparisons.setEnabled(false);
   187          target.setText(null);
   188          target.setEnabled(false);
   189      }
   190  
   191      /** Handles the component changing. */
   192      private void handleComponentChange() {
                 /* 
    P/P           *  Method: void handleComponentChange()
                  * 
                  *  Preconditions:
                  *    this.comparisons != null
                  *    this.components != null
                  *    this.condition != null
                  *    this.target != null
                  */
   193          condition.setComponent((ActionComponent) components.getSelectedItem());
   194          populateComparisons();
   195          comparisons.setEnabled(true);
   196          comparisons.setSelectedItem(null);
   197          target.setText(null);
   198          target.setEnabled(false);
   199      }
   200  
   201      /** Handles the comparison changing. */
   202      private void handleComparisonChange() {
                 /* 
    P/P           *  Method: void handleComparisonChange()
                  * 
                  *  Preconditions:
                  *    this.comparisons != null
                  *    this.condition != null
                  *    this.target != null
                  */
   203          condition.setComparison((ActionComparison) comparisons.getSelectedItem());
   204          populateTarget();
   205          target.setEnabled(true);
   206      }
   207  
   208      /** Adds the listeners. */
   209      private void addListeners() {
                 /* 
    P/P           *  Method: void addListeners()
                  * 
                  *  Preconditions:
                  *    this.arguments != null
                  *    this.comparisons != null
                  *    this.components != null
                  *    this.target != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTextField:getDocument(...)@213 != null
                  */
   210          arguments.addActionListener(this);
   211          components.addActionListener(this);
   212          comparisons.addActionListener(this);
   213          target.getDocument().addDocumentListener(this);
   214          target.addPropertyChangeListener("validationResult", this);
   215      }
   216  
   217      /** Lays out the components. */
   218      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.arguments)
                  *    init'ed(this.comparisons)
                  *    init'ed(this.components)
                  *    init'ed(this.target)
                  */
   219          setLayout(new MigLayout("wrap 2, pack"));
   220  
   221          add(new JLabel("Argument:"), "align right");
   222          add(arguments, "growx, pushx");
   223          add(new JLabel("Component:"), "align right");
   224          add(components, "growx, pushx");
   225          add(new JLabel("Comparison:"), "align right");
   226          add(comparisons, "growx, pushx");
   227          add(new JLabel("Target:"), "align right");
   228          add(target, "growx, pushx");
   229      }
   230  
   231      /** 
   232       * {@inheritDoc} 
   233       * 
   234       * @param e Action event
   235       */
   236      @Override
   237      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    (soft) this.arguments != null
                  *    (soft) this.comparisons != null
                  *    (soft) this.components != null
                  *    (soft) this.condition != null
                  *    (soft) this.target != null
                  *    (soft) this.trigger != null
                  */
   238          if (e.getSource() == arguments) {
   239              handleArgumentChange();
   240          } else if (e.getSource() == components) {
   241              handleComponentChange();
   242          } else if (e.getSource() == comparisons) {
   243              handleComparisonChange();
   244          }
   245          firePropertyChange("edit", null, null);
   246      }
   247  
   248      /** {@inheritDoc} */
   249      @Override
   250      public void insertUpdate(final DocumentEvent e) {
                 /* 
    P/P           *  Method: void insertUpdate(DocumentEvent)
                  * 
                  *  Preconditions:
                  *    this.condition != null
                  *    this.target != null
                  */
   251          synchronized (condition) {
   252              condition.setTarget(target.getText());
   253          }
   254          firePropertyChange("edit", null, null);
   255      }
   256  
   257      /** {@inheritDoc} */
   258      @Override
   259      public void removeUpdate(final DocumentEvent e) {
                 /* 
    P/P           *  Method: void removeUpdate(DocumentEvent)
                  * 
                  *  Preconditions:
                  *    this.condition != null
                  *    this.target != null
                  */
   260          synchronized (condition) {
   261              condition.setTarget(target.getText());
   262          }
   263          firePropertyChange("edit", null, null);
   264      }
   265  
   266      /** {@inheritDoc} */
   267      @Override
   268      public void changedUpdate(final DocumentEvent e) {
   269      //Ignore
             /* 
    P/P       *  Method: void changedUpdate(DocumentEvent)
              */
   270      }
   271  
   272      /** {@inheritDoc} */
   273      @Override
   274      public void setEnabled(boolean enabled) {
                 /* 
    P/P           *  Method: void setEnabled(bool)
                  * 
                  *  Preconditions:
                  *    this.arguments != null
                  *    this.comparisons != null
                  *    this.components != null
                  *    this.target != null
                  * 
                  *  Test Vectors:
                  *    enabled: {0}, {1}
                  */
   275          super.setEnabled(enabled);
   276          arguments.setEnabled(enabled);
   277          if (enabled) {
   278              components.setEnabled(arguments.getSelectedIndex() != -1);
   279              comparisons.setEnabled(components.getSelectedIndex() != -1);
   280              target.setEnabled(comparisons.getSelectedIndex() != -1);
   281          } else {
   282              components.setEnabled(false);
   283              comparisons.setEnabled(false);
   284              target.setEnabled(false);
   285          }
   286      }
   287  
   288      /**
   289       * Sets the action trigger.
   290       * 
   291       * @param trigger new trigger
   292       */
   293      void setTrigger(final ActionType trigger) {
                 /* 
    P/P           *  Method: void setTrigger(ActionType)
                  * 
                  *  Preconditions:
                  *    this.arguments != null
                  *    this.comparisons != null
                  *    this.components != null
                  *    this.target != null
                  *    (soft) this.condition != null
                  * 
                  *  Postconditions:
                  *    this.trigger == trigger
                  *    init'ed(this.trigger)
                  * 
                  *  Test Vectors:
                  *    trigger: Addr_Set{null}, Inverse{null}
                  *    java.lang.Object:equals(...)@297: {1}, {0}
                  */
   294          this.trigger = trigger;
   295  
   296          setEnabled(trigger != null);
   297          if (trigger != null && !trigger.equals(this.trigger)) {
   298              populateArguments();
   299          }
   300      }
   301  
   302      /** {@inheritDoc} */
   303      @Override
   304      public void propertyChange(PropertyChangeEvent evt) {
                 /* 
    P/P           *  Method: void propertyChange(PropertyChangeEvent)
                  * 
                  *  Preconditions:
                  *    evt != null
                  */
   305          firePropertyChange("validationResult", evt.getOldValue(),
   306                  evt.getNewValue());
   307      }
   308  
   309      /**
   310       * Checks if this editor panel has errored.
   311       * 
   312       * @return true iif the content it valid
   313       */
   314      public boolean checkError() {
                 /* 
    P/P           *  Method: bool checkError()
                  * 
                  *  Preconditions:
                  *    this.target != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   315          return target.isEnabled();
   316      }
   317  }








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