Skip to content
This repository has been archived by the owner on Jun 26, 2022. It is now read-only.

Commit

Permalink
display in html and auto refresh of pages
Browse files Browse the repository at this point in the history
  • Loading branch information
requinDr committed Jan 7, 2022
1 parent add0f2a commit 76ab024
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 31 deletions.
61 changes: 41 additions & 20 deletions src/main/java/fr/fatestaynight/packager/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand All @@ -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());

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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("<body>" + pageFetch.fetchText(lang.dir_name) + "</body>");
}
}
};
Expand Down
45 changes: 34 additions & 11 deletions src/main/java/fr/fatestaynight/packager/PageFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 + "<br>";
}
}
file.close();
Expand All @@ -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;
}

Expand All @@ -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 = "<span class='error'>La page " + pageNum + " n'existe pas dans le fichier " + language
+ "/" + nomFichier + ".</span>";
return content;
} catch (IOException e) {
String content = "Problème d'accès au fichier " + nomFichier;
String content = "<span class='error'>Problème d'accès au fichier " + nomFichier + "</span>";
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("<br>");
String contentWCode = "";

for (int i = 0; i < lines.length; i++) {
Expand All @@ -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] + "<br><br>";
}
}

return contentWCode;
}

/**
* Style the page content
* @param content
* @return contentStyled
*/
public static String styleContent(String content) {
String lines[] = content.split("<br>");
String contentStyled = "";

for (int i = 0; i < lines.length; i++) {
// Define the class for lines beginning with @
if (lines[i].startsWith("@")) {
lines[i] = "<span class='atCode'>" + lines[i] + "</span>";
}
// Define the class for [text]
if (lines[i].contains("[")) {
lines[i] = lines[i].replaceAll("\\[", "<span class='bracketCode'>[");
lines[i] = lines[i].replaceAll("\\]", "]</span>");
}
contentStyled = contentStyled + lines[i] + "<br>";
}

return contentStyled;
}

}

0 comments on commit 76ab024

Please sign in to comment.