From 6dacf4052b7865e3ae8c9f0f710cacfda62fd480 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 7 May 2020 21:24:45 +0200 Subject: [PATCH] touch: text viewer --- .../nikiroo/fanfix_swing/gui/TouchFrame.java | 88 +++++++++++------- .../fanfix_swing/gui/viewer/ViewerImages.java | 5 +- .../gui/viewer/ViewerNonImages.java | 93 ++++++++++++------- 3 files changed, 118 insertions(+), 68 deletions(-) diff --git a/src/be/nikiroo/fanfix_swing/gui/TouchFrame.java b/src/be/nikiroo/fanfix_swing/gui/TouchFrame.java index b2daba06..68407c32 100644 --- a/src/be/nikiroo/fanfix_swing/gui/TouchFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/TouchFrame.java @@ -118,9 +118,11 @@ public class TouchFrame extends JFrame { private void open(Story story) { final JComponent[] comps = new JComponent[3]; - // Integrate it with showViewer or something + final JFrame viewer; if (story.getMeta().isImageDocument()) { - final ViewerImages viewer = new ViewerImages(story) { + viewer = new ViewerImages(story) { + private static final long serialVersionUID = 1L; + @Override protected JToolBar createToolbar() { // we need it to be created to steal its content @@ -137,43 +139,67 @@ public class TouchFrame extends JFrame { comps[2] = zoombox; } }; + } else { + viewer = new ViewerNonImages(Instance.getInstance().getLibrary(), + story) { + private static final long serialVersionUID = 1L; - removeShows(); - - JComponent scroll = comps[0]; + @Override + protected JToolBar createToolbar() { + // we need it to be created to steal its content + super.createToolbar(); + return null; + } - JPanel navbar = new JPanel(); - navbar.add(comps[1]); + @Override + protected JPanel createDescPane() { + return null; + } - JPanel zoombox = new JPanel(); - JButton exit = new JButton(IconGenerator.get(Icon.back, Size.x32)); - exit.addActionListener(new ActionListener() { @Override - public void actionPerformed(ActionEvent e) { - removeShows(); - viewer.dispose(); - showBooks(); + protected void initGui() { + super.initGui(); + title.setFont(title.getFont() + .deriveFont(title.getFont().getSize() * 0.75f)); + comps[0] = scroll; + comps[1] = navbar; + comps[2] = title; } - }); - zoombox.add(exit); - zoombox.add(Box.createRigidArea(new Dimension(10, 10))); - zoombox.add(comps[2]); + }; + } - active.add(scroll); - active.add(navbar); - active.add(zoombox); + removeShows(); - TouchFrame.this.add(navbar, BorderLayout.NORTH); - TouchFrame.this.add(zoombox, BorderLayout.SOUTH); - root.add(scroll); + JComponent scroll = comps[0]; - revalidate(); - repaint(); - } else { - ViewerNonImages viewer = new ViewerNonImages( - Instance.getInstance().getLibrary(), story); - viewer.setVisible(true); - } + JPanel navbar = new JPanel(); + navbar.add(comps[1]); + + JButton exit = new JButton(IconGenerator.get(Icon.back, Size.x32)); + exit.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + removeShows(); + viewer.dispose(); + showBooks(); + } + }); + + JPanel zoombox = new JPanel(); + zoombox.add(exit); + zoombox.add(Box.createRigidArea(new Dimension(10, 10))); + zoombox.add(comps[2]); + + active.add(scroll); + active.add(navbar); + active.add(zoombox); + + TouchFrame.this.add(navbar, BorderLayout.NORTH); + TouchFrame.this.add(zoombox, BorderLayout.SOUTH); + root.add(scroll); + + revalidate(); + repaint(); } private void removeShows() { diff --git a/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerImages.java b/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerImages.java index 35985228..befdc0ed 100644 --- a/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerImages.java +++ b/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerImages.java @@ -121,7 +121,7 @@ public class ViewerImages extends JFrame { } /** - * Initialise the GUI (after this call, all the raphical elements are in + * Initialise the GUI (after this call, all the graphical elements are in * place). */ protected void initGui() { @@ -148,9 +148,8 @@ public class ViewerImages extends JFrame { /** * Create the main toolbar used for this viewer. * - * @return the toolbar + * @return the toolbar, can be NULL */ - protected JToolBar createToolbar() { final JToolBar toolbar = new JToolBar(); diff --git a/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java b/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java index c5a50919..f72524ee 100644 --- a/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java +++ b/src/be/nikiroo/fanfix_swing/gui/viewer/ViewerNonImages.java @@ -44,12 +44,17 @@ import be.nikiroo.utils.ui.UIUtils; public class ViewerNonImages extends JFrame { private static final long serialVersionUID = 1L; + private BasicLibrary lib; private Story story; private ViewerTextOutput html; - private NavBar navbar; - private JLabel title; - private JScrollPane scroll; + /** The navigation bar. */ + protected NavBar navbar; + /** The story title */ + protected JLabel title; + /** The main element of this viewer: the scrolled text. */ + protected JScrollPane scroll; + private JEditorPane area; private JPanel descPane; @@ -66,6 +71,7 @@ public class ViewerNonImages extends JFrame { * */ public ViewerNonImages(BasicLibrary lib, Story story) { + this.lib = lib; this.story = story; this.setTitle(Instance.getInstance().getTransGui().getString( StringIdGui.TITLE_STORY, story.getMeta().getLuid(), @@ -77,7 +83,7 @@ public class ViewerNonImages extends JFrame { worker = new DelayWorker(100); worker.start(); - initGui(lib); + initGui(); setChapter(0); UiHelper.setFrameIcon(this, lib, story.getMeta()); @@ -85,13 +91,10 @@ public class ViewerNonImages extends JFrame { } /** - * Initialise the base panel. - * - * @param lib - * the {@link BasicLibrary} to use to retrieve the cover image in - * the description panel + * Initialise the GUI (after this call, all the graphical elements are in + * place). */ - private void initGui(BasicLibrary lib) { + protected void initGui() { this.setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)); title = new JLabel(); @@ -101,8 +104,10 @@ public class ViewerNonImages extends JFrame { title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); title.setToolTipText(story.getMeta().getTitle()); - JToolBar toolbarTitle = createToolBar(); - toolbarTitle.add(title); + JToolBar toolbarTitle = createToolbar(); + if (toolbarTitle != null) { + toolbarTitle.add(title); + } area = new JEditorPane("text/html", ""); area.setEditable(false); @@ -114,28 +119,16 @@ public class ViewerNonImages extends JFrame { scroll = UIUtils.scroll(area, false); - JLabel descLabel = new JLabel("Description"); - descLabel.setFont(new Font(Font.SERIF, Font.BOLD, - (int) Math.round(descLabel.getFont().getSize() * 1.5))); - descLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); - descLabel.setHorizontalAlignment(JLabel.CENTER); - descLabel.setOpaque(true); - Color bg = descLabel.getBackground(); - descLabel.setBackground(descLabel.getForeground()); - descLabel.setForeground(bg); - - descPane = new JPanel(new BorderLayout()); - PropertiesPanel desc = new PropertiesPanel(lib, story.getMeta(), false); - desc.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); - descPane.add(desc, BorderLayout.CENTER); - descPane.add(descLabel, BorderLayout.SOUTH); + descPane = createDescPane(); area.setSize(scroll.getViewport().getSize()); area.setPreferredSize(this.getSize()); // make it as big as possible area.requestFocus(); - this.add(toolbarTitle); - this.add(descPane); + if (toolbarTitle != null) + this.add(toolbarTitle); + if (descPane != null) + this.add(descPane); this.add(scroll); listen(); @@ -150,7 +143,12 @@ public class ViewerNonImages extends JFrame { }); } - private JToolBar createToolBar() { + /** + * Create the main toolbar used for this viewer. + * + * @return the toolbar, can be NULL + */ + protected JToolBar createToolbar() { JToolBar toolbar = new JToolBar(); navbar = new NavBar(0, story.getChapters().size()); navbar.setIcons( // @@ -163,6 +161,31 @@ public class ViewerNonImages extends JFrame { return toolbar; } + /** + * Create the story description panel used for this viewer. + * + * @return the panel, can be NULL + */ + protected JPanel createDescPane() { + JLabel descLabel = new JLabel("Description"); + descLabel.setFont(new Font(Font.SERIF, Font.BOLD, + (int) Math.round(descLabel.getFont().getSize() * 1.5))); + descLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + descLabel.setHorizontalAlignment(JLabel.CENTER); + descLabel.setOpaque(true); + Color bg = descLabel.getBackground(); + descLabel.setBackground(descLabel.getForeground()); + descLabel.setForeground(bg); + + JPanel descPane = new JPanel(new BorderLayout()); + PropertiesPanel desc = new PropertiesPanel(lib, story.getMeta(), false); + desc.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); + descPane.add(desc, BorderLayout.CENTER); + descPane.add(descLabel, BorderLayout.SOUTH); + + return descPane; + } + /** * Set the current chapter, 0-based. *

@@ -186,10 +209,12 @@ public class ViewerNonImages extends JFrame { protected void done() { try { String text = get(); - if (chapter <= 0) { - descPane.setVisible(true); - } else { - descPane.setVisible(false); + if (descPane != null) { + if (chapter <= 0) { + descPane.setVisible(true); + } else { + descPane.setVisible(false); + } } area.setText(text); -- 2.27.0