From 76ab02473448d3cdf4cf6aba1a55ae1b339bacb5 Mon Sep 17 00:00:00 2001 From: requinDr Date: Fri, 7 Jan 2022 12:55:59 +0100 Subject: [PATCH] display in html and auto refresh of pages --- .../java/fr/fatestaynight/packager/Main.java | 61 +++++++++++++------ .../fatestaynight/packager/PageFetcher.java | 45 ++++++++++---- 2 files changed, 75 insertions(+), 31 deletions(-) diff --git a/src/main/java/fr/fatestaynight/packager/Main.java b/src/main/java/fr/fatestaynight/packager/Main.java index ad40b0a..1f6a074 100644 --- a/src/main/java/fr/fatestaynight/packager/Main.java +++ b/src/main/java/fr/fatestaynight/packager/Main.java @@ -20,12 +20,16 @@ import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSpinner; -import javax.swing.JTextArea; +import javax.swing.JTextPane; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.JSplitPane; import javax.swing.SwingConstants; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; import javax.swing.text.NumberFormatter; +import javax.swing.text.html.HTMLEditorKit; +import javax.swing.text.html.StyleSheet; /** * Spinner used for day, scene and page. @@ -158,23 +162,23 @@ enum Language { * @author requinDr, loicfr */ public class Main extends JFrame { - private static final String WINDOW_TITLE = "Fate/Stay Night [Translation helper] - 0.4"; - private static final int DEFAULT_WIDTH = 780; - private static final int DEFAULT_HEIGHT = 400; + private static final String WINDOW_TITLE = "Fate/stay night [Translation helper] - 0.5"; + private static final int DEFAULT_WIDTH = 820; + private static final int DEFAULT_HEIGHT = 480; - //Composants graphiques + // Composants graphiques private JButton start_btn; private JTextField fileName_tf; private NumberSpinner pageSpinner; private JCheckBox hideCode_cb; - private JTextArea[] textPanels; + private JTextPane[] textPanels; private PageFetcher pageFetch; private Language[] languages; public Main() { - //Mise en page de la fenêtre + // Mise en page de la fenêtre this.setTitle(WINDOW_TITLE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); @@ -189,6 +193,13 @@ public Main() { this.setVisible(true); } + // Automatically refresh pages when spinner value is changed + class SpinnerListener implements ChangeListener { + public void stateChanged(ChangeEvent evt) { + updateTexts(); + } + } + /** * Initializes the top configuration menu with file and page selection, * and other options. @@ -209,12 +220,18 @@ private void setupTopMenu() { routeSelect.setSelectedIndex(0); NumberSpinner daySelect = new NumberSpinner(2, 1, 20); NumberSpinner sceneSelect = new NumberSpinner(2, 0, 99); - //fileConstructPane.add(new JLabel("Route :", SwingConstants.RIGHT)); + fileConstructPane.add(routeSelect); fileConstructPane.add(new JLabel("Jour :", SwingConstants.RIGHT)); fileConstructPane.add(daySelect); fileConstructPane.add(new JLabel("Scène :", SwingConstants.RIGHT)); fileConstructPane.add(sceneSelect); + this.pageSpinner = new NumberSpinner(3, 1, 999); + pageSpinner.addChangeListener(new SpinnerListener()); // check for a value change + + fileConstructPane.add(new JLabel("Page :", SwingConstants.RIGHT)); + fileConstructPane.add(this.pageSpinner); + fileSelectPane.add(fileConstructPane); fileSelectPane.setMinimumSize(fileSelectPane.getSize()); @@ -235,10 +252,6 @@ private void setupTopMenu() { topPane.add(fileSelectPane); JPanel navigationPane = new JPanel(new FlowLayout(FlowLayout.LEFT)); - this.pageSpinner = new NumberSpinner(3, 1, 999); - - navigationPane.add(new JLabel("Page :", SwingConstants.RIGHT)); - navigationPane.add(this.pageSpinner); this.hideCode_cb = new JCheckBox("Cacher le code"); navigationPane.add(hideCode_cb); @@ -260,17 +273,25 @@ private void setupTextPanes() { // one panel for each parameter int nb_panels = languages.length; JPanel[] panes = new JPanel[nb_panels]; - this.textPanels = new JTextArea[nb_panels]; + this.textPanels = new JTextPane[nb_panels]; + + // add a HTMLEditorKit to the jpane + HTMLEditorKit kit = new HTMLEditorKit(); + // add a stylesheet for the displayed content + StyleSheet styleSheet = kit.getStyleSheet(); for(int i=0; i< nb_panels; i++) { panes[i] = new JPanel(new BorderLayout()); - final JTextArea textArea = new JTextArea(); - textArea.setRows(15); - textArea.setEditable(false); - textArea.setLineWrap(true); - textArea.setWrapStyleWord(true); - this.textPanels[i] = textArea; + final JTextPane textPane = new JTextPane(); + textPane.setEditorKit(kit); + styleSheet.addRule("body {margin:4px; text-align:justify;}"); + styleSheet.addRule(".error {color:#922B21;}"); + styleSheet.addRule(".atCode {color:#21618C;}"); + styleSheet.addRule(".bracketCode {color:#196F3D;}"); + textPane.setEditable(false); + textPane.setContentType("text/html"); + this.textPanels[i] = textPane; JScrollPane scrollPane = new JScrollPane(this.textPanels[i]); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); @@ -284,7 +305,7 @@ private void setupTextPanes() { if (btn.isSelected()) { Language lang = Language.valueOf(btn.getText().replaceAll("-", "_")); this.languages[index] = lang; - textArea.setText(pageFetch.fetchText(lang.dir_name)); + textPane.setText("" + pageFetch.fetchText(lang.dir_name) + ""); } } }; diff --git a/src/main/java/fr/fatestaynight/packager/PageFetcher.java b/src/main/java/fr/fatestaynight/packager/PageFetcher.java index 047e2cf..cdef661 100644 --- a/src/main/java/fr/fatestaynight/packager/PageFetcher.java +++ b/src/main/java/fr/fatestaynight/packager/PageFetcher.java @@ -44,7 +44,7 @@ public String fetchText(String language) { return hideCode(content); } - return content; + return styleContent(content); } public static String lectureFichier(String nomFichier, int pageNum, String language, Charset charset) throws IOException { @@ -65,7 +65,7 @@ public static String lectureFichier(String nomFichier, int pageNum, String langu if (line != null) { // sauvegarde les lignes jusqu'à la fin de la page while (((line = file.readLine()) != null) && (line.indexOf(pageEnd) == -1)) { - content += line + "\n"; + content += line + "
"; } } file.close(); @@ -74,9 +74,6 @@ public static String lectureFichier(String nomFichier, int pageNum, String langu return null; } - // Corrige tous les sauts de ligne - content = content.replaceAll("\\n\\r", "\n"); - return content; } @@ -93,22 +90,23 @@ public static String lectureFichier(String nomFichier, int pageNum, String langu if (content == null) content = lectureFichier(nomFichier, pageNum, language, StandardCharsets.UTF_8); if (content == null) - content = "La page " + pageNum + " n'existe pas dans le fichier " + language + "/" + nomFichier + "."; + content = "La page " + pageNum + " n'existe pas dans le fichier " + language + + "/" + nomFichier + "."; return content; } catch (IOException e) { - String content = "Problème d'accès au fichier " + nomFichier; + String content = "Problème d'accès au fichier " + nomFichier + ""; System.err.println(content); return content; } } /** - * Cache le code de la page + * Hide the page script parts * @param content - * @return + * @return the content without the code */ public static String hideCode(String content) { - String lines[] = content.split("\\r?\\n"); + String lines[] = content.split("
"); String contentWCode = ""; for (int i = 0; i < lines.length; i++) { @@ -119,11 +117,36 @@ public static String hideCode(String content) { // Supprime les crochets et leur contenu pour les lignes qui en ont lines[i] = lines[i].replaceAll("\\[[A-z0123456789]*]", ""); // Recrée un texte dépourvu de code - contentWCode = contentWCode + lines[i] + "\n\n"; + contentWCode = contentWCode + lines[i] + "

"; } } return contentWCode; } + + /** + * Style the page content + * @param content + * @return contentStyled + */ + public static String styleContent(String content) { + String lines[] = content.split("
"); + String contentStyled = ""; + + for (int i = 0; i < lines.length; i++) { + // Define the class for lines beginning with @ + if (lines[i].startsWith("@")) { + lines[i] = "" + lines[i] + ""; + } + // Define the class for [text] + if (lines[i].contains("[")) { + lines[i] = lines[i].replaceAll("\\[", "["); + lines[i] = lines[i].replaceAll("\\]", "]"); + } + contentStyled = contentStyled + lines[i] + "
"; + } + + return contentStyled; + } }