-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloModel.java
More file actions
83 lines (68 loc) · 2.41 KB
/
Copy pathHelloModel.java
File metadata and controls
83 lines (68 loc) · 2.41 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
package com.example;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.File;
/**
* Model layer: encapsulates application data and business logic.
*/
public class HelloModel {
private final NtfyConnection connection;
private final ObservableList<NtfyMessageDto> messages = FXCollections.observableArrayList();
private final StringProperty messageToSend = new SimpleStringProperty();
public HelloModel(NtfyConnection connection) {
this.connection = connection;
startReceivingMessages();
}
public ObservableList<NtfyMessageDto> getMessages() {
return messages;
}
public String getMessageToSend() {
return messageToSend.get();
}
public StringProperty messageToSendProperty() {
return messageToSend;
}
public void setMessageToSend(String message) {
messageToSend.set(message);
}
/**
* Returns a greeting based on the current Java and JavaFX versions.
*/
public String getGreeting() {
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
return "Welcome to JavaFX Chat App " + javafxVersion + ", running on Java " + javaVersion + ".";
}
public boolean sendMessage() {
String message = getMessageToSend();
if (message == null || message.trim().isEmpty()) {
return false;
}
return connection.send(message.trim());
}
public boolean sendFile(File file) {
if (file == null || !file.exists()) {
return false;
}
return connection.sendFile(file);
}
private void startReceivingMessages() {
connection.receive(this::addMessageToUI);
}
private void addMessageToUI(NtfyMessageDto message) {
// Check if we're on JavaFX application thread, if not use Platform.runLater
if (Platform.isFxApplicationThread()) {
messages.add(message);
} else {
Platform.runLater(() -> messages.add(message));
}
}
// Test helper method - package private for testing
void addTestMessage(NtfyMessageDto message) {
// Direct add for testing (bypasses Platform.runLater)
messages.add(message);
}
}