【问题标题】:Trigger evaluation of PropertyTester触发 PropertyTester 评估
【发布时间】:2017-09-25 03:49:19
【问题描述】:

code from two years back 必须升级到 E4,现在一堆东西不能用了。其中之一是IEvaluationService,如果这样使用:

<handler class="org.acme.PrintHandler" commandId="org.eclipse.ui.file.print">
  <activeWhen>
    <with variable="activePart">
      <test property="org.acme.printable" />
    </with>
  </activeWhen>
</handler>
IEvaluationService service = (IEvaluationService) PlatformUI.getWorkbench().getService(IEvaluationService.class);
service.requestEvaluation("org.acme.printable");

我如何(重新)触发对PropertyTester 的评估?由于 E4 实际上还没有准备好投入生产,我需要 E3(兼容层)的解决方法。

Related question - 但该用户正在 E4 中搜索等价物,而我需要在 E3 中工作的。

有趣的是,如果我用&lt;enabledWhen&gt; 替换&lt;activeWhen&gt; 标签,它就可以工作。在这种情况下,IEventBroker#post 和 IEventBroker#send 也可以工作。

这是similar question。该用户使用 Eclipse 4.2 - 我测试了 4.5、4.6 和 4.7 的问题。

【问题讨论】:

    标签: eclipse-plugin e4


    【解决方案1】:

    我将分享我的解决方法,它不好,并且在所有情况下都不起作用。只有真正有效,因为在我的用例中,我有一个 IWorkbenchPart 和一个 ISelectionProvider... 但也许它会帮助下一个人:

    IWorkbenchPart activePart = // get active view or editor
    ISelectionProvider selectionProvider = activePart.getSite().getSelectionProvider();
    ISelection selection = selectionProvider.getSelection();
    selectionProvider.setSelection(new StructuredSelection());
    selectionProvider.setSelection(selection);
    

    此代码只是重置选择,通常会触发PropertyTester。如果没有选择任何内容,我认为它不会起作用。

    【讨论】:

      【解决方案2】:
      eventBroker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
      

      另请参阅Eclipse bug 436755 和 Eclipse Wiki: Eclipse 4 - RCP - Event Model

      【讨论】:

      • 这仅适用于标签&lt;enabledWhen&gt;,不适用于&lt;activeWhen&gt;。但IEvaluationService 也适用于&lt;enabledWhen&gt;。
      【解决方案3】:

      EvaluationService 与 E3 兼容层中的 API 兼容。但在 E4 中的实现完全不同,导致requestEvaluation 的行为根本不同。

      我能找到的这个问题的最佳解决方案是手动停用和激活当前活动部件的所有上下文。这会导致内部重新评估,并在需要时重新渲染各个部分的所有 UI 元素。

      有人可能会争辩说,这比请求评估非常具体的属性效率低,因为评估服务应该这样做。但由于评估仅限于活动部分,因此不应产生太多开销。而且它确实在全球范围内工作,因为不再需要特定的属性字符串。

      唯一未涵盖的用例可能是您的 RCP 应用程序的主工具栏。

      /**
       * Triggers evaluation of all UI elements (buttons, etc.) of the active part.
       * Also causes test of all property testers of all opened parts implicitly.
       * Workaround of the broken <code>IEvaluationService.requestEvaluation</code>.
       */
      public static void triggerUIElementsEvaluation() {
          try {
              final EPartService partService = PlatformUI.getWorkbench().getService(EPartService.class);
              final MPart activePart = partService.getActivePart();
      
              /* Toggle context of active part to trigger re-evaluation of its UI elements. */
              if (activePart != null) {
                  activePart.getContext().deactivate();
                  activePart.getContext().activateBranch();
              }
          } catch (IllegalStateException e) {
              /* Ignore "Application does not have an active window" exception to allow program to continue. */
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-06-16
        • 2015-10-15
        • 2014-01-31
        • 1970-01-01
        • 2010-10-23
        • 2021-01-16
        • 2014-06-17
        • 1970-01-01
        相关资源
        最近更新 更多