File Source: InviteLabel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.statusbar.InviteLabel__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2008 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.components.statusbar;
    24  
    25  import com.dmdirc.FrameContainer;
    26  import com.dmdirc.ui.IconManager;
    27  import com.dmdirc.Invite;
    28  import com.dmdirc.Server;
    29  import com.dmdirc.ServerManager;
    30  import com.dmdirc.actions.ActionManager;
    31  import com.dmdirc.actions.interfaces.ActionType;
    32  import com.dmdirc.actions.CoreActionType;
    33  import com.dmdirc.addons.ui_swing.MainFrame;
    34  import com.dmdirc.interfaces.ActionListener;
    35  import com.dmdirc.interfaces.InviteListener;
    36  import com.dmdirc.ui.interfaces.StatusBarComponent;
    37  import com.dmdirc.util.MapList;
    38  
    39  import java.awt.event.ActionEvent;
    40  import java.awt.event.MouseEvent;
    41  import java.util.ArrayList;
    42  import java.util.Arrays;
    43  import java.util.List;
    44  
    45  import javax.swing.AbstractAction;
    46  import javax.swing.BorderFactory;
    47  import javax.swing.JLabel;
    48  import javax.swing.JMenuItem;
    49  import javax.swing.JPopupMenu;
    50  import javax.swing.JSeparator;
    51  
    52  
    53  /**
    54   * Invite label.
    55   */
         /* 
    P/P   *  Method: MapList access$000(InviteLabel)
          * 
          *  Preconditions:
          *    x0 != null
          * 
          *  Postconditions:
          *    return_value == x0.inviteList
          *    init'ed(return_value)
          */
    56  public class InviteLabel extends StatusbarPopupPanel implements StatusBarComponent,
    57          InviteListener, ActionListener {
    58  
    59      /**
    60       * A version number for this class. It should be changed whenever the class
    61       * structure is changed (or anything else that would prevent serialized
    62       * objects being unserialized with the new class).
    63       */
    64      private static final long serialVersionUID = 1;
    65      /** Active server. */
    66      private Server activeServer;
    67      /** Invite map list. */
    68      private final MapList<Server, Invite> inviteList;
    69      /** Invite popup menu. */
    70      private final JPopupMenu menu;
    71      /** Dismiss invites menu item. */
    72      private final JMenuItem dismiss;
    73      /** Accept invites menu item. */
    74      private final JMenuItem accept;
    75      /** Parent window. */
    76      private MainFrame mainFrame;
    77      /** Active frame. */
    78      private FrameContainer activeFrame;
    79  
    80      /**
    81       * Instantiates a new invite label.
    82       * 
    83       * @param mainFrame Parent window
    84       */
    85      public InviteLabel(final MainFrame mainFrame) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.statusbar.InviteLabel(MainFrame)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.ServerManager:getServerManager(...)@125 != null
                  *    com.dmdirc.ServerManager:getServers(...)@125 != null
                  *    init'ed(com.dmdirc.actions.CoreActionType.CLIENT_FRAME_CHANGED)
                  *    init'ed(com.dmdirc.actions.CoreActionType.SERVER_CONNECTED)
                  *    init'ed(com.dmdirc.actions.CoreActionType.SERVER_CONNECTERROR)
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.accept == &amp;new JMenuItem(InviteLabel#6)
                  *    init'ed(this.activeFrame)
                  *    init'ed(this.activeServer)
                  *    init'ed(this.dialog)
                  *    this.dismiss == &amp;new JMenuItem(InviteLabel#4)
                  *    this.inviteList == &amp;new MapList(InviteLabel#2)
                  *    this.label == &amp;new JLabel(InviteLabel#1)
                  *    this.mainFrame == mainFrame
                  *    init'ed(this.mainFrame)
                  *    this.menu == &amp;new JPopupMenu(InviteLabel#3)
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@125: {0}, {1}
                  */
    86          super(new JLabel());
    87          
    88          this.mainFrame = mainFrame;
    89  
    90          setBorder(BorderFactory.createEtchedBorder());
    91          label.setIcon(IconManager.getIconManager().getIcon("invite"));
    92  
    93          inviteList = new MapList<Server, Invite>();
    94          menu = new JPopupMenu();
    95          dismiss = new JMenuItem("Dismiss all invites");
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.statusbar.InviteLabel$1(InviteLabel)
                  */
    96          dismiss.addActionListener(new java.awt.event.ActionListener() {
    97  
    98              /** {@inheritDoc} */
    99              @Override
   100              public void actionPerformed(final ActionEvent e) {
                         /* 
    P/P                   *  Method: void actionPerformed(ActionEvent)
                          * 
                          *  Preconditions:
                          *    this.inviteList != null
                          * 
                          *  Presumptions:
                          *    com.dmdirc.Invite:getServer(...)@104 != null
                          *    com.dmdirc.util.MapList:keySet(...)@101 != null
                          *    java.util.Iterator:next(...)@103 != null
                          * 
                          *  Test Vectors:
                          *    java.util.Iterator:hasNext(...)@101: {0}, {1}
                          *    java.util.Iterator:hasNext(...)@103: {0}, {1}
                          */
   101                  for (Server server : inviteList.keySet()) {
   102                      final List<Invite> invites = new ArrayList<Invite>(inviteList.values(server));
   103                      for (Invite invite : invites) {
   104                          invite.getServer().removeInvite(invite);
   105                      }
   106                  }
   107              }
   108          });
   109  
   110          accept = new JMenuItem("Accept all invites");
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.statusbar.InviteLabel$2(InviteLabel)
                  */
   111          accept.addActionListener(new java.awt.event.ActionListener() {
   112  
   113              /** {@inheritDoc} */
   114              @Override
   115              public void actionPerformed(final ActionEvent e) {
                         /* 
    P/P                   *  Method: void actionPerformed(ActionEvent)
                          * 
                          *  Preconditions:
                          *    this.inviteList != null
                          * 
                          *  Presumptions:
                          *    com.dmdirc.util.MapList:keySet(...)@116 != null
                          *    java.util.Iterator:next(...)@118 != null
                          * 
                          *  Test Vectors:
                          *    java.util.Iterator:hasNext(...)@116: {0}, {1}
                          *    java.util.Iterator:hasNext(...)@118: {0}, {1}
                          */
   116                  for (Server server : inviteList.keySet()) {
   117                      final List<Invite> invites = new ArrayList<Invite>(inviteList.values(server));
   118                      for (Invite invite : invites) {
   119                          invite.accept();
   120                      }
   121                  }
   122              }
   123          });
   124  
   125          for (Server server : ServerManager.getServerManager().getServers()) {
   126              inviteList.add(server, server.getInvites());
   127              server.addInviteListener(this);
   128          }
   129  
   130          ActionManager.addListener(this, CoreActionType.CLIENT_FRAME_CHANGED);
   131          ActionManager.addListener(this, CoreActionType.SERVER_CONNECTED);
   132          ActionManager.addListener(this, CoreActionType.SERVER_DISCONNECTED);
   133          ActionManager.addListener(this, CoreActionType.SERVER_CONNECTERROR);
   134  
   135          update();
   136      }
   137  
   138      /** {@inheritDoc} */
   139      @Override
   140      protected StatusbarPopupWindow getWindow() {
                 /* 
    P/P           *  Method: StatusbarPopupWindow getWindow()
                  * 
                  *  Preconditions:
                  *    init'ed(this.activeServer)
                  *    init'ed(this.mainFrame)
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new InvitePopup(getWindow#1)
                  *    new InvitePopup(getWindow#1) num objects == 1
                  *    return_value.parent == this
                  *    return_value.parent != null
                  *    return_value.parentWindow == this.mainFrame
                  *    init'ed(return_value.parentWindow)
                  *    return_value.server == this.activeServer
                  *    init'ed(return_value.server)
                  */
   141          return new InvitePopup(this, activeServer, mainFrame);
   142      }
   143  
   144      /**
   145       * Popuplates the menu.
   146       */
   147      private void popuplateMenu() {
                 /* 
    P/P           *  Method: void popuplateMenu()
                  * 
                  *  Preconditions:
                  *    this.activeServer != null
                  *    this.menu != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Server:getInvites(...)@150 != null
                  *    java.util.Iterator:next(...)@151 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@151: {0}, {1}
                  */
   148          menu.removeAll();
   149  
   150          final List<Invite> invites = activeServer.getInvites();
   151          for (Invite invite : invites) {
   152              menu.add(new JMenuItem(new InviteAction(invite)));
   153          }
   154          menu.add(new JSeparator());
   155          menu.add(accept);
   156          menu.add(dismiss);
   157      }
   158  
   159      /**
   160       * Updates the invite label for the currently active server.
   161       */
   162      private void update() {
                 /* 
    P/P           *  Method: void update()
                  * 
                  *  Preconditions:
                  *    init'ed(this.activeFrame)
                  *    (soft) init'ed(this.dialog)
                  *    (soft) this.dialog.parent != null
                  *    (soft) this.dialog.parentWindow != null
                  *    (soft) this.inviteList != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.util.MapList:get(...)@173 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.activeServer)
                  *    this.dialog == One-of{old this.dialog, null}
                  *    init'ed(this.dialog)
                  * 
                  *  Test Vectors:
                  *    this.activeFrame: Inverse{null}, Addr_Set{null}
                  *    com.dmdirc.util.MapList:containsKey(...)@168: {1}, {0}
                  *    java.util.List:isEmpty(...)@173: {0}, {1}
                  */
   163          if (activeFrame == null) {
   164              activeServer = null;
   165          } else {
   166              activeServer = activeFrame.getServer();
   167          }
   168          if (activeServer != null && !inviteList.containsKey(activeServer)) {
   169              inviteList.add(activeServer, activeServer.getInvites());
   170              activeServer.addInviteListener(this);
   171          }
   172  
   173          if (activeServer == null || inviteList.get(activeServer).isEmpty()) {
   174              setVisible(false);
   175              closeDialog();
   176          } else {
   177              refreshDialog();
   178              setVisible(true);
   179          }
   180      }
   181  
   182      /** {@inheritDoc} */
   183      @Override
   184      public void inviteReceived(final Server server, final Invite invite) {
                 /* 
    P/P           *  Method: void inviteReceived(Server, Invite)
                  * 
                  *  Preconditions:
                  *    init'ed(this.activeFrame)
                  *    this.inviteList != null
                  *    (soft) init'ed(this.dialog)
                  *    (soft) this.dialog.parent != null
                  *    (soft) this.dialog.parentWindow != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.activeServer)
                  *    this.dialog == One-of{old this.dialog, null}
                  *    init'ed(this.dialog)
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.util.MapList:containsKey(...)@185: {1}, {0}
                  */
   185          if (!inviteList.containsKey(server)) {
   186              inviteList.add(server);
   187          }
   188          inviteList.add(server, invite);
   189          update();
   190      }
   191  
   192      /** {@inheritDoc} */
   193      @Override
   194      public void inviteExpired(final Server server, final Invite invite) {
                 /* 
    P/P           *  Method: void inviteExpired(Server, Invite)
                  * 
                  *  Preconditions:
                  *    init'ed(this.activeFrame)
                  *    this.inviteList != null
                  *    (soft) init'ed(this.dialog)
                  *    (soft) this.dialog.parent != null
                  *    (soft) this.dialog.parentWindow != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.activeServer)
                  *    this.dialog == One-of{old this.dialog, null}
                  *    init'ed(this.dialog)
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.util.MapList:containsKey(...)@195: {1}, {0}
                  */
   195          if (!inviteList.containsKey(server)) {
   196              inviteList.add(server);
   197          }
   198          inviteList.remove(server, invite);
   199          update();
   200      }
   201  
   202      /** {@inheritDoc} */
   203      @Override
   204      public void processEvent(final ActionType type, final StringBuffer format,
   205              final Object... arguments) {
                 /* 
    P/P           *  Method: void processEvent(ActionType, StringBuffer, Object[])
                  * 
                  *  Preconditions:
                  *    arguments != null
                  *    arguments.length >= 1
                  *    init'ed(arguments[0])
                  *    (soft) init'ed(this.dialog)
                  *    (soft) this.dialog.parent != null
                  *    (soft) this.dialog.parentWindow != null
                  *    (soft) this.inviteList != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.actions.CoreActionType.CLIENT_FRAME_CHANGED)
                  *    init'ed(com.dmdirc.actions.CoreActionType.SERVER_CONNECTED)
                  * 
                  *  Postconditions:
                  *    this.activeFrame == One-of{old this.activeFrame, arguments[0]}
                  *    possibly_updated(this.activeServer)
                  *    this.dialog == One-of{old this.dialog, null}
                  *    init'ed(this.dialog)
                  */
   206          if (type == CoreActionType.CLIENT_FRAME_CHANGED) {
   207              if (arguments[0] instanceof FrameContainer) {
   208              activeFrame = (FrameContainer) arguments[0];
   209                  update();
   210              }
   211          } else if (type == CoreActionType.SERVER_CONNECTED) {
   212              if (arguments[0] instanceof Server) {
   213                  ((Server) arguments[0]).addInviteListener(this);
   214              }
   215          } else {
   216              if (arguments[0] instanceof Server) {
   217                  ((Server) arguments[0]).removeInviteListener(this);
   218              }
   219          }
   220      }
   221  
   222      /**
   223       * {@inheritDoc}
   224       *
   225       * @param e Mouse event
   226       */
   227      @Override
   228      public void mouseClicked(final MouseEvent e) {
                 /* 
    P/P           *  Method: void mouseClicked(MouseEvent)
                  * 
                  *  Preconditions:
                  *    this.activeServer != null
                  *    this.menu != null
                  *    (soft) e != null
                  * 
                  *  Test Vectors:
                  *    javax.swing.JPopupMenu:getComponentCount(...)@231: {-231..0}, {1..232-1}
                  */
   229          super.mouseClicked(e);
   230          popuplateMenu();
   231          if (menu.getComponentCount() > 0) {
   232              menu.show(this, e.getX(), e.getY());
   233          }
   234      }
   235  
   236  }
   237  /**
   238   * Invite action.
   239   */
   240  class InviteAction extends AbstractAction {
   241  
   242      /**
   243       * A version number for this class. It should be changed whenever the class
   244       * structure is changed (or anything else that would prevent serialized
   245       * objects being unserialized with the new class).
   246       */
   247      private static final long serialVersionUID = 1;
   248      /** Invite. */
   249      private final Invite invite;
   250  
   251      /**
   252       * Instantiates a new invite action.
   253       *
   254       * @param invite Invite for the action
   255       */
   256      public InviteAction(final Invite invite) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.statusbar.InviteAction(Invite)
                  * 
                  *  Preconditions:
                  *    invite != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Invite:getSource(...).length@257 >= 1
                  *    com.dmdirc.Invite:getSource(...)@257 != null
                  * 
                  *  Postconditions:
                  *    this.invite == invite
                  *    this.invite != null
                  */
   257          super(invite.getChannel() + " (" + invite.getSource()[0] + ")");
   258  
   259          this.invite = invite;
   260      }
   261  
   262      /**
   263       * {@inheritDoc}
   264       *
   265       * @param e Action event
   266       */
   267      @Override
   268      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    this.invite != null
                  */
   269          invite.accept();
   270      }
   271  
   272      /** {@inheritDoc} */
   273      @Override
   274      public String toString() {
                 /* 
    P/P           *  Method: String toString()
                  * 
                  *  Preconditions:
                  *    this.invite != null
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuilder:toString(...)._tainted == 0
                  *    return_value == &amp;java.lang.StringBuilder:toString(...)
                  */
   275          return invite.getChannel() + "(" + Arrays.toString(invite.getSource()) + ")";
   276      }
   277  }








SofCheck Inspector Build Version : 2.17854
InviteLabel.java 2009-Jun-25 01:54:24
InviteAction.class 2009-Sep-02 17:04:15
InviteLabel.class 2009-Sep-02 17:04:14
InviteLabel$1.class 2009-Sep-02 17:04:14
InviteLabel$2.class 2009-Sep-02 17:04:14