File Source: ActionResponsePanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionResponsePanel__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.config.IdentityManager;
    26  
    27  import java.util.Comparator;
    28  import java.util.TreeSet;
    29  
    30  import javax.swing.BorderFactory;
    31  import javax.swing.DefaultComboBoxModel;
    32  import javax.swing.JComboBox;
    33  import javax.swing.JLabel;
    34  import javax.swing.JPanel;
    35  import javax.swing.JScrollPane;
    36  import javax.swing.JTextArea;
    37  
    38  import net.miginfocom.swing.MigLayout;
    39  
    40  /**
    41   * Action response panel.
    42   */
    43  public class ActionResponsePanel extends JPanel {
    44  
    45      /**
    46       * A version number for this class. It should be changed whenever the class
    47       * structure is changed (or anything else that would prevent serialized
    48       * objects being unserialized with the new class).
    49       */
    50      private static final long serialVersionUID = 1;
    51      /** Response text area. */
    52      private JTextArea response;
    53      /** Formatter combo box. */
    54      private JComboBox formatter;
    55  
    56      /** Instantiates the panel. */
    57      public ActionResponsePanel() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionResponsePanel()
                  * 
                  *  Postconditions:
                  *    this.formatter == &new JComboBox(initComponents#2)
                  *    this.response == &new JTextArea(initComponents#1)
                  *    new JComboBox(initComponents#2) num objects == 1
                  *    new JTextArea(initComponents#1) num objects == 1
                  */
    58          super();
    59  
    60          initComponents();
    61          addListeners();
    62          layoutComponents();
    63      }
    64  
    65      /** Initialises the components. */
    66      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.ConfigManager:getOptions(...)@75 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@75 != null
                  *    init'ed(java.lang.String.CASE_INSENSITIVE_ORDER)
                  *    javax.swing.JComboBox:getModel(...)@71 != null
                  *    javax.swing.JComboBox:getModel(...)@72 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.formatter == &new JComboBox(initComponents#2)
                  *    this.response == &new JTextArea(initComponents#1)
                  *    new JComboBox(initComponents#2) num objects == 1
                  *    new JTextArea(initComponents#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@77: {0}, {1}
                  */
    67          response = new JTextArea();
    68          response.setRows(4);
    69          formatter = new JComboBox(new DefaultComboBoxModel());
    70  
    71          ((DefaultComboBoxModel) formatter.getModel()).addElement("No change");
    72          ((DefaultComboBoxModel) formatter.getModel()).addElement("No response");
    73  
    74          final TreeSet<String> formatters = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    75          formatters.addAll(IdentityManager.getGlobalConfig().getOptions("formatter").keySet());
    76  
    77          for (String format : formatters) {
    78              ((DefaultComboBoxModel) formatter.getModel()).addElement(format);
    79          }
    80      }
    81  
    82      /** Adds the listeners. */
    83      private void addListeners() {
             /* 
    P/P       *  Method: void addListeners()
              */
    84      }
    85  
    86      /** Lays out the components. */
    87      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.formatter)
                  *    init'ed(this.response)
                  */
    88          setBorder(BorderFactory.createTitledBorder(getBorder(), "Response"));
    89          setLayout(new MigLayout("fill, wrap 1"));
    90  
    91          add(new JLabel("Execute these commands: "));
    92          add(new JScrollPane(response), "grow, push");
    93          add(new JLabel("Alter the event's formatter"));
    94          add(formatter, "growx, pushx");
    95      }
    96      
    97      /**
    98       * Sets the response.
    99       * 
   100       * @param response new response
   101       */
   102      public void setResponse(final String[] response) {
                 /* 
    P/P           *  Method: void setResponse(String[])
                  * 
                  *  Preconditions:
                  *    response != null
                  *    response.length <= 232-1
                  *    (soft) response[...] != null
                  *    (soft) this.response != null
                  * 
                  *  Presumptions:
                  *    java.lang.StringBuilder:length(...)@110 >= -231+1
                  * 
                  *  Test Vectors:
                  *    java.lang.StringBuilder:length(...)@109: {-231..0}, {1..232-1}
                  */
   103          final StringBuilder sb = new StringBuilder();
   104          for (String responseLine : response) {
   105              responseLine = responseLine.replaceAll("\n", "\\\\n");
   106              sb.append(responseLine).append('\n');
   107          }
   108  
   109          if (sb.length() > 0) {
   110              this.response.setText(sb.substring(0, sb.length() - 1));
   111          }
   112      }
   113  
   114      /**
   115       * Sets the new formatter for the response panel.
   116       * 
   117       * @param newFormat new formatter.
   118       */
   119      public void setFormatter(final String newFormat) {
                 /* 
    P/P           *  Method: void setFormatter(String)
                  * 
                  *  Preconditions:
                  *    this.formatter != null
                  * 
                  *  Test Vectors:
                  *    newFormat: Inverse{null}, Addr_Set{null}
                  *    java.lang.String:isEmpty(...)@122: {0}, {1}
                  */
   120          if (newFormat == null) {
   121              formatter.setSelectedIndex(0);
   122          } else if (newFormat.isEmpty()) {
   123              formatter.setSelectedIndex(1);
   124          } else {
   125              formatter.setSelectedItem(newFormat);
   126          }
   127      }
   128  
   129      /**
   130       * Returns the current response.
   131       * 
   132       * @return Response text
   133       */
   134      public String[] getResponse() {
                 /* 
    P/P           *  Method: String[] getResponse()
                  * 
                  *  Preconditions:
                  *    this.response != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTextArea:getText(...)@135 != null
                  * 
                  *  Postconditions:
                  *    java.lang.String:split(...)._tainted == 0
                  *    java.lang.String:split(...).length == undefined
                  *    java.lang.String:split(...).length == 0, if init'ed
                  *    java.lang.String:split(...)[...] == null
                  *    return_value == &amp;java.lang.String:split(...)
                  */
   135          final String[] text = response.getText().split("\n");
   136          for (int i = 0; i < text.length; i++) {
   137              text[i] = text[i].replaceAll("\\\\n", "\n");
   138          }
   139          return text;
   140      }
   141  
   142      /**
   143       * Returns the current formatter.
   144       * 
   145       * @return Formatter text
   146       */
   147      public String getFormatter() {
                 /* 
    P/P           *  Method: String getFormatter()
                  * 
                  *  Preconditions:
                  *    this.formatter != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@149: {0}, {1}
                  *    java.lang.String:equals(...)@151: {0}, {1}
                  */
   148          final String newFormat = (String) formatter.getSelectedItem();
   149          if ("No change".equals(newFormat)) {
   150              return null;
   151          } else if ("No response".equals(newFormat)) {
   152              return "";
   153          } else {
   154              return newFormat;
   155          }
   156      }
   157  
   158      /** {@inheritDoc} */
   159      @Override
   160      public void setEnabled(final boolean enabled) {
                 /* 
    P/P           *  Method: void setEnabled(bool)
                  * 
                  *  Preconditions:
                  *    this.formatter != null
                  *    this.response != null
                  */
   161          response.setEnabled(enabled);
   162          formatter.setEnabled(enabled);
   163      }
   164  }








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