-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloFX.java
More file actions
79 lines (63 loc) · 2.25 KB
/
HelloFX.java
File metadata and controls
79 lines (63 loc) · 2.25 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
package com.example;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import io.github.cdimascio.dotenv.Dotenv;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class HelloFX extends Application {
private NtfyConnection connection;
private ImageServer imageServer;
@Override
public void start(Stage stage) {
Dotenv dotenv = Dotenv.load();
String hostName = dotenv.get("HOST_NAME");
if (hostName == null || hostName.isBlank()) {
showError("Configuration error", "HOST_NAME is not set in .env");
return;
}
connection = new NtfyConnectionImpl(hostName);
try {
Path imageDir = Path.of("images");
Files.createDirectories(imageDir);
imageServer = new ImageServer(8081, imageDir);
} catch (IOException e) {
showError("Image server error", "Could not start local image server:\n" + e.getMessage());
return;
}
FXMLLoader fxmlLoader = new FXMLLoader(HelloFX.class.getResource("hello-view.fxml"));
Scene scene;
try {
scene = new Scene(fxmlLoader.load(), 600, 400);
} catch (IOException e) {
showError("FXML loading error", "Could not load GUI:\n" + e.getMessage());
return;
}
HelloController controller = fxmlLoader.getController();
controller.setConnection(connection);
stage.setTitle("HelloFX Chat");
stage.setScene(scene);
stage.show();
stage.setOnCloseRequest(event -> {
System.out.println("🛑 Application closing...");
try {
if (connection != null) connection.stopReceiving();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (imageServer != null) imageServer.stop();
} catch (Exception e) {
e.printStackTrace();
}
});
}
private void showError(String title, String message) {
System.err.println("❌ " + title + ": " + message);
}
public static void main(String[] args) {
launch();
}
}