定義動作

瀏覽器範例中 BrowserAdvisor 提供的主要自訂是指定工作台視窗的動作列內容:

	public void fillActionBars(IWorkbenchWindow window, IActionBarConfigurer configurer, int flags) {
		...
		BrowserActionBuilder builder = new BrowserActionBuilder(window);
		getWorkbenchConfigurer().getWindowConfigurer(window).setData(BUILDER_KEY, builder); 
		builder.fillActionBars(configurer, flags);
	}

讓我們更仔細瞭解如何在 BrowserActionBuilder 中定義這些動作。我們特別要查看瀏覽器視圖所處理的動作。

private void makeActions() {
		...				
		backAction = new RetargetAction("back", "&Back");  
		backAction.setToolTipText("Back");
		backAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_BACK));
		window.getPartService().addPartListener(backAction);
		
		forwardAction = new RetargetAction("forward", "&Forward");  
		forwardAction.setToolTipText("Forward");
		forwardAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
		window.getPartService().addPartListener(forwardAction);
		
		stopAction = new RetargetAction("stop", "Sto&p"); 
		stopAction.setToolTipText("Stop");
		window.getPartService().addPartListener(stopAction);
		
		refreshAction = new RetargetAction("refresh", "&Refresh");  
		refreshAction.setToolTipText("Refresh");
		window.getPartService().addPartListener(refreshAction);
		...
	}

這些動作是定義為可重訂目標的動作,因而使個別視圖可以實作處理常式動作。BrowserView 在建立視圖的控制項時,會建立其處理常式動作與視窗可重訂目標動作的關聯性:

	private Browser createBrowser(Composite parent, final IActionBars actionBars) {
		
		...
		actionBars.setGlobalActionHandler("back", backAction); 
		actionBars.setGlobalActionHandler("forward", forwardAction); 
		actionBars.setGlobalActionHandler("stop", stopAction); 
		actionBars.setGlobalActionHandler("refresh", refreshAction); 
		...
	}

這些動作是在第一次建立視圖時所建立的。

	private Action backAction = new Action("Back") {
public void run() {			browser.back();
		}
	};

請參閱可重訂目標的動作以取得可重訂目標動作以及如何定義和實作的完整討論。