-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatController.java
More file actions
142 lines (120 loc) · 4.57 KB
/
ChatController.java
File metadata and controls
142 lines (120 loc) · 4.57 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
package com.example.controller;
import com.example.model.ChatModel;
import com.example.model.NtfyMessage;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.stage.FileChooser;
import java.io.File;
public class ChatController {
@FXML
private ListView<String> messageListView;
@FXML
private TextField messageInput;
@FXML
private Label statusLabel;
private ChatModel model;
@FXML
public void initialize() {
this.model = new ChatModel();
updateStatusOnline();
messageListView.setCellFactory(listView -> new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
setStyle("");
} else {
setText(item);
setAlignment(Pos.CENTER_LEFT);
setStyle("-fx-background-color: rgba(255, 250, 240, 0.6); " +
"-fx-text-fill: #3d3d3d; " +
"-fx-padding: 14 18 14 18; " +
"-fx-border-color: rgba(214, 69, 69, 0.15); " +
"-fx-border-width: 0 0 0 3; " +
"-fx-font-size: 13px; " +
"-fx-background-radius: 0; " +
"-fx-border-radius: 0; " +
"-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.05), 3, 0, 0, 2);");
}
}
});
model.getMessages().addListener((javafx.collections.ListChangeListener.Change<? extends NtfyMessage> change) -> {
while (change.next()) {
if (change.wasAdded()) {
for (NtfyMessage msg : change.getAddedSubList()) {
String formatted = "🌸 " + msg.message();
messageListView.getItems().add(formatted);
}
messageListView.scrollTo(messageListView.getItems().size() - 1);
}
}
});
}
@FXML
private void handleSendButtonAction() {
String text = messageInput.getText();
if (text != null && !text.trim().isEmpty()) {
Task<Void> task = new Task<>() {
@Override
protected Void call() throws Exception {
model.sendMessage(text.trim());
return null;
}
};
task.setOnSucceeded(e -> {
messageInput.clear();
updateStatusOnline();
});
task.setOnFailed(e -> {
System.err.println("Send failed: " + task.getException().getMessage());
updateStatusOffline();
});
new Thread(task).start();
}
}
@FXML
private void handleAttachFileAction() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select File");
File file = fileChooser.showOpenDialog(messageInput.getScene().getWindow());
if (file != null) {
Task<Void> task = new Task<>() {
@Override
protected Void call() throws Exception {
model.sendFile(file);
return null;
}
};
task.setOnSucceeded(e -> {
System.out.println("✅ File info sent: " + file.getName());
});
task.setOnFailed(e -> {
System.err.println("❌ File send failed: " + task.getException().getMessage());
});
new Thread(task).start();
}
}
private void updateStatusOnline() {
if (statusLabel != null) {
statusLabel.setText("● online");
statusLabel.setStyle("-fx-text-fill: #c93939; " +
"-fx-font-size: 10px; " +
"-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 2, 0, 0, 1);");
}
}
private void updateStatusOffline() {
if (statusLabel != null) {
statusLabel.setText("● offline");
statusLabel.setStyle("-fx-text-fill: #6b5d54; " +
"-fx-font-size: 10px; " +
"-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 2, 0, 0, 1);");
}
}
}