File Source: UserModesPane.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.serversetting.UserModesPane__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.addons.ui_swing.UIUtilities;
    27  import com.dmdirc.parser.irc.IRCParser;
    28  
    29  import java.awt.Insets;
    30  import java.util.Hashtable;
    31  import java.util.Map;
    32  
    33  import javax.swing.BorderFactory;
    34  import javax.swing.JCheckBox;
    35  import javax.swing.JPanel;
    36  
    37  import net.miginfocom.swing.MigLayout;
    38  
    39  /** User mode panel. */
    40  public final class UserModesPane extends JPanel {
    41  
    42      /**
    43       * A version number for this class. It should be changed whenever the class
    44       * structure is changed (or anything else that would prevent serialized
    45       * objects being unserialized with the new class).
    46       */
    47      private static final long serialVersionUID = 1;
    48      /** Parent server. */
    49      private final Server server;
    50      /** The checkboxes used for user modes. */
    51      private Map<String, JCheckBox> modeCheckBoxes;
    52  
    53      /**
    54       * Creates a new instance of UserModesPane.
    55       *
    56       * @param server Parent server
    57       */
    58      public UserModesPane(final Server server) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.serversetting.UserModesPane(Server)
                  * 
                  *  Preconditions:
                  *    server != null
                  * 
                  *  Postconditions:
                  *    this.modeCheckBoxes == &amp;new Hashtable(initModesPanel#1)
                  *    this.server == server
                  *    this.server != null
                  *    new Hashtable(initModesPanel#1) num objects == 1
                  */
    59          super();
    60  
    61          this.server = server;
    62  
    63          this.setOpaque(UIUtilities.getTabbedPaneOpaque());
    64          initModesPanel();
    65          layoutComponents();
    66  
    67          setVisible(true);
    68      }
    69  
    70      /** Updates the panel. */
    71      public void update() {
                 /* 
    P/P           *  Method: void update()
                  * 
                  *  Preconditions:
                  *    this.server != null
                  * 
                  *  Postconditions:
                  *    this.modeCheckBoxes == &amp;new Hashtable(initModesPanel#1)
                  *    new Hashtable(initModesPanel#1) num objects == 1
                  */
    72          setVisible(false);
    73          removeAll();
    74          initModesPanel();
    75          setVisible(true);
    76      }
    77  
    78      /** Initialises the modes panel. */
    79      private void initModesPanel() {
                 /* 
    P/P           *  Method: void initModesPanel()
                  * 
                  *  Preconditions:
                  *    this.server != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Server:getConfigManager(...)@101 != null
                  *    com.dmdirc.Server:getConfigManager(...)@107 != null
                  *    com.dmdirc.Server:getConfigManager(...)@108 != null
                  *    com.dmdirc.Server:getConfigManager(...)@99 != null
                  *    com.dmdirc.Server:getParser(...)@80 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.modeCheckBoxes == &amp;new Hashtable(initModesPanel#1)
                  *    new Hashtable(initModesPanel#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.config.ConfigManager:getOptionBool(...)@99: {0}, {1}
                  *    com.dmdirc.config.ConfigManager:hasOptionString(...)@107: {0}, {1}
                  *    com.dmdirc.config.ConfigManager:hasOptionString(...)@99: {0}, {1}
                  */
    80          final IRCParser parser = server.getParser();
    81  
    82          final String userModes = parser.getUserModeString();
    83          final String ourUserModes = parser.getMyself().getUserModeStr();
    84  
    85          modeCheckBoxes =
    86                  new Hashtable<String, JCheckBox>();
    87  
    88          final boolean opaque = UIUtilities.getTabbedPaneOpaque();
    89  
    90          // Lay out all the boolean mode checkboxes
    91          for (int i = 0; i < userModes.length();
    92                  i++) {
    93              final String mode = userModes.substring(i, i + 1);
    94              final boolean state =
    95                      ourUserModes.split(" ")[0].contains(mode.subSequence(0, 1));
    96              String text;
    97              String tooltip;
    98  
    99              if (server.getConfigManager().getOptionBool("server", "friendlymodes") &&
   100                      server.getConfigManager().hasOptionString("server", "umode" + mode)) {
   101                  text =  server.getConfigManager().
   102                          getOption("server", "umode" + mode);
   103              } else {
   104                  text = "Mode " + mode;
   105              }
   106  
   107              if (server.getConfigManager().hasOptionString("server", "umode" + mode)) {
   108                  tooltip = "Mode " + mode + ": " + server.getConfigManager().
   109                          getOption("server", "umode" + mode);
   110              } else {
   111                  tooltip = "Mode " + mode + ": Unknown";
   112              }
   113  
   114              final JCheckBox checkBox = new JCheckBox(text, state);
   115              checkBox.setMargin(new Insets(0, 0, 0, 0));
   116              checkBox.setToolTipText(tooltip);
   117              checkBox.setOpaque(opaque);
   118  
   119              modeCheckBoxes.put(mode, checkBox);
   120          }
   121      }
   122  
   123      /** Lays out the components. */
   124      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    this.modeCheckBoxes != null
                  * 
                  *  Presumptions:
                  *    java.util.Map:values(...)@127 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@127: {0}, {1}
                  */
   125          final JPanel userModes =
   126                  new JPanel(new MigLayout("wrap 2, fillx"));
   127          for (JCheckBox checkBox : modeCheckBoxes.values()) {
   128              userModes.add(checkBox);
   129          }
   130  
   131          userModes.setBorder(BorderFactory.createTitledBorder("User modes"));
   132          userModes.setOpaque(UIUtilities.getTabbedPaneOpaque());
   133          
   134          setLayout(new MigLayout("flowy, fillx", "fill", ""));
   135          add(userModes);
   136      }
   137  
   138      /**
   139       * Sends changed modes to the server.
   140       */
   141      public void save() {
                 /* 
    P/P           *  Method: void save()
                  * 
                  *  Preconditions:
                  *    (soft) this.modeCheckBoxes != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Server:getParser(...)@146 != null
                  *    com.dmdirc.Server:getParser(...)@159 != null
                  *    com.dmdirc.Server:getParser(...)@165 != null
                  *    com.dmdirc.parser.irc.ClientInfo:getUserModeStr(...)@148 != null
                  *    com.dmdirc.parser.irc.IRCParser:getMyself(...)@148 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    this.server: Addr_Set{null}, Inverse{null}
                  *    com.dmdirc.Server:getParser(...)@142: Inverse{null}, Addr_Set{null}
                  *    java.util.Map:get(...)@156: Addr_Set{null}, Inverse{null}
                  */
   142          if (server == null || server.getParser() == null) {
   143              return;
   144          }
   145          boolean changed = false;
   146          final IRCParser parser = server.getParser();
   147          final String userModes = parser.getUserModeString();
   148          final String ourUserModes = parser.getMyself().getUserModeStr();
   149  
   150          for (int i = 0; i < userModes.length();
   151                  i++) {
   152              final String mode = userModes.substring(i, i + 1);
   153              final boolean state =
   154                      ourUserModes.split(" ")[0].contains(mode.subSequence(0, 1));
   155  
   156              if (modeCheckBoxes.get(mode) != null &&
   157                      state != modeCheckBoxes.get(mode).isSelected()) {
   158                  changed = true;
   159                  server.getParser().getMyself().
   160                          alterMode(modeCheckBoxes.get(mode).isSelected(),
   161                          mode.toCharArray()[0]);
   162              }
   163          }
   164          if (changed) {
   165              server.getParser().getMyself().sendModes();
   166          }
   167      }
   168  }








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