-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTimePickerComboBox.java
More file actions
116 lines (94 loc) · 3.34 KB
/
TimePickerComboBox.java
File metadata and controls
116 lines (94 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package de.doubleslash.keeptime.view;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.util.StringConverter;
import java.time.LocalTime;
import java.util.stream.IntStream;
public class TimePickerComboBox extends HBox {
private final ComboBox<Integer> hourBox = new ComboBox<>();
private final ComboBox<Integer> minuteBox = new ComboBox<>();
private final ObjectProperty<LocalTime> value = new SimpleObjectProperty<>(this, "value");
private boolean updating = false;
public TimePickerComboBox() {
init24HourControls();
attachListeners();
setSpacing(4);
}
public ObjectProperty<LocalTime> valueProperty() {
return value;
}
public LocalTime getValue() {
return value.get();
}
public void setValue(LocalTime time) {
value.set(time);
}
private void attachListeners() {
hourBox.valueProperty().addListener((obs, ov, nv) -> updateValueFromUi());
minuteBox.valueProperty().addListener((obs, ov, nv) -> updateValueFromUi());
value.addListener((obs, ov, nv) -> updateUiFromValue(nv));
}
private void init24HourControls() {
// Populate hours 0-23
hourBox.getItems().setAll(IntStream.range(0, 24).boxed().toList());
// Populate minutes 0-59
minuteBox.getItems().setAll(IntStream.range(0, 60).boxed().toList());
// Two-digit formatting for hour/minute
StringConverter<Integer> twoDigitConverter = new StringConverter<>() {
@Override public String toString(Integer object) {
return object == null ? "" : String.format("%02d", object);
}
@Override public Integer fromString(String string) {
try { return Integer.valueOf(string); } catch (Exception e) { return null; }
}
};
hourBox.setConverter(twoDigitConverter);
minuteBox.setConverter(twoDigitConverter);
// Ensure editors exist for formatting updates
hourBox.setEditable(true);
minuteBox.setEditable(true);
getChildren().clear();
getChildren().addAll(hourBox, new Label(":"), minuteBox);
}
private void updateValueFromUi() {
if (updating) return;
Integer hSel = hourBox.getValue();
Integer mSel = minuteBox.getValue();
if (hSel == null || mSel == null) {
value.set(null);
return;
}
int hour24 = hSel;
updating = true;
try {
value.set(LocalTime.of(hour24, mSel));
} finally {
updating = false;
}
}
private void updateUiFromValue(LocalTime t) {
if (updating) return;
updating = true;
try {
if (t == null) {
hourBox.getSelectionModel().clearSelection();
minuteBox.getSelectionModel().clearSelection();
return;
}
int h24 = t.getHour();
int m = t.getMinute();
hourBox.setValue(h24);
minuteBox.setValue(m);
Platform.runLater(() -> {
minuteBox.getEditor().setText(minuteBox.getConverter().toString(minuteBox.getValue()));
hourBox.getEditor().setText(hourBox.getConverter().toString(hourBox.getValue()));
});
} finally {
updating = false;
}
}
}