diff --git a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ImageTagsEntryControl.kt b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ImageTagsEntryControl.kt index 2ce57ad..08ee1d5 100644 --- a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ImageTagsEntryControl.kt +++ b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ImageTagsEntryControl.kt @@ -2,11 +2,17 @@ package dev.nuculabs.imagetagger.ui.controls import javafx.fxml.FXML import javafx.fxml.FXMLLoader +import javafx.scene.control.Alert +import javafx.scene.control.Alert.AlertType +import javafx.scene.control.ButtonType import javafx.scene.control.Label import javafx.scene.control.TextArea import javafx.scene.image.Image import javafx.scene.image.ImageView +import javafx.scene.input.MouseEvent import javafx.scene.layout.HBox +import javafx.scene.layout.Region +import java.awt.Desktop import java.io.File import java.io.IOException import java.util.logging.Logger @@ -15,8 +21,7 @@ import java.util.logging.Logger /** * This class is used to create a custom control for the image prediction entry. */ -class ImageTagsEntryControl - (imagePath: String, predictions: List) : HBox() { +class ImageTagsEntryControl(private val imagePath: String, predictions: List) : HBox() { private val logger: Logger = Logger.getLogger("ImageTagsEntryControl") /** @@ -50,6 +55,15 @@ class ImageTagsEntryControl } setImage(imagePath) setText(predictions) + + setupEventHandlers() + } + + private fun setupEventHandlers() { + imageView.addEventHandler(MouseEvent.MOUSE_CLICKED) { + onOpenImageClick() + it.consume() + } } /** @@ -57,14 +71,14 @@ class ImageTagsEntryControl * * @param predictions The prediction list. */ - fun setText(predictions: List) { + private fun setText(predictions: List) { predictedImageTags.text = predictions.joinToString { it } } /** * Setter for setting the image. */ - fun setImage(imagePath: String) { + private fun setImage(imagePath: String) { val file = File(imagePath) file.inputStream().use { imageView.image = Image(it, 244.0, 244.0, true, true) @@ -72,4 +86,23 @@ class ImageTagsEntryControl } fileNameLabel.text = "File: ${file.name}" } + + /** + * Opens the image in the user's default image viewing application. + * If the operation fails it will display an error alert. + */ + fun onOpenImageClick() { + if (Desktop.isDesktopSupported()) { + val desktop = Desktop.getDesktop() + if (desktop.isSupported(Desktop.Action.OPEN)) { + desktop.open(File(imagePath)) + } + } else { + logger.severe("Cannot open image $imagePath. Desktop action not supported!") + val alert = + Alert(AlertType.ERROR, "Can't open file: $imagePath\nOperation is not supported!", ButtonType.CLOSE) + alert.dialogPane.minHeight = Region.USE_PREF_SIZE + alert.show() + } + } } \ No newline at end of file diff --git a/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/image-tags-entry.fxml b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/image-tags-entry.fxml index 2141126..7d0f83c 100644 --- a/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/image-tags-entry.fxml +++ b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/image-tags-entry.fxml @@ -6,20 +6,16 @@ - - - - - - - - - - - -