官术网_书友最值得收藏!

Time for action – propagating selection

When the user selects an item in a viewer component, any registered selection listeners will be notified. In order to forward changes to the E4 selection service, a listener needs to be created to forward the selection to the ESelectionService.

  1. Add the required packages to the bundle's package imports, by right-clicking on the com.packtpub.e4.clock.ui project and navigating to Plug-in Tools | Open Manifest to open the bundle's manifest. On the Dependencies tab, click on Add under the Imported Packages section and enter org.eclipse.e4.ui.workbench.modeling as well as org.eclipse.e4.core.di.annotations.
  2. In order to forward selection changes to the E4 selection service, it must be injected into the view. Add a field ESelectionService selectionService annotated with @Inject and @Optional. When the part is created, the field will be filled with a selection service instance or null as appropriate:
    @Inject
    @Optional
    private ESelectionService selectionService;
  3. The selection changed event needs to trigger finding the selected object, then forwarding that on to the selection service. Add an ISelectionChangeListener to the treeViewer with the following:
    treeViewer.addSelectionChangedListener(event -> {
      // forward selection
      Object selection = ((IStructuredSelection)event.getSelection()).
       getFirstElement();
      if (selection != null && selectionService != null) {
        selectionService.setSelection(selection);
      }
    });
  4. Test the application by putting a breakpoint in the stSelection method, and launch the Eclipse application in Debug mode. Once it is running, open the Time Zone Tree View and select a time zone, after which the debugger should be presented. Verify that the selected time zone and the selectionService have been injected correctly.

What just happened?

To receive selection events from the tree viewer, a listener must be registered to pick up changes. In order to forward this to the E4 selection service, it must be obtained using injection. By marking the field with @Inject, the selection service will be automatically injected when the part is created.

By marking the field as @Optional, if the selection service is not available, then a null value will be injected instead. If the field is not marked as @Optional and the service is not available, the part would fail to be created. Since the selection updates aren't a core part of the view's functionality, it is better to fail safely and work with reduced functionality rather than fail completely.

Finally, when the selection change event occurs, the selected object is extracted from the viewer and then forwarded onto the selection service, but only if both the selection and the selection service are not null.

Tip

Although the selection mechanism in Eclipse 3.x uses ISelection interfaces (almost always IStructuredSelection), the E4 selection service permits objects to be set directly. This also allows the type of the object to be selectively filtered when received. In Eclipse 3.x, the viewer would set the selectionProvider on the viewPart, which would handle the selection changes automatically.

主站蜘蛛池模板: 岚皋县| 昆明市| 四子王旗| 满洲里市| 扶余县| 海门市| 浦北县| 喀喇| 永清县| 武平县| 清远市| 加查县| 万全县| 四平市| 陈巴尔虎旗| 道孚县| 四会市| 云阳县| 垦利县| 连城县| 宝坻区| 沧州市| 双柏县| 垫江县| 白城市| 阳江市| 任丘市| 永和县| 东城区| 柳州市| 阿合奇县| 堆龙德庆县| 凯里市| 许昌市| 玛沁县| 锡林浩特市| 赤壁市| 乌鲁木齐县| 浦东新区| 黎平县| 天津市|