import java.awt.AWTException;
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.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.apache.log4j.Logger;
import com.huawei.ams5090.nac.util.SystemGlobalCfg;
/**
* 工具系统托盘展示
*
* @version 1.0
*
*/
public class SystemTrayFrame extends JFrame
{
/**
* 序列码
*
*/
private static final long serialVersionUID = -7887733949701809433L;
/**
* 托盘图标所在路径
*/
private static final String TRAYPATH = SystemGlobalCfg.getSysRootPath()
+ File.separator
+ "lib"
+ File.separator
+ "nac.gif";
/**
* 中英文资源所在路径
*/
private static final String LANGUAGEPATH = SystemGlobalCfg.getSysRootPath()
+ File.separator
+ "message.properties";
private static final Logger logger = Logger.getLogger(SystemTrayFrame.class);
/**
* 默认编码格式
*/
private static final String DEFAULT_ENCODING = "UTF-8";
/**
* 托盘图标
*/
private TrayIcon trayIcon = null;
/**
* 本操作系统托盘的实例
*/
private SystemTray tray = null;
/**
* 系统托盘入口
*/
public SystemTrayFrame()
{
try
{
// 将LookAndFeel设置成Windows样式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
logger.warn(ex.toString());
}
// 如果操作系统支持托盘
if (SystemTray.isSupported())
{
this.tray();
}
}
/**
* 启动系统托盘
*/
private void tray()
{
// 获得本操作系统托盘的实例
tray = SystemTray.getSystemTray();
ImageIcon icon = new ImageIcon(TRAYPATH);
Properties properties = this.getInformationFile(LANGUAGEPATH,
DEFAULT_ENCODING);
// 托盘提示信息
String systemTip = properties.getProperty("SYSTEM_TRAY_TIP");
// 托盘菜单
String systemTrayMenu = properties.getProperty("SYSTEM_TRAY_MENU");
// 提示
final String systemTrayPromf = properties.getProperty("SYSTEM_TRAY_PROMF");
// 点击“退出”后,系统弹出的提示框信息
final String systemTrayIdea = properties.getProperty("SYSTEM_TRAY_IDEA");
// 构造一个右键弹出式菜单
PopupMenu pop = new PopupMenu();
MenuItem exit = new MenuItem(systemTrayMenu);
// TrayIcon(Image image, String tooltip, PopupMenu popup)
// 用“图标”,“ToolTip”,“弹出菜单”来构造一个托盘图标
trayIcon = new TrayIcon(icon.getImage(), systemTip, pop);
try
{
tray.add(trayIcon);
}
catch (AWTException e1)
{
logger.error("System Tray start errot:" + e1.toString(), e1);
}
// 点击“Exit”菜单后退出程序
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
int selectID = JOptionPane.showConfirmDialog(null,
systemTrayIdea,
systemTrayPromf,
JOptionPane.YES_NO_OPTION);
if (selectID == 0)
{
stopService();
tray.remove(trayIcon);
Runtime.getRuntime().exit(1);
}
}
catch (Exception e3)
{
logger.error("Stop service error: " + e3.toString(), e3);
}
}
});
pop.add(exit);
}
/**
* 读取配置文件信息
*
* @param path 路径
* @param encoding 编码方式名
* @return 配置对象
*/
public Properties getInformationFile(String path, String encoding)
{
Properties property = new Properties();
// 路径为空返回null
if (null == path)
{
return property;
}
InputStreamReader isr = null;
try
{
// 文件不存在返回null
File file = new File(path);
if (!file.exists())
{
return property;
}
// 读取文件
isr = new InputStreamReader(new FileInputStream(path), encoding);
property.load(isr);
return property;
}
catch (Exception e)
{
logger.error("failed to load InputStreamReader", e);
return property;
}
finally
{
closeStream(isr);
}
}
/**
* 关闭流
*
* @param closeable 可关闭流对象
*/
public static void closeStream(Closeable closeable)
{
try
{
if (closeable != null)
{
closeable.close();
}
}
catch (Exception e)
{
logger.error("Failed to close closeable", e);
}
}
/**
* 停止后台任务(mysql、tomcat)
*/
private void stopService()
{
// 关闭后台数据库
boolean mySqlStoped = MySQLAdmin.getInstance().stopService();
String mysqlStopedMsg = mySqlStoped ? "Succeed to stop MySQL server." : "Failed to stop MySQL server.";
// 关闭tomcat服务器
boolean tomcatStoped = TomcatAdmin.getInstance().stopService();
String tomcatStopedMsg = tomcatStoped ? "Succeed to stop Tomcat server." : "Failed to stop Tomcat server.";
MessageBox.showMsgBox("INFO", mysqlStopedMsg);
MessageBox.showMsgBox("INFO", tomcatStopedMsg);
}
/**
* 移除托盘图标
*
*/
public void removeTray()
{
tray.remove(trayIcon);
}
}