33import javafx .application .Platform ;
44import javafx .event .ActionEvent ;
55import javafx .fxml .FXML ;
6- import javafx .scene .control .Label ;
7- import javafx .scene .control .ListView ;
6+ import javafx .scene .control .*;
87import javafx .stage .FileChooser ;
98
9+ import javafx .scene .layout .VBox ;
10+ import javafx .geometry .Insets ;
11+ import javafx .scene .control .Label ;
12+
13+ import java .awt .*;
14+ import java .net .URI ;
15+ import java .time .Instant ;
16+ import java .time .ZoneId ;
17+
18+
1019import java .io .File ;
1120import java .nio .file .Path ;
1221
22+ import static com .example .HelloModel .runOnFx ;
23+
1324/**
1425 * Controller layer: mediates between the view (FXML) and the model.
1526 */
@@ -25,10 +36,56 @@ public class HelloController {
2536 @ FXML
2637 private void initialize () {
2738 System .out .println ("Controller init: kopplar ListView" );
39+
2840 if (messageLabel != null ) {
2941 messageLabel .setText (model .getGreeting ());
3042 }
43+
3144 messageView .setItems (model .getMessages ());
45+
46+ messageView .setCellFactory (listView -> new ListCell <>() {
47+ @ Override
48+ protected void updateItem (NtfyMessageDto item , boolean empty ) {
49+ super .updateItem (item , empty );
50+ if (empty || item == null ) {
51+ setText (null );
52+ setGraphic (null );
53+ } else {
54+ VBox container = new VBox ();
55+ container .setSpacing (4 );
56+
57+ Label topicLabel = new Label (item .topic ());
58+ topicLabel .setStyle ("-fx-font-weight: bold; -fx-text-fill: #2a9df4;" );
59+
60+ Label messageLabel = new Label (item .message ());
61+ messageLabel .setWrapText (true );
62+ messageLabel .setStyle ("-fx-text-fill: #333333;" );
63+
64+ Label timeLabel = new Label ("⏰ " + Instant .ofEpochMilli (item .time ()).atZone (ZoneId .systemDefault ()).toLocalDateTime ());
65+ timeLabel .setStyle ("-fx-font-size: 10px; -fx-text-fill: #888888;" );
66+
67+ container .getChildren ().addAll (topicLabel , messageLabel , timeLabel );
68+
69+ // Lägg till nedladdningslänk om fil finns
70+ if (item .attachmentUrl () != null && !item .attachmentUrl ().isEmpty ()) {
71+ Hyperlink downloadLink = new Hyperlink ("📎 Ladda ner fil" );
72+ downloadLink .setOnAction (e -> {
73+ try {
74+ Desktop .getDesktop ().browse (new URI (item .attachmentUrl ()));
75+ } catch (Exception ex ) {
76+ ex .printStackTrace ();
77+ }
78+ });
79+ container .getChildren ().add (downloadLink );
80+ }
81+
82+ container .setPadding (new Insets (8 ));
83+ container .setStyle ("-fx-background-color: #f4f4f4; -fx-background-radius: 6;" );
84+
85+ setGraphic (container );
86+ }
87+ }
88+ });
3289 }
3390
3491 @ FXML
@@ -50,7 +107,7 @@ private void sendFile(ActionEvent event) {
50107 if (selectedFile != null ) {
51108 Path filePath = selectedFile .toPath ();
52109 model .sendFile (filePath ).thenAccept (success -> {
53- Platform . runLater (() -> {
110+ runOnFx (() -> {
54111 if (success ) {
55112 System .out .println ("Fil skickad: " + filePath .getFileName ());
56113 } else {
0 commit comments