簡體   English   中英

Eclipse RCP:用於視圖和編輯器的命令處理程序

[英]Eclipse RCP: Command handler for view and editor

我有一個命令已添加到視圖(具有樹形視圖)的上下文菜單和自定義編輯器的上下文菜單中。

在我的處理程序中,我有什么方法可以區分調用命令的上下文菜單? 這是因為在視圖的情況下,我使用類似的方法來獲取所需的數據,

ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
                .getActivePage().getSelection();
        if (selection != null & selection instanceof IStructuredSelection)
        {
            IStructuredSelection strucSelection = (IStructuredSelection) selection;
.....

對於編輯器,選擇當然為空。 因此,我添加了以下內容來處理編輯器部分,

IEditorPart editor = HandlerUtil.getActiveEditor(event);
IEditorInput input = editor.getEditorInput();
        IPath path = ((FileEditorInput)input).getPath();

但是發生的是,即使我從視圖執行此命令,它也始終會返回一個活動的編輯器。 這是否意味着我必須根據視圖或編輯器是否處於焦點狀態來編寫將處於活動狀態的單獨處理程序?

謝謝!

這是否意味着我必須根據視圖或編輯器是否處於焦點狀態來編寫將處於活動狀態的單獨處理程序?

是的,它確實。 但是,您單獨的處理程序可以是小的類,它們可以調用公共類來完成命令的大部分工作。 我不知道您的命令需要執行什么數據,但是您單獨的處理程序可以准備該數據,並通過一個或多個構造函數將其傳遞給公共類。

您可以具有相同的處理程序,並根據HandlerUtil.getActiveEditor(event) (當活動部件是編輯器時,該值僅為非null )和HandlerUtil.getActiveView(event) (當該情況下的非null時)來決定行為。活動部分是視圖)。

或者,您可以讓處理程序實現IExecutableExtension並提供方法setInitializationData(IConfigurationElement config, String propertyName, Object data)' which is invoked when the handler is created. Here setInitializationData(IConfigurationElement config, String propertyName, Object data)' which is invoked when the handler is created. Here數據is usually given in the attribute of the handler declaration (see the Javadoc of is usually given in the class attribute of the handler declaration (see the Javadoc of is usually given in the attribute of the handler declaration (see the Javadoc of setInitializationData` attribute of the handler declaration (see the Javadoc of )...

在這種情況下,我更喜歡前一種方法,但是當我無法確定運行時的行為時,我經常使用后一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM