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.

SystrayManager.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.ClientModule.GlobalConfig;
  24. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.addons.ui_swing.MainFrame;
  26. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  27. import com.dmdirc.config.prefs.PreferencesCategory;
  28. import com.dmdirc.config.prefs.PreferencesDialogModel;
  29. import com.dmdirc.config.prefs.PreferencesSetting;
  30. import com.dmdirc.config.prefs.PreferencesType;
  31. import com.dmdirc.addons.ui_swing.events.ClientMinimisedEvent;
  32. import com.dmdirc.events.ClientPrefsOpenedEvent;
  33. import com.dmdirc.interfaces.EventBus;
  34. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  35. import com.dmdirc.plugins.PluginDomain;
  36. import com.dmdirc.addons.ui_swing.components.IconManager;
  37. import com.dmdirc.plugins.PluginInfo;
  38. import com.dmdirc.ui.messages.Styliser;
  39. import java.awt.AWTException;
  40. import java.awt.Frame;
  41. import java.awt.MenuItem;
  42. import java.awt.PopupMenu;
  43. import java.awt.SystemTray;
  44. import java.awt.TrayIcon;
  45. import java.awt.event.ActionEvent;
  46. import java.awt.event.ActionListener;
  47. import java.awt.event.MouseEvent;
  48. import java.awt.event.MouseListener;
  49. import javax.inject.Inject;
  50. import net.engio.mbassy.listener.Handler;
  51. public class SystrayManager implements ActionListener, MouseListener {
  52. private final PluginInfo pluginInfo;
  53. /** Main frame instance. */
  54. private final MainFrame mainFrame;
  55. /** This plugin's settings domain. */
  56. private final String domain;
  57. /** The config to read settings from. */
  58. private final AggregateConfigProvider globalConfig;
  59. /** Icon manager to get images from. */
  60. private final IconManager iconManager;
  61. /** The event bus to listen to events on. */
  62. private final EventBus eventBus;
  63. /** The tray icon we're currently using. */
  64. private TrayIcon icon;
  65. @Inject
  66. public SystrayManager(
  67. @GlobalConfig final AggregateConfigProvider globalConfig,
  68. @PluginDomain(SystrayPlugin.class) final String domain,
  69. @PluginDomain(SystrayPlugin.class) final PluginInfo pluginInfo,
  70. final MainFrame mainFrame,
  71. final IconManager iconManager,
  72. final EventBus eventBus) {
  73. this.globalConfig = globalConfig;
  74. this.domain = domain;
  75. this.pluginInfo = pluginInfo;
  76. this.mainFrame = mainFrame;
  77. this.iconManager = iconManager;
  78. this.eventBus = eventBus;
  79. }
  80. public void load() {
  81. final MenuItem show = new MenuItem("Show/hide");
  82. final MenuItem quit = new MenuItem("Quit");
  83. final PopupMenu menu = new PopupMenu();
  84. menu.add(show);
  85. menu.add(quit);
  86. show.addActionListener(this);
  87. quit.addActionListener(this);
  88. icon = new TrayIcon(iconManager.getImage("logo"), "DMDirc", menu);
  89. icon.setImageAutoSize(true);
  90. icon.addMouseListener(this);
  91. try {
  92. SystemTray.getSystemTray().add(icon);
  93. eventBus.subscribe(this);
  94. } catch (AWTException ex) {
  95. throw new IllegalStateException("Unable to load plugin", ex);
  96. }
  97. }
  98. public void unload() {
  99. SystemTray.getSystemTray().remove(icon);
  100. eventBus.unsubscribe(this);
  101. icon = null;
  102. }
  103. /**
  104. * Sends a notification via the system tray icon.
  105. *
  106. * @param title The title of the notification
  107. * @param message The contents of the notification
  108. * @param type The type of notification
  109. */
  110. public void notify(final String title, final String message,
  111. final TrayIcon.MessageType type) {
  112. icon.displayMessage(title, Styliser.stipControlCodes(message), type);
  113. }
  114. /**
  115. * Sends a notification via the system tray icon.
  116. *
  117. * @param title The title of the notification
  118. * @param message The contents of the notification
  119. */
  120. public void notify(final String title, final String message) {
  121. notify(title, message, TrayIcon.MessageType.NONE);
  122. }
  123. @Override
  124. public void actionPerformed(final ActionEvent e) {
  125. switch (e.getActionCommand()) {
  126. case "Show/hide":
  127. mainFrame.setVisible(!mainFrame.isVisible());
  128. break;
  129. case "Quit":
  130. mainFrame.quit();
  131. break;
  132. }
  133. }
  134. @Override
  135. public void mouseClicked(final MouseEvent e) {
  136. if (e.getButton() == MouseEvent.BUTTON1) {
  137. if (mainFrame.isVisible()) {
  138. mainFrame.setVisible(false);
  139. } else {
  140. mainFrame.setVisible(true);
  141. mainFrame.setState(Frame.NORMAL);
  142. mainFrame.toFront();
  143. }
  144. }
  145. }
  146. @Handler(invocation = EdtHandlerInvocation.class)
  147. public void handleClientMinimised(final ClientMinimisedEvent event) {
  148. if (globalConfig.getOptionBool(domain, "autominimise")) {
  149. mainFrame.setVisible(false);
  150. }
  151. }
  152. @Handler
  153. public void showConfig(final ClientPrefsOpenedEvent event) {
  154. final PreferencesDialogModel manager = event.getModel();
  155. final PreferencesCategory category = new PluginPreferencesCategory(
  156. pluginInfo, "System Tray",
  157. "General configuration settings");
  158. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  159. pluginInfo.getDomain(), "autominimise", "Auto-hide DMDirc when minimised",
  160. "If this option is enabled, the systray plugin will hide DMDirc"
  161. + " to the system tray whenever DMDirc is minimised",
  162. manager.getConfigManager(), manager.getIdentity()));
  163. manager.getCategory("Plugins").addSubCategory(category);
  164. }
  165. @Override
  166. public void mousePressed(final MouseEvent e) {
  167. //Ignore
  168. }
  169. @Override
  170. public void mouseReleased(final MouseEvent e) {
  171. //Ignore
  172. }
  173. @Override
  174. public void mouseEntered(final MouseEvent e) {
  175. //Ignore
  176. }
  177. @Override
  178. public void mouseExited(final MouseEvent e) {
  179. //Ignore
  180. }
  181. }