package testrcp.app;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
private TrayItem trayItem;//系统托盘对象
private Image trayImage;//系统托盘图标对象
/** 程序的菜单条 */
private ApplicationActionBarAdvisor actionBarAdvisor;
public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
/** 创建菜单条对象 */
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
actionBarAdvisor = new ApplicationActionBarAdvisor(configurer);
return actionBarAdvisor;
}
/** 打开窗口前调用该方法,对窗口初始化设置 */
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(600, 400));
configurer.setShowCoolBar(true);//工具栏
configurer.setShowStatusLine(false);//状态栏
configurer.setShowPerspectiveBar(true);//透视图
configurer.setShowProgressIndicator(true);//进度条
final IWorkbenchWindow window = super.getWindowConfigurer().getWindow();
/** 创建系统托盘 */
trayItem = initTrayItem(window);
/** 如果支持系统托盘,则创建托盘的菜单 */
if(trayItem != null) {
createPopupMenu(window);
}
}
/**
* 创建系统托盘菜单
*
* @param window
* 工作台窗口对象
*/
private void createPopupMenu(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());
/**
* 调用fillTrayItem方法创建系统托盘对象,可以直接利用菜单栏中的操作 而不需要,重新创建操作
*/
actionBarAdvisor.fillTrayItem(trayMenu);
menu.setVisible(true);
}
});
}
/**
* 初始化系统托盘对象
*
* @param window
* 工作台窗口对象
* @return 该程序所对应的系统托盘对象
*/
private TrayItem initTrayItem(IWorkbenchWindow window) {
final Tray tray = Display.getCurrent().getSystemTray();
if(tray == null)
return null;
TrayItem trayItem = new TrayItem(tray, SWT.NONE);
trayImage = Activator.getImageDescriptor("icons/logo.gif").createImage();
trayItem.setImage(trayImage);
trayItem.setToolTipText("System Tray");
return trayItem;
}
@Override
public void postWindowOpen() {
//窗口居中显示
Shell shell = getWindowConfigurer().getWindow().getShell();
Rectangle screenSize = Display.getDefault().getClientArea();
Rectangle frameSize = shell.getBounds();
shell.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
}
/** 释放窗口,释放系统托盘 */
public void dispose() {
if (trayImage != null) {
trayImage.dispose();
trayItem.dispose();
}
}
}
posted on 2008-12-05 23:42
Ke 阅读(911)
评论(0) 编辑 收藏 所属分类:
eclipse RCP