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.0KB

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