Skip to content

Commit

Permalink
Print error to info-label
Browse files Browse the repository at this point in the history
  • Loading branch information
nihas101 committed Feb 22, 2019
1 parent 6b2273c commit c894619
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void startLoadImagesThread(ImageMap imageMap) {
imageMap,
directoryIterator,
new TrivialProgressUpdater(),
() -> {
(success) -> {
setupObservableList(directoryIterator, imageMap);
return Unit.INSTANCE;
});
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void startSetupIteratorFromDragAndDropThread(List<File> files) {
disableInput(true);
return Unit.INSTANCE;
},
() -> {
(success) -> {
runLater(() -> {
addRemainingFiles(files);
disableInput(false);
Expand Down Expand Up @@ -190,7 +190,7 @@ private void startSetupIteratorThread() {
disableInput(true);
return Unit.INSTANCE;
},
() -> {
(success) -> {
runLater(this::tryToSetupListView);
return Unit.INSTANCE;
});
Expand Down Expand Up @@ -230,7 +230,7 @@ private void startLoadImagesThread(DirectoryIterator directoryIterator) {
mainWindow.imageMap,
directoryIterator,
createLoadProgressUpdater(directoryIterator),
() -> {
(success) -> {
setupObservableList(directoryIterator);
return Unit.INSTANCE;
}
Expand Down Expand Up @@ -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;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package de.nihas101.image_to_pdf_converter.tasks

data class CallClosure(val before: () -> Unit = {}, val after: () -> Unit = {})
data class CallClosure(val before: () -> Unit = {}, val after: (Boolean) -> Unit = {})
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LoadImagesTask private constructor(
/* The task was cancelled */
logger.warn("{}", exception)
}
callClosure.after()
callClosure.after(true)
}

companion object LoadImagesTaskFactory {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class PdfBuilderCommandLineInterface private constructor(
if (progress == 1.toDouble()) pdfBuilderCommandLineOutput.printFinishedBuilding()
else pdfBuilderCommandLineOutput.printProgress()
}

override fun reportError(message: String) {
/* DO NOTHING */
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 */
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ class TestProgressUpdater(private val buildSuccess: AtomicBoolean) : ProgressUpd
updateProgress(progress, "")
}

override fun reportError(message: String) {
/* DO NOTHING */
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit c894619

Please sign in to comment.