1. 在WorkbenchWindowAdvisor实现类中初始化trayitem
public void postWindowOpen() {
initStatusLine();
final IWorkbenchWindow window = getWindowConfigurer().getWindow();
trayItem = initTaskItem(window);
if (trayItem != null) {
hookPopupMenu(window);
hookMinimize(window);
}

}

//初始化trayitem
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 = Activator.getImageDescriptor(IImageKeys.ONLINE)
.createImage();
trayItem.setImage(trayImage);
trayItem.setToolTipText("test");
return trayItem;
}

//初始化trayItem上的context menu
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);
}
});
}
//自定义minimize到系统托盘和恢复的行为
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);
}
}
});
}
2. fillTrayItem可以写在ActionBarAdvisor的实现类中
protected void fillTrayItem(IMenuManager trayItem) {
trayItem.add(aboutAction);
trayItem.add(exitAction);
}
转自http://www.ceclipse.org/bbs/read-cec-tid-12193.html





















































2. fillTrayItem可以写在ActionBarAdvisor的实现类中




转自http://www.ceclipse.org/bbs/read-cec-tid-12193.html