File Source: SystrayPlugin.java

         /* 
    P/P   *  Method: com.dmdirc.addons.systray.SystrayPlugin__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.systray;
    24  
    25  import com.dmdirc.ui.IconManager;
    26  import com.dmdirc.Main;
    27  import com.dmdirc.actions.ActionManager;
    28  import com.dmdirc.actions.interfaces.ActionType;
    29  import com.dmdirc.actions.CoreActionType;
    30  import com.dmdirc.addons.ui_swing.MainFrame;
    31  import com.dmdirc.config.IdentityManager;
    32  import com.dmdirc.config.prefs.PreferencesCategory;
    33  import com.dmdirc.config.prefs.PreferencesManager;
    34  import com.dmdirc.config.prefs.PreferencesSetting;
    35  import com.dmdirc.config.prefs.PreferencesType;
    36  import com.dmdirc.config.prefs.validator.ValidationResponse;
    37  import com.dmdirc.plugins.Plugin;
    38  import com.dmdirc.ui.messages.Styliser;
    39  
    40  import java.awt.AWTException;
    41  import java.awt.Frame;
    42  import java.awt.MenuItem;
    43  import java.awt.PopupMenu;
    44  import java.awt.SystemTray;
    45  import java.awt.TrayIcon;
    46  import java.awt.event.ActionEvent;
    47  import java.awt.event.ActionListener;
    48  import java.awt.event.MouseEvent;
    49  import java.awt.event.MouseListener;
    50  
    51  /**
    52   * The Systray plugin shows DMDirc in the user's system tray, and allows
    53   * notifications to be disabled.
    54   * @author chris
    55   */
    56  public final class SystrayPlugin extends Plugin implements ActionListener,
    57          MouseListener, com.dmdirc.interfaces.ActionListener {
    58      
    59      /** The command we registered. */
    60      private PopupCommand command;
    61      
    62      /** The tray icon we're currently using. */
    63      private final TrayIcon icon;
    64      
    65      /** Creates a new system tray plugin. */
    66      public SystrayPlugin() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.systray.SystrayPlugin()
                  * 
                  *  Presumptions:
                  *    com.dmdirc.ui.IconManager:getIconManager(...)@78 != null
                  * 
                  *  Postconditions:
                  *    this.icon == &new TrayIcon(SystrayPlugin#4)
                  *    new TrayIcon(SystrayPlugin#4) num objects == 1
                  */
    67          super();
    68          final MenuItem show = new MenuItem("Show/hide");
    69          final MenuItem quit = new MenuItem("Quit");
    70          
    71          final PopupMenu menu = new PopupMenu();
    72          menu.add(show);
    73          menu.add(quit);
    74          
    75          show.addActionListener(this);
    76          quit.addActionListener(this);
    77          
    78          icon = new TrayIcon(IconManager.getIconManager().getImage("logo"), 
    79                  "DMDirc", menu);
    80          icon.setImageAutoSize(true);
    81          icon.addMouseListener(this);
    82      }
    83      
    84      /**
    85       * Sends a notification via the system tray icon.
    86       * @param title The title of the notification
    87       * @param message The contents of the notification
    88       * @param type The type of notification
    89       */
    90      public void notify(final String title, final String message, final TrayIcon.MessageType type) {
                 /* 
    P/P           *  Method: void notify(String, String, TrayIcon$MessageType)
                  * 
                  *  Preconditions:
                  *    this.icon != null
                  */
    91          icon.displayMessage(title, Styliser.stipControlCodes(message), type);
    92      }
    93      
    94      /**
    95       * Sends a notification via the system tray icon.
    96       * @param title The title of the notification
    97       * @param message The contents of the notification
    98       */
    99      public void notify(final String title, final String message) {
                 /* 
    P/P           *  Method: void notify(String, String)
                  * 
                  *  Preconditions:
                  *    this.icon != null
                  * 
                  *  Presumptions:
                  *    init'ed(java.awt.TrayIcon$MessageType.NONE)
                  */
   100          notify(title, message, TrayIcon.MessageType.NONE);
   101      }
   102      
   103      /**
   104       * {@inheritDoc}
   105       * 
   106       * @param e Action event
   107       */
   108      @Override
   109      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Main:getUI(...)@111 != null
                  *    com.dmdirc.ui.interfaces.UIController:getMainWindow(...)@111 != null
                  *    java.awt.event.ActionEvent:getActionCommand(...)@110 != null
                  *    java.awt.event.ActionEvent:getActionCommand(...)@112 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@110: {0}, {1}
                  *    java.lang.String:equals(...)@112: {0}, {1}
                  */
   110          if (e.getActionCommand().equals("Show/hide")) {
   111              Main.getUI().getMainWindow().setVisible(!Main.getUI().getMainWindow().isVisible());
   112          } else if (e.getActionCommand().equals("Quit")) {
   113              Main.quit();
   114          }
   115      }
   116      
   117      /** {@inheritDoc} */
   118      @Override
   119      public ValidationResponse checkPrerequisites() {
                 /* 
    P/P           *  Method: ValidationResponse checkPrerequisites()
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&new ValidationResponse(checkPrerequisites#2),&new ValidationResponse(checkPrerequisites#1)}
                  *    new ValidationResponse(checkPrerequisites#1) num objects <= 1
                  *    new ValidationResponse(checkPrerequisites#2) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    java.awt.SystemTray:isSupported(...)@120: {0}, {1}
                  */
   120          if (SystemTray.isSupported()) {
   121              return new ValidationResponse();
   122          } else {
   123              return new ValidationResponse("System tray is not supported on this platform.");
   124          }
   125      }
   126      
   127      /** {@inheritDoc} */
   128      @Override
   129      public void onLoad() {
   130          try {
                     /* 
    P/P               *  Method: void onLoad()
                      * 
                      *  Presumptions:
                      *    init'ed(com.dmdirc.actions.CoreActionType.CLIENT_MINIMISED)
                      *    java.awt.SystemTray:getSystemTray(...)@131 != null
                      * 
                      *  Postconditions:
                      *    this.command == One-of{&amp;new PopupCommand(onLoad#1), old this.command}
                      *    new PopupCommand(onLoad#1) num objects <= 1
                      *    new PopupCommand(onLoad#1).parent == this
                      *    new PopupCommand(onLoad#1).parent != null
                      */
   131              SystemTray.getSystemTray().add(icon);
   132              command = new PopupCommand(this);
   133          } catch (AWTException ex) {
   134              // Should probably unload ourself here?
   135          }
   136          
   137          ActionManager.addListener(this, CoreActionType.CLIENT_MINIMISED);
   138      }
   139      
   140      /** {@inheritDoc} */
   141      @Override
   142      public void onUnload() {
                 /* 
    P/P           *  Method: void onUnload()
                  * 
                  *  Preconditions:
                  *    this.command != null
                  * 
                  *  Presumptions:
                  *    java.awt.SystemTray:getSystemTray(...)@143 != null
                  */
   143          SystemTray.getSystemTray().remove(icon);
   144          command.unregister();
   145          
   146          ActionManager.removeListener(this);
   147      }
   148  
   149      /** {@inheritDoc} */
   150      @Override
   151      public void showConfig(final PreferencesManager manager) {
                 /* 
    P/P           *  Method: void showConfig(PreferencesManager)
                  * 
                  *  Preconditions:
                  *    manager != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.prefs.PreferencesManager:getCategory(...)@161 != null
                  *    init'ed(com.dmdirc.config.prefs.PreferencesType.BOOLEAN)
                  */
   152          final PreferencesCategory category = new PreferencesCategory("System Tray", 
   153                  "General configuration settings");
   154          
   155          category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
   156                  getDomain(), "autominimise", "Auto-hide DMDirc " +
   157                  "when minimised", "If this option is enabled, the systray " +
   158                  "plugin will hide DMDirc to the system tray whenever DMDirc is" +
   159                  " minimised"));
   160          
   161          manager.getCategory("Plugins").addSubCategory(category);
   162      }
   163      
   164      /** 
   165       * {@inheritDoc}
   166       * 
   167       * @param e Mouse event
   168       */
   169      @Override
   170      public void mouseClicked(final MouseEvent e) {
                 /* 
    P/P           *  Method: void mouseClicked(MouseEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Main:getUI(...)@172 != null
                  *    com.dmdirc.Main:getUI(...)@173 != null
                  *    com.dmdirc.Main:getUI(...)@175 != null
                  *    com.dmdirc.Main:getUI(...)@176 != null
                  *    com.dmdirc.Main:getUI(...)@177 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.ui.interfaces.MainWindow:isVisible(...)@172: {0}, {1}
                  *    java.awt.event.MouseEvent:getButton(...)@171: {-231..0, 2..232-1}, {1}
                  */
   171          if (e.getButton() == MouseEvent.BUTTON1) {
   172              if (Main.getUI().getMainWindow().isVisible()) {               
   173                  Main.getUI().getMainWindow().setVisible(false);
   174              } else {
   175                  Main.getUI().getMainWindow().setVisible(true);
   176                  ((MainFrame) Main.getUI().getMainWindow()).setState(Frame.NORMAL);
   177                  ((MainFrame) Main.getUI().getMainWindow()).toFront();
   178              }
   179          }
   180      }
   181      
   182      /** 
   183       * {@inheritDoc}
   184       * 
   185       * @param e Mouse event
   186       */
   187      @Override
   188      public void mousePressed(final MouseEvent e) {
   189          //Ignore
             /* 
    P/P       *  Method: void mousePressed(MouseEvent)
              */
   190      }
   191      
   192      /** 
   193       * {@inheritDoc}
   194       * 
   195       * @param e Mouse event
   196       */
   197      @Override
   198      public void mouseReleased(final MouseEvent e) {
   199          //Ignore
             /* 
    P/P       *  Method: void mouseReleased(MouseEvent)
              */
   200      }
   201      
   202      /** 
   203       * {@inheritDoc}
   204       * 
   205       * @param e Mouse event
   206       */
   207      @Override
   208      public void mouseEntered(final MouseEvent e) {
   209          //Ignore
             /* 
    P/P       *  Method: void mouseEntered(MouseEvent)
              */
   210      }
   211      
   212      /** 
   213       * {@inheritDoc}
   214       * 
   215       * @param e Mouse event
   216       */
   217      @Override
   218      public void mouseExited(final MouseEvent e) {
   219          //Ignore
             /* 
    P/P       *  Method: void mouseExited(MouseEvent)
              */
   220      }
   221      
   222      /** {@inheritDoc} */
   223      @Override
   224      public void processEvent(final ActionType type, final StringBuffer format,
   225              final Object... arguments) {
                 /* 
    P/P           *  Method: void processEvent(ActionType, StringBuffer, Object[])
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Main:getUI(...)@229 != null
                  *    init'ed(com.dmdirc.actions.CoreActionType.CLIENT_MINIMISED)
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@226 != null
                  *    com.dmdirc.ui.interfaces.UIController:getMainWindow(...)@229 != null
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.config.ConfigManager:getOptionBool(...)@226: {0}, {1}
                  */
   226          if (type == CoreActionType.CLIENT_MINIMISED
   227                  && IdentityManager.getGlobalConfig().getOptionBool(getDomain(),
   228                  "autominimise")) {
   229              Main.getUI().getMainWindow().setVisible(false);
   230          }
   231      }
   232      
   233  }








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