001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2004-2005 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix;
021
022 import java.awt.*;
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 import java.net.*;
026 import java.util.logging.Level;
027 import java.util.logging.Logger;
028 import javax.swing.*;
029
030 import net.jetrix.config.ServerConfig;
031 import net.jetrix.listeners.HttpListener;
032
033 /**
034 * Manages the system trayIcon (windows only).
035 *
036 * @author Emmanuel Bourg
037 * @version $Revision: 861 $, $Date: 2010-08-17 12:31:31 +0200 (mar., 17 août 2010) $
038 * @since 0.2
039 */
040 public class SystrayManager
041 {
042 private static Logger log = Logger.getLogger("net.jetrix");
043 private static TrayIcon trayIcon;
044 private static final String TITLE = "Jetrix TetriNET Server";
045
046 /**
047 * Create and display the system trayIcon menu.
048 */
049 public static void open()
050 {
051 if (trayIcon == null)
052 {
053 SystemTray tray;
054
055 try
056 {
057 tray = SystemTray.getSystemTray();
058 }
059 catch (Throwable t)
060 {
061 log.log(Level.FINE, "System tray unavailable", t);
062 return;
063 }
064
065
066 // build the menu items
067 Font font = new Font(Font.DIALOG, Font.PLAIN, 11);
068
069 MenuItem itemAdmin = new MenuItem("Administration");
070 itemAdmin.setFont(font.deriveFont(Font.BOLD));
071 itemAdmin.addActionListener(new ActionListener()
072 {
073 public void actionPerformed(ActionEvent e)
074 {
075 openWebAdmin();
076 }
077 });
078
079 MenuItem itemLink = new MenuItem("Jetrix Website");
080 itemLink.addActionListener(new OpenURLActionListener("http://jetrix.sourceforge.net"));
081
082 MenuItem itemSupport = new MenuItem("Technical Support");
083 itemSupport.addActionListener(new OpenURLActionListener("http://sourceforge.net/projects/jetrix/forums/forum/172941"));
084
085 MenuItem itemExit = new MenuItem("Stop & Exit");
086 itemExit.addActionListener(new ActionListener()
087 {
088 public void actionPerformed(ActionEvent e)
089 {
090 Server.getInstance().stop();
091 }
092 });
093
094 // build the menu
095 final PopupMenu menu = new PopupMenu();
096 menu.add(itemAdmin);
097 menu.add(itemLink);
098 menu.add(itemSupport);
099 menu.addSeparator();
100 menu.add(itemExit);
101
102 menu.setFont(font);
103
104 // build the trayIcon icon
105 String osname = System.getProperty("os.name");
106 String iconname = osname.contains("Linux") ? "jetrix-32x32.png" : "jetrix-16x16.png";
107 ClassLoader loader = Thread.currentThread().getContextClassLoader();
108 Image icon = new ImageIcon(loader.getResource("icons/" + iconname)).getImage();
109
110 trayIcon = new TrayIcon(icon, TITLE, menu);
111 trayIcon.setImageAutoSize(true);
112 trayIcon.addActionListener(new ActionListener()
113 {
114 public void actionPerformed(ActionEvent e)
115 {
116 openWebAdmin();
117 }
118 });
119
120 // display the trayIcon icon
121 try {
122 tray.add(trayIcon);
123 } catch (AWTException e) {
124 log.log(Level.WARNING, "Unable to display the tray", e);
125 }
126 }
127 }
128
129 /**
130 * Remove the system trayIcon menu.
131 */
132 public static void close()
133 {
134 if (trayIcon != null)
135 {
136 SystemTray.getSystemTray().remove(trayIcon);
137 trayIcon = null;
138 }
139 }
140
141 /**
142 * Display a baloon message on the trayIcon icon.
143 *
144 * @param message the message to display
145 * @param type the type of the message
146 * @since 0.3
147 */
148 public static void notify(String message, TrayIcon.MessageType type)
149 {
150 if (trayIcon != null)
151 {
152 trayIcon.displayMessage(TITLE, message, type);
153 }
154 }
155
156 /**
157 * Open the web administration console in a browser window (Win32 only).
158 */
159 private static void openWebAdmin()
160 {
161 ServerConfig config = Server.getInstance().getConfig();
162
163 // find the port of the HttpListener
164 int port = 0;
165 for (Listener listener : config.getListeners())
166 {
167 if (listener instanceof HttpListener)
168 {
169 port = listener.getPort();
170 break;
171 }
172 }
173
174 if (port != 0)
175 {
176 openURL("http://admin:" + config.getAdminPassword() + "@localhost:" + port);
177 }
178 }
179
180 /**
181 * Open the specified URL in the Browser.
182 *
183 * @since 0.3
184 */
185 private static void openURL(String url)
186 {
187 try
188 {
189 Desktop.getDesktop().browse(new URI(url));
190 }
191 catch (Exception e)
192 {
193 log.log(Level.WARNING, "Unable to open the url: " + url, e);
194 }
195 }
196
197 /**
198 * Action listener opening an URL.
199 *
200 * @since 0.3
201 */
202 private static class OpenURLActionListener implements ActionListener
203 {
204 private String url;
205
206 public OpenURLActionListener(String url)
207 {
208 this.url = url;
209 }
210
211 public void actionPerformed(ActionEvent e)
212 {
213 openURL(url);
214 }
215 }
216
217 }