Skip to content

Commit

Permalink
Supports merging PEF-files (fixes #55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Håkansson committed Mar 2, 2018
1 parent 7000bcd commit 24cc158
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ used "braille" text formats.
* Convert from text to a PEF-file
* Convert from a PEF-file to text
* ~~Split a PEF-file into one file per volume~~ [not implemented]
* ~~Merge several PEF-files into one~~ [not implemented]
* Merge several PEF-files into one

### Supported embossers ###
Dotify Studio supports a range of embossers, including popular [Index](http://www.indexbraille.com/) and [Braillo](http://www.braillo.com/) embossers. Note however that several embossers are untested, due to lack of access and/or time.
Expand Down
1 change: 1 addition & 0 deletions src/application/l10n/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public enum Messages {
TITLE_IMPORT_SOURCE_DOCUMENT_DIALOG("title-import-source-document-dialog"),
TITLE_IMPORT_BRAILLE_OPTIONS_DIALOG("title-import-braille-options-dialog"),
TITLE_IMPORT_BRAILLE_TEXT_DIALOG("title-import-braille-text-dialog"),
TITLE_IMPORT_BRAILLE_MERGE_DIALOG("title-import-braille-merge-dialog"),
TITLE_EXPORT_DIALOG("title-export-dialog"),
TITLE_SAVE_AS_DIALOG("title-save-as-dialog"),
TITLE_TEMPLATES_DIALOG("title-templates-dialog"),
Expand Down
6 changes: 6 additions & 0 deletions src/application/l10n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ checkbox-watch-source=Watch source
title-set-search-folder=Set search folder
title-open-dialog=Open file
title-import-braille-text-dialog=Import braille text
title-import-braille-merge-dialog=Merge PEF-files
title-import-braille-options-dialog=Options
title-import-source-document-dialog=Import source document
title-export-dialog=Export
Expand Down Expand Up @@ -85,6 +86,7 @@ label-source=Source
label-result=Result
label-word-wrap=Word wrap
label-line-numbers=Line numbers
label-merge-order=Merge order
message-six-dot-only=6-dot only
message-search-result={0} by {1}
message-unknown-author=Unknown
Expand All @@ -97,6 +99,7 @@ message-file-modified-by-another-application=The file has been modified by anoth
message-confirm-save-malformed-xml=The contents is not well-formed XML. Do you want to save anyway?
message-confirm-quit-unsaved-changes=Some open files have been modified but not saved. Do you want to quit anyway?
message-confirm-close-unsaved-changes={0} has been modified. Do you want to close the tab anyway?
message-other-metadata-from-first-file=Other meta data is collected from the first file.
menu-item-preferences=Preferences
menu-item-file=File
menu-item-edit=Edit
Expand All @@ -112,6 +115,7 @@ menu-item-save-as=Save As...
menu-item-import=Import
menu-item-import-document=Source document...
menu-item-import-braille-document=Braille text file...
menu-item-merge-pef-files=Merge PEF-files...
menu-item-export=Export...
menu-item-emboss=Emboss...
menu-item-close=Close
Expand Down Expand Up @@ -139,6 +143,8 @@ tooltip-simple-line-spacing=Supports simple line spacing only
tooltip-show-options=Show options
tooltip-hide-options=Hide options
tooltip-correct-formatting=Correct formatting
tooltip-move-up=Move the selected file up (Alt + Up)
tooltip-move-down=Move the selected file down (Alt + Down)
value-use-default=[use default]
extension-filter-supported-files=Supported files
extension-filter-all-files=All files
Expand Down
6 changes: 6 additions & 0 deletions src/application/l10n/messages_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ checkbox-watch-source=Overv
title-set-search-folder=Velg søkemappe
title-open-dialog=Åpne fil
title-import-braille-text-dialog=Importer punktskriftformatert tekstfil
title-import-braille-merge-dialog=Merge PEF-files
title-import-braille-options-dialog=Alternativer
title-import-source-document-dialog=Importer kildedokument
title-export-dialog=Eksporter
Expand Down Expand Up @@ -86,6 +87,7 @@ label-result=Result
label-word-wrap=Tekstbryting
label-line-numbers=Linjenummer
message-six-dot-only=Bare 6-punkt
label-merge-order=Merge order
message-search-result={0} av {1}
message-unknown-author=ukjent
message-unknown-title=ingen tittel
Expand All @@ -97,6 +99,7 @@ message-file-modified-by-another-application=Filen har blitt endret av et annet
message-confirm-save-malformed-xml=Innholdet er ikke velformet XML. Vil du lagre alikevel?
message-confirm-quit-unsaved-changes=Noen filer har blitt endret men ikke lagret. Vil du avslutte alikevel?
message-confirm-close-unsaved-changes={0} har blitt endret. Vil du lukke fanen alikevel?
message-other-metadata-from-first-file=Other meta data is collected from the first file.
menu-item-preferences=Innstillinger
menu-item-file=Fil
menu-item-edit=Rediger
Expand All @@ -112,6 +115,7 @@ menu-item-save-as=Lagre som...
menu-item-import=Importer
menu-item-import-document=Kildedokument...
menu-item-import-braille-document=Punktskriftformatert tekstfil...
menu-item-merge-pef-files=Merge PEF-files...
menu-item-export=Eksporter...
menu-item-emboss=Skriv ut...
menu-item-close=Lukk
Expand Down Expand Up @@ -139,6 +143,8 @@ tooltip-simple-line-spacing=Supports simple line spacing only
tooltip-show-options=Vis alternativer
tooltip-hide-options=Skjul alternativer
tooltip-correct-formatting=Rett formatering
tooltip-move-up=Move the selected file up (Alt + Up)
tooltip-move-down=Move the selected file down (Alt + Down)
value-use-default=[bruk standard]
extension-filter-supported-files=Filer som støttes
extension-filter-all-files=Alle filer
Expand Down
6 changes: 6 additions & 0 deletions src/application/l10n/messages_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ checkbox-watch-source=
title-set-search-folder=Välj sökmapp
title-open-dialog=Öppna fil
title-import-braille-text-dialog=Importera punktskriftsformatterad textfil
title-import-braille-merge-dialog=Slå ihop PEF-filer
title-import-braille-options-dialog=Alternativ
title-import-source-document-dialog=Importera källdokument
title-export-dialog=Exportera
Expand Down Expand Up @@ -85,6 +86,7 @@ label-source=K
label-result=Resultat
label-word-wrap=Radbrytning
label-line-numbers=Radnummer
label-merge-order=Sammanslagningsordning
message-six-dot-only=Endast 6-punkt
message-search-result={0} av {1}
message-unknown-author=okänd
Expand All @@ -97,6 +99,7 @@ message-file-modified-by-another-application=Filen har
message-confirm-save-malformed-xml=Innehållet är inte välformad XML. Vill du spara ändå?
message-confirm-quit-unsaved-changes=Vissa öppna filer har ändrats men inte sparats. Vill du avsluta ändå?
message-confirm-close-unsaved-changes={0} har ändrats. Vill du stänga fliken ändå?
message-other-metadata-from-first-file=Övrig metadata hämtas från den första filen.
menu-item-preferences=Inställningar
menu-item-file=Arkiv
menu-item-edit=Redigera
Expand All @@ -112,6 +115,7 @@ menu-item-save-as=Spara som...
menu-item-import=Importera
menu-item-import-document=Källdokument...
menu-item-import-braille-document=Punktskriftsformatterad textfil...
menu-item-merge-pef-files=Slå ihop PEF-filer...
menu-item-export=Exportera...
menu-item-emboss=Skriv ut...
menu-item-close=Stäng
Expand Down Expand Up @@ -139,6 +143,8 @@ tooltip-simple-line-spacing=St
tooltip-show-options=Visa alternativ
tooltip-hide-options=Dölj alternativ
tooltip-correct-formatting=Rätta formatering
tooltip-move-up=Flytta vald fil uppåt (Alt + Upp)
tooltip-move-down=Flytta vald fil nedåt (Alt + Ner)
value-use-default=[använd standard]
extension-filter-supported-files=Filer som stödjs
extension-filter-all-files=Alla filer
Expand Down
2 changes: 2 additions & 0 deletions src/application/ui/Main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
<KeyCodeCombination alt="UP" code="O" control="UP" meta="UP" shift="DOWN" shortcut="DOWN" />
</accelerator></MenuItem>
<MenuItem mnemonicParsing="false" onAction="#showImportBrailleDialog" text="%menu-item-import-braille-document" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" onAction="#showImportMergeDialog" text="%menu-item-merge-pef-files" />
</items>
</Menu>
<MenuItem fx:id="exportMenuItem" mnemonicParsing="false" onAction="#exportFile" text="%menu-item-export" />
Expand Down
66 changes: 66 additions & 0 deletions src/application/ui/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.Desktop;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
Expand All @@ -23,11 +24,16 @@
import java.util.stream.Stream;

import org.daisy.braille.utils.api.table.TableCatalog;
import org.daisy.braille.utils.pef.PEFFileMerger;
import org.daisy.braille.utils.pef.PEFFileMerger.SortType;
import org.daisy.braille.utils.pef.TextHandler;
import org.daisy.dotify.studio.api.Editor;
import org.daisy.streamline.api.identity.IdentityProvider;
import org.daisy.streamline.api.media.AnnotatedFile;
import org.daisy.streamline.api.tasks.TaskGroupFactoryMaker;
import org.daisy.streamline.api.validity.Validator;
import org.daisy.streamline.api.validity.ValidatorFactoryMaker;
import org.daisy.streamline.api.validity.ValidatorFactoryMakerService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand All @@ -36,6 +42,7 @@
import application.l10n.Messages;
import application.ui.about.AboutView;
import application.ui.imports.ImportBrailleView;
import application.ui.imports.ImportMergeView;
import application.ui.prefs.PreferencesView;
import application.ui.preview.EditorWrapperController;
import application.ui.preview.server.StartupDetails;
Expand Down Expand Up @@ -607,6 +614,65 @@ protected Void call() throws Exception {
}
}

@FXML void showImportMergeDialog() {
ValidatorFactoryMakerService factory = ValidatorFactoryMaker.newInstance();
Validator validator = factory.newValidator("application/x-pef+xml");
if (validator==null) {
Alert alert = new Alert(AlertType.ERROR, "Failed to initialize file merge.", ButtonType.OK);
alert.showAndWait();
return;
}
Window stage = root.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(Messages.TITLE_IMPORT_BRAILLE_MERGE_DIALOG.localize());
fileChooser.getExtensionFilters().add(new ExtensionFilter(Messages.EXTENSION_FILTER_FILE.localize("PEF"), "*.pef"));
Settings.getSettings().getLastOpenPath().ifPresent(v->fileChooser.setInitialDirectory(v));
List<File> selected = fileChooser.showOpenMultipleDialog(stage);
if (selected!=null) {
selected.stream().findAny().ifPresent(v->Settings.getSettings().setLastOpenPath(v.getParentFile()));
File[] selectedArray = selected.toArray(new File[selected.size()]);
SortType.NUMERAL_GROUPING.sort(selectedArray);
ImportMergeView brailleView = new ImportMergeView(Arrays.asList(selectedArray));
brailleView.initOwner(root.getScene().getWindow());
brailleView.initModality(Modality.APPLICATION_MODAL);
brailleView.showAndWait();
if (!brailleView.isCancelled()) {
String identifier = brailleView.getIdentifier().orElse("AUTO-ID-"+System.currentTimeMillis());
try {
File[] files = brailleView.getFiles().toArray(new File[brailleView.getFiles().size()]);
PEFFileMerger merger = new PEFFileMerger(t->validator.validate(t).isValid());
File output = File.createTempFile("unsaved", ".pef");
output.deleteOnExit();
Task<Void> importTask = new Task<Void>() {
@Override
protected Void call() throws Exception {
try (OutputStream os = new FileOutputStream(output)) {
if (!merger.merge(files, os, identifier)) {
throw new RuntimeException("Failed to merge.");
}
return null;
}
}
};
importTask.setOnFailed(e->{
output.delete();
logger.log(Level.WARNING, "Import failed.", importTask.getException());
Platform.runLater(()->{
Alert alert = new Alert(AlertType.ERROR, importTask.getException().toString(), ButtonType.OK);
alert.showAndWait();
});
});
importTask.setOnSucceeded(e->{
Platform.runLater(()->addTab(output));
});
exeService.submit(importTask);
} catch (IOException e) {
logger.log(Level.WARNING, "An IOException occurred.", e);
}
}
}
}

/**
* Returns a new stream with unique input formats.
* @return
Expand Down
7 changes: 7 additions & 0 deletions src/application/ui/imports/ImportMerge.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#upButton {
-fx-graphic: url('resource-files/arrow-up.png');
}

#downButton {
-fx-graphic: url('resource-files/arrow-down.png');
}
82 changes: 82 additions & 0 deletions src/application/ui/imports/ImportMerge.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.text.TextFlow?>

<VBox prefHeight="300.0" prefWidth="300.0" spacing="10.0" stylesheets="@ImportMerge.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ui.imports.ImportMergeController">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="%label-identifier" />
<Region HBox.hgrow="ALWAYS" />
<TextField fx:id="identifier" />
</children>
</HBox>
<TextFlow prefHeight="200.0" prefWidth="200.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="%message-other-metadata-from-first-file">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Text>
</children>
</TextFlow>
<Label text="%label-merge-order">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<HBox VBox.vgrow="ALWAYS">
<children>
<ListView fx:id="filesList" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets />
</HBox.margin>
</ListView>
<VBox alignment="TOP_CENTER" spacing="10.0">
<children>
<Button fx:id="upButton" mnemonicParsing="false" onAction="#moveUp">
<tooltip>
<Tooltip text="%tooltip-move-up" />
</tooltip></Button>
<Button fx:id="downButton" mnemonicParsing="false" onAction="#moveDown">
<tooltip>
<Tooltip text="%tooltip-move-down" />
</tooltip></Button>
</children>
<HBox.margin>
<Insets />
</HBox.margin>
<padding>
<Insets bottom="10.0" left="10.0" />
</padding>
</VBox>
</children>
</HBox>
<HBox alignment="CENTER" spacing="20.0">
<children>
<Button fx:id="ok" mnemonicParsing="false" onAction="#doImport" text="%button-ok" />
<Button fx:id="cancel" mnemonicParsing="false" onAction="#closeWindow" text="%button-cancel" />
</children>
<VBox.margin>
<Insets />
</VBox.margin>
<padding>
<Insets left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</VBox>
Loading

0 comments on commit 24cc158

Please sign in to comment.