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

Time for action – responding to input

To show the effect of changing the TimeZone, it is necessary to add an hour hand to the clock. When the TimeZone is changed in the drop-down, the hour hand will be updated.

  1. Add a zone field to the ClockWidget along with a setter:
    private ZoneId zone = ZoneId.systemDefault();
    public void setZone(ZoneId zone) {
      this.zone = zone;
    }
  2. Getters and setters can be generated automatically. Once the field is added, navigate to Source | Generate Getters and Setters. It can be used to generate all missing getters and/or setters; in addition, a single getter/setter can be generated by typing set in the class body, followed by Ctrl + Space (Cmd + Space on macOS).
  3. Add an hour hand in the drawClock method using the following:
    e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK));
    ZonedDateTime now = ZonedDateTime.now(zone);
    int hours = now.getHour();
    arc = (3 - hours) * 30 % 360;
    e.gc.fillArc(e.x, e.y, e.width-1, e.height-1, arc - 5, 10);
  4. To update the clock when the time zone is selected, register a SelectionListener on the Combo in the createPartControl method of the ClockView class:
    timeZones.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        String id = timeZones.getText();
        clock3.setZone(ZoneId.of(id));
        clock3.redraw();
      }
      public void widgetDefaultSelected(SelectionEvent e) {
        clock3.setZone(ZoneId.systemDefault());
        clock3.redraw();
      }
    });
  5. Run the target Eclipse instance, and change the time zone. The updates should be drawn on the last clock instance:

What just happened?

The Combo method addSelectionListener notifies any changes in the drop-down list. When a notification is received, the text from the Combo box is used to look up the corresponding time zone from the ZoneId class.

If the selection is not found or the default selection is chosen (in this case, the one with no value), then the offset is reset to the system time zone.

The clock's hour hand doesn't quite behave properly; typically, the hour hand is shorter than the second hand, and the hour hand jumps between hours instead of smoothly moving around as time progresses. Fixing this is left as an exercise for the reader.

To render the changes immediately, the clock is asked to redraw itself. This could be done inside the ClockView method setZone; but it would have to verify that it was done from the SWT thread, or arrange for it to be posted on the thread asynchronously. Instead, for convenience clock3.redraw() is done immediately after setting the offset while still inside the SWT thread.

Pop quiz: understanding widgets

Q1. How do you mark the default widget of a view?

Q2. How do you update a widget after modifying it?

Q3. What listener can you register with a Combo?

Q4. What's the purpose of the widgetDefaultSelected method?

Have a go hero – updating the clock widget

Now that the ClockWidget can handle time zones, do the following:

  • Update the hour hand so that the position is calculated based on fractional hours
  • Display the time zone underneath the clock face in the ClockWidget
  • Show whether the time displayed is in summer or not
主站蜘蛛池模板: 祁东县| 蚌埠市| 清苑县| 浮梁县| 庆安县| 宜丰县| 浠水县| 郁南县| 化州市| 克山县| 太原市| 班玛县| 云阳县| 九江市| 潢川县| 江阴市| 新兴县| 宜兰县| 渑池县| 平谷区| 古田县| 周至县| 汤原县| 盐亭县| 顺义区| 宿迁市| 蓬莱市| 申扎县| 安平县| 龙里县| 巴里| 松原市| 桃源县| 婺源县| 遂平县| 高唐县| 鱼台县| 开江县| 安乡县| 乌兰浩特市| 陵水|