move code a bit
authorNiki Roo <niki@nikiroo.be>
Mon, 4 May 2020 07:52:42 +0000 (09:52 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 4 May 2020 07:52:42 +0000 (09:52 +0200)
src/be/nikiroo/fanfix_swing/gui/search/COPY_OF_ViewerTextOutput.java [new file with mode: 0644]
src/be/nikiroo/fanfix_swing/gui/search/SearchAction.java
src/be/nikiroo/fanfix_swing/gui/search/TODEL_ViewerPanel.java [moved from src/be/nikiroo/fanfix_swing/gui/viewer/TODEL_ViewerPanel.java with 98% similarity]
src/be/nikiroo/fanfix_swing/gui/viewer/NavBar.java
src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java

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 (file)
index 0000000..f0ff379
--- /dev/null
@@ -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("<HTML style='line-height: normal;'>");
+
+                               if (chapterName) {
+                                       builder.append("<H1>");
+                                       builder.append("Chapter ");
+                                       builder.append(chap.getNumber());
+                                       if (chap.getName() != null
+                                                       && !chap.getName().trim().isEmpty()) {
+                                               builder.append(": ");
+                                               builder.append(chap.getName());
+                                       }
+                                       builder.append("</H1>");
+                               }
+
+                               builder.append("<DIV align='justify'>");
+                       }
+
+                       @Override
+                       protected void writeChapterFooter(Chapter chap) throws IOException {
+                               if (paraInQuote) {
+                                       builder.append("</DIV>");
+                               }
+                               paraInQuote = false;
+
+                               builder.append("</DIV>");
+                               builder.append("</HTML>");
+                       }
+
+                       @Override
+                       protected void writeParagraph(Paragraph para) throws IOException {
+                               if ((para.getType() == ParagraphType.QUOTE) == !paraInQuote) {
+                                       paraInQuote = !paraInQuote;
+                                       if (paraInQuote) {
+                                               builder.append("<BR>");
+                                               builder.append("<DIV>");
+                                       } else {
+                                               builder.append("</DIV>");
+                                               builder.append("<BR>");
+                                       }
+                               }
+
+                               switch (para.getType()) {
+                               case NORMAL:
+                                       builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
+                                       builder.append(decorateText(para.getContent()));
+                                       builder.append("<BR>");
+                                       break;
+                               case BLANK:
+                                       builder.append("<BR><BR>");
+                                       break;
+                               case BREAK:
+                                       builder.append("<BR><P COLOR='#7777DD' ALIGN='CENTER'><B>");
+                                       builder.append("* * *");
+                                       builder.append("</B></P><BR><BR>");
+                                       break;
+                               case QUOTE:
+                                       builder.append("<DIV>");
+                                       builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
+                                       builder.append("&mdash;&nbsp;");
+                                       builder.append(decorateText(para.getContent()));
+                                       builder.append("</DIV>");
+
+                                       break;
+                               case IMAGE:
+                               }
+                       }
+
+                       @Override
+                       protected String enbold(String word) {
+                               return "<B COLOR='#7777DD'>" + 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
+        * @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();
+       }
+}
index e57ff058d492ebb6edd1e14008a3db7edd99a4f6..f4be4459f539dfe98cf7fe698fe134401019db87 100644 (file)
@@ -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;
 
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 2286215c59c0a83c3db1dd18c1219ad34b27687a..89cbf84edf1a3dc7f6d2d8066e6eede4b06322fe 100644 (file)
@@ -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);
index 91965ef327bd4b175f9669b93047acafde6c4c37..bc79815790b8de7cb79eedea7746229b15477905 100644 (file)
@@ -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.
+        * <p>
+        * 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.
         */
index 26008dc3dc0267bc8f4efa7b14f4a75d1d61e48d..564b599587d73a766b08367b3a4674e333371718 100644 (file)
@@ -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;
        }