From: Niki Roo Date: Sat, 23 Mar 2019 22:25:35 +0000 (+0100) Subject: gui: code cleanup + bold/ita X-Git-Tag: fanfix-2.0.0~8 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=1ee6709558f5e1a6200dd6ec20c0f206c22797cc;p=fanfix.git gui: code cleanup + bold/ita --- diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReader.java b/src/be/nikiroo/fanfix/reader/ui/GuiReader.java index b1e9802..620df26 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReader.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReader.java @@ -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); } /** diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewer.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewer.java index 8f27c6c..281f082 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewer.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewer.java @@ -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("  Chapter " + chap.getNumber() + ": " + chap.getName() + ""); - 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(""); - - builder.append("

"); - builder.append("Chapter "); - builder.append(chap.getNumber()); - builder.append(": "); - builder.append(chap.getName()); - builder.append("

"); - - builder.append(""); - for (Paragraph para : chap) { - addPara(builder, para); - } - - if (paraInQuote) { - builder.append(""); - } - paraInQuote = false; - - builder.append(""); - - builder.append(""); - } - - private boolean paraInQuote; - - private void addPara(StringBuilder builder, Paragraph para) { - switch (para.getType()) { - case NORMAL: - builder.append(para.getContent()); - builder.append("
"); - break; - case BLANK: - builder.append("
"); - break; - case BREAK: - builder.append("
* * *

"); - break; - case QUOTE: - if (!paraInQuote) { - builder.append("
"); - } else { - builder.append("
"); - } - paraInQuote = !paraInQuote; - - builder.append("
"); - builder.append("–  "); - builder.append(para.getContent()); - builder.append("
"); - - 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 index 0000000..d247c1c --- /dev/null +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderTextViewerOutput.java @@ -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(""); + + builder.append("

"); + builder.append("Chapter "); + builder.append(chap.getNumber()); + builder.append(": "); + builder.append(chap.getName()); + builder.append("

"); + + builder.append(""); + for (Paragraph para : chap) { + writeParagraph(para); + } + } + + @Override + protected void writeChapterFooter(Chapter chap) throws IOException { + if (paraInQuote) { + builder.append(""); + } + paraInQuote = false; + + builder.append(""); + builder.append(""); + } + + @Override + protected void writeParagraph(Paragraph para) throws IOException { + switch (para.getType()) { + case NORMAL: + builder.append(decorateText(para.getContent())); + builder.append("
"); + break; + case BLANK: + builder.append("
"); + break; + case BREAK: + builder.append("
* * *

"); + break; + case QUOTE: + if (!paraInQuote) { + builder.append("
"); + } else { + builder.append("
"); + } + paraInQuote = !paraInQuote; + + builder.append("
"); + builder.append("–  "); + builder.append(decorateText(para.getContent())); + builder.append("
"); + + break; + case IMAGE: + } + } + + @Override + protected String enbold(String word) { + return "" + word + ""; + } + + @Override + protected String italize(String word) { + return "" + word + ""; + } + }; + } + + /** + * 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(); + } +}