/* Only enable for Primary Window */
IWorkbenchWindow primaryWindow = OwlUI.getPrimaryWindow();
if (primaryWindow == null || !primaryWindow.equals(getWindowConfigurer().getWindow()))
return false;
final Shell shell = primaryWindow.getShell();
final Tray tray = shell.getDisplay().getSystemTray();
/* Tray not support on the OS */
if (tray == null)
return false;
/* Create Item in Tray */
fTrayItem = new TrayItem(tray, SWT.NONE);
fTrayItem.setToolTipText("RSSOwl");
fTrayEnabled = true;
if (Application.IS_WINDOWS)
fTrayItem.setVisible(false);
/* Apply Image */
fTrayItem.setImage(OwlUI.getImage(fResources, OwlUI.TRAY_OWL));
/* Minimize to Tray on Shell Iconify if set */
fTrayShellListener = new ShellAdapter() {
@Override
public void shellIconified(ShellEvent e) {
if (!fBlockIconifyEvent && (fMinimizeFromClose || fPreferences.getBoolean(DefaultPreferences.TRAY_ON_MINIMIZE)))
moveToTray(shell);
}
};
shell.addShellListener(fTrayShellListener);
/* Show Menu on Selection */
fTrayItem.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
MenuManager trayMenu = new MenuManager();
/* Restore */
trayMenu.add(new ContributionItem() {
@Override
public void fill(Menu menu, int index) {
MenuItem restoreItem = new MenuItem(menu, SWT.PUSH);
restoreItem.setText("Restore");
restoreItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
restoreFromTray(shell);
}
});
menu.setDefaultItem(restoreItem);
}
});
/* Separator */
trayMenu.add(new Separator());
/* Other Items */
fActionBarAdvisor.fillTrayItem(trayMenu);
Menu menu = trayMenu.createContextMenu(shell);
menu.setVisible(true);
}
});
/* Handle DefaultSelection */
fTrayItem.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event event) {
/* Restore from Tray */
if (!shell.isVisible())
restoreFromTray(shell);
/* Move to Tray */
else if (!Application.IS_WINDOWS)
moveToTray(shell);
}
});
/* Indicate new News in Tray */
fNewsListener = new NewsAdapter() {
@Override
public void entitiesAdded(Set<NewsEvent> events) {
JobRunner.runInUIThread(fTrayItem, new Runnable() {
public void run() {
/* Update Icon only when Tray is visible and not yet teasing */
if (!fTrayItem.getVisible() || fTrayTeasing || shell.getVisible())
return;
fTrayTeasing = true;
fTrayItem.setImage(OwlUI.getImage(fResources, OwlUI.TRAY_OWL_TEASING));
}