Trigger evaluation of PropertyTester

前端 未结 3 815
再見小時候
再見小時候 2020-12-22 12:09

The code from two years back had to be upgraded to E4, and now a bunch of stuff does not work anymore. One of these is the IEvaluationService if used like this:

相关标签:
3条回答
  • 2020-12-22 12:36

    The EvaluationService is API compatible in the E3 Compatibility Layer. But the implementation in E4 is completely different, causing the behaviour of requestEvaluation to be fundamentally different.

    The best solution to this problem I could find is to manually deactivate and activate all contexts of currently active parts. This causes internally to re-evaluate and, when required, re-render all UI elements of the respective parts.

    One could argue this is less efficient than requesting the evaluation of a very specific property, as the EvaluationService is supposed to do. But since the evaluation is limited to active parts only, it should not create too much overhead. And it does work globally, as no specific property string is required anymore.

    The only usecase not covered by this yet may be the main toolbar of your RCP application.

    /**
     * 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. */
        }
    }
    
    0 讨论(0)
  • 2020-12-22 12:44
    eventBroker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
    

    See also Eclipse bug 436755 and Eclipse Wiki: Eclipse 4 - RCP - Event Model

    0 讨论(0)
  • 2020-12-22 12:48

    I'm going to share my workaround, which is not good, and does not work in alles cases. And only really works because in my use case I have a IWorkbenchPart with an ISelectionProvider... but maybe it will help the next person:

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

    This code just resets the selection, which normally triggers the PropertyTester. I don't think it'll work if nothing was selected.

    0 讨论(0)
提交回复
热议问题