您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SystrayPlugin.java 7.3KB

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