001: /* tjws - SysTrayControl.java
002: * Copyright (C) 1999-2007 Dmitriy Rogatkin. All rights reserved.
003: * Redistribution and use in source and binary forms, with or without
004: * modification, are permitted provided that the following conditions
005: * are met:
006: * 1. Redistributions of source code must retain the above copyright
007: * notice, this list of conditions and the following disclaimer.
008: * 2. Redistributions in binary form must reproduce the above copyright
009: * notice, this list of conditions and the following disclaimer in the
010: * documentation and/or other materials provided with the distribution.
011: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
012: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
013: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
014: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
015: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
016: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
017: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
018: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
019: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
020: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
021: * SUCH DAMAGE.
022: *
023: * Visit http://tjws.sourceforge.net to get the latest infromation
024: * about Rogatkin's products.
025: * $Id: SysTrayControl.java,v 1.5 2007/04/12 05:24:51 rogatkin Exp $
026: * Created on Jul 2, 2006
027: * @author Dmitriy
028: */
029: package rogatkin.web;
030:
031: import java.awt.AWTException;
032: import java.awt.Desktop;
033: import java.awt.MenuItem;
034: import java.awt.PopupMenu;
035: import java.awt.SplashScreen;
036: import java.awt.SystemTray;
037: import java.awt.Toolkit;
038: import java.awt.TrayIcon;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041: import java.io.IOException;
042: import java.lang.reflect.InvocationTargetException;
043: import java.lang.reflect.Method;
044: import java.net.URI;
045: import java.net.URISyntaxException;
046: import java.util.Arrays;
047: import java.util.Timer;
048: import java.util.TimerTask;
049:
050: import rogatkin.web.WebApp.ServiceController;
051: import Acme.Serve.Serve;
052:
053: public class SysTrayControl implements ServiceController,
054: ActionListener {
055:
056: private Method s;
057:
058: private TrayIcon ti;
059:
060: private String contextPath;
061:
062: private String port = "" + Serve.DEF_PORT;
063:
064: public void attachServe(Method stop, String contextPath) {
065: s = stop;
066: this .contextPath = contextPath;
067: if (SystemTray.isSupported() == false)
068: return;
069: SystemTray st = SystemTray.getSystemTray();
070: // TODO get text from localized resource
071: PopupMenu popup = new PopupMenu();
072: MenuItem mi;
073: if (Desktop.isDesktopSupported()) {
074: popup.add(mi = new MenuItem("Go " + contextPath));
075: mi.addActionListener(this );
076: }
077: popup.add(mi = new MenuItem("stop"));
078: mi.addActionListener(this );
079: popup.add(mi = new MenuItem("exit"));
080: mi.addActionListener(this );
081: //java.net.URL u;
082: ti = new TrayIcon(Toolkit.getDefaultToolkit().getImage(
083: getClass().getClassLoader().getResource(
084: "rogatkin/resource/tjws.gif")),
085: "TJWS control panel", popup);
086: //javax.swing.JOptionPane.showMessageDialog(null, String.format("Created sys tray icon with image%s%n",u));
087: ti.setImageAutoSize(true);
088:
089: try {
090: st.add(ti);
091: } catch (AWTException e) {
092:
093: }
094: new Timer("Splash closer", true).schedule(new TimerTask() {
095: @Override
096: public void run() {
097: SplashScreen ss = SplashScreen.getSplashScreen();
098: if (ss != null)
099: ss.close();
100: }
101: }, 3 * 1000);
102: }
103:
104: public String[] massageSettings(String[] args) {
105: boolean nohup = false;
106: boolean nextPort = false;
107: for (String arg : args)
108: if ("-nohup".equals(arg)) {
109: nohup = true;
110: } else if ("-p".equals(arg))
111: nextPort = true;
112: else if (nextPort) {
113: port = arg;
114: nextPort = false;
115: }
116:
117: if (SystemTray.isSupported()) {
118: if (nohup == false) {
119: args = Arrays.copyOf(args, args.length + 1);
120: args[args.length - 1] = "-nohup";
121: }
122: } else {
123: // remove '-nohup'
124:
125: }
126: return args;
127: }
128:
129: public void actionPerformed(ActionEvent event) {
130: try {
131: if ("stop".equals(event.getActionCommand())) {
132: ti.displayMessage("TJWS", "Server stopped",
133: TrayIcon.MessageType.INFO);
134: s.invoke(null); // TODO suspend it
135: } else if ("exit".equals(event.getActionCommand())) {
136: s.invoke(null);
137: System.exit(0);
138: } else if (event.getActionCommand().startsWith("Go")) {
139: //javax.swing.JOptionPane.showMessageDialog(null, String.format("http://localhost:%s/%s", port, contextPath));
140: ti.displayMessage("TJWS", "Going to " + contextPath,
141: TrayIcon.MessageType.INFO);
142: Desktop desktop = Desktop.getDesktop();
143: desktop.browse(new URI(String.format(
144: "http://localhost:%s/%s", port, contextPath)));
145: } //else
146: //javax.swing.JOptionPane.showMessageDialog(null, "Command "+event.getActionCommand());
147: } catch (URISyntaxException e) {
148: //e.printStackTrace();
149: } catch (IOException e) {
150: //e.printStackTrace();
151: } catch (IllegalArgumentException e) {
152: e.printStackTrace();
153: } catch (IllegalAccessException e) {
154: e.printStackTrace();
155: } catch (InvocationTargetException e) {
156: e.printStackTrace();
157: }
158: }
159:
160: }
|