You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystrayPlugin.java 7.4KB

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