2014-09-25 92 views
0

基本插件定義了應用程序後來啓用或禁用或使(可見)的命令和處理程序。現在我試圖評估雙擊並需要訪問命令。這很簡單:獲取處理程序的可見性

private boolean executeCommand(String commandId) { 
    IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class); 
    try { 
     handlerService.executeCommand(commandId, null); 
     return true; 
    } catch (ExecutionException | NotDefinedException | NotEnabledException 
      | NotHandledException e) { 
     MessageUtil.logError(e); 
     return false; 
    } 
} 

現在的問題是:有兩個處理程序(「編輯」和「意見」),這是可能要執行,我想只執行「編輯」時,它的存在,否則「視圖」。

ICommandService commandService = (ICommandService) getSite().getService(ICommandService.class); 
    Command command = commandService.getCommand(commandId); 
    IHandler handler = command.getHandler(); 

我試着問命令isDefined()isEnabled()isHandled()isEnabled()isHandled()處理程序,但一切都返回true。

如何找出處理程序是否可見?

+0

我不明白爲什麼只執行命令是不夠的。這應該運行當前上下文有效的處理程序。 – 2014-09-25 12:24:57

+0

@ greg-449因爲有兩個命令。如果第一個不包含處理程序(因爲它已停用),我需要調用第二個。 – 2014-09-25 12:37:04

回答

0

我想我終於找到了答案:

public boolean isCommandEnabled(String commandId) { 
    final IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench().getActivitySupport(); 
    final IActivityManager activityManager = activitySupport.getActivityManager(); 

    for (String activityId : (Set<String>) activitySupport.getActivityManager().getDefinedActivityIds()) { 
     // we iterate through all activities... 
     IActivity activity = activityManager.getActivity(activityId); 
     for (IActivityPatternBinding activityBinding : (Set<IActivityPatternBinding>) activity.getActivityPatternBindings()) { 
      // ...and check if one of the bindings match the command... 
      if (isMatch(activityBinding, commandId) && !activity.isEnabled()) { 
       // ... to get the command's enablement 
       return false; 
      } 
     } 
    } 
    // if no activity was found to disable the command, it's enabled by default 
    return true; 
} 

private static boolean isMatch(IActivityPatternBinding activityBinding, String toMatch) { 
    if (activityBinding.isEqualityPattern()) { 
     return activityBinding.getPattern().equals(toMatch); 
    } 
    return activityBinding.getPattern().matcher(toMatch).matches(); 
} 

我真的不知道這是正常的,或只是爲我們的應用程序,但在這個commandId是情況是沿着org.acme.plugin/org.acme.plugin.commands.commandId線的東西。

相關問題