我們知道,Title 是在 ApplicationWorkbenchWindowAdvisor 裡面通過
public void preWindowOpen()
{ IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(600, 500)); configurer.setShowCoolBar(false); configurer.setShowStatusLine(true); configurer.setShowProgressIndicator(true); configurer.setTitle(xxx); }
來實現的,那麼如何在運行時動態更改它呢?
最開始一直在想如何獲得IWorkbenchWindowConfigurer,但是一點頭緒都沒有,後來到RCP新聞群組裡面問了一下,兩個人給了回複,不過都不怎麼滿意,但其中一個給了我啟示,他信中寫道:
I've used the following technique:
public static void changeAppTitle(String newTitle) { Display display = Display.getDefault(); if (display != null) { // Look at all the shells and pick the first one // that is a workbench window. Shell shells[] = display.getShells(); for (int i = 0; i < shells.length; i++) { Object data = shells[i].getData(); // Check whether this shell points to the // Application main window's shell if (data instanceof IWorkbenchWindow) { shells[i].setText(newTitle); break; } } } }
既然可以通過IWorkbenchWindow的Shell來設定標題,那麼一切就好解決了。在ActionBarAdvisor中初始化Action時,把WorkbenchWindow作為參數傳入構造器中,然後在Action中就可以使用window.getShell().setText()了 :)
備忘:本文轉載自:http://www.iteye.com/topic/40853