diff --git a/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt b/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt index 005a261..1e763fa 100644 --- a/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt +++ b/img-core/src/main/kotlin/dev/nuculabs/imagetagger/core/AnalyzedImage.kt @@ -15,6 +15,7 @@ class AnalyzedImage(private val file: File, imageTagsPrediction: IImageTagsPredi private var predictedTags: List = emptyList() private val logger: Logger = Logger.getLogger("AnalyzedImage") private var error: String = "" + private var hasError: Boolean = false /** * Initializes the analyzed image and predicts its tags. @@ -23,24 +24,39 @@ class AnalyzedImage(private val file: File, imageTagsPrediction: IImageTagsPredi try { bufferedImage = ImageIO.read(File(file.absolutePath)) predictedTags = imageTagsPrediction.predictTags(bufferedImage!!) + } catch (e: NullPointerException) { + val message = "Error while predicting image: invalid image type or type not supported." + logger.warning(message) + hasError = true + error = message } catch (e: Exception) { - logger.warning("Error while predicting images $e") + val message = "Error loading image $e" + logger.warning(message) + hasError = true error = e.message.toString() } } + /** + * Returns a boolean indicating if the image analysis has errors. + * The flag is `True` if it has errors, `False` otherwise. + */ + fun hasError(): Boolean { + return hasError + } + /** * Returns the prediction error */ fun errorMessage(): String { - return error; + return error } /** * Returns the absolute file path of the image. */ fun absolutePath(): String { - return file.absolutePath; + return file.absolutePath } /** 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 51439a1..aa047e1 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 @@ -60,8 +60,13 @@ class ImageTagsEntryControl(private val image: AnalyzedImage) : HBox() { } catch (exception: IOException) { throw RuntimeException(exception) } - setImage(image.absolutePath()) - setText(image.tags()) + setImage(image) + if (image.hasError()) { + setText(listOf(image.errorMessage())) + } else { + setText(image.tags()) + } + setupEventHandlers() } @@ -95,13 +100,20 @@ class ImageTagsEntryControl(private val image: AnalyzedImage) : HBox() { /** * Setter for setting the image. */ - private fun setImage(imagePath: String) { - val file = File(imagePath) - file.inputStream().use { - imageView.image = Image(it, 244.0, 244.0, true, true) + private fun setImage(analyzedImage: AnalyzedImage) { + val file = File(analyzedImage.absolutePath()) + if (analyzedImage.hasError()) { + fileNameLabel.text = "Failed: ${file.name}" + fileNameLabel.styleClass.add("errorLabel") + imageView.image = Image(this.javaClass.getResourceAsStream("images/failed.png")) imageView.isCache = true + } else { + file.inputStream().use { + imageView.image = Image(it, 244.0, 244.0, true, true) + imageView.isCache = true + } + fileNameLabel.text = "File: ${file.name}" } - fileNameLabel.text = "File: ${file.name}" } /** diff --git a/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/image-tags-entry.css b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/image-tags-entry.css new file mode 100644 index 0000000..56f99bc --- /dev/null +++ b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/image-tags-entry.css @@ -0,0 +1,5 @@ + +.errorLabel { + -fx-text-background-color: red; + -fx-font-family: "Inter Bold" +} \ 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 a38a24f..46a7b7c 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,8 +6,8 @@ - - + + diff --git a/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/images/failed.png b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/images/failed.png new file mode 100644 index 0000000..4df4629 Binary files /dev/null and b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/images/failed.png differ