123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.systray;
-
- import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
- import com.dmdirc.addons.ui_swing.MainFrame;
- import com.dmdirc.addons.ui_swing.components.IconManager;
- import com.dmdirc.addons.ui_swing.events.ClientMinimisedEvent;
- import com.dmdirc.config.GlobalConfig;
- import com.dmdirc.config.prefs.PluginPreferencesCategory;
- import com.dmdirc.config.prefs.PreferencesCategory;
- import com.dmdirc.config.prefs.PreferencesDialogModel;
- import com.dmdirc.config.prefs.PreferencesSetting;
- import com.dmdirc.config.prefs.PreferencesType;
- import com.dmdirc.events.ClientPrefsOpenedEvent;
- import com.dmdirc.interfaces.EventBus;
- import com.dmdirc.interfaces.config.AggregateConfigProvider;
- import com.dmdirc.plugins.PluginDomain;
- import com.dmdirc.plugins.PluginInfo;
- import com.dmdirc.ui.messages.Styliser;
- import java.awt.AWTException;
- import java.awt.Frame;
- import java.awt.MenuItem;
- import java.awt.PopupMenu;
- import java.awt.SystemTray;
- import java.awt.TrayIcon;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.inject.Inject;
- import net.engio.mbassy.listener.Handler;
-
- public class SystrayManager implements ActionListener, MouseListener {
-
- private final PluginInfo pluginInfo;
- /** Main frame instance. */
- private final MainFrame mainFrame;
- /** This plugin's settings domain. */
- private final String domain;
- /** The config to read settings from. */
- private final AggregateConfigProvider globalConfig;
- /** Icon manager to get images from. */
- private final IconManager iconManager;
- /** The event bus to listen to events on. */
- private final EventBus eventBus;
- /** The tray icon we're currently using. */
- private TrayIcon icon;
-
- @Inject
- public SystrayManager(
- @GlobalConfig final AggregateConfigProvider globalConfig,
- @PluginDomain(SystrayPlugin.class) final String domain,
- @PluginDomain(SystrayPlugin.class) final PluginInfo pluginInfo,
- final MainFrame mainFrame,
- final IconManager iconManager,
- final EventBus eventBus) {
- this.globalConfig = globalConfig;
- this.domain = domain;
- this.pluginInfo = pluginInfo;
- this.mainFrame = mainFrame;
- this.iconManager = iconManager;
- this.eventBus = eventBus;
- }
-
- public void load() {
- final MenuItem show = new MenuItem("Show/hide");
- final MenuItem quit = new MenuItem("Quit");
-
- final PopupMenu menu = new PopupMenu();
- menu.add(show);
- menu.add(quit);
-
- show.addActionListener(this);
- quit.addActionListener(this);
-
- icon = new TrayIcon(iconManager.getImage("logo"), "DMDirc", menu);
- icon.setImageAutoSize(true);
- icon.addMouseListener(this);
-
- try {
- SystemTray.getSystemTray().add(icon);
- eventBus.subscribe(this);
- } catch (AWTException ex) {
- throw new IllegalStateException("Unable to load plugin", ex);
- }
- }
-
- public void unload() {
- SystemTray.getSystemTray().remove(icon);
- eventBus.unsubscribe(this);
- icon = null;
- }
-
- /**
- * Sends a notification via the system tray icon.
- *
- * @param title The title of the notification
- * @param message The contents of the notification
- * @param type The type of notification
- */
- public void notify(final String title, final String message,
- final TrayIcon.MessageType type) {
- icon.displayMessage(title, Styliser.stipControlCodes(message), type);
- }
-
- /**
- * Sends a notification via the system tray icon.
- *
- * @param title The title of the notification
- * @param message The contents of the notification
- */
- public void notify(final String title, final String message) {
- notify(title, message, TrayIcon.MessageType.NONE);
- }
-
- @Override
- public void actionPerformed(final ActionEvent e) {
- switch (e.getActionCommand()) {
- case "Show/hide":
- mainFrame.setVisible(!mainFrame.isVisible());
- break;
- case "Quit":
- mainFrame.quit();
- break;
- }
- }
-
- @Override
- public void mouseClicked(final MouseEvent e) {
- if (e.getButton() == MouseEvent.BUTTON1) {
- if (mainFrame.isVisible()) {
- mainFrame.setVisible(false);
- } else {
- mainFrame.setVisible(true);
- mainFrame.setState(Frame.NORMAL);
- mainFrame.toFront();
- }
- }
- }
-
- @Handler(invocation = EdtHandlerInvocation.class)
- public void handleClientMinimised(final ClientMinimisedEvent event) {
- if (globalConfig.getOptionBool(domain, "autominimise")) {
- mainFrame.setVisible(false);
- }
- }
-
- @Handler
- public void showConfig(final ClientPrefsOpenedEvent event) {
- final PreferencesDialogModel manager = event.getModel();
- final PreferencesCategory category = new PluginPreferencesCategory(
- pluginInfo, "System Tray",
- "General configuration settings");
-
- category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
- pluginInfo.getDomain(), "autominimise", "Auto-hide DMDirc when minimised",
- "If this option is enabled, the systray plugin will hide DMDirc"
- + " to the system tray whenever DMDirc is minimised",
- manager.getConfigManager(), manager.getIdentity()));
-
- manager.getCategory("Plugins").addSubCategory(category);
- }
-
- @Override
- public void mousePressed(final MouseEvent e) {
- //Ignore
- }
-
- @Override
- public void mouseReleased(final MouseEvent e) {
- //Ignore
- }
-
- @Override
- public void mouseEntered(final MouseEvent e) {
- //Ignore
- }
-
- @Override
- public void mouseExited(final MouseEvent e) {
- //Ignore
- }
-
- }
|