From 8312353e29809aadac31ec20dd4a9432503a5131 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 4 May 2020 09:52:42 +0200 Subject: [PATCH] move code a bit --- .../gui/search/COPY_OF_ViewerTextOutput.java | 137 ++++++++++++++++++ .../fanfix_swing/gui/search/SearchAction.java | 1 - .../{viewer => search}/TODEL_ViewerPanel.java | 6 +- .../fanfix_swing/gui/viewer/NavBar.java | 42 ++++-- .../gui/viewer/ViewerNonImages.java | 9 ++ 5 files changed, 182 insertions(+), 13 deletions(-) create mode 100644 src/be/nikiroo/fanfix_swing/gui/search/COPY_OF_ViewerTextOutput.java rename src/be/nikiroo/fanfix_swing/gui/{viewer => search}/TODEL_ViewerPanel.java (98%) diff --git a/src/be/nikiroo/fanfix_swing/gui/search/COPY_OF_ViewerTextOutput.java b/src/be/nikiroo/fanfix_swing/gui/search/COPY_OF_ViewerTextOutput.java new file mode 100644 index 00000000..f0ff3793 --- /dev/null +++ b/src/be/nikiroo/fanfix_swing/gui/search/COPY_OF_ViewerTextOutput.java @@ -0,0 +1,137 @@ +package be.nikiroo.fanfix_swing.gui.search; + +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.Paragraph.ParagraphType; +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 + */ +class COPY_OF_ViewerTextOutput { + private StringBuilder builder; + private BasicOutput output; + private Story fakeStory; + private boolean chapterName; + + /** + * Create a new {@link ViewerTextOutput} that will convert a {@link Chapter} + * into HTML3 suited for Java Swing. + */ + public COPY_OF_ViewerTextOutput() { + builder = new StringBuilder(); + fakeStory = new Story(); + + output = new BasicOutput() { + private boolean paraInQuote; + + @Override + protected void writeChapterHeader(Chapter chap) throws IOException { + builder.append(""); + + if (chapterName) { + builder.append("

"); + builder.append("Chapter "); + builder.append(chap.getNumber()); + if (chap.getName() != null + && !chap.getName().trim().isEmpty()) { + builder.append(": "); + builder.append(chap.getName()); + } + builder.append("

"); + } + + builder.append("
"); + } + + @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 { + if ((para.getType() == ParagraphType.QUOTE) == !paraInQuote) { + paraInQuote = !paraInQuote; + if (paraInQuote) { + builder.append("
"); + builder.append("
"); + } else { + builder.append("
"); + builder.append("
"); + } + } + + switch (para.getType()) { + case NORMAL: + builder.append("    "); + builder.append(decorateText(para.getContent())); + builder.append("
"); + break; + case BLANK: + builder.append("

"); + break; + case BREAK: + builder.append("

"); + builder.append("* * *"); + builder.append("



"); + break; + case QUOTE: + builder.append("
"); + 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 + * @param chapterName + * display the chapter name + * + * @return HTML3 code tested with Java Swing + */ + public String convert(Chapter chap, boolean chapterName) { + this.chapterName = chapterName; + builder.setLength(0); + try { + fakeStory.setChapters(Arrays.asList(chap)); + output.process(fakeStory, null, null); + } catch (IOException e) { + Instance.getInstance().getTraceHandler().error(e); + } + return builder.toString(); + } +} diff --git a/src/be/nikiroo/fanfix_swing/gui/search/SearchAction.java b/src/be/nikiroo/fanfix_swing/gui/search/SearchAction.java index e57ff058..f4be4459 100644 --- a/src/be/nikiroo/fanfix_swing/gui/search/SearchAction.java +++ b/src/be/nikiroo/fanfix_swing/gui/search/SearchAction.java @@ -16,7 +16,6 @@ import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix_swing.gui.PropertiesPanel; import be.nikiroo.fanfix_swing.gui.book.BookInfo; import be.nikiroo.fanfix_swing.gui.utils.UiHelper; -import be.nikiroo.fanfix_swing.gui.viewer.TODEL_ViewerPanel; import be.nikiroo.utils.Progress; import be.nikiroo.utils.ui.ProgressBar; diff --git a/src/be/nikiroo/fanfix_swing/gui/viewer/TODEL_ViewerPanel.java b/src/be/nikiroo/fanfix_swing/gui/search/TODEL_ViewerPanel.java similarity index 98% rename from src/be/nikiroo/fanfix_swing/gui/viewer/TODEL_ViewerPanel.java rename to src/be/nikiroo/fanfix_swing/gui/search/TODEL_ViewerPanel.java index 2286215c..89cbf84e 100644 --- a/src/be/nikiroo/fanfix_swing/gui/viewer/TODEL_ViewerPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/search/TODEL_ViewerPanel.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix_swing.gui.viewer; +package be.nikiroo.fanfix_swing.gui.search; import java.awt.BorderLayout; import java.awt.EventQueue; @@ -36,7 +36,7 @@ public class TODEL_ViewerPanel extends JPanel { private boolean imageDocument; private Chapter chap; private JScrollPane scroll; - private ViewerTextOutput htmlOutput; + private COPY_OF_ViewerTextOutput htmlOutput; // text only: private JEditorPane text; @@ -74,7 +74,7 @@ public class TODEL_ViewerPanel extends JPanel { this.text = new JEditorPane("text/html", ""); text.setEditable(false); text.setAlignmentY(TOP_ALIGNMENT); - htmlOutput = new ViewerTextOutput(); + htmlOutput = new COPY_OF_ViewerTextOutput(); image = new JLabel(); image.setHorizontalAlignment(SwingConstants.CENTER); diff --git a/src/be/nikiroo/fanfix_swing/gui/viewer/NavBar.java b/src/be/nikiroo/fanfix_swing/gui/viewer/NavBar.java index 91965ef3..bc798157 100644 --- a/src/be/nikiroo/fanfix_swing/gui/viewer/NavBar.java +++ b/src/be/nikiroo/fanfix_swing/gui/viewer/NavBar.java @@ -6,13 +6,11 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; +import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; -import be.nikiroo.fanfix_swing.images.IconGenerator; -import be.nikiroo.fanfix_swing.images.IconGenerator.Icon; -import be.nikiroo.fanfix_swing.images.IconGenerator.Size; import be.nikiroo.utils.ui.ListenerPanel; /** @@ -67,8 +65,7 @@ public class NavBar extends ListenerPanel { setLayout(layout); // Page navigation - first = new JButton( - IconGenerator.get(Icon.arrow_double_left, Size.x32)); + first = new JButton(); first.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -76,7 +73,7 @@ public class NavBar extends ListenerPanel { } }); - previous = new JButton(IconGenerator.get(Icon.arrow_left, Size.x32)); + previous = new JButton(); previous.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -106,7 +103,7 @@ public class NavBar extends ListenerPanel { maxPage = new JLabel(" of " + max); - next = new JButton(IconGenerator.get(Icon.arrow_right, Size.x32)); + next = new JButton(); next.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -114,8 +111,7 @@ public class NavBar extends ListenerPanel { } }); - last = new JButton( - IconGenerator.get(Icon.arrow_double_right, Size.x32)); + last = new JButton(); last.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -123,6 +119,9 @@ public class NavBar extends ListenerPanel { } }); + // Set the << < > >> "icons" + setIcons(null, null, null, null); + this.add(first); this.add(previous); this.add(page); @@ -321,6 +320,31 @@ public class NavBar extends ListenerPanel { return false; } + /** + * Set icons for the buttons instead of square brackets. + *

+ * Any NULL value will make the button use square brackets again. + * + * @param first + * the icon of the button "go to first page" + * @param previous + * the icon of the button "go to previous page" + * @param next + * the icon of the button "go to next page" + * @param last + * the icon of the button "go to last page" + */ + public void setIcons(Icon first, Icon previous, Icon next, Icon last) { + this.first.setIcon(first); + this.first.setText(first == null ? "<<" : ""); + this.previous.setIcon(previous); + this.previous.setText(previous == null ? "<" : ""); + this.next.setIcon(next); + this.next.setText(next == null ? ">" : ""); + this.last.setIcon(last); + this.last.setText(last == null ? ">>" : ""); + } + /** * Update the label displayed in the UI. */ diff --git a/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java b/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java index 26008dc3..564b5995 100644 --- a/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java +++ b/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java @@ -26,6 +26,9 @@ import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix_swing.gui.PropertiesPanel; import be.nikiroo.fanfix_swing.gui.utils.UiHelper; +import be.nikiroo.fanfix_swing.images.IconGenerator; +import be.nikiroo.fanfix_swing.images.IconGenerator.Icon; +import be.nikiroo.fanfix_swing.images.IconGenerator.Size; import be.nikiroo.utils.ui.DelayWorker; import be.nikiroo.utils.ui.UIUtils; @@ -148,6 +151,12 @@ public class ViewerNonImages extends JFrame { private JToolBar createToolBar() { JToolBar toolbar = new JToolBar(); navbar = new NavBar(0, story.getChapters().size()); + navbar.setIcons( // + IconGenerator.get(Icon.arrow_double_left, Size.x32), // + IconGenerator.get(Icon.arrow_left, Size.x32), // + IconGenerator.get(Icon.arrow_right, Size.x32), // + IconGenerator.get(Icon.arrow_double_right, Size.x32) // + ); toolbar.add(navbar); return toolbar; } -- 2.27.0