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

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
主站蜘蛛池模板: 亚东县| 南部县| 阜阳市| 宁远县| 嘉荫县| 岳普湖县| 武山县| 宜黄县| 喀喇沁旗| 瓮安县| 温州市| 贵州省| 安溪县| 德保县| 关岭| 洛阳市| 莎车县| 依兰县| 丘北县| 内乡县| 常熟市| 县级市| 于都县| 嘉峪关市| 沈阳市| 沅江市| 天峻县| 石家庄市| 炎陵县| 贵溪市| 安徽省| 金阳县| 中江县| 焉耆| 池州市| 娄底市| 肇庆市| 揭西县| 沧州市| 临江市| 扶沟县|