-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloController.java
More file actions
59 lines (47 loc) · 1.68 KB
/
HelloController.java
File metadata and controls
59 lines (47 loc) · 1.68 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
package com.example;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
/**
* Controller layer: mediates between the view (FXML) and the model.
*/
public class HelloController {
private final HelloModel model = new HelloModel(new NtfyConnectionImpl());
@FXML
public ListView<NtfyMessageDto> messageView;
@FXML
private Label messageLabel;
@FXML
private TextField messageInput;
@FXML
private void initialize() {
if (messageLabel != null) {
messageLabel.setText(model.getGreeting());
}
messageView.setItems(model.getMessages());
if (messageInput != null) {
messageInput.textProperty().bindBidirectional(model.messageToSendProperty());
}
messageView.setCellFactory(param -> new ListCell<>() {
@Override
protected void updateItem(NtfyMessageDto item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
var time = java.time.Instant.ofEpochSecond(item.time())
.atZone(java.time.ZoneId.systemDefault())
.toLocalTime();
var formatter = java.time.format.DateTimeFormatter.ofPattern("HH:mm");
setText(time.format(formatter) + " " + item.message());
}
}
});
}
public void sendMessage(ActionEvent actionEvent) {
model.sendMessage();
}
}