File Source: AliasPanel.java

             /* 
    P/P       *  Method: com.dmdirc.addons.ui_swing.dialogs.aliases.AliasPanel__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.aliases;
    24  
    25  import com.dmdirc.addons.ui_swing.components.renderers.ActionComparisonCellRenderer;
    26  import com.dmdirc.actions.wrappers.Alias;
    27  import com.dmdirc.actions.ActionCondition;
    28  import com.dmdirc.actions.CoreActionComparison;
    29  import com.dmdirc.actions.CoreActionComponent;
    30  import com.dmdirc.config.prefs.validator.FileNameValidator;
    31  import com.dmdirc.addons.ui_swing.UIUtilities;
    32  import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
    33  
    34  import com.dmdirc.config.prefs.validator.CommandNameValidator;
    35  import com.dmdirc.config.prefs.validator.ValidatorChain;
    36  import java.awt.event.ActionEvent;
    37  import java.awt.event.ActionListener;
    38  import java.util.ArrayList;
    39  import java.util.List;
    40  
    41  import javax.swing.JComboBox;
    42  import javax.swing.JLabel;
    43  import javax.swing.JPanel;
    44  import javax.swing.JScrollPane;
    45  import javax.swing.JSpinner;
    46  import javax.swing.JTextArea;
    47  import javax.swing.SpinnerNumberModel;
    48  
    49  import net.miginfocom.swing.MigLayout;
    50  
    51  /**
    52   * Panel to display an alias.
    53   */
    54  public final class AliasPanel extends JPanel implements ActionListener {
    55  
    56      /**
    57       * A version number for this class. It should be changed whenever the class
    58       * structure is changed (or anything else that would prevent serialized
    59       * objects being unserialized with the new class).
    60       */
    61      private static final long serialVersionUID = 2;
    62      /** Name field. */
    63      private final ValidatingJTextField command;
    64      /** argument component combo box. */
    65      private final JComboBox argumentComponent;
    66      /** Argument number spinner. */
    67      private final JSpinner argumentNumber;
    68      /** Response field. */
    69      private final JTextArea response;
    70      /** Alias. */
    71      private Alias alias;
    72  
    73      /** Creates a new instance of AliasPanel. */
    74      @SuppressWarnings("unchecked")
    75      public AliasPanel() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.aliases.AliasPanel()
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.INT_EQUALS)
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.INT_GREATER)
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.INT_LESS)
                  *    init'ed(java.lang.Boolean.TRUE)
                  * 
                  *  Postconditions:
                  *    this.alias == null
                  *    this.argumentComponent == &new JComboBox(AliasPanel#6)
                  *    this.argumentNumber == &new JSpinner(AliasPanel#8)
                  *    this.command == &new ValidatingJTextField(AliasPanel#1)
                  *    this.response == &new JTextArea(AliasPanel#10)
                  *    new JComboBox(AliasPanel#6) num objects == 1
                  *    new JLabel(ValidatingJTextField#1) num objects == 1
                  *    new JSpinner(AliasPanel#8) num objects == 1
                  *    new JTextArea(AliasPanel#10) num objects == 1
                  *    new JTextField(ValidatingJTextField#1) num objects == 1
                  *    ...
                  */
    76          super();
    77  
    78          command = new ValidatingJTextField(new ValidatorChain<String>(
    79                  new CommandNameValidator(), new FileNameValidator()));
    80          command.setEnabled(false);
    81  
    82          argumentComponent = new JComboBox(new CoreActionComparison[]{null,
    83              CoreActionComparison.INT_GREATER, CoreActionComparison.INT_EQUALS,
    84              CoreActionComparison.INT_LESS,
    85          });
    86          argumentNumber = new JSpinner(new SpinnerNumberModel(0, 0,
    87                  Integer.MAX_VALUE, 1));
    88          response = new JTextArea();
    89  
    90          argumentNumber.setEnabled(false);
    91          response.setRows(5);
    92  
    93          argumentComponent.setRenderer(new ActionComparisonCellRenderer());
    94          argumentComponent.putClientProperty("JComboBox.isTableCellEditor",
    95                  Boolean.TRUE);
    96          argumentComponent.addActionListener(this);
    97  
    98          UIUtilities.addUndoManager(response);
    99  
   100          layoutComponents();
   101  
   102          clear();
   103      }
   104  
   105      /** Lays out and initialises the components. */
   106      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  */
   107          setLayout(new MigLayout("fill"));
   108  
   109          add(new JLabel("Command: "));
   110          add(command, "span 2, growx, pushx, wrap");
   111  
   112          add(new JLabel("#Arguments: "));
   113          add(argumentComponent, "sgy args");
   114          add(argumentNumber, "sgy args, growx, pushx, wrap");
   115  
   116          add(new JLabel("Response: "));
   117          add(new JScrollPane(response), "span 2, grow, push, wrap");
   118      }
   119  
   120      /** Clears the details. */
   121      public void clear() {
                 /* 
    P/P           *  Method: void clear()
                  * 
                  *  Preconditions:
                  *    this.argumentComponent != null
                  *    this.argumentNumber != null
                  *    this.command != null
                  *    this.command.errorIcon != null
                  *    this.command.textField != null
                  *    this.response != null
                  *    (soft) this.command.validator != null
                  * 
                  *  Postconditions:
                  *    this.alias == null
                  */
   122          alias = null;
   123          command.setText("");
   124          command.setEnabled(false);
   125          argumentComponent.setSelectedItem(null);
   126          argumentNumber.setValue(0);
   127          response.setText("");
   128          command.setEnabled(false);
   129          argumentComponent.setEnabled(false);
   130          argumentNumber.setEnabled(false);
   131          response.setEnabled(false);
   132      }
   133  
   134      /**
   135       * Sets the alias details.
   136       *
   137       * @param alias List of alias details to display
   138       */
   139      public void setAlias(final Alias alias) {
                 /* 
    P/P           *  Method: void setAlias(Alias)
                  * 
                  *  Preconditions:
                  *    (soft) this.argumentComponent != null
                  *    (soft) this.argumentNumber != null
                  *    (soft) this.command != null
                  *    (soft) this.command.errorIcon != null
                  *    (soft) this.command.textField != null
                  *    (soft) this.command.validator != null
                  *    (soft) this.response != null
                  * 
                  *  Presumptions:
                  *    arr$.length@169 <= 232-1
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.STRING_EQUALS)
                  *    com.dmdirc.actions.wrappers.Alias:getArguments(...)@150 != null
                  *    com.dmdirc.actions.wrappers.Alias:getResponse(...)@169 != null
                  *    java.lang.StringBuffer:length(...)@174 >= -231+1
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.alias == One-of{null, alias}
                  *    init'ed(this.alias)
                  * 
                  *  Test Vectors:
                  *    alias: Inverse{null}, Addr_Set{null}
                  *    java.lang.StringBuffer:length(...)@173: {-231..1}, {2..232-1}
                  *    java.util.List:size(...)@153: {-231..0, 2..232-1}, {1}
                  */
   140          if (alias == null) {
   141              clear();
   142              return;
   143          }
   144          this.alias = alias;
   145          command.setEnabled(true);
   146          argumentComponent.setEnabled(true);
   147          response.setEnabled(true);
   148          command.setText(alias.getCommand());
   149  
   150          final List<ActionCondition> arguments = alias.getArguments();
   151          ActionCondition argument;
   152  
   153          if (arguments.size() == 1) {
   154              argumentComponent.setSelectedItem(null);
   155              argumentNumber.setValue(0);
   156              argumentNumber.setEnabled(false);
   157          } else {
   158              argument = arguments.get(0);
   159  
   160              if (argument.getComparison() == CoreActionComparison.STRING_EQUALS) {
   161                  argument = arguments.get(1);
   162              }
   163              argumentComponent.setSelectedItem(argument.getComparison());
   164              argumentNumber.setValue(Integer.parseInt(argument.getTarget()));
   165              argumentNumber.setEnabled(true);
   166          }
   167  
   168          final StringBuffer sb = new StringBuffer();
   169          for (String line : alias.getResponse()) {
   170              sb.append(line).append('\n');
   171          }
   172  
   173          if (sb.length() > 1) {
   174              response.setText(sb.substring(0, sb.length() - 1));
   175          } else {
   176              response.setText("");
   177          }
   178      }
   179  
   180      /** 
   181       * {@inheritDoc}.
   182       * 
   183       * @param e Action event
   184       */
   185      @Override
   186      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    this.argumentComponent != null
                  *    this.argumentNumber != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.INT_LESS)
                  *    javax.swing.JSpinner:getModel(...)@187 != null
                  *    javax.swing.JSpinner:getModel(...)@192 != null
                  *    javax.swing.JSpinner:getModel(...)@193 != null
                  *    javax.swing.JSpinner:getModel(...)@195 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.lang.Object:equals(...)@192: {0}, {1}
                  *    javax.swing.JComboBox:getSelectedIndex(...)@188: {-231..0}, {1..232-1}
                  */
   187          ((SpinnerNumberModel) argumentNumber.getModel()).setMinimum(0);
   188          if (argumentComponent.getSelectedIndex() > 0) {
   189              argumentNumber.setEnabled(true);
   190              if (argumentComponent.getSelectedItem() ==
   191                      CoreActionComparison.INT_LESS) {
   192                  if (argumentNumber.getModel().getValue().equals(0)) {
   193                      argumentNumber.getModel().setValue(1);
   194                  }
   195                  ((SpinnerNumberModel) argumentNumber.getModel()).setMinimum(1);
   196              }
   197          } else {
   198              argumentNumber.setEnabled(false);
   199          }
   200      }
   201  
   202      /**
   203       * Returns the current command.
   204       *
   205       * @return Alias command
   206       */
   207      public String getCommand() {
                 /* 
    P/P           *  Method: String getCommand()
                  * 
                  *  Preconditions:
                  *    this.command != null
                  *    this.command.textField != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   208          return command.getText();
   209      }
   210  
   211      /**
   212       * Returns the arguments condition.
   213       *
   214       * @return Action argument condition
   215       */
   216      public ActionCondition getArguments() {
                 /* 
    P/P           *  Method: ActionCondition getArguments()
                  * 
                  *  Preconditions:
                  *    this.argumentComponent != null
                  *    (soft) init'ed(com.dmdirc.addons.ui_swing.dialogs.aliases.AliasPanel$1__static_init.new int[](AliasPanel$1__static_init#1)[...])
                  *    (soft) this.argumentNumber != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.INT_EQUALS)
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.INT_GREATER)
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.INT_LESS)
                  *    com.dmdirc.actions.CoreActionComparison:ordinal(...)@220 >= 0
                  *    com.dmdirc.actions.CoreActionComparison:values(...).length >= 1
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{null,&amp;new ActionCondition(getArguments#1),&amp;new ActionCondition(getArguments#2),&amp;new ActionCondition(getArguments#3)}
                  *    new ActionCondition(getArguments#1) num objects <= 1
                  *    new ActionCondition(getArguments#2) num objects <= 1
                  *    new ActionCondition(getArguments#3) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.ui_swing.dialogs.aliases.AliasPanel$1__static_init.new int[](AliasPanel$1__static_init#1)[...]: {1}, {2}, {3}, {-231..0, 4..232-1}
                  *    javax.swing.JComboBox:getSelectedItem(...)@217: Inverse{null}, Addr_Set{null}
                  */
   217          if (argumentComponent.getSelectedItem() == null) {
   218              return null;
   219          }
                 /* 
    P/P           *  Method: com.dmdirc.addons.ui_swing.dialogs.aliases.AliasPanel$1__static_init
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.CoreActionComparison.INT_EQUALS != null
                  *    com.dmdirc.actions.CoreActionComparison.INT_GREATER != null
                  *    com.dmdirc.actions.CoreActionComparison.INT_LESS != null
                  *    com.dmdirc.actions.CoreActionComparison:ordinal(...)@220 >= 0
                  *    com.dmdirc.actions.CoreActionComparison:ordinal(...)@220 < com.dmdirc.actions.CoreActionComparison:values(...).length@220
                  *    ...
                  * 
                  *  Postconditions:
                  *    new int[](AliasPanel$1__static_init#1) num objects == 1
                  */
   220          switch ((CoreActionComparison) argumentComponent.getSelectedItem()) {
   221              case INT_EQUALS:
   222                  return new ActionCondition(2,
   223                          CoreActionComponent.STRINGARRAY_LENGTH,
   224                          CoreActionComparison.INT_EQUALS,
   225                          argumentNumber.getValue().toString());
   226              case INT_GREATER:
   227                  return new ActionCondition(2,
   228                          CoreActionComponent.STRINGARRAY_LENGTH,
   229                          CoreActionComparison.INT_GREATER,
   230                          argumentNumber.getValue().toString());
   231              case INT_LESS:
   232                  return new ActionCondition(2,
   233                          CoreActionComponent.STRINGARRAY_LENGTH,
   234                          CoreActionComparison.INT_LESS,
   235                          argumentNumber.getValue().toString());
   236              default:
   237                  return null;
   238          }
   239      }
   240  
   241      /**
   242       * Returns the user response to the alias.
   243       *
   244       * @return Alias response
   245       */
   246      protected String[] getResponse() {
                 /* 
    P/P           *  Method: String[] getResponse()
                  * 
                  *  Preconditions:
                  *    this.response != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTextArea:getText(...)@247 != null
                  * 
                  *  Postconditions:
                  *    java.lang.String:split(...)._tainted == 0
                  *    return_value == &amp;java.lang.String:split(...)
                  */
   247          return response.getText().split("\n");
   248      }
   249  
   250      /**
   251       * Returns the alias being shown in this panel.
   252       *
   253       * @return Alias
   254       */
   255      protected Alias getAlias() {
                 /* 
    P/P           *  Method: Alias getAlias()
                  * 
                  *  Preconditions:
                  *    init'ed(this.alias)
                  * 
                  *  Postconditions:
                  *    return_value == this.alias
                  *    init'ed(return_value)
                  */
   256          return alias;
   257      }
   258  
   259      /**
   260       * Returns an alias reflecting the changes in this panel.
   261       *
   262       * @return New alias reflecting the edited alias.
   263       */
   264      protected Alias getNewAlias() {
                 /* 
    P/P           *  Method: Alias getNewAlias()
                  * 
                  *  Preconditions:
                  *    this.argumentComponent != null
                  *    this.command != null
                  *    this.command.textField != null
                  *    this.response != null
                  *    (soft) init'ed(com.dmdirc.addons.ui_swing.dialogs.aliases.AliasPanel$1__static_init.new int[](AliasPanel$1__static_init#1)[...])
                  *    (soft) this.argumentNumber != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.actions.CoreActionComparison.STRING_EQUALS)
                  *    com.dmdirc.actions.CoreActionComparison:values(...).length >= 1
                  *    init'ed(com.dmdirc.actions.CoreActionComponent.STRING_STRING)
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new Alias(getNewAlias#3)
                  *    new Alias(getNewAlias#3) num objects == 1
                  */
   265          final List<ActionCondition> conditions =
   266                  new ArrayList<ActionCondition>();
   267          conditions.add(new ActionCondition(1,
   268                  CoreActionComponent.STRING_STRING,
   269                  CoreActionComparison.STRING_EQUALS, getCommand()));
   270          if (getArguments() != null) {
   271              conditions.add(getArguments());
   272          }
   273          return new Alias(getCommand(), conditions, getResponse());
   274      }
   275  
   276      /** Focuses the command field. */
   277      public void focusCommand() {
                 /* 
    P/P           *  Method: void focusCommand()
                  * 
                  *  Preconditions:
                  *    this.command != null
                  *    this.command.textField != null
                  */
   278          command.requestFocusInWindow();
   279      }
   280  }








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