File Source: TopicPane.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.channelsetting.TopicPane__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.Topic;
    27  import com.dmdirc.logger.ErrorLevel;
    28  import com.dmdirc.logger.Logger;
    29  import com.dmdirc.addons.ui_swing.UIUtilities;
    30  import com.dmdirc.addons.ui_swing.actions.NoNewlinesPasteAction;
    31  import com.dmdirc.addons.ui_swing.components.SwingInputHandler;
    32  import com.dmdirc.addons.ui_swing.components.TextAreaInputField;
    33  import com.dmdirc.addons.ui_swing.components.text.TextLabel;
    34  
    35  import java.awt.Color;
    36  import java.awt.event.ActionEvent;
    37  import java.awt.event.ActionListener;
    38  import java.awt.event.KeyEvent;
    39  import java.util.Collections;
    40  import java.util.Date;
    41  import java.util.List;
    42  import java.util.Map;
    43  
    44  import javax.swing.AbstractAction;
    45  import javax.swing.DefaultComboBoxModel;
    46  import javax.swing.JComboBox;
    47  import javax.swing.JLabel;
    48  import javax.swing.JPanel;
    49  import javax.swing.JScrollPane;
    50  import javax.swing.KeyStroke;
    51  import javax.swing.event.DocumentEvent;
    52  import javax.swing.event.DocumentListener;
    53  
    54  import net.miginfocom.swing.MigLayout;
    55  
    56  /** Topic panel. */
         /* 
    P/P   *  Method: ChannelSettingsDialog access$100(TopicPane)
          * 
          *  Preconditions:
          *    x0 != null
          * 
          *  Postconditions:
          *    return_value == x0.parent
          *    init'ed(return_value)
          */
    57  public final class TopicPane extends JPanel implements DocumentListener,
    58          ActionListener {
    59  
    60      /**
    61       * A version number for this class. It should be changed whenever the class
    62       * structure is changed (or anything else that would prevent serialized
    63       * objects being unserialized with the new class).
    64       */
    65      private static final long serialVersionUID = 2;
    66      /** Parent channel. */
    67      private final Channel channel;
    68      /** Parent dialog. */
    69      private final ChannelSettingsDialog parent;
    70      /** the maximum length allowed for a topic. */
    71      private int topicLengthMax;
    72      /** label showing the number of characters left in a topic.*/
    73      private JLabel topicLengthLabel;
    74      /** Topic text entry text area. */
    75      private TextAreaInputField topicText;
    76      /** Topic who. */
    77      private TextLabel topicWho;
    78      /** Topic history. */
    79      private JComboBox topicHistory;
    80  
    81      /**
    82       * Creates a new instance of TopicModesPane.
    83       *
    84       * @param channel Parent channel
    85       * @param parent Parent dialog
    86       */
    87      public TopicPane(final Channel channel,
    88              final ChannelSettingsDialog parent) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.channelsetting.TopicPane(Channel, ChannelSettingsDialog)
                  * 
                  *  Preconditions:
                  *    channel != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Channel:getServer(...)@95 != null
                  *    com.dmdirc.Server:getParser(...)@95 != null
                  *    init'ed(com.dmdirc.logger.ErrorLevel.LOW)
                  *    com.dmdirc.parser.irc.IRCParser:get005(...)@95 != null
                  * 
                  *  Postconditions:
                  *    this.channel == channel
                  *    this.channel != null
                  *    this.parent == parent
                  *    init'ed(this.parent)
                  *    this.topicHistory == &new JComboBox(initTopicsPanel#3)
                  *    this.topicLengthLabel == &new JLabel(initTopicsPanel#1)
                  *    init'ed(this.topicLengthMax)
                  *    this.topicText == &new TextAreaInputField(initTopicsPanel#2)
                  *    this.topicWho == &new TextLabel(initTopicsPanel#5)
                  *    new JComboBox(initTopicsPanel#3) num objects == 1
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.Map:containsKey(...)@97: {0}, {1}
                  */
    89          super();
    90  
    91          this.setOpaque(UIUtilities.getTabbedPaneOpaque());
    92          this.channel = channel;
    93          this.parent = parent;
    94  
    95          final Map<String, String> iSupport =
    96                  channel.getServer().getParser().get005();
    97          if (iSupport.containsKey("TOPICLEN")) {
    98              try {
    99                  topicLengthMax =
   100                          Integer.parseInt(iSupport.get("TOPICLEN"));
   101              } catch (NumberFormatException ex) {
   102                  topicLengthMax = 0;
   103                  Logger.userError(ErrorLevel.LOW,
   104                          "IRCD doesnt supply topic length");
   105              }
   106          }
   107  
   108          update();
   109      }
   110  
   111      /** Updates the panel. */
   112      public void update() {
                 /* 
    P/P           *  Method: void update()
                  * 
                  *  Preconditions:
                  *    this.channel != null
                  *    init'ed(this.topicLengthMax)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.TextAreaInputField:getDocument(...)@119 != null
                  * 
                  *  Postconditions:
                  *    this.topicHistory == &amp;new JComboBox(initTopicsPanel#3)
                  *    this.topicLengthLabel == &amp;new JLabel(initTopicsPanel#1)
                  *    this.topicText == &amp;new TextAreaInputField(initTopicsPanel#2)
                  *    this.topicWho == &amp;new TextLabel(initTopicsPanel#5)
                  *    new JComboBox(initTopicsPanel#3) num objects == 1
                  *    new JLabel(initTopicsPanel#1) num objects == 1
                  *    new SimpleAttributeSet(TextLabel#6) num objects == 1
                  *    new TextAreaInputField(initTopicsPanel#2) num objects == 1
                  *    new TextLabel(initTopicsPanel#5) num objects == 1
                  *    this.topicWho.sas == &amp;new SimpleAttributeSet(TextLabel#6)
                  */
   113          setVisible(false);
   114  
   115          removeAll();
   116          initTopicsPanel();
   117          layoutComponents();
   118  
   119          topicText.getDocument().addDocumentListener(this);
   120          topicHistory.addActionListener(this);
   121  
   122          setVisible(true);
   123      }
   124  
   125      /** Initialises the topic panel. */
   126      private void initTopicsPanel() {
                 /* 
    P/P           *  Method: void initTopicsPanel()
                  * 
                  *  Preconditions:
                  *    this.channel != null
                  *    init'ed(this.topicLengthMax)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Channel:getChannelInfo(...)@140 != null
                  *    com.dmdirc.Channel:getFrame(...)@145 != null
                  *    com.dmdirc.Channel:getTopics(...)@127 != null
                  *    com.dmdirc.addons.ui_swing.components.TextAreaInputField:getActionMap(...)@148 != null
                  *    com.dmdirc.addons.ui_swing.components.TextAreaInputField:getInputMap(...)@151 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.topicHistory == &amp;new JComboBox(initTopicsPanel#3)
                  *    this.topicLengthLabel == &amp;new JLabel(initTopicsPanel#1)
                  *    this.topicText == &amp;new TextAreaInputField(initTopicsPanel#2)
                  *    this.topicWho == &amp;new TextLabel(initTopicsPanel#5)
                  *    new JComboBox(initTopicsPanel#3) num objects == 1
                  *    new JLabel(initTopicsPanel#1) num objects == 1
                  *    new SimpleAttributeSet(TextLabel#6) num objects == 1
                  *    new TextAreaInputField(initTopicsPanel#2) num objects == 1
                  *    new TextLabel(initTopicsPanel#5) num objects == 1
                  *    this.topicWho.sas == &amp;new SimpleAttributeSet(TextLabel#6)
                  * 
                  *  Test Vectors:
                  *    javax.swing.ComboBoxModel:getSize(...)@136: {-231..-1, 1..232-1}, {0}
                  */
   127          final List<Topic> topics = channel.getTopics();
   128          Collections.reverse(topics);
   129          topicLengthLabel = new JLabel();
   130          topicText = new TextAreaInputField(100, 4);
   131          topicHistory =
   132                  new JComboBox(new DefaultComboBoxModel(topics.toArray()));
   133          topicHistory.setPrototypeDisplayValue("This is a substantial prototype value");
   134          topicWho = new TextLabel();
   135  
   136          if (topicHistory.getModel().getSize() == 0) {
   137              topicHistory.setEnabled(false);
   138          }
   139  
   140          topicText.setText(channel.getChannelInfo().getTopic());
   141          topicText.setLineWrap(true);
   142          topicText.setWrapStyleWord(true);
   143          topicText.setRows(5);
   144          topicText.setColumns(30);
   145          new SwingInputHandler(topicText, channel.getFrame().getCommandParser(), 
   146                  channel.getFrame()).setTypes(false, false, true, false);
   147  
   148          topicText.getActionMap().
   149                  put("paste-from-clipboard",
   150                  new NoNewlinesPasteAction());
   151          topicText.getInputMap().
   152                  put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
   153                  new EnterAction());
   154          topicText.getInputMap().
   155                  put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, UIUtilities.getCtrlDownMask()),
   156                  new EnterAction());
   157          
   158          UIUtilities.addUndoManager(topicText);
   159  
   160          topicChanged();
   161          actionPerformed(null);
   162      }
   163  
   164      /** Lays out the components. */
   165      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.topicHistory)
                  *    init'ed(this.topicLengthLabel)
                  *    init'ed(this.topicText)
                  *    init'ed(this.topicWho)
                  */
   166          setLayout(new MigLayout("wrap 1, fill, wmax 450"));
   167  
   168          add(topicHistory, "growx, pushx");
   169          add(new JScrollPane(topicText), "grow, push");
   170          add(topicLengthLabel, "pushx, growx, pushx");
   171          add(topicWho, "growx, pushx");
   172      }
   173  
   174      /** Processes the topic and changes it if necessary. */
   175      protected void setChangedTopic() {
                 /* 
    P/P           *  Method: void setChangedTopic()
                  * 
                  *  Preconditions:
                  *    this.channel != null
                  *    this.topicText != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Channel:getChannelInfo(...)@176 != null
                  *    com.dmdirc.parser.irc.ChannelInfo:getTopic(...)@176 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@176: {1}, {0}
                  */
   176          if (!channel.getChannelInfo().getTopic().equals(topicText.getText())) {
   177              channel.setTopic(topicText.getText());
   178          }
   179      }
   180  
   181      /** Handles the topic change. */
   182      private void topicChanged() {
                 /* 
    P/P           *  Method: void topicChanged()
                  * 
                  *  Preconditions:
                  *    this.topicLengthLabel != null
                  *    init'ed(this.topicLengthMax)
                  *    this.topicText != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.TextAreaInputField:getText(...)@185 != null
                  *    com.dmdirc.addons.ui_swing.components.TextAreaInputField:getText(...)@187 != null
                  *    init'ed(java.awt.Color.BLACK)
                  *    init'ed(java.awt.Color.RED)
                  *    java.lang.String:length(...)@187 - this.topicLengthMax in {-232+1..231}
                  * 
                  *  Test Vectors:
                  *    this.topicLengthMax: {1..232-2}, {0}
                  *    java.lang.String:length(...)@187 - this.topicLengthMax: {1..231}, {-232+1..0}
                  */
   183          if (topicLengthMax == 0) {
   184              topicLengthLabel.setForeground(Color.BLACK);
   185              topicLengthLabel.setText(topicText.getText().length() + " characters");
   186          } else {
   187              final int charsLeft = topicLengthMax - topicText.getText().length();
   188              if (charsLeft >= 0) {
   189                  topicLengthLabel.setForeground(Color.BLACK);
   190                  topicLengthLabel.setText(charsLeft + " of " + topicLengthMax +
   191                          " available");
   192              } else {
   193                  topicLengthLabel.setForeground(Color.RED);
   194                  topicLengthLabel.setText(0 + " of " + topicLengthMax +
   195                          " available " + (-1 * charsLeft) + " too many characters");
   196              }
   197          }
   198      }
   199  
   200      /** {@inheritDoc}. */
   201      @Override
   202      public void insertUpdate(final DocumentEvent e) {
                 /* 
    P/P           *  Method: void insertUpdate(DocumentEvent)
                  * 
                  *  Preconditions:
                  *    this.topicLengthLabel != null
                  *    init'ed(this.topicLengthMax)
                  *    this.topicText != null
                  */
   203          topicChanged();
   204      }
   205  
   206      /** {@inheritDoc}. */
   207      @Override
   208      public void removeUpdate(final DocumentEvent e) {
                 /* 
    P/P           *  Method: void removeUpdate(DocumentEvent)
                  * 
                  *  Preconditions:
                  *    this.topicLengthLabel != null
                  *    init'ed(this.topicLengthMax)
                  *    this.topicText != null
                  */
   209          topicChanged();
   210      }
   211  
   212      /** {@inheritDoc}. */
   213      @Override
   214      public void changedUpdate(final DocumentEvent e) {
   215          //Ignore
             /* 
    P/P       *  Method: void changedUpdate(DocumentEvent)
              */
   216      }
   217  
   218      /** 
   219       * {@inheritDoc}. 
   220       * 
   221       * @param e Action event
   222       */
   223      @Override
   224      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    this.topicHistory != null
                  *    this.topicWho != null
                  *    (soft) this.topicText != null
                  *    (soft) init'ed(this.topicWho.sas)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Topic:getTime(...)@229 in {-9_223_372_036_854_775..18_446_744_073_709_551}
                  * 
                  *  Test Vectors:
                  *    javax.swing.JComboBox:getSelectedItem(...)@225: Inverse{null}, Addr_Set{null}
                  */
   225          final Topic topic = (Topic) topicHistory.getSelectedItem();
   226          if (topic == null) {
   227              topicWho.setText("No topic set.");
   228          } else {
   229              topicWho.setText("Set by " + topic.getClient() + "\n on " +
   230                      new Date(1000 * topic.getTime()));
   231              topicText.setText(topic.getTopic());
   232          }
   233      }
   234  
   235  /** Closes and saves the topic when enter is pressed. */
             /* 
    P/P       *  Method: void com.dmdirc.addons.ui_swing.dialogs.channelsetting.TopicPane$EnterAction(TopicPane, TopicPane$1)
              */
   236      private class EnterAction extends AbstractAction {
   237  
   238          /**
   239           * A version number for this class. It should be changed whenever the class
   240           * structure is changed (or anything else that would prevent serialized
   241           * objects being unserialized with the new class).
   242           */
   243          private static final long serialVersionUID = 1;
   244  
   245          /** 
   246           * {@inheritDoc}
   247           * 
   248           * @param e Action event
   249           */
   250          @Override
   251          public void actionPerformed(final ActionEvent e) {
                     /* 
    P/P               *  Method: void actionPerformed(ActionEvent)
                      * 
                      *  Preconditions:
                      *    init'ed(com/dmdirc/addons/ui_swing/dialogs/channelsetting/ChannelSettingsDialog.me)
                      *    this.parent != null
                      *    this.parent.channelListModesPane != null
                      *    this.parent.channelListModesPane.channel != null
                      *    this.parent.channelListModesPane.listModesArray != null
                      *    this.parent.channelListModesPane.listModesArray.length <= 232-1
                      *    this.parent.channelListModesPane.toggle != null
                      *    this.parent.channelModesPane != null
                      *    this.parent.channelModesPane.channel != null
                      *    this.parent.channelSettingsPane != null
                      *    ...
                      * 
                      *  Postconditions:
                      *    com/dmdirc/addons/ui_swing/dialogs/channelsetting/ChannelSettingsDialog.me == null
                      */
   252              parent.save();
   253          }
   254      }
   255  }








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