-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloController.java
More file actions
156 lines (126 loc) · 4.92 KB
/
HelloController.java
File metadata and controls
156 lines (126 loc) · 4.92 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.example;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/**
* Controller for the chat app.
* <p>
* Connects the FXML view to the HelloModel.
* Handles sending messages, changing topics, and updating the UI.
*/
public class HelloController {
private final HelloModel model = new HelloModel(new NtfyConnectionImpl());
@FXML
private Button sendButton;
@FXML
private Label messageLabel;
@FXML
private Label topicLabel;
@FXML
private ListView<NtfyMessageDto> messageView;
@FXML
private TextArea messageInput;
@FXML
private TextField topicInput;
@FXML
private Button changeTopicButton;
private final DateTimeFormatter timeFormatter =
DateTimeFormatter.ofPattern("HH:mm:ss")
.withZone(ZoneId.systemDefault());
@FXML
private void initialize() {
messageLabel.setText(model.getGreeting());
Platform.runLater(() -> messageInput.requestFocus());
topicLabel.setText("/" + model.getCurrentTopic());
model.currentTopicProperty().addListener((obs, oldVal, newVal) -> {
topicLabel.setText("/" + newVal);
});
messageView.setItems(model.getMessages());
messageInput.textProperty().bindBidirectional(model.messageToSendProperty());
sendButton.disableProperty().bind(Bindings.createBooleanBinding(
() -> {
String text = messageInput.getText();
return text == null || text.trim().isEmpty();
},
messageInput.textProperty()
));
if (changeTopicButton != null) {
changeTopicButton.disableProperty().bind(Bindings.createBooleanBinding(
() -> {
String text = topicInput.getText();
return text == null || text.trim().isEmpty();
},
topicInput.textProperty()
));
}
messageView.setCellFactory(lv -> new ListCell<>() {
@Override
protected void updateItem(NtfyMessageDto msg, boolean empty) {
super.updateItem(msg, empty);
if (empty || msg == null || msg.message() == null || msg.message().isBlank()) {
setText(null);
setGraphic(null);
} else {
// Skapa bubble-label
Label bubble = new Label(msg.message());
bubble.setWrapText(true);
bubble.setMaxWidth(250);
bubble.setPadding(new Insets(10));
bubble.getStyleClass().add("chat-bubble"); // Basstyle
HBox container = new HBox(bubble);
container.setPadding(new Insets(5));
// Använd CSS-klasser för skickat/mottaget
if (model.getUserId().equals(msg.id())) {
bubble.getStyleClass().add("chat-bubble-sent");
container.setAlignment(Pos.CENTER_RIGHT);
} else {
bubble.getStyleClass().add("chat-bubble-received");
container.setAlignment(Pos.CENTER_LEFT);
}
setText(null);
setGraphic(container);
}
}
});
// Scrolla ner till senaste meddelandet
model.getMessages().addListener((javafx.collections.ListChangeListener<NtfyMessageDto>) change -> {
Platform.runLater(() -> {
if (!messageView.getItems().isEmpty()) {
messageView.scrollTo(messageView.getItems().size() - 1);
}
});
});
}
@FXML
private void sendMessage(ActionEvent actionEvent) {
model.sendMessageAsync(success -> {
if (success) {
Platform.runLater(() -> messageInput.clear());
Platform.runLater(() -> messageInput.requestFocus());
} else {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Send Failed");
alert.setHeaderText("Failed to send message");
alert.setContentText("Could not send your message. Please try again.");
alert.showAndWait();
});
}
});
}
@FXML
private void changeTopic(ActionEvent actionEvent) {
String newTopic = topicInput.getText();
if (newTopic != null && !newTopic.isBlank()) {
model.setCurrentTopic(newTopic);
topicInput.clear();
}
}
}