RCP應用中創建系統托盤和狀態欄

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();

	}
}

發佈了11 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章