在开发RCP工程过程中,有时候在最小化窗口的时候希望将窗口托管在系统栏。添加代码如下:
在ApplicationWorkbenchWindowAdvisor类中添加下面的代码:
private Image statusImage;
private TrayItem trayItem;
private Image trayImage;
private ApplicationActionBarAdvisor actionBarAdvisor; public void postWindowOpen() { initStatusLine(); final IWorkbenchWindow window = getWindowConfigurer().getWindow(); trayItem = initTaskItem(window); if (trayItem != null) { hookPopupMenu(window); hookMinimize(window); } } private void hookMinimize(final IWorkbenchWindow window) { window.getShell().addShellListener(new ShellAdapter() { public void shellIconified(ShellEvent e) { window.getShell().setVisible(false); } }); trayItem.addListener(SWT.DefaultSelection, new Listener() { public void handleEvent(Event event) { Shell shell = window.getShell(); if (!shell.isVisible()) { shell.setVisible(true); window.getShell().setMinimized(false); } } }); } private void hookPopupMenu(final IWorkbenchWindow window) { trayItem.addListener(SWT.MenuDetect, new Listener() { public void handleEvent(Event event) { MenuManager trayMenu = new MenuManager(); Menu menu = trayMenu.createContextMenu(window.getShell()); actionBarAdvisor.fillTrayItem(trayMenu); menu.setVisible(true); } }); } private TrayItem initTaskItem(IWorkbenchWindow window) { final Tray tray = window.getShell().getDisplay().getSystemTray(); if (tray == null) return null; TrayItem trayItem = new TrayItem(tray, SWT.NONE); trayImage = AbstractUIPlugin.imageDescriptorFromPlugin( "org.eclipsercp.hyperbola", IImageKeys.ONLINE).createImage(); trayItem.setImage(trayImage); trayItem.setToolTipText("Hyperbola"); return trayItem; } public void dispose() { if (trayImage != null) { trayImage.dispose(); trayItem.dispose(); } }
在ApplicationActionBarAdvisor中添加下面函数
protected void fillTrayItem(IMenuManager trayItem) {
trayItem.add(aboutAction);
trayItem.add(exitAction);
}