为SWT应用程序创建系统托盘

package rcp.test; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.events.ShellEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tray; import org.eclipse.swt.widgets.TrayItem; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; import org.eclipse.ui.application.WorkbenchWindowAdvisor; import org.eclipse.ui.handlers.IHandlerService; import org.eclipse.ui.plugin.AbstractUIPlugin; public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { super(configurer); } public ActionBarAdvisor createActionBarAdvisor( IActionBarConfigurer configurer) { return new ApplicationActionBarAdvisor(configurer); } public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(800, 400)); configurer.setShowCoolBar(false); configurer.setShowStatusLine(false); configurer.setTitle("RCP Application"); configurer.setShowPerspectiveBar(true); } /** * 创建系统托盘 */ @Override public void postWindowOpen() { super.postWindowOpen(); IWorkbenchWindow window=getWindowConfigurer().getWindow(); TrayItem trayItem=initTrayItem(window); createMinimize(window,trayItem); hookPopupMenu(window,trayItem); } private void hookPopupMenu(final IWorkbenchWindow window, TrayItem trayItem) { trayItem.addListener(SWT.MenuDetect, new Listener() { @Override public void handleEvent(Event event) { Menu menu=new Menu(window.getShell(),SWT.POP_UP); MenuItem exit=new MenuItem(menu,SWT.NONE); exit.setText("Exit"); exit.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { IHandlerService handlerService = (IHandlerService) window .getService(IHandlerService.class); try { handlerService.executeCommand(R.Commands.EXIT, event); }catch (Exception e) { e.printStackTrace(); } } }); menu.setVisible(true); } }); } private void createMinimize(final IWorkbenchWindow window, TrayItem trayItem) { window.getShell().addShellListener(new ShellAdapter() { @Override public void shellIconified(ShellEvent e) { window.getShell().setVisible(false); } }); trayItem.addListener(SWT.DefaultSelection, new Listener() { @Override public void handleEvent(Event event) { Shell shell=window.getShell(); if(!shell.isVisible()){ shell.setVisible(true); shell.setMinimized(false); } } }); } private TrayItem initTrayItem(IWorkbenchWindow window) { final Tray tray=window.getShell().getDisplay().getSystemTray(); TrayItem trayItem=new TrayItem(tray,SWT.NONE); Image image=AbstractUIPlugin.imageDescriptorFromPlugin("rcp.test", "/icons/alt_window_32.gif").createImage(); trayItem.setImage(image); trayItem.setToolTipText("TrayItem"); return trayItem; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值