From c894619db506e96969cb1dd0e9f2db274c879627 Mon Sep 17 00:00:00 2001 From: Nikita Hasert Date: Fri, 22 Feb 2019 17:45:33 +0100 Subject: [PATCH] Print error to info-label --- .../DirectoryContentDisplayController.java | 4 ++-- .../gui/controller/MainWindowController.java | 20 ++++++++++--------- .../DirectoryIteratorDisplayer.java | 2 +- .../pdf/builders/ImagePdfBuilder.kt | 9 +++++++-- .../tasks/BuildPdfTask.kt | 5 +++-- .../tasks/CallClosure.kt | 2 +- .../tasks/LoadImagesTask.kt | 4 ++-- .../tasks/SetupIteratorTask.kt | 4 +++- .../PdfBuilderCommandLineInterface.kt | 4 ++++ .../PdfBuilderCommandLineOutput.kt | 2 +- .../util/ProgressUpdater.kt | 19 +++++++++++++++++- .../ImageDirectoriesPdfBuilderTest.kt | 3 +++ .../tasks/TaskManagerTest.kt | 4 ++-- 13 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/DirectoryContentDisplayController.java b/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/DirectoryContentDisplayController.java index da4aa12..4823d56 100644 --- a/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/DirectoryContentDisplayController.java +++ b/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/DirectoryContentDisplayController.java @@ -91,7 +91,7 @@ private void startLoadImagesThread(ImageMap imageMap) { imageMap, directoryIterator, new TrivialProgressUpdater(), - () -> { + (success) -> { setupObservableList(directoryIterator, imageMap); return Unit.INSTANCE; }); @@ -158,7 +158,7 @@ public void buildPDF(ActionEvent actionEvent) { CallClosure callClosure = new CallClosure( () -> Unit.INSTANCE, - () -> { + (success) -> { runLater(() -> { mainWindowController.buildProgressBar.setProgress(0); mainWindowController.notifyUser("Finished building: " + saveFile.getAbsolutePath(), GREEN); diff --git a/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/MainWindowController.java b/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/MainWindowController.java index 577b6ab..d77bc19 100644 --- a/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/MainWindowController.java +++ b/src/main/java/de/nihas101/image_to_pdf_converter/gui/controller/MainWindowController.java @@ -126,7 +126,7 @@ private void startSetupIteratorFromDragAndDropThread(List files) { disableInput(true); return Unit.INSTANCE; }, - () -> { + (success) -> { runLater(() -> { addRemainingFiles(files); disableInput(false); @@ -190,7 +190,7 @@ private void startSetupIteratorThread() { disableInput(true); return Unit.INSTANCE; }, - () -> { + (success) -> { runLater(this::tryToSetupListView); return Unit.INSTANCE; }); @@ -230,7 +230,7 @@ private void startLoadImagesThread(DirectoryIterator directoryIterator) { mainWindow.imageMap, directoryIterator, createLoadProgressUpdater(directoryIterator), - () -> { + (success) -> { setupObservableList(directoryIterator); return Unit.INSTANCE; } @@ -340,14 +340,16 @@ private void startPdfBuilderThread(PdfBuilder pdfBuilder) { runLater(() -> disableInput(true)); return Unit.INSTANCE; }, - () -> { + (success) -> { runLater(() -> { disableInput(false); - notifyUser( - "Finished building: " - + Objects.requireNonNull(mainWindow.imageToPdfOptions.getPdfOptions().getSaveLocation()).getAbsolutePath(), - GREEN - ); + if (success) { + notifyUser( + "Finished building: " + + Objects.requireNonNull(mainWindow.imageToPdfOptions.getPdfOptions().getSaveLocation()).getAbsolutePath(), + GREEN + ); + } }); return Unit.INSTANCE; }); diff --git a/src/main/java/de/nihas101/image_to_pdf_converter/gui/sub_stages/DirectoryIteratorDisplayer.java b/src/main/java/de/nihas101/image_to_pdf_converter/gui/sub_stages/DirectoryIteratorDisplayer.java index 05546e5..518ccb1 100644 --- a/src/main/java/de/nihas101/image_to_pdf_converter/gui/sub_stages/DirectoryIteratorDisplayer.java +++ b/src/main/java/de/nihas101/image_to_pdf_converter/gui/sub_stages/DirectoryIteratorDisplayer.java @@ -85,7 +85,7 @@ private void displayDirectory(int index, MainWindowController mainWindowControll CallClosure callClosure = new CallClosure(() -> { runLater(() -> mainWindowController.disableInput(true)); return Unit.INSTANCE; - }, () -> { + }, (success) -> { runLater(() -> { mainWindowController.buildProgressBar.setProgress(0); mainWindowController.notifyUser("Files: " + directoryIterator.numberOfFiles(), WHITE); diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImagePdfBuilder.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImagePdfBuilder.kt index 575bdbf..b70c55f 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImagePdfBuilder.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImagePdfBuilder.kt @@ -21,12 +21,14 @@ package de.nihas101.image_to_pdf_converter.pdf.builders import com.itextpdf.io.image.ImageDataFactory import com.itextpdf.layout.element.Image import de.nihas101.image_to_pdf_converter.directory_iterators.DirectoryIterator +import de.nihas101.image_to_pdf_converter.directory_iterators.exceptions.NoMoreFilesException import de.nihas101.image_to_pdf_converter.pdf.ImagePdf import de.nihas101.image_to_pdf_converter.pdf.ImagePdf.ImagePdfFactory.createPdf import de.nihas101.image_to_pdf_converter.pdf.pdf_options.ImageToPdfOptions import de.nihas101.image_to_pdf_converter.util.JaKoLogger.createLogger import de.nihas101.image_to_pdf_converter.util.ProgressUpdater import java.io.File +import java.io.IOException class ImagePdfBuilder : PdfBuilder() { companion object ImagePdfBuilderFactory { @@ -54,9 +56,12 @@ class ImagePdfBuilder : PdfBuilder() { addNextFileToPDF(file, imagePdf) file = directoryIterator.nextFile() } - } catch (exception: Exception) { + } catch (exception: IOException) { + progressUpdater.reportError("An error occurred building: ${file.name}") logException(file, exception) wasSuccess = false + } catch (exception: NoMoreFilesException) { + // END WAS REACHED } finally { imagePdf.close() return wasSuccess @@ -68,7 +73,7 @@ class ImagePdfBuilder : PdfBuilder() { args[0] = file.absolutePath args[1] = exception - logger.error("Exception caused by: {}\n{}", args) + logger.error("Exception caused by: {}", args) } private fun createFileAtSameLocation(directoryIterator: DirectoryIterator): File { diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/BuildPdfTask.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/BuildPdfTask.kt index d585afa..01a6139 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/BuildPdfTask.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/BuildPdfTask.kt @@ -34,13 +34,14 @@ class BuildPdfTask private constructor( override fun call() { callClosure.before() + var wasSuccessful = false try { - pdfBuilder.build(directoryIterator, imageToPdfOptions, progressUpdater) + wasSuccessful = pdfBuilder.build(directoryIterator, imageToPdfOptions, progressUpdater) } catch (exception: InterruptedException) { /* The task was cancelled */ logger.warn("{}", exception) } - callClosure.after() + callClosure.after(wasSuccessful) } companion object BuildPdfTaskFactory { diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/CallClosure.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/CallClosure.kt index cd09113..4840344 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/CallClosure.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/CallClosure.kt @@ -1,3 +1,3 @@ package de.nihas101.image_to_pdf_converter.tasks -data class CallClosure(val before: () -> Unit = {}, val after: () -> Unit = {}) \ No newline at end of file +data class CallClosure(val before: () -> Unit = {}, val after: (Boolean) -> Unit = {}) \ No newline at end of file diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/LoadImagesTask.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/LoadImagesTask.kt index 2a02770..65338d9 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/LoadImagesTask.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/LoadImagesTask.kt @@ -38,7 +38,7 @@ class LoadImagesTask private constructor( /* The task was cancelled */ logger.warn("{}", exception) } - callClosure.after() + callClosure.after(true) } companion object LoadImagesTaskFactory { @@ -48,7 +48,7 @@ class LoadImagesTask private constructor( imageMap: ImageMap, directoryIterator: DirectoryIterator, progressUpdater: ProgressUpdater, - after: () -> Unit + after: (Boolean) -> Unit ): LoadImagesTask { val callClosure = CallClosure(after = after) return LoadImagesTask(imageMap, directoryIterator, progressUpdater, callClosure) diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/SetupIteratorTask.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/SetupIteratorTask.kt index 8a62936..74d2b01 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/SetupIteratorTask.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/tasks/SetupIteratorTask.kt @@ -32,13 +32,15 @@ open class SetupIteratorTask protected constructor( override fun call() { callClosure.before() + var wasSuccess = true try { directoryIterator.addDirectory(directory, progressUpdater) } catch (exception: InterruptedException) { /* The task was cancelled */ + wasSuccess = false logger.warn("{}", exception) } - callClosure.after() + callClosure.after(wasSuccess) } companion object SetupIteratorTaskFactory { diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineInterface.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineInterface.kt index 8193adc..f7b77e2 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineInterface.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineInterface.kt @@ -137,6 +137,10 @@ class PdfBuilderCommandLineInterface private constructor( if (progress == 1.toDouble()) pdfBuilderCommandLineOutput.printFinishedBuilding() else pdfBuilderCommandLineOutput.printProgress() } + + override fun reportError(message: String) { + /* DO NOTHING */ + } } } diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineOutput.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineOutput.kt index 558fcde..c44c2f3 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineOutput.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/ui/command_line_io/PdfBuilderCommandLineOutput.kt @@ -41,7 +41,7 @@ class PdfBuilderCommandLineOutput private constructor(private val printStream: P fun printPdfModificationInstructions(pdfBuildInformation: PdfBuildInformation) { printStream.println("Instructions:") - IteratorAction.getInstructions().forEach({ instruction -> printStream.println(instruction) }) + IteratorAction.getInstructions().forEach { instruction -> printStream.println(instruction) } printStream.println() printPdfContent(pdfBuildInformation) printMessage("") diff --git a/src/main/kotlin/de/nihas101/image_to_pdf_converter/util/ProgressUpdater.kt b/src/main/kotlin/de/nihas101/image_to_pdf_converter/util/ProgressUpdater.kt index e89da64..f39a072 100644 --- a/src/main/kotlin/de/nihas101/image_to_pdf_converter/util/ProgressUpdater.kt +++ b/src/main/kotlin/de/nihas101/image_to_pdf_converter/util/ProgressUpdater.kt @@ -20,15 +20,17 @@ package de.nihas101.image_to_pdf_converter.util import de.nihas101.image_to_pdf_converter.gui.controller.MainWindowController import javafx.application.Platform.runLater +import javafx.scene.paint.Color.RED import javafx.scene.paint.Color.WHITE import javafx.scene.paint.Paint import org.slf4j.LoggerFactory import java.io.File - +// TODO: Implement reportError in at least ImagePDFBuilder, ImageDirectoriesPDFBuilder and ImageUnzipper interface ProgressUpdater { fun updateProgress(progress: Double, file: File) fun updateProgress(progress: Double, message: String) + fun reportError(message: String) } class TrivialProgressUpdater : ProgressUpdater { @@ -39,6 +41,10 @@ class TrivialProgressUpdater : ProgressUpdater { override fun updateProgress(progress: Double, file: File) { /* DO NOTHING */ } + + override fun reportError(message: String) { + /* DO NOTHING */ + } } abstract class FileProgressUpdater(protected val mainWindowController: MainWindowController) : ProgressUpdater { @@ -52,6 +58,13 @@ abstract class FileProgressUpdater(protected val mainWindowController: MainWindo mainWindowController.notifyUser(message, WHITE) } } + + override fun reportError(message: String) { + runLater { + mainWindowController.buildProgressBar.progress = -1.0 + mainWindowController.notifyUser(message, RED) + } + } } class BuildProgressUpdater(mainWindowController: MainWindowController) : FileProgressUpdater(mainWindowController) { @@ -84,4 +97,8 @@ class LoadProgressUpdater(private val notifyUser: (String, Paint) -> Unit, priva runLater { notifyUser(message, WHITE) } logger.info(message) } + + override fun reportError(message: String) { + /* DO NOTHING */ + } } \ No newline at end of file diff --git a/src/test/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImageDirectoriesPdfBuilderTest.kt b/src/test/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImageDirectoriesPdfBuilderTest.kt index 8da58ad..e265938 100644 --- a/src/test/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImageDirectoriesPdfBuilderTest.kt +++ b/src/test/kotlin/de/nihas101/image_to_pdf_converter/pdf/builders/ImageDirectoriesPdfBuilderTest.kt @@ -67,4 +67,7 @@ class TestProgressUpdater(private val buildSuccess: AtomicBoolean) : ProgressUpd updateProgress(progress, "") } + override fun reportError(message: String) { + /* DO NOTHING */ + } } \ No newline at end of file diff --git a/src/test/kotlin/de/nihas101/image_to_pdf_converter/tasks/TaskManagerTest.kt b/src/test/kotlin/de/nihas101/image_to_pdf_converter/tasks/TaskManagerTest.kt index 92594b0..6cc396a 100644 --- a/src/test/kotlin/de/nihas101/image_to_pdf_converter/tasks/TaskManagerTest.kt +++ b/src/test/kotlin/de/nihas101/image_to_pdf_converter/tasks/TaskManagerTest.kt @@ -19,10 +19,10 @@ class TaskManagerTest { } } -class SetTrueTask(private val booleanSetter: BooleanSetter, assertEquals: () -> Unit) : CancellableTask(booleanSetter, CallClosure({}, assertEquals)) { +class SetTrueTask(private val booleanSetter: BooleanSetter, assertEquals: (Boolean) -> Unit) : CancellableTask(booleanSetter, CallClosure({}, assertEquals)) { override fun call() { booleanSetter.setBoolean(true) - callClosure.after() + callClosure.after(true) } }