@@ -14,43 +14,53 @@ public class HelloModel {
1414 /**
1515 * Returns a greeting based on the current Java and JavaFX versions.
1616 */
17+
18+ private final ObservableList <NtfyMessage > messages = FXCollections .observableArrayList ();
19+ private final BooleanProperty connected = new SimpleBooleanProperty (false );
20+ private boolean isTesting = false ; // <-- Lägg till denna rad
21+
1722 public String getGreeting () {
1823 String javaVersion = System .getProperty ("java.version" );
1924 String javafxVersion = System .getProperty ("javafx.version" );
2025 return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + "." ;
2126 }
2227
23- //Nya fält för chat-funktionalitet
24- private final ObservableList <NtfyMessage > messages = FXCollections .observableArrayList ();
25- private final BooleanProperty connected = new SimpleBooleanProperty (false );
26-
27- //Getter för meddelandelistan
2828 public ObservableList <NtfyMessage > getMessages () {
2929 return messages ;
3030 }
3131
32- //Getter för anslutningstillstånd
32+
3333 public ReadOnlyBooleanProperty connectedProperty () {
3434 return connected ;
3535 }
3636
37- //Metod för att lägga till meddelande
37+ public void setTesting (boolean testing ) { // <-- Lägg till denna metod
38+ this .isTesting = testing ;
39+ }
40+
41+ // Ersätt den gamla runOnFX-metoden med denna:
42+ private void runOnFX (Runnable task ) {
43+ if (isTesting ) { // <-- Ny logik för tester
44+ task .run ();
45+ } else if (Platform .isFxApplicationThread ()) {
46+ task .run ();
47+ } else {
48+ try {
49+ Platform .runLater (task );
50+ } catch (IllegalThreadStateException notInitialized ) {
51+ task .run (); // Fallback för enhetstester (om JavaFX inte är initierat)
52+ }
53+ }
54+
55+ }
56+
3857 public void addMessage (NtfyMessage message ) {
3958 runOnFX (() -> messages .add (message ));
4059 }
4160
42- //Metod för att uppdatera anslutningstillstånd
4361 public void setConnected (boolean connected ) {
4462 runOnFX (() -> this .connected .set (connected ));
4563 }
4664
47- //Dispatcher Utility
48- private static void runOnFX (Runnable task ) {
49- try {
50- if (Platform .isFxApplicationThread ()) task .run ();
51- else Platform .runLater (task );
52- } catch (IllegalThreadStateException notInitialized ) {
53- task .run (); //Fallback för enhetstester
54- }
55- }
65+
5666}
0 commit comments