gui: code cleanup + bold/ita
authorNiki Roo <niki@nikiroo.be>
Sat, 23 Mar 2019 22:25:35 +0000 (23:25 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 23 Mar 2019 22:25:35 +0000 (23:25 +0100)
src/be/nikiroo/fanfix/reader/ui/GuiReader.java
src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewer.java
src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewerOutput.java [new file with mode: 0644]

index b1e980215953a0b4f3cddbea3e88d4cd50b874af..620df26075159b7560977bc65d8733733d692d9b 100644 (file)
@@ -278,9 +278,9 @@ class GuiReader extends BasicReader {
                        } else {
                                viewer.setVisible(true);
                        }
+               } else {
+                       openExternal(getLibrary().getInfo(luid), file, sync);
                }
-
-               openExternal(getLibrary().getInfo(luid), file, sync);
        }
 
        /**
index 8f27c6c3c6b7200e0893dd066903369e60398746..281f082a0b82aa3842d59d12abedaa948ca4e0f6 100644 (file)
@@ -18,7 +18,6 @@ import javax.swing.SwingConstants;
 
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
-import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
 
@@ -32,6 +31,7 @@ public class GuiReaderTextViewer extends JFrame {
        private JLabel chapterLabel;
        private GuiReaderPropertiesPane descPane;
        private int currentChapter = -42; // cover = -1
+       private GuiReaderTextViewerOutput htmlOutput = new GuiReaderTextViewerOutput();
 
        public GuiReaderTextViewer(BasicLibrary lib, Story story) {
                super(story.getMeta().getLuid() + ": " + story.getMeta().getTitle());
@@ -135,71 +135,11 @@ public class GuiReaderTextViewer extends JFrame {
                        chapterLabel.setText("<HTML>&nbsp;&nbsp;<B>Chapter "
                                        + chap.getNumber() + "</B>: " + chap.getName() + "</HTML>");
 
-                       StringBuilder builder = new StringBuilder();
-                       addChapter(builder, chap);
-                       text.setText(builder.toString());
+                       text.setText(htmlOutput.convert(chap));
                }
        }
 
        private void setCoverPage() {
                descPane.setVisible(true);
        }
-
-       // htmlInsert = no need to add HTML tags
-       private void addChapter(StringBuilder builder, Chapter chap) {
-               builder.append("<HTML>");
-
-               builder.append("<H1>");
-               builder.append("Chapter ");
-               builder.append(chap.getNumber());
-               builder.append(": ");
-               builder.append(chap.getName());
-               builder.append("</H1>");
-
-               builder.append("<JUSTIFY>");
-               for (Paragraph para : chap) {
-                       addPara(builder, para);
-               }
-
-               if (paraInQuote) {
-                       builder.append("</DIV>");
-               }
-               paraInQuote = false;
-
-               builder.append("</JUSTIFY>");
-
-               builder.append("</HTML>");
-       }
-
-       private boolean paraInQuote;
-
-       private void addPara(StringBuilder builder, Paragraph para) {
-               switch (para.getType()) {
-               case NORMAL:
-                       builder.append(para.getContent());
-                       builder.append("<BR>");
-                       break;
-               case BLANK:
-                       builder.append("<BR>");
-                       break;
-               case BREAK:
-                       builder.append("<BR>* * *<BR><BR>");
-                       break;
-               case QUOTE:
-                       if (!paraInQuote) {
-                               builder.append("<DIV>");
-                       } else {
-                               builder.append("</DIV>");
-                       }
-                       paraInQuote = !paraInQuote;
-
-                       builder.append("<DIV>");
-                       builder.append("&ndash;&nbsp;&nbsp;");
-                       builder.append(para.getContent());
-                       builder.append("</DIV>");
-
-                       break;
-               case IMAGE:
-               }
-       }
 }
diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewerOutput.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewerOutput.java
new file mode 100644 (file)
index 0000000..d247c1c
--- /dev/null
@@ -0,0 +1,122 @@
+package be.nikiroo.fanfix.reader.ui;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.data.Chapter;
+import be.nikiroo.fanfix.data.Paragraph;
+import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.output.BasicOutput;
+
+/**
+ * This class can export a chapter into HTML3 code ready for Java Swing support.
+ * 
+ * @author niki
+ */
+public class GuiReaderTextViewerOutput {
+       private StringBuilder builder;
+       private BasicOutput output;
+       private Story fakeStory;
+
+       /**
+        * Create a new {@link GuiReaderTextViewerOutput} that will convert a
+        * {@link Chapter} into HTML3 suited for Java Swing.
+        */
+       public GuiReaderTextViewerOutput() {
+               builder = new StringBuilder();
+               fakeStory = new Story();
+
+               output = new BasicOutput() {
+                       private boolean paraInQuote;
+
+                       @Override
+                       protected void writeChapterHeader(Chapter chap) throws IOException {
+                               builder.append("<HTML>");
+
+                               builder.append("<H1>");
+                               builder.append("Chapter ");
+                               builder.append(chap.getNumber());
+                               builder.append(": ");
+                               builder.append(chap.getName());
+                               builder.append("</H1>");
+
+                               builder.append("<JUSTIFY>");
+                               for (Paragraph para : chap) {
+                                       writeParagraph(para);
+                               }
+                       }
+
+                       @Override
+                       protected void writeChapterFooter(Chapter chap) throws IOException {
+                               if (paraInQuote) {
+                                       builder.append("</DIV>");
+                               }
+                               paraInQuote = false;
+
+                               builder.append("</JUSTIFY>");
+                               builder.append("</HTML>");
+                       }
+
+                       @Override
+                       protected void writeParagraph(Paragraph para) throws IOException {
+                               switch (para.getType()) {
+                               case NORMAL:
+                                       builder.append(decorateText(para.getContent()));
+                                       builder.append("<BR>");
+                                       break;
+                               case BLANK:
+                                       builder.append("<BR>");
+                                       break;
+                               case BREAK:
+                                       builder.append("<BR>* * *<BR><BR>");
+                                       break;
+                               case QUOTE:
+                                       if (!paraInQuote) {
+                                               builder.append("<DIV>");
+                                       } else {
+                                               builder.append("</DIV>");
+                                       }
+                                       paraInQuote = !paraInQuote;
+
+                                       builder.append("<DIV>");
+                                       builder.append("&ndash;&nbsp;&nbsp;");
+                                       builder.append(decorateText(para.getContent()));
+                                       builder.append("</DIV>");
+
+                                       break;
+                               case IMAGE:
+                               }
+                       }
+
+                       @Override
+                       protected String enbold(String word) {
+                               return "<B COLOR='BLUE'>" + word + "</B>";
+                       }
+
+                       @Override
+                       protected String italize(String word) {
+                               return "<I COLOR='GRAY'>" + word + "</I>";
+                       }
+               };
+       }
+
+       /**
+        * Convert the chapter into HTML3 code.
+        * 
+        * @param chap
+        *            the {@link Chapter} to convert.
+        * 
+        * @return HTML3 code tested with Java Swing
+        */
+       public String convert(Chapter chap) {
+               builder.setLength(0);
+               try {
+                       fakeStory.setChapters(Arrays.asList(chap));
+                       output.process(fakeStory, null, null);
+               } catch (IOException e) {
+                       Instance.getTraceHandler().error(e);
+               }
+               return builder.toString();
+       }
+}