Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SystrayManager.java 7.2KB

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