您的位置:首页 > 其它

RCP应用中创建系统托盘和状态栏

2012-12-28 13:56 148 查看
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

private Image statusImage = null;

private Image trayImage = null;

private TrayItem trayItem = null;

private Image image = null;

public ApplicationWorkbenchWindowAdvisor(
IWorkbenchWindowConfigurer configurer) {
super(configurer);
}

@Override
public void postWindowCreate() {
// 设置默认最大化启动
image = ImageFactory.loadImage("163.com.ico", this.getClass());
getWindowConfigurer().getWindow().getShell().setImage(image);
getWindowConfigurer().getWindow().getShell().setMaximized(true);
super.postWindowCreate();

}

@Override
public ActionBarAdvisor createActionBarAdvisor(
IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}

@Override
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(800, 600));
configurer.setShowMenuBar(true);
configurer.setShowCoolBar(true);
configurer.setShowStatusLine(true);
configurer.setShowProgressIndicator(true);
configurer.setTitle("RCP Mail");
}

@Override
public void postWindowOpen() {

// 创建状态栏
statusImage = AbstractUIPlugin.imageDescriptorFromPlugin("rcp_mail",
"icons/163.com.ico").createImage();
IStatusLineManager statusline = getWindowConfigurer()
.getActionBarConfigurer().getStatusLineManager();
statusline.setMessage(statusImage, "RCP mail 客户端");

// 创建系统托盘
final IWorkbenchWindow window = getWindowConfigurer().getWindow();
trayItem = initTaskItem(window);
if (trayItem != null) {
hookPopupMenu(window);
hookMinimize(window);
}

super.postWindowOpen();
}

private void hookMinimize(final IWorkbenchWindow window) {
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);
window.getShell().setMinimized(false);
}
}
});
}

private void hookPopupMenu(final IWorkbenchWindow window) {
trayItem.addListener(SWT.MenuDetect, new Listener() {
@Override
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("rcp_mail",
"icons/163.com.ico").createImage();
trayItem.setImage(trayImage);
trayItem.setToolTipText("RCP mail");
return trayItem;
}

@Override
public void dispose() {
if (statusImage != null) {
statusImage.dispose();
}
if (image != null) {
image.dispose();
}

if (trayImage != null) {
trayImage.dispose();
trayItem.dispose();
}
super.dispose();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: