File Source: ChannelModesPane.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelModesPane__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.channelsetting;
    24  
    25  import com.dmdirc.Channel;
    26  import com.dmdirc.parser.irc.IRCParser;
    27  import com.dmdirc.addons.ui_swing.components.ParamModePanel;
    28  import com.dmdirc.addons.ui_swing.UIUtilities;
    29  
    30  import java.awt.Insets;
    31  import java.util.Hashtable;
    32  import java.util.Map;
    33  import javax.swing.BorderFactory;
    34  
    35  import javax.swing.JCheckBox;
    36  import javax.swing.JPanel;
    37  
    38  import net.miginfocom.swing.MigLayout;
    39  
    40  /** Non list mode panel. */
    41  public final class ChannelModesPane extends JPanel {
    42  
    43      /**
    44       * A version number for this class. It should be changed whenever the class
    45       * structure is changed (or anything else that would prevent serialized
    46       * objects being unserialized with the new class).
    47       */
    48      private static final long serialVersionUID = 1;
    49      /** Parent channel. */
    50      private final Channel channel;
    51      /** The checkboxes used for boolean modes. */
    52      private Map<String, JCheckBox> modeCheckBoxes;
    53      /** The ParamModePanels used for parameter-requiring modes. */
    54      private Map<String, ParamModePanel> modeInputs;
    55  
    56      /**
    57       * Creates a new instance of ChannelModesPane.
    58       *
    59       * @param channel Parent channel
    60       */
    61      public ChannelModesPane(final Channel channel) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelModesPane(Channel)
                  * 
                  *  Preconditions:
                  *    channel != null
                  * 
                  *  Postconditions:
                  *    this.channel == channel
                  *    this.channel != null
                  *    this.modeCheckBoxes == &amp;new Hashtable(initModesPanel#2)
                  *    this.modeInputs == &amp;new Hashtable(initModesPanel#14)
                  *    new Hashtable(initModesPanel#14) num objects == 1
                  *    new Hashtable(initModesPanel#2) num objects == 1
                  */
    62          super();
    63  
    64          this.channel = channel;
    65  
    66          this.setOpaque(UIUtilities.getTabbedPaneOpaque());
    67          initModesPanel();
    68          layoutComponents();
    69  
    70          setVisible(true);
    71      }
    72  
    73      /** Updates the panel. */
    74      public void update() {
                 /* 
    P/P           *  Method: void update()
                  * 
                  *  Preconditions:
                  *    this.channel != null
                  * 
                  *  Postconditions:
                  *    this.modeCheckBoxes == &amp;new Hashtable(initModesPanel#2)
                  *    this.modeInputs == &amp;new Hashtable(initModesPanel#14)
                  *    new Hashtable(initModesPanel#14) num objects == 1
                  *    new Hashtable(initModesPanel#2) num objects == 1
                  */
    75          setVisible(false);
    76          removeAll();
    77          initModesPanel();
    78          setVisible(true);
    79      }
    80  
    81      /** Initialises the modes panel. */
    82      private void initModesPanel() {
                 /* 
    P/P           *  Method: void initModesPanel()
                  * 
                  *  Preconditions:
                  *    this.channel != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Channel:getChannelInfo(...)@144 != null
                  *    com.dmdirc.Channel:getChannelInfo(...)@86 != null
                  *    com.dmdirc.Channel:getConfigManager(...)@105 != null
                  *    com.dmdirc.Channel:getConfigManager(...)@107 != null
                  *    com.dmdirc.Channel:getConfigManager(...)@113 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.modeCheckBoxes == &amp;new Hashtable(initModesPanel#2)
                  *    this.modeInputs == &amp;new Hashtable(initModesPanel#14)
                  *    new Hashtable(initModesPanel#14) num objects == 1
                  *    new Hashtable(initModesPanel#2) num objects == 1
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.config.ConfigManager:getOptionBool(...)@105: {0}, {1}
                  *    com.dmdirc.config.ConfigManager:getOptionBool(...)@128: {0}, {1}
                  *    com.dmdirc.config.ConfigManager:hasOptionString(...)@105: {0}, {1}
                  *    com.dmdirc.config.ConfigManager:hasOptionString(...)@113: {0}, {1}
                  *    com.dmdirc.config.ConfigManager:hasOptionString(...)@128: {0}, {1}
                  *    com.dmdirc.parser.irc.IRCParser:isUserSettable(...)@132: {1}, {0}
                  */
    83          final IRCParser parser = channel.getServer().getParser();
    84  
    85          final String booleanModes = parser.getBoolChanModes();
    86          final String ourBooleanModes = channel.getChannelInfo().getModeStr();
    87          final String paramModes =
    88                  parser.getSetOnlyChanModes() + parser.getSetUnsetChanModes();
    89  
    90          modeCheckBoxes =
    91                  new Hashtable<String, JCheckBox>();
    92  
    93          // Lay out all the boolean mode checkboxes
    94          for (int i = 0; i < booleanModes.length();
    95                  i++) {
    96              final String mode = booleanModes.substring(i, i + 1);
    97              final char modeChar = mode.toCharArray()[0];
    98              final boolean state =
    99                      ourBooleanModes.split(" ")[0].contains(mode.subSequence(0, 1));
   100              String text;
   101              String tooltip;
   102              
   103              final boolean opaque = UIUtilities.getTabbedPaneOpaque();
   104  
   105              if (channel.getConfigManager().getOptionBool("server", "friendlymodes") &&
   106                      channel.getConfigManager().hasOptionString("server", "mode" + mode)) {
   107                  text =  channel.getConfigManager().
   108                          getOption("server", "mode" + mode);
   109              } else {
   110                  text = "Mode " + mode;
   111              }
   112  
   113              if (channel.getConfigManager().hasOptionString("server", "mode" + mode)) {
   114                  tooltip =
   115                          "Mode " + mode + ": " +
   116                          channel.getConfigManager().
   117                          getOption("server", "mode" + mode);
   118              } else {
   119                  tooltip = "Mode " + mode + ": Unknown";
   120              }
   121  
   122              final JCheckBox checkBox = new JCheckBox(text, state);
   123              checkBox.setMargin(new Insets(0, 0, 0, 0));
   124              checkBox.setToolTipText(tooltip);
   125              checkBox.setOpaque(opaque);
   126  
   127              modeCheckBoxes.put(mode, checkBox);
   128              if (!channel.getConfigManager().hasOptionString("server", "enablemode" + modeChar)
   129                      || channel.getConfigManager().getOptionBool("server",
   130                      "enablemode" + modeChar)) {
   131                  checkBox.setEnabled(true);
   132              } else if (!channel.getServer().getParser().isUserSettable(modeChar)) {
   133                  checkBox.setEnabled(false);
   134              }
   135          }
   136  
   137          // Lay out all the parameter-requiring modes
   138          modeInputs =
   139                  new Hashtable<String, ParamModePanel>();
   140  
   141          for (int i = 0; i < paramModes.length();
   142                  i++) {
   143              final String mode = paramModes.substring(i, i + 1);
   144              final String value =
   145                      channel.getChannelInfo().getModeParam(mode.charAt(0));
   146              final boolean state =
   147                      ourBooleanModes.split(" ")[0].contains(mode.subSequence(0, 1));
   148  
   149              final ParamModePanel panel =
   150                      new ParamModePanel(mode, state, value,
   151                      channel.getConfigManager());
   152  
   153              modeInputs.put(mode, panel);
   154          }
   155      }
   156  
   157      /** Lays out the components. */
   158      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    this.modeCheckBoxes != null
                  *    this.modeInputs != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@167 != null
                  *    java.util.Map:values(...)@161 != null
                  *    java.util.Map:values(...)@167 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@161: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@167: {0}, {1}
                  */
   159          final JPanel booleanModes =
   160                  new JPanel(new MigLayout("wrap 2, fillx"));
   161          for (JCheckBox checkBox : modeCheckBoxes.values()) {
   162              booleanModes.add(checkBox);
   163          }
   164  
   165          final JPanel paramModes =
   166                  new JPanel(new MigLayout("wrap 2, fillx"));
   167          for (ParamModePanel modePanel : modeInputs.values()) {
   168              paramModes.add(modePanel.getCheckboxComponent());
   169              paramModes.add(modePanel.getValueComponent(), "growx, pushx");
   170          }
   171  
   172          booleanModes.setBorder(BorderFactory.createTitledBorder("Boolean modes"));
   173          paramModes.setBorder(BorderFactory.createTitledBorder("Parameter modes"));
   174          
   175          booleanModes.setOpaque(UIUtilities.getTabbedPaneOpaque());
   176          paramModes.setOpaque(UIUtilities.getTabbedPaneOpaque());
   177          
   178          setLayout(new MigLayout("flowy, fillx", "fill", ""));
   179          add(booleanModes);
   180          add(paramModes);
   181      }
   182  
   183      /**
   184       * Processes the channel settings dialog and constructs a mode string for
   185       * changed modes, then sends this to the server.
   186       */
   187      public void setChangedBooleanModes() {
                 /* 
    P/P           *  Method: void setChangedBooleanModes()
                  * 
                  *  Preconditions:
                  *    this.channel != null
                  *    (soft) this.modeCheckBoxes != null
                  *    (soft) this.modeInputs != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Channel:getChannelInfo(...)@191 != null
                  *    com.dmdirc.Channel:getChannelInfo(...)@204 != null
                  *    com.dmdirc.Channel:getChannelInfo(...)@213 != null
                  *    com.dmdirc.Channel:getChannelInfo(...)@222 != null
                  *    com.dmdirc.Channel:getChannelInfo(...)@228 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@219: {1}, {0}
                  *    java.util.Map:get(...)@201: Addr_Set{null}, Inverse{null}
                  */
   188          boolean changed = false;
   189          final IRCParser parser = channel.getServer().getParser();
   190          final String booleanModes = parser.getBoolChanModes();
   191          final String ourBooleanModes = channel.getChannelInfo().getModeStr();
   192          final String paramModes =
   193                  parser.getSetOnlyChanModes() + parser.getSetUnsetChanModes();
   194  
   195          for (int i = 0; i < booleanModes.length();
   196                  i++) {
   197              final String mode = booleanModes.substring(i, i + 1);
   198              final boolean state =
   199                      ourBooleanModes.split(" ")[0].contains(mode.subSequence(0, 1));
   200  
   201              if (modeCheckBoxes.get(mode) != null &&
   202                      state != modeCheckBoxes.get(mode).isSelected()) {
   203                  changed = true;
   204                  channel.getChannelInfo().
   205                          alterMode(modeCheckBoxes.get(mode).isSelected(),
   206                          mode.toCharArray()[0], "");
   207              }
   208          }
   209  
   210          for (int i = 0; i < paramModes.length();
   211                  i++) {
   212              final String mode = paramModes.substring(i, i + 1);
   213              final String value =
   214                      channel.getChannelInfo().getModeParam(mode.charAt(0));
   215              final boolean state =
   216                      ourBooleanModes.split(" ")[0].contains(mode.subSequence(0, 1));
   217              final ParamModePanel paramModePanel = modeInputs.get(mode);
   218  
   219              if (state != paramModePanel.getState() ||
   220                      !value.equals(paramModePanel.getValue())) {
   221                  changed = true;
   222                      channel.getChannelInfo().
   223                              alterMode(paramModePanel.getState(),
   224                              mode.toCharArray()[0], paramModePanel.getValue());
   225              }
   226          }
   227          if (changed) {
   228              channel.getChannelInfo().sendModes();
   229          }
   230      }
   231  }








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