-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloController.java
More file actions
86 lines (71 loc) · 2.68 KB
/
Copy pathHelloController.java
File metadata and controls
86 lines (71 loc) · 2.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
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
package com.example;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
/**
* Controller layer: mediates between the view (FXML) and the model.
*/
public class HelloController {
private final HelloModel model = new HelloModel(new NtfyConnectionImpl());
public ListView<NtfyMessageDto> messageView;
@FXML
private Label messageLabel;
@FXML
private TextField messageField;
@FXML
private void initialize() {
if (messageLabel != null) {
messageLabel.setText(model.getGreeting());
}
messageView.setItems(model.getMessages());
messageView.setCellFactory(list -> new ListCell<>() {
private final Label messageLabel = new Label();
private final HBox bubble = new HBox(messageLabel);
{
bubble.setPadding(new Insets(5, 10, 5, 10));
bubble.setMaxWidth(200);
messageLabel.setWrapText(true);
bubble.getStyleClass().add("chat-bubble");
}
@Override
protected void updateItem(NtfyMessageDto item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
} else {
// Format tid + text
java.time.LocalTime time = java.time.Instant.ofEpochSecond(item.time())
.atZone(java.time.ZoneId.systemDefault())
.toLocalTime();
String formattedTime = time.format(java.time.format.DateTimeFormatter.ofPattern("HH:mm"));
messageLabel.setText(formattedTime + "\n" + item.message());
setGraphic(bubble);
}
}
});
model.messageToSendProperty().bind(messageField.textProperty());
}
public void sendMessage(ActionEvent actionEvent) {
String message = messageField.getText();
if(message == null || message.isBlank()){
showTemporaryAlert("You must write something before sending!");
return;
}
model.sendMessage();
messageField.clear();
}
private void showTemporaryAlert(String alertMessage) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setHeaderText(null);
alert.setContentText(alertMessage);
alert.initOwner(messageField.getScene().getWindow());
alert.show();
new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
}).start();
}
}