论坛首页 Java企业应用论坛

实现漂亮的系统托盘

浏览 2586 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-13   最后修改:2009-11-26
由于工作忙,对于代码不多做解释,直接贴上来,请大家见谅
package com.netunit.workbench.sys;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.netunit.workbench.actions.LogoffAction;
import com.netunit.workbench.intro.Application;

/**
 * 托盘实现类
 * @author 何明
 *
 */
public class HookSysTray {

	private TrayItem trayItem;

	public HookSysTray() {

	}

	public void createSysTray(final IWorkbenchWindow window) {
		trayItem = initTrayItem(window);
		if (trayItem != null) {
			trayPopupMenu(window);
			trayMinimize(window);
		}
	}

	// 最小化程序窗口
	public void windowMinimized(final Shell shell) {
		shell.setMinimized(true);
		shell.setVisible(false);
	}

	// 最小化程序到托盘
	private void trayMinimize(final IWorkbenchWindow window) {
		window.getShell().addShellListener(new ShellAdapter() {
			public void shellIconified(ShellEvent e) {
				window.getShell().setVisible(false);
			}
		});
		trayItem.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				Shell shell = window.getShell();
				if (!shell.isVisible()) {
					shell.setVisible(true);
					window.getShell().setMinimized(false);
				}
			}
		});
	}

	// 托盘弹出菜单
	private void trayPopupMenu(final IWorkbenchWindow window) {
		trayItem.addListener(SWT.MenuDetect, new Listener() {
			public void handleEvent(Event event) {
				MenuManager trayMenu = new MenuManager();
				Menu menu = trayMenu.createContextMenu(window.getShell());
				fillTrayItem(trayMenu, window);
				menu.setVisible(true);
			}
		});
	}

	// 构造托盘菜单项
	private void fillTrayItem(IMenuManager trayItem,
			final IWorkbenchWindow window) {
		Action exitSystem = new Action("退出系统[&E]", AbstractUIPlugin
				.imageDescriptorFromPlugin(Application.ID,
						"icons/exit.gif")) {
			public void run() {
				PlatformUI.getWorkbench().close();
			}
		};
		trayItem.add(new LogoffAction());
		trayItem.add(exitSystem);
	}

	// 初始化托盘项目的文字和图标
	private TrayItem initTrayItem(final IWorkbenchWindow window) {
		final Tray tray = window.getShell().getDisplay().getSystemTray();
		if (tray == null)
			return null;
		trayItem = new TrayItem(tray, SWT.NONE);
		trayItem.setImage(CacheImage.getINSTANCE().getImage(
				Application.ID, "icons/logo.gif"));
		// 定时显示气泡提示文本
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				window.getShell().getDisplay().syncExec(new Runnable() {
					public void run() {
						ToolTip tip = new ToolTip(window.getShell(),
								SWT.BALLOON | SWT.ICON_INFORMATION);
						tip.setAutoHide(true);
						tip.setMessage("银驼铃MSAP2000网元管理系统");
						tip.setText("欢迎使用");
						trayItem.setToolTip(tip);
						tip.setVisible(true);
					}
				});
			}
		}, 0, 30 * 60 * 1000);
		return trayItem;
	}

	public void Dispose() {
		if (trayItem != null)
			trayItem.dispose();
	}
}


再在ApplicationWorkbenchWindowAdvisor类中添加两个方法
/**
	 * 创建系统托盘
	 */
	public void createSystemTray() {

		hookSysTray = new HookSysTray();

		hookSysTray.createSysTray(getWindowConfigurer().getWindow());

	}

	/**
	 * 判断窗口是否被关闭
	 */
	public boolean preWindowShellClose() {
		hookSysTray.windowMinimized(getWindowConfigurer().getWindow()
				.getShell());
		return false;
	}


再在重写dispose()方法
public void dispose() {
		hookSysTray.Dispose();
		CacheImage.getINSTANCE().dispose();
	}



再在postWindowCreate()方法中添加如下代码即可
createSystemTray();
		String[] args=Platform.getApplicationArgs();
		if(args.length==1&&args[0].equals("system")) 
			getWindowConfigurer().getWindow().getShell().setMinimized(true);

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics