|
| 1 | +package com.DesktopApplicationClientJava.controllers; |
| 2 | + |
| 3 | +import com.DesktopApplicationClientJava.controllers.utils.Controller; |
| 4 | +import com.DesktopApplicationClientJava.entities.FileInfo; |
| 5 | +import com.DesktopApplicationClientJava.entities.Resource; |
| 6 | +import com.DesktopApplicationClientJava.services.ServiceException; |
| 7 | +import com.DesktopApplicationClientJava.services.filter.ResourceFilter; |
| 8 | +import com.DesktopApplicationClientJava.services.mock.MockService; |
| 9 | +import java.io.File; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.time.LocalDateTime; |
| 12 | +import java.time.format.DateTimeFormatter; |
| 13 | +import java.util.List; |
| 14 | +import java.util.UUID; |
| 15 | +import java.util.function.Consumer; |
| 16 | +import javafx.event.ActionEvent; |
| 17 | +import javafx.fxml.FXML; |
| 18 | +import javafx.scene.control.Button; |
| 19 | +import javafx.scene.control.Label; |
| 20 | +import javafx.scene.control.TableCell; |
| 21 | +import javafx.scene.control.TableColumn; |
| 22 | +import javafx.scene.control.TableView; |
| 23 | +import javafx.scene.control.cell.PropertyValueFactory; |
| 24 | +import javafx.stage.DirectoryChooser; |
| 25 | +import javafx.stage.Window; |
| 26 | +import lombok.Setter; |
| 27 | + |
| 28 | +public class ResourceDetailsController extends Controller { |
| 29 | + |
| 30 | + static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"); |
| 31 | + |
| 32 | + @FXML private Label titleLabel; |
| 33 | + @FXML private Label descriptionLabel; |
| 34 | + @FXML private Label createdLabel; |
| 35 | + @FXML private Label updatedLabel; |
| 36 | + @FXML private Label uuidLabel; |
| 37 | + @FXML private Label errorLabel; |
| 38 | + @FXML private Label statusLabel; |
| 39 | + @FXML private TableView<FileInfo> filesTable; |
| 40 | + @FXML private TableColumn<FileInfo, String> nameColumn; |
| 41 | + @FXML private TableColumn<FileInfo, Long> sizeColumn; |
| 42 | + @FXML private TableColumn<FileInfo, String> contentTypeColumn; |
| 43 | + @FXML private TableColumn<FileInfo, Void> downloadColumn; |
| 44 | + @FXML private Button backButton; |
| 45 | + |
| 46 | + @Setter private UUID resourceId; |
| 47 | + |
| 48 | + @FXML |
| 49 | + private void initialize() { |
| 50 | + hideError(); |
| 51 | + nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); |
| 52 | + sizeColumn.setCellValueFactory(new PropertyValueFactory<>("size")); |
| 53 | + contentTypeColumn.setCellValueFactory(new PropertyValueFactory<>("contentType")); |
| 54 | + downloadColumn.setCellFactory(col -> new DownloadCell(this::onDownload)); |
| 55 | + autoSelectFirstInMockMode(); |
| 56 | + } |
| 57 | + |
| 58 | + public void load() { |
| 59 | + hideError(); |
| 60 | + try { |
| 61 | + showResource(services.getResourceService().getById(resourceId)); |
| 62 | + } catch (ServiceException e) { |
| 63 | + showError(e.getMessage()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @FXML |
| 68 | + private void onBack(ActionEvent event) { |
| 69 | + navigator.goTo("/fxml/ResourceList.fxml"); |
| 70 | + } |
| 71 | + |
| 72 | + private void onDownload(FileInfo file) { |
| 73 | + File dir = chooseDirectory(backButton.getScene().getWindow()); |
| 74 | + if (dir == null) { |
| 75 | + return; |
| 76 | + } |
| 77 | + try { |
| 78 | + Path saved = services.getFileService().downloadFile(resourceId, file.getUuid(), dir.toPath()); |
| 79 | + statusLabel.setText("Сохранено: " + saved.getFileName()); |
| 80 | + } catch (ServiceException e) { |
| 81 | + showError(e.getMessage()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + protected File chooseDirectory(Window owner) { |
| 86 | + return new DirectoryChooser().showDialog(owner); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Dev-only fallback: when the page is opened without an id (e.g. straight via INITIAL_PAGE_PATH) |
| 91 | + * against a mock service, show the first seeded resource so the UI is clickable without the |
| 92 | + * resource list. Never triggers against the real backend. |
| 93 | + */ |
| 94 | + private void autoSelectFirstInMockMode() { |
| 95 | + if (resourceId != null || !(services.getResourceService() instanceof MockService mock)) { |
| 96 | + return; |
| 97 | + } |
| 98 | + mock.doIfInitialPage(); |
| 99 | + services.getResourceService().search(ResourceFilter.empty(), 0, 1).stream() |
| 100 | + .map(Resource::getUuid) |
| 101 | + .findFirst() |
| 102 | + .ifPresent( |
| 103 | + id -> { |
| 104 | + setResourceId(id); |
| 105 | + load(); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + private void showResource(Resource resource) { |
| 110 | + titleLabel.setText(resource.getTitle()); |
| 111 | + descriptionLabel.setText(resource.getDescription()); |
| 112 | + createdLabel.setText(formatDateTime(resource.getCreatedAt())); |
| 113 | + updatedLabel.setText(formatDateTime(resource.getUpdatedAt())); |
| 114 | + uuidLabel.setText(resource.getUuid() == null ? "" : resource.getUuid().toString()); |
| 115 | + List<FileInfo> files = resource.getFiles() == null ? List.of() : resource.getFiles(); |
| 116 | + filesTable.getItems().setAll(files); |
| 117 | + } |
| 118 | + |
| 119 | + private static String formatDateTime(LocalDateTime value) { |
| 120 | + return value == null ? "" : DATE_TIME_FORMAT.format(value); |
| 121 | + } |
| 122 | + |
| 123 | + private void showError(String message) { |
| 124 | + errorLabel.setText(message); |
| 125 | + errorLabel.setVisible(true); |
| 126 | + errorLabel.setManaged(true); |
| 127 | + } |
| 128 | + |
| 129 | + private void hideError() { |
| 130 | + errorLabel.setText(""); |
| 131 | + errorLabel.setVisible(false); |
| 132 | + errorLabel.setManaged(false); |
| 133 | + } |
| 134 | + |
| 135 | + private static final class DownloadCell extends TableCell<FileInfo, Void> { |
| 136 | + private final Button button = new Button("Скачать"); |
| 137 | + |
| 138 | + DownloadCell(Consumer<FileInfo> onDownload) { |
| 139 | + button.getStyleClass().setAll("btn", "btn-default"); |
| 140 | + button.setOnAction(event -> onDownload.accept(getTableView().getItems().get(getIndex()))); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + protected void updateItem(Void item, boolean empty) { |
| 145 | + super.updateItem(item, empty); |
| 146 | + setGraphic(empty ? null : button); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments