|
1 | 1 | package com.example; |
2 | 2 |
|
| 3 | +import javafx.application.Platform; |
| 4 | +import javafx.collections.ListChangeListener; |
3 | 5 | import javafx.event.ActionEvent; |
4 | 6 | import javafx.fxml.FXML; |
5 | | -import javafx.scene.control.Label; |
6 | | -import javafx.scene.control.ListView; |
| 7 | +import javafx.scene.Node; |
| 8 | +import javafx.scene.control.*; |
| 9 | +import javafx.scene.input.DragEvent; |
| 10 | +import javafx.scene.input.Dragboard; |
| 11 | +import javafx.scene.input.TransferMode; |
| 12 | +import javafx.scene.layout.HBox; |
| 13 | +import javafx.stage.FileChooser; |
| 14 | + |
| 15 | +import javax.swing.event.HyperlinkListener; |
| 16 | +import javax.tools.Tool; |
| 17 | +import java.awt.Desktop; |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileNotFoundException; |
| 20 | +import java.net.URI; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.time.format.DateTimeFormatter; |
| 23 | +import java.util.List; |
7 | 24 |
|
8 | 25 | public class HelloController { |
9 | 26 |
|
10 | 27 | private final HelloModel model = new HelloModel(new NtfyConnectionImpl()); |
11 | | - public ListView<NtfyMessageDto> messageView; |
12 | 28 |
|
13 | | - @FXML |
14 | | - private Label messageLabel; |
| 29 | + private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm"); |
15 | 30 |
|
16 | | - @FXML |
17 | | - private void initialize() { |
18 | | - if (messageLabel != null) { |
19 | | - messageLabel.setText(model.getGreeting()); |
20 | | - } |
| 31 | + @FXML private ListView<NtfyMessageDto> messageView; |
| 32 | + @FXML private TextArea messageInput; |
| 33 | + |
| 34 | + @FXML private void initialize() { |
21 | 35 | messageView.setItems(model.getMessages()); |
| 36 | + messageView.setCellFactory(lv -> new ListCell<>() { |
| 37 | + @Override |
| 38 | + protected void updateItem(NtfyMessageDto item, boolean empty) { |
| 39 | + super.updateItem(item, empty); |
| 40 | + if (empty || item == null) { |
| 41 | + setText(null); |
| 42 | + setGraphic(null); |
| 43 | + return; |
| 44 | + } |
| 45 | + String text = item.message(); |
| 46 | + var att = item.attachment(); |
| 47 | + |
| 48 | + if (att != null && att.url() != null && !att.url().isBlank()) { |
| 49 | + Label msg = new Label(text != null ? text + " ": ""); |
| 50 | + msg.setWrapText(true); |
| 51 | + |
| 52 | + String linkText = att.name() != null && !att.name().isBlank() |
| 53 | + ? att.name() |
| 54 | + : "Attachment"; |
| 55 | + Hyperlink link = new Hyperlink(linkText); |
| 56 | + if (att.size() > 0) { |
| 57 | + link.setTooltip(new Tooltip(humanSize(att.size()) + (att.type() != null ? " - " + att.type() : ""))); |
| 58 | + } |
| 59 | + link.setOnAction(e -> openInBrowser(att.url())); |
| 60 | + |
| 61 | + HBox row = new HBox(8.0, (Node) msg, (Node) link); |
| 62 | + row.setFillHeight(true); |
| 63 | + |
| 64 | + setText(null); |
| 65 | + setGraphic(row); |
| 66 | + } else { |
| 67 | + setText(text != null ? text : ""); |
| 68 | + setGraphic(null); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + model.getMessages().addListener((ListChangeListener<NtfyMessageDto>) |
| 74 | + c -> Platform.runLater(() -> { |
| 75 | + if (!messageView.getItems(). isEmpty()) { |
| 76 | + messageView.scrollTo(messageView.getItems().size() - 1); |
| 77 | + } |
| 78 | + }) |
| 79 | + ); |
| 80 | + messageView.setOnDragOver(this::handleDragOver); |
| 81 | + messageView.setOnDragDropped(this::handleDragDropped); |
| 82 | + model.loadInitialMessagesAsync(); |
| 83 | + } |
| 84 | + |
| 85 | + @FXML public void sendFile(ActionEvent actionEvent) throws FileNotFoundException { |
| 86 | + FileChooser chooser = new FileChooser(); |
| 87 | + chooser.setTitle("Välj fil att skicka"); |
| 88 | + File file = chooser.showOpenDialog(messageView.getScene().getWindow()); |
| 89 | + if (file != null) { |
| 90 | + Path path = file.toPath(); |
| 91 | + model.sendFile(path); |
| 92 | + } |
22 | 93 | } |
23 | 94 |
|
24 | 95 | public void sendMessage(ActionEvent actionEvent) { |
25 | | - model.sendMessage(); |
| 96 | + String text = messageInput != null ? messageInput.getText() : ""; |
| 97 | + if (text != null && !text.isBlank() && model.sendMessage(text)) { |
| 98 | + messageInput.clear(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void handleDragOver(DragEvent e) { |
| 103 | + Dragboard db = e.getDragboard(); |
| 104 | + if (db.hasFiles()) { |
| 105 | + e.acceptTransferModes(TransferMode.COPY); |
| 106 | + } |
| 107 | + e.consume(); |
| 108 | + } |
| 109 | + |
| 110 | + private void handleDragDropped(DragEvent e) { |
| 111 | + Dragboard db = e.getDragboard(); |
| 112 | + boolean success = false; |
| 113 | + if (db.hasFiles()) { |
| 114 | + List<File> files = db.getFiles(); |
| 115 | + for (File f : files) { |
| 116 | + try { |
| 117 | + model.sendFile(f.toPath()); |
| 118 | + } catch (FileNotFoundException ex) { |
| 119 | + throw new RuntimeException(ex); |
| 120 | + } |
| 121 | + } |
| 122 | + success = true; |
| 123 | + } |
| 124 | + e.setDropCompleted(success); |
| 125 | + e.consume(); |
| 126 | + } |
| 127 | + |
| 128 | + private void openInBrowser(String url) { |
| 129 | + try { |
| 130 | + if (Desktop.isDesktopSupported()) { |
| 131 | + Desktop.getDesktop().browse(URI.create(url)); |
| 132 | + } else { |
| 133 | + // fallback: ingen Desktop, gör inget (kan ersättas med HostServices) |
| 134 | + } |
| 135 | + } catch (Exception ignored) { |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static String humanSize(long bytes) { |
| 140 | + // kort & enkel |
| 141 | + String[] units = {"B","KB","MB","GB","TB"}; |
| 142 | + double v = bytes; |
| 143 | + int i = 0; |
| 144 | + while (v >= 1024 && i < units.length - 1) { v /= 1024; i++; } |
| 145 | + return String.format("%.1f %s", v, units[i]); |
26 | 146 | } |
27 | 147 | } |
0 commit comments