diff --git a/build.gradle b/build.gradle index ee9a00f0f..bbfd69f6b 100644 --- a/build.gradle +++ b/build.gradle @@ -62,8 +62,7 @@ sourceSets { } dependencies { - compile 'org.languagetool:languagetool-core:3.1', - 'org.languagetool:language-fr:3.1', + compile 'org.languagetool:language-fr:3.1', 'org.apache.httpcomponents:httpclient:4.5.1', 'org.apache.httpcomponents:httpcore:4.4.3', 'org.apache.httpcomponents:fluent-hc:4.5.1', diff --git a/src/com/zestedesavoir/zestwriter/utils/Corrector.java b/src/com/zestedesavoir/zestwriter/utils/Corrector.java index 501fb84b6..4e60eb8a2 100644 --- a/src/com/zestedesavoir/zestwriter/utils/Corrector.java +++ b/src/com/zestedesavoir/zestwriter/utils/Corrector.java @@ -142,13 +142,18 @@ private AnnotatedText makeAnnotatedText(String pseudoXml) { return builder.build(); } - public String checkHtmlContent(String htmlContent) throws IOException { + public String checkHtmlContent(String htmlContent) { AnnotatedText markup = makeAnnotatedText(htmlContent); StringBuilder bf = new StringBuilder(htmlContent); langTool.getAllActiveRules().stream().filter(rule -> rule instanceof SpellingCheckRule).forEach(rule -> ((SpellingCheckRule) rule).addIgnoreTokens(wordsToIgnore)); - - List matches = langTool.check(markup); + List matches = new ArrayList<>(); + try { + matches = langTool.check(markup); + } + catch (Exception e) { + e.printStackTrace(); + } int offset = 0; for (RuleMatch match : matches) { String desc = "Note : " + match.getRule().getDescription(); @@ -167,13 +172,16 @@ public String checkHtmlContent(String htmlContent) throws IOException { return bf.toString(); } - public String checkHtmlContentToText(String htmlContent, String source) throws IOException { + public String checkHtmlContentToText(String htmlContent, String source) { AnnotatedText markup = makeAnnotatedText(htmlContent); StringBuilder bf = new StringBuilder(); - langTool.getAllActiveRules().stream().filter(rule -> rule instanceof SpellingCheckRule).forEach(rule -> ((SpellingCheckRule) rule).addIgnoreTokens(wordsToIgnore)); - - List matches = langTool.check(markup); + List matches = new ArrayList<>(); + try { + matches = langTool.check(markup); + } catch (IOException e) { + e.printStackTrace(); + } int offset = 0; for (RuleMatch match : matches) { String txt = htmlContent.substring(match.getFromPos(), match.getToPos()); diff --git a/src/com/zestedesavoir/zestwriter/utils/readability/Utilities.java b/src/com/zestedesavoir/zestwriter/utils/readability/Utilities.java index 601a13ef9..75f087381 100644 --- a/src/com/zestedesavoir/zestwriter/utils/readability/Utilities.java +++ b/src/com/zestedesavoir/zestwriter/utils/readability/Utilities.java @@ -37,7 +37,8 @@ public class Utilities { public static Double round(double d, int decimalPlace) { // see the Javadoc about why we use a String in the constructor // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double) - BigDecimal bd = new BigDecimal(Double.toString(d)); + + BigDecimal bd = new BigDecimal(d); bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); return bd.doubleValue(); } diff --git a/src/com/zestedesavoir/zestwriter/view/MdConvertController.java b/src/com/zestedesavoir/zestwriter/view/MdConvertController.java index 71aeefb72..b8c5348c7 100644 --- a/src/com/zestedesavoir/zestwriter/view/MdConvertController.java +++ b/src/com/zestedesavoir/zestwriter/view/MdConvertController.java @@ -311,7 +311,7 @@ public void HandleValidateButtonAction() { WebEngine webEngine = renderView.getEngine(); webEngine.loadContent("" + result + ""); webEngine.setUserStyleSheetLocation(getClass().getResource("content.css").toExternalForm()); - } catch (DOMException | IOException e) { + } catch (DOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/src/com/zestedesavoir/zestwriter/view/MenuController.java b/src/com/zestedesavoir/zestwriter/view/MenuController.java index de6b0b7e2..92f439d12 100644 --- a/src/com/zestedesavoir/zestwriter/view/MenuController.java +++ b/src/com/zestedesavoir/zestwriter/view/MenuController.java @@ -7,8 +7,12 @@ import com.zestedesavoir.zestwriter.utils.ZipUtil; import com.zestedesavoir.zestwriter.utils.readability.Readability; import javafx.application.Platform; +import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import javafx.concurrent.Worker; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.geometry.Insets; @@ -18,12 +22,13 @@ import javafx.scene.control.ButtonBar.ButtonData; import javafx.scene.image.ImageView; import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; +import javafx.scene.text.Font; +import javafx.scene.text.Text; import javafx.stage.DirectoryChooser; import javafx.util.Pair; import org.apache.commons.lang.StringEscapeUtils; -import org.codehaus.jackson.JsonParseException; -import org.codehaus.jackson.map.JsonMappingException; import org.python.core.PyString; import org.python.util.PythonInterpreter; @@ -51,6 +56,10 @@ public class MenuController { MenuItem menuReport; @FXML MenuItem menuLisibility; + @FXML + HBox hBottomBox; + final ProgressBar pb = new ProgressBar(0); + final Text labelField = new Text(""); public void setMainApp(MainApp mainApp) { this.mainApp = mainApp; @@ -61,35 +70,44 @@ private void HandleQuitButtonAction(ActionEvent event) { System.exit(0); } + private List getExtractFilesFromTree(TreeItem node) { + List extractFiles = new ArrayList<>(); + for (TreeItem child : node.getChildren()) { + if (child.getChildren().isEmpty()) { + extractFiles.add(child.getValue()); + } + else { + extractFiles.addAll(getExtractFilesFromTree(child)); + } + } + return extractFiles; + } + private void correctChildren(TreeItem root, boolean typo) throws IOException { - for (TreeItem child : root.getChildren()) { + List myExtracts = getExtractFilesFromTree(root); + for(ExtractFile extract:myExtracts) { String markdown = ""; - if (child.getChildren().isEmpty()) { - // load mdText - Path path = Paths.get(child.getValue().getFilePath()); - Scanner scanner; - StringBuilder bfString = new StringBuilder(); - try { - scanner = new Scanner(path, StandardCharsets.UTF_8.name()); - while (scanner.hasNextLine()) { - bfString.append(scanner.nextLine()); - bfString.append("\n"); - } - markdown = bfString.toString(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + // load mdText + Path path = Paths.get(extract.getFilePath()); + Scanner scanner; + StringBuilder bfString = new StringBuilder(); + try { + scanner = new Scanner(path, StandardCharsets.UTF_8.name()); + while (scanner.hasNextLine()) { + bfString.append(scanner.nextLine()); + bfString.append("\n"); } + markdown = bfString.toString(); + } catch (IOException e) { + e.printStackTrace(); + } - Corrector cr = new Corrector(); - if (!typo) { - cr.ignoreRule("FRENCH_WHITESPACE"); - } - String htmlText = StringEscapeUtils.unescapeHtml(markdownToHtml(mainApp.getIndex(), markdown)); - textArea.appendText(cr.checkHtmlContentToText(htmlText, child.getValue().getTitle().getValue())); - } else { - correctChildren(child, typo); + Corrector cr = new Corrector(); + if (!typo) { + cr.ignoreRule("FRENCH_WHITESPACE"); } + String htmlText = StringEscapeUtils.unescapeHtml(markdownToHtml(mainApp.getIndex(), markdown)); + textArea.appendText(cr.checkHtmlContentToText(htmlText, extract.getTitle().getValue())); } } @@ -270,13 +288,7 @@ private void HandleGunningButtonAction(ActionEvent event) { } @FXML - private void HandleReportWithTypoButtonAction(ActionEvent event) throws IOException { - Alert alert = new Alert(AlertType.ERROR); - alert.setTitle("Validation du contenu"); - alert.setHeaderText("Rapport de validation du contenu prêt à être copié sur ZdS"); - - Label label = new Label("Liste des erreurs dans votre contenu"); - + private void HandleReportWithoutTypoButtonAction(ActionEvent event) { textArea = new TextArea(); textArea.setEditable(true); textArea.setWrapText(true); @@ -288,46 +300,77 @@ private void HandleReportWithTypoButtonAction(ActionEvent event) throws IOExcept GridPane expContent = new GridPane(); expContent.setMaxWidth(Double.MAX_VALUE); - expContent.add(label, 0, 0); + expContent.add(new Label("Rapport de correction"), 0, 0); expContent.add(textArea, 0, 1); - // Set expandable Exception into the dialog pane. - alert.getDialogPane().setExpandableContent(expContent); - alert.getDialogPane().setPrefSize(760, 700); - - correctChildren(mainApp.getIndex().getSummary().getRoot(), true); - - alert.showAndWait(); - } - - @FXML - private void HandleReportWithoutTypoButtonAction(ActionEvent event) throws IOException { - Alert alert = new Alert(AlertType.ERROR); - alert.setTitle("Validation du contenu"); - alert.setHeaderText("Rapport de validation du contenu prêt à être copié sur ZdS"); - - Label label = new Label("The exception stacktrace was:"); - - textArea = new TextArea(); - textArea.setEditable(true); - textArea.setWrapText(true); - - textArea.setMaxWidth(Double.MAX_VALUE); - textArea.setMaxHeight(Double.MAX_VALUE); - GridPane.setVgrow(textArea, Priority.ALWAYS); - GridPane.setHgrow(textArea, Priority.ALWAYS); - - GridPane expContent = new GridPane(); - expContent.setMaxWidth(Double.MAX_VALUE); - expContent.add(label, 0, 0); - expContent.add(textArea, 0, 1); - - // Set expandable Exception into the dialog pane. - alert.getDialogPane().setExpandableContent(expContent); - - correctChildren(mainApp.getIndex().getSummary().getRoot(), false); + hBottomBox.getChildren().addAll(labelField); + List myExtracts = getExtractFilesFromTree(mainApp.getIndex().getSummary().getRoot()); + MdTextController mdText = mainApp.getIndex(); + Corrector corrector = new Corrector(); + corrector.ignoreRule("FRENCH_WHITESPACE"); + + Service correctTask = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected String call(){ + updateMessage("Préparation du rapport de validation ..."); + StringBuilder resultCorrect = new StringBuilder(); + for(ExtractFile extract:myExtracts) { + updateMessage("Préparation du rapport de validation de "+extract.getTitle().getValue()); + String markdown = ""; + // load mdText + Path path = Paths.get(extract.getFilePath()); + Scanner scanner; + StringBuilder bfString = new StringBuilder(); + try { + scanner = new Scanner(path, StandardCharsets.UTF_8.name()); + while (scanner.hasNextLine()) { + bfString.append(scanner.nextLine()); + bfString.append("\n"); + } + markdown = bfString.toString(); + } catch (IOException e) { + e.printStackTrace(); + } + + String htmlText = StringEscapeUtils.unescapeHtml(markdownToHtml(mdText, markdown)); + String note = corrector.checkHtmlContentToText(htmlText, extract.getTitle().getValue()); + resultCorrect.append(note); + } + return resultCorrect.toString(); + } + }; + } + }; + labelField.textProperty().bind(correctTask.messageProperty()); + textArea.textProperty().bind(correctTask.valueProperty()); + correctTask.stateProperty().addListener((ObservableValue observableValue, Worker.State oldValue, Worker.State newValue) -> { + Alert alert; + switch (newValue) { + case FAILED: + alert = new Alert(AlertType.ERROR); + alert.setTitle("Validation du contenu"); + alert.setHeaderText("Erreur de validation"); + alert.setContentText("Désolé une erreur nous empêche de trouver les erreurs dans votre contenu"); - alert.showAndWait(); + alert.showAndWait(); + break; + case CANCELLED: + case SUCCEEDED: + alert = new Alert(AlertType.ERROR); + alert.setTitle("Validation du contenu"); + alert.setHeaderText("Rapport de validation du contenu prêt à être copié sur ZdS"); + + // Set expandable Exception into the dialog pane. + alert.getDialogPane().setExpandableContent(expContent); + alert.showAndWait(); + break; + } + hBottomBox.getChildren().clear(); + }); + correctTask.start(); } @FXML @@ -415,65 +458,116 @@ private void HandleLoginButtonAction(ActionEvent event) { Optional> result = dialog.showAndWait(); result.ifPresent(usernamePassword -> { - try { - mainApp.getZdsutils().login(usernamePassword.getKey(), usernamePassword.getValue()); - - menuDownload.setDisable(false); - menuLogin.setDisable(true); - menuLogout.setDisable(false); - if (mainApp.getContents().containsKey("dir")) { - menuUpload.setDisable(false); + hBottomBox.getChildren().addAll(labelField); + Service loginTask = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + updateMessage("Connexion au site en cours ..."); + mainApp.getZdsutils().login(usernamePassword.getKey(), usernamePassword.getValue()); + updateMessage("Recherche des contenus ..."); + mainApp.getZdsutils().initInfoOnlineContent("tutorial"); + mainApp.getZdsutils().initInfoOnlineContent("article"); + return null; + } + }; } - mainApp.getZdsutils().initInfoOnlineContent("tutorial"); - mainApp.getZdsutils().initInfoOnlineContent("article"); - - Alert alert = new Alert(AlertType.INFORMATION); - alert.setTitle("Connexion"); - alert.setHeaderText("Confirmation de connexion"); - alert.setContentText("Félicitations, vous êtes a présent connecté."); - - alert.showAndWait(); - } catch (Exception e) { - Alert alert = new Alert(AlertType.ERROR); - alert.setTitle("Connexion"); - alert.setHeaderText("Erreur de connexion"); - alert.setContentText("Désolé mais vous n'avez pas été authentifié sur le serveur de Zeste de Savoir."); - - alert.showAndWait(); - } + }; + labelField.textProperty().bind(loginTask.messageProperty()); + loginTask.stateProperty().addListener((ObservableValue observableValue, Worker.State oldValue, Worker.State newValue) -> { + Alert alert; + switch (newValue) { + case FAILED: + alert = new Alert(AlertType.ERROR); + alert.setTitle("Connexion"); + alert.setHeaderText("Erreur de connexion"); + alert.setContentText("Désolé mais vous n'avez pas été authentifié sur le serveur de Zeste de Savoir."); + + alert.showAndWait(); + break; + case CANCELLED: + case SUCCEEDED: + menuDownload.setDisable(false); + menuLogin.setDisable(true); + menuLogout.setDisable(false); + if (mainApp.getContents().containsKey("dir")) { + menuUpload.setDisable(false); + } + alert = new Alert(AlertType.INFORMATION); + alert.setTitle("Connexion"); + alert.setHeaderText("Confirmation de connexion"); + alert.setContentText("Félicitations, vous êtes a présent connecté."); + + alert.showAndWait(); + hBottomBox.getChildren().clear(); + break; + } + }); + loginTask.start(); }); } @FXML private void HandleDownloadButtonAction(ActionEvent event) { - if (mainApp.getZdsutils().isAuthenticated()) { - try { - - for (MetadataContent meta : mainApp.getZdsutils().getContentListOnline()) { - mainApp.getZdsutils().downloaDraft(meta.getId(), meta.getType()); - mainApp.getZdsutils().unzipOnlineContent(mainApp.getZdsutils().getOnlineContentPathDir() + File.separator + meta.getSlug() + ".zip"); - } - - Alert alert = new Alert(AlertType.INFORMATION); - alert.setTitle("Connexion"); - alert.setHeaderText("Confirmation de connexion"); - alert.setContentText("Vos contenus (tutoriels et articles) de ZdS ont été téléchargés en local. \nVous pouvez maintenant travailler en mode Hors ligne !"); - alert.showAndWait(); - } catch (IOException e) { - e.printStackTrace(); - - Alert alert = new Alert(AlertType.ERROR); - alert.setTitle("Connexion"); - alert.setHeaderText("Erreur de connexion"); - alert.setContentText("Désolé mais un problème vous empêche de télécharger le contenu de ZdS en local."); - - alert.showAndWait(); + hBottomBox.getChildren().addAll(pb, labelField); + + Service downloadContentTask = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + int max = mainApp.getZdsutils().getContentListOnline().size(); + int iterations = 0; + if (mainApp.getZdsutils().isAuthenticated()) { + for (MetadataContent meta : mainApp.getZdsutils().getContentListOnline()) { + updateMessage("Téléchargement : " + meta.getSlug()); + updateProgress(iterations, max); + mainApp.getZdsutils().downloaDraft(meta.getId(), meta.getType()); + iterations++; + } + + iterations = 0; + for (MetadataContent meta : mainApp.getZdsutils().getContentListOnline()) { + updateMessage("Décompression : " + meta.getSlug()); + updateProgress(iterations, max); + mainApp.getZdsutils().unzipOnlineContent(mainApp.getZdsutils().getOnlineContentPathDir() + File.separator + meta.getSlug() + ".zip"); + iterations++; + } + updateMessage("Terminé"); + updateProgress(iterations, max); + } + return null; + } + }; } - } + }; + labelField.textProperty().bind(downloadContentTask.messageProperty()); + pb.progressProperty().bind(downloadContentTask.progressProperty()); + downloadContentTask.stateProperty().addListener((ObservableValue observableValue, Worker.State oldValue, Worker.State newValue) -> { + switch (newValue) { + case FAILED: + case CANCELLED: + case SUCCEEDED: + Alert alert = new Alert(AlertType.INFORMATION); + alert.setTitle("Téléchargement des contenus"); + alert.setHeaderText("Confirmation du téléchargement"); + alert.setContentText("Vos contenus (tutoriels et articles) de ZdS ont été téléchargés en local. \n" + + "Vous pouvez maintenant travailler en mode Hors ligne !"); + alert.showAndWait(); + hBottomBox.getChildren().clear(); + break; + } + }); + downloadContentTask.start(); } @FXML private void HandleUploadButtonAction(ActionEvent event) { + hBottomBox.getChildren().addAll(labelField); + List contents = mainApp.getZdsutils().getContentListOnline(); ChoiceDialog dialog = new ChoiceDialog<>(null, contents); @@ -481,33 +575,58 @@ private void HandleUploadButtonAction(ActionEvent event) { dialog.setHeaderText("Choisissez le tutoriel vers lequel importer"); dialog.setContentText("Tutoriel: "); - // Traditional way to get the response value. Optional result = dialog.showAndWait(); - if (result.isPresent()) { - if (mainApp.getZdsutils().isAuthenticated()) { - String targetId = result.get().getId(); - String localSlug = mainApp.getZdsutils().getLocalSlug(); - String targetSlug = result.get().getSlug(); - try { - String pathDir = mainApp.getZdsutils().getOfflineContentPathDir() + File.separator + localSlug; - ZipUtil.zipContent(pathDir, pathDir + ".zip"); - mainApp.getZdsutils().importContent(pathDir + ".zip", targetId, targetSlug); - Alert alert = new Alert(AlertType.INFORMATION); - alert.setTitle("Import de contenu"); - alert.setHeaderText("Confirmation de l'import"); - alert.setContentText("Votre contenu à été importé sur ZdS avec succès !"); - alert.showAndWait(); - } catch (IOException e) { - e.printStackTrace(); - Alert alert = new Alert(AlertType.ERROR); + Service uploadContentTask = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() throws Exception { + if (mainApp.getZdsutils().isAuthenticated()) { + String targetId = result.get().getId(); + String localSlug = mainApp.getZdsutils().getLocalSlug(); + String targetSlug = result.get().getSlug(); + try { + String pathDir = mainApp.getZdsutils().getOfflineContentPathDir() + File.separator + localSlug; + updateMessage("Compression : "+targetSlug+" en cours ..."); + ZipUtil.zipContent(pathDir, pathDir + ".zip"); + updateMessage("Import : "+targetSlug+" en cours ..."); + mainApp.getZdsutils().importContent(pathDir + ".zip", targetId, targetSlug); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + }; + } + }; + labelField.textProperty().bind(uploadContentTask.messageProperty()); + uploadContentTask.stateProperty().addListener((ObservableValue observableValue, Worker.State oldValue, Worker.State newValue) -> { + Alert alert; + switch (newValue) { + case FAILED: + alert = new Alert(AlertType.ERROR); alert.setTitle("Import de contenu"); alert.setHeaderText("Erreur d'import"); alert.setContentText("Désolé mais un problème vous empêche d'importer votre contenu sur ZdS"); - alert.showAndWait(); - } + break; + case CANCELLED: + case SUCCEEDED: + alert = new Alert(AlertType.INFORMATION); + alert.setTitle("Import de contenu"); + alert.setHeaderText("Confirmation de l'import"); + alert.setContentText("Votre contenu à été importé sur ZdS avec succès !"); + alert.showAndWait(); + hBottomBox.getChildren().clear(); + break; } + }); + + if (result.isPresent()) { + uploadContentTask.start(); } } diff --git a/src/com/zestedesavoir/zestwriter/view/Root.fxml b/src/com/zestedesavoir/zestwriter/view/Root.fxml index db9774e9b..1f7cc40cf 100644 --- a/src/com/zestedesavoir/zestwriter/view/Root.fxml +++ b/src/com/zestedesavoir/zestwriter/view/Root.fxml @@ -1,9 +1,13 @@ + + + + @@ -90,4 +94,7 @@ + + +