Skip to content

Commit f1a7498

Browse files
Linus WestlingLinus Westling
authored andcommitted
Skicka, samt ladda ner/visa fil fungerar
1 parent fe65275 commit f1a7498

7 files changed

Lines changed: 101 additions & 14 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example;
2+
3+
public record AttachmentDto(
4+
String name,
5+
String url,
6+
String type,
7+
long size) {}

src/main/java/com/example/HelloController.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33
import javafx.application.Platform;
44
import javafx.event.ActionEvent;
55
import javafx.fxml.FXML;
6-
import javafx.scene.control.Label;
7-
import javafx.scene.control.ListView;
6+
import javafx.scene.control.*;
87
import 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+
1019
import java.io.File;
1120
import 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 {

src/main/java/com/example/HelloFX.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public void start(Stage stage) throws Exception {
1313
FXMLLoader fxmlLoader = new FXMLLoader(HelloFX.class.getResource("hello-view.fxml"));
1414
Parent root = fxmlLoader.load();
1515
Scene scene = new Scene(root, 640, 480);
16+
scene.getStylesheets().add(getClass().getResource("/com/example/style.css").toExternalForm());
1617
stage.setTitle("Hello MVC");
1718
stage.setScene(scene);
1819
stage.show();

src/main/java/com/example/HelloModel.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,21 @@ public CompletableFuture<Boolean> sendFile(Path filePath) {
5959
}
6060

6161
public void receiveMessage() {
62-
connection.receive(m -> Platform.runLater(() -> messages.add(m)));
62+
connection.receive(m -> runOnFx(() -> messages.add(m)));
6363
}
6464

6565
public void testAddMessage() {
66-
NtfyMessageDto test = new NtfyMessageDto("id123", System.currentTimeMillis(), "message", "mytopic", "Testmeddelande");
67-
Platform.runLater(() -> messages.add(test));
66+
NtfyMessageDto test = new NtfyMessageDto("id123", System.currentTimeMillis(), "message", "mytopic", "Testmeddelande", null);
67+
runOnFx(() -> messages.add(test));
6868
}
69+
70+
static void runOnFx(Runnable task) {
71+
try {
72+
if (Platform.isFxApplicationThread()) task.run();
73+
else Platform.runLater(task);
74+
} catch (IllegalStateException notInitialized) {
75+
task.run();
76+
}
77+
}
78+
6979
}

src/main/java/com/example/NtfyMessageDto.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,15 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44

55
@JsonIgnoreProperties(ignoreUnknown = true)
6-
public record NtfyMessageDto(String id, long time, String event, String topic, String message) {
7-
}
6+
public record NtfyMessageDto(
7+
String id,
8+
long time,
9+
String event,
10+
String topic,
11+
String message,
12+
AttachmentDto attachment
13+
) {
14+
public String attachmentUrl() {
15+
return attachment != null ? attachment.url() : null;
16+
}
17+
}

src/main/resources/com/example/style.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
}
2828

2929
.message-list {
30-
-fx-background-color: #2e2e3e;
30+
-fx-background-color: white;
31+
-fx-border-color: #cccccc;
3132
-fx-border-radius: 5;
32-
-fx-background-radius: 5;
33-
-fx-pref-height: 200;
34-
-fx-cell-size: 40;
33+
-fx-padding: 10;
3534
}
35+

src/test/java/com/example/HelloModelTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ void receiveShouldTriggerHandler() {
5454
System.currentTimeMillis(),
5555
"message",
5656
"mytopic",
57-
"Hej från testet!"
57+
"Hej från testet!",
58+
null
5859
);
5960

6061
// Act
@@ -76,7 +77,8 @@ void receiveShouldAddMessageToModel() {
7677
System.currentTimeMillis(),
7778
"message",
7879
"mytopic",
79-
"Hej från testet!"
80+
"Hej från testet!",
81+
null
8082
);
8183

8284
// Act

0 commit comments

Comments
 (0)