Eclipse Plug-in Development:Beginner's Guide(Second Edition)
上QQ阅读APP看书,第一时间看更新

Time for action – getting in focus

To allow the time zone of the clock widgets to be changed, a drop-down box (known as Combo) as well as a Button will be added to the view. The Combo will be created from a set of ZoneId instances.

  1. Create a timeZones field in the ClockView class:
    private Combo timeZones;
  2. At the end of the createPartControl method, add this snippet to create the drop-down list:
    public void createPartControl(Composite parent) {
      ...
      timeZones = new Combo(parent, SWT.DROP_DOWN);
      timeZones.setVisibleItemCount(5);
      for (String zone : ZoneId.getAvailableZoneIds()) {
        timeZones.add(zone);
      }
    }
  3. Run the target Eclipse and open the Clock View again; a list of time zone names will be shown in a drop-down:
  4. It's conventional to set the focus on a particular widget when a view is opened. Implement the appropriate call in the ClockView method setFocus:
    public void setFocus() {
      timeZones.setFocus();
    }
  5. Run Eclipse and show the Clock View; the time zone drop-down widget will be focused automatically.

What just happened?

Every SWT Control has a setFocus method, which is used to switch focus for the application to that particular widget. When the view is focused (which happens both when it's opened and also when the user switches to it after being in a different view), its setFocus method is called.

Note

As will be discussed in Chapter 7, Creating Eclipse 4 Applications, in E4 the method may be called anything and annotated with the @Focus annotation. Conventionally, and to save sanity, it helps to call this method setFocus.