??????? 在一个rcp应用程序中添加托盘的代码如下:
?
?? 添加托盘的代码如下:
???
package com.vnvntrip.plugin.dev.views.custom; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; 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.PlatformUI; import com.vnvntrip.plugin.dev.commons.ImageManager; /** * RCP 项目程序的托盘实现类 * @author longgangbai * */ public class SystemTrayMaster implements SelectionListener ,Listener{ private Menu menu; private MenuItem[] menuItems=new MenuItem[0]; private RestoreWindowListener restoreWindowListener; public SystemTrayMaster(){ restoreWindowListener=new RestoreWindowListener(); } /** * 关闭应用程序 */ protected void closeApplication(){ PlatformUI.getWorkbench().close(); } private final class RestoreWindowListener extends SelectionAdapter{ @Override public void widgetDefaultSelected(SelectionEvent e) { restoreWindow(); } } public void restoreWindow(){ Shell shell=getShell(); shell.open(); shell.setMinimized(false); shell.forceActive(); shell.forceFocus(); } /** * xx菜单所占的资源 */ public void clearItems() { for (MenuItem menuItem : menuItems) { menuItem.removeSelectionListener(restoreWindowListener); menuItem.dispose(); } } /** * 显示菜单的方法 */ public void showMenu(){ clearItems(); MenuItem openItem=new MenuItem(menu,SWT.PUSH); MenuItem closeItem=new MenuItem(menu,SWT.NONE); closeItem.setText("关闭"); closeItem.addSelectionListener(new SelectionListener(){ public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { closeApplication(); } }); menuItems=new MenuItem[]{openItem,closeItem}; openItem.setText("xx"); openItem.addSelectionListener(restoreWindowListener); menu.setVisible(true); } /** * 点击托盘的操作 */ public void widgetSelected(SelectionEvent e) { } /** * 处理的应用 */ public void handleEvent(Event e) { showMenu(); } /** * 双击托盘时的操作 */ public void widgetDefaultSelected(SelectionEvent e) { restoreWindow(); } /** * 获取系统个shell * @return */ private Shell getShell(){ return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); } /** * 图片最小化的操作 */ public void minimizeWindow(){ getShell().setMinimized(true); getShell().setVisible(false); } /** * 创建托盘方法 */ public void createSystemTray(){ Tray tray=Display.getDefault().getSystemTray(); TrayItem item=new TrayItem(tray,SWT.NONE); item.setText("rcp系统"); item.setToolTipText("rcp系统"); Image image=ImageManager.getImage("QQRCP"); //PlatformUI.getWorkbench(). // getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); item.setImage(image); item.addSelectionListener(this); item.addListener(SWT.MenuDetect, this); menu=new Menu(getShell(),SWT.POP_UP); } }
?
?
?
在ApplicationWorkbenchWindowAdvisor类中添加创建托盘的方法:
?
?
package com.vnvntrip.plugin.dev; import org.eclipse.swt.graphics.Point; 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 com.vnvntrip.plugin.dev.views.custom.SystemTrayMaster; /** * rcp的workbenchWindow ,隶属于当前的workbench,可以有多个workbechWindow. * * * 在rcp的workbenchWindowAdvisor的postWidowOpen()中创建一个系统托盘,另外要 * 重写ovveride divWindowShellClose()方法,否则按程序右上角的x,整个程序会被关闭, * 重写实现程序最小化的功能 * @author longgangbai * */ public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { private SystemTrayMaster trayMaster; public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { super(configurer); } public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) { return new ApplicationActionBarAdvisor(configurer); } /** * */ @Override public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(600, 400)); //设置显示工具栏 configurer.setShowCoolBar(true); configurer.setShowStatusLine(false); configurer.setTitle("RCP 技术开发记事本"); } /** * 设置窗体{zd0}化 */ @Override public void postWindowOpen() { super.postWindowOpen(); IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.getWindow().getShell().setMaximized(true); createSystemTray(); } /** * 创建托盘的信息 */ private void createSystemTray(){ trayMaster=new SystemTrayMaster(); trayMaster.createSystemTray(); } /** * 此方法必须重写应用 * @return */ public boolean divWindowShellClose(){ trayMaster.minimizeWindow(); return false; } }
?
?
? 希望大家拍砖!!!
?