Skip to content

Commit

Permalink
Added a line break for the generated files and the explorer opens at …
Browse files Browse the repository at this point in the history
…the current output path

Implemented issue #10  and #5
  • Loading branch information
Martin Beyer committed Apr 21, 2020
1 parent 1f61cad commit 1e1ec1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OutputController {

Expand All @@ -54,11 +55,11 @@ public class OutputController {
private final Stage owner;
private final SettingsProvider settingsProvider;
private final BooleanProperty isReadyToGenerate;
private static final int REVEAL_TIMEOUT_MS = 5000;

private Settings settings;
private MonthlyInvoices monthlyInvoices;


public OutputController(Stage owner, SettingsProvider settingsProvider, MonthlyInvoices monthlyInvoices) {
this.owner = owner;
this.settingsProvider = settingsProvider;
Expand Down Expand Up @@ -188,6 +189,7 @@ public void generateInvoices() {
try {
Map<String, StringBuilder> htmlInvoices = htmlGenerator.createHTMLInvoices(templatePath.get(), invoices);
new HTMLWriter().write(outputPath.get(), htmlInvoices);
reveal(outputPath.get());
} catch (IOException e) {
//TODO: better error handling
Alerts.genericError(e, "Generating the invoices from template and save them to hard disk.").showAndWait();
Expand All @@ -200,6 +202,27 @@ public void back() {
owner.setScene(parseSF.createScene());
}

private boolean reveal(Path pathToReveal) {
try {
ProcessBuilder revealCommand = new ProcessBuilder("explorer", pathToReveal.toString());
Process proc = revealCommand.start();
boolean finishedInTime = proc.waitFor(REVEAL_TIMEOUT_MS, TimeUnit.MILLISECONDS);
if (finishedInTime) {
// The check proc.exitValue() == 0 is always false since Windows explorer return every time an exit value of 1
return true;
} else {
proc.destroyForcibly();
return false;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
} catch (IOException e) {
Alerts.genericError(e, "Failed to open output path").showAndWait();
return false;
}
}

// Getter & Setter

public BooleanProperty isReadyToGenerateProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public Map<String, StringBuilder> createHTMLInvoices(Path templatePath, Collecti
}
for (Invoice i : invoices) {
sbs.get(i.getNumberString()).append(line, lastEndPosPlus2, line.length());
sbs.get(i.getNumberString()).append("\n");
}

});
Expand Down

0 comments on commit 1e1ec1d

Please sign in to comment.