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.
zone
field to the ClockWidget
along with a setter:private ZoneId zone = ZoneId.systemDefault(); public void setZone(ZoneId zone) { this.zone = zone; }
set
in the class body, followed by Ctrl + Space (Cmd + Space on macOS).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);
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(); } });
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.
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?
3.142.196.27