File Source: PerformPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.serversetting.PerformPanel__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.serversetting;
    24  
    25  import com.dmdirc.Server;
    26  import com.dmdirc.actions.Action;
    27  import com.dmdirc.actions.wrappers.PerformWrapper;
    28  import com.dmdirc.addons.ui_swing.UIUtilities;
    29  
    30  import java.awt.event.ActionEvent;
    31  import java.awt.event.ActionListener;
    32  
    33  import javax.swing.DefaultComboBoxModel;
    34  import javax.swing.JComboBox;
    35  import javax.swing.JPanel;
    36  import javax.swing.JScrollPane;
    37  import javax.swing.JTextArea;
    38  
    39  import net.miginfocom.swing.MigLayout;
    40  
    41  /**
    42   * Perform panel.
    43   */
    44  public final class PerformPanel extends JPanel implements ActionListener {
    45      
    46      /**
    47       * A version number for this class. It should be changed whenever the class
    48       * structure is changed (or anything else that would prevent serialized
    49       * objects being unserialized with the new class).
    50       */
    51      private static final long serialVersionUID = 1;
    52      
    53      /** Parent server. */
    54      private final Server server;
    55      
    56      /** The action used for server performs. */
    57      private Action serverAction;
    58      /** The action used for network performs. */
    59      private Action networkAction;
    60      
    61      /** The server perform string. */
    62      private String serverPerform;
    63      /** The network perform string. */
    64      private String networkPerform;
    65      
    66      /** Keeps track of which perform we're editing. */
    67      private boolean isNetworkPerform;
    68      
    69      /** Network/server combo box. */
    70      private JComboBox target;
    71      
    72      /** Perform text area. */
    73      private JTextArea textarea;
    74      
    75      /**
    76       * Creates a new instance of IgnoreList.
    77       *
    78       * @param server Parent server
    79       */
    80      public PerformPanel(final Server server) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.serversetting.PerformPanel(Server)
                  * 
                  *  Preconditions:
                  *    server != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.isNetworkPerform)
                  *    init'ed(this.networkAction)
                  *    this.networkPerform != null
                  *    this.server == server
                  *    this.server != null
                  *    init'ed(this.serverAction)
                  *    this.serverPerform != null
                  *    this.target == &new JComboBox(initComponents#3)
                  *    this.textarea == &new JTextArea(initComponents#6)
                  *    new JComboBox(initComponents#3) num objects == 1
                  *    ...
                  */
    81          super();
    82          
    83          this.server = server;
    84          
    85          this.setOpaque(UIUtilities.getTabbedPaneOpaque());
    86          initComponents();
    87          addListeners();
    88          loadPerforms();
    89          populatePerform();
    90      }
    91      
    92      /** Initialises teh components. */
    93      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Preconditions:
                  *    this.server != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Server:getName(...)@102 != null
                  *    com.dmdirc.Server:getNetwork(...)@99 != null
                  * 
                  *  Postconditions:
                  *    this.target == &new JComboBox(initComponents#3)
                  *    this.textarea == &new JTextArea(initComponents#6)
                  *    new JComboBox(initComponents#3) num objects == 1
                  *    new JTextArea(initComponents#6) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:isEmpty(...)@102: {1}, {0}
                  *    java.lang.String:isEmpty(...)@99: {1}, {0}
                  */
    94          setLayout(new MigLayout("fill"));
    95          
    96          final DefaultComboBoxModel model = new DefaultComboBoxModel();
    97          target = new JComboBox(model);
    98          
    99          if (!server.getNetwork().isEmpty()) {
   100              model.addElement("Network perform (" + server.getNetwork() + ")");
   101          }
   102          if (!server.getName().isEmpty()) {
   103              model.addElement("Server perform (" + server.getName() + ")");
   104          }
   105          
   106          add(target, "growx, pushx, wrap");
   107          
   108          textarea = new JTextArea();
   109          textarea.setColumns(40);
   110          
   111          add(new JScrollPane(textarea), "grow, push");
   112      }
   113      
   114      /** Adds listeners to the components. */
   115      private void addListeners() {
                 /* 
    P/P           *  Method: void addListeners()
                  * 
                  *  Preconditions:
                  *    this.target != null
                  */
   116          target.addActionListener(this);
   117      }
   118      
   119      /** Loads the perform actions. */
   120      private void loadPerforms() {
                 /* 
    P/P           *  Method: void loadPerforms()
                  * 
                  *  Preconditions:
                  *    this.server != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.Action:getResponse(...).length@127 <= 232-1
                  *    com.dmdirc.actions.Action:getResponse(...).length@133 <= 232-1
                  *    com.dmdirc.actions.Action:getResponse(...)@127 != null
                  *    com.dmdirc.actions.Action:getResponse(...)@133 != null
                  *    com.dmdirc.actions.wrappers.PerformWrapper:getPerformWrapper(...)@121 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    init'ed(this.networkAction)
                  *    this.networkPerform != null
                  *    init'ed(this.serverAction)
                  *    this.serverPerform != null
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.actions.wrappers.PerformWrapper:getActionForNetwork(...)@122: Inverse{null}, Addr_Set{null}
                  *    com.dmdirc.actions.wrappers.PerformWrapper:getActionForServer(...)@121: Inverse{null}, Addr_Set{null}
                  */
   121          serverAction = PerformWrapper.getPerformWrapper().getActionForServer(server.getName());
   122          networkAction = PerformWrapper.getPerformWrapper().getActionForNetwork(server.getNetwork());
   123          
   124          if (serverAction == null) {
   125              serverPerform = "";
   126          } else {
   127              serverPerform = implode(serverAction.getResponse());
   128          }
   129          
   130          if (networkAction == null) {
   131              networkPerform = "";
   132          } else {
   133              networkPerform = implode(networkAction.getResponse());
   134          }
   135      }
   136      
   137      /**
   138       * Implodes the specified string array, joining each line with a LF.
   139       *
   140       * @param lines The lines to be joined together
   141       * @return A string containing each element of lines, separated by a LF.
   142       */
   143      private String implode(final String[] lines) {
                 /* 
    P/P           *  Method: String implode(String[])
                  * 
                  *  Preconditions:
                  *    lines != null
                  *    lines.length <= 232-1
                  *    (soft) init'ed(lines[...])
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
   144          final StringBuilder res = new StringBuilder();
   145          
   146          for (String line : lines) {
   147              res.append('\n');
   148              res.append(line);
   149          }
   150          
   151          return res.length() == 0 ? "" : res.substring(1);
   152      }
   153      
   154      /** Populates the perform text area. */
   155      private void populatePerform() {
                 /* 
    P/P           *  Method: void populatePerform()
                  * 
                  *  Preconditions:
                  *    this.target != null
                  *    this.textarea != null
                  *    (soft) init'ed(this.networkPerform)
                  *    (soft) init'ed(this.serverPerform)
                  * 
                  *  Postconditions:
                  *    init'ed(this.isNetworkPerform)
                  * 
                  *  Test Vectors:
                  *    javax.swing.JComboBox:getSelectedIndex(...)@156: {-231..-1, 1..232-1}, {0}
                  */
   156          if (target.getSelectedIndex() == 0) {
   157              isNetworkPerform = true;
   158              textarea.setText(networkPerform);
   159          } else {
   160              isNetworkPerform = false;
   161              textarea.setText(serverPerform);
   162          }
   163      }
   164      
   165      /** Stores the text currently in the textarea into the perform strings. */
   166      private void storeText() {
                 /* 
    P/P           *  Method: void storeText()
                  * 
                  *  Preconditions:
                  *    init'ed(this.isNetworkPerform)
                  *    this.textarea != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.networkPerform)
                  *    possibly_updated(this.serverPerform)
                  * 
                  *  Test Vectors:
                  *    this.isNetworkPerform: {0}, {1}
                  */
   167          if (isNetworkPerform) {
   168              networkPerform = textarea.getText();
   169          } else {
   170              serverPerform = textarea.getText();
   171          }
   172      }
   173      
   174      /** Saves the performs. */
   175      public void savePerforms() {
                 /* 
    P/P           *  Method: void savePerforms()
                  * 
                  *  Preconditions:
                  *    init'ed(this.networkAction)
                  *    init'ed(this.networkPerform)
                  *    init'ed(this.serverAction)
                  *    init'ed(this.serverPerform)
                  *    init'ed(this.isNetworkPerform)
                  *    this.textarea != null
                  *    (soft) this.server != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.wrappers.PerformWrapper:createActionForNetwork(...)@188 != null
                  *    com.dmdirc.actions.wrappers.PerformWrapper:createActionForServer(...)@180 != null
                  *    com.dmdirc.actions.wrappers.PerformWrapper:getPerformWrapper(...)@180 != null
                  *    com.dmdirc.actions.wrappers.PerformWrapper:getPerformWrapper(...)@188 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.networkAction)
                  *    this.networkPerform != null
                  *    init'ed(this.serverAction)
                  *    this.serverPerform != null
                  * 
                  *  Test Vectors:
                  *    this.networkAction: Addr_Set{null}, Inverse{null}
                  *    this.serverAction: Addr_Set{null}, Inverse{null}
                  */
   176          storeText();
   177          
   178          if (!serverPerform.isEmpty() || serverAction != null) {
   179              if (serverAction == null) {
   180                  serverAction = PerformWrapper.getPerformWrapper().createActionForServer(server.getName());
   181              }
   182              serverAction.setResponse(serverPerform.split("\n"));
   183              serverAction.save();
   184          }
   185          
   186          if (!networkPerform.isEmpty() || networkAction != null) {
   187              if (networkAction == null) {
   188                  networkAction = PerformWrapper.getPerformWrapper().createActionForNetwork(server.getNetwork());
   189              }
   190              networkAction.setResponse(networkPerform.split("\n"));
   191              networkAction.save();
   192          }
   193      }
   194      
   195      /** {@inheritDoc} */
   196      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    init'ed(this.isNetworkPerform)
                  *    this.target != null
                  *    this.textarea != null
                  *    (soft) init'ed(this.networkPerform)
                  *    (soft) init'ed(this.serverPerform)
                  * 
                  *  Postconditions:
                  *    init'ed(this.isNetworkPerform)
                  *    init'ed(this.networkPerform)
                  *    init'ed(this.serverPerform)
                  */
   197          storeText();
   198          populatePerform();
   199      }
   200      
   201  }








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