gui: code cleanup for properties
authorNiki Roo <niki@nikiroo.be>
Fri, 22 Mar 2019 18:18:18 +0000 (19:18 +0100)
committerNiki Roo <niki@nikiroo.be>
Fri, 22 Mar 2019 18:18:18 +0000 (19:18 +0100)
src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java
src/be/nikiroo/fanfix/reader/ui/GuiReaderPropertiesFrame.java
src/be/nikiroo/fanfix/reader/ui/GuiReaderPropertiesPane.java [new file with mode: 0644]

index df8707c72331e5eeefb8f53012c8be4ea97a683d..045bb46374d6d0546f9efd77e34d7b4bbb5af197 100644 (file)
@@ -84,7 +84,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
 
                setSize(800, 600);
                setLayout(new BorderLayout());
-               add(mainPanel);
+               add(mainPanel, BorderLayout.CENTER);
        }
 
        @Override
@@ -738,8 +738,9 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                        mainPanel.outOfUi(null, new Runnable() {
                                                @Override
                                                public void run() {
-                                                       new GuiReaderPropertiesFrame(reader, selectedBook
-                                                                       .getInfo()).setVisible(true);
+                                                       new GuiReaderPropertiesFrame(reader.getLibrary(),
+                                                                       selectedBook.getInfo().getMeta())
+                                                                       .setVisible(true);
                                                }
                                        });
                                }
@@ -763,8 +764,8 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                if (selectedBook != null) {
                                        if (selectedBook.getInfo().getMeta() == null) {
                                                mainPanel.removeBookPanes();
-                                               mainPanel.addBookPane(selectedBook.getInfo().getMainInfo(),
-                                                               mainPanel.getCurrentType());
+                                               mainPanel.addBookPane(selectedBook.getInfo()
+                                                               .getMainInfo(), mainPanel.getCurrentType());
                                                mainPanel.refreshBooks();
                                        } else {
                                                mainPanel.openBook(selectedBook);
index 18417716816d25c0a7f15e72dc650f30927cef10..deddc3cd84dbbd10b0355853487247bf46ef7e61 100644 (file)
@@ -1,22 +1,12 @@
 package be.nikiroo.fanfix.reader.ui;
 
 import java.awt.BorderLayout;
-import java.awt.Color;
-import java.awt.Font;
-import java.util.Map;
 
-import javax.swing.BorderFactory;
-import javax.swing.BoxLayout;
-import javax.swing.ImageIcon;
 import javax.swing.JFrame;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.JTextArea;
 
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.reader.BasicReader;
-import be.nikiroo.fanfix.reader.Reader;
+import be.nikiroo.fanfix.library.BasicLibrary;
 
 /**
  * A frame displaying properties and other information of a {@link Story}.
@@ -29,71 +19,20 @@ public class GuiReaderPropertiesFrame extends JFrame {
        /**
         * Create a new {@link GuiReaderPropertiesFrame}.
         * 
-        * @param reader
-        *            the linked reader
+        * @param ib
+        *            the library to use for the cover image
         * @param meta
         *            the meta to describe
         */
-       public GuiReaderPropertiesFrame(Reader reader, GuiReaderBookInfo info) {
-               MetaData meta = info.getMeta();
-
-               // Borders
-               int top = 20;
-               int space = 10;
-
-               // Image
-               ImageIcon img = GuiReaderCoverImager.generateCoverIcon(
-                               reader.getLibrary(), info);
-
-               // frame
+       public GuiReaderPropertiesFrame(BasicLibrary lib, MetaData meta) {
                setTitle(meta.getLuid() + ": " + meta.getTitle());
 
-               setSize(800, img.getIconHeight() + 2 * top);
-               setLayout(new BorderLayout());
-
-               // Main panel
-               JPanel mainPanel = new JPanel(new BorderLayout());
-               JPanel mainPanelKeys = new JPanel();
-               mainPanelKeys.setLayout(new BoxLayout(mainPanelKeys, BoxLayout.Y_AXIS));
-               JPanel mainPanelValues = new JPanel();
-               mainPanelValues.setLayout(new BoxLayout(mainPanelValues,
-                               BoxLayout.Y_AXIS));
-
-               mainPanel.add(mainPanelKeys, BorderLayout.WEST);
-               mainPanel.add(mainPanelValues, BorderLayout.CENTER);
+               GuiReaderPropertiesPane desc = new GuiReaderPropertiesPane(lib, meta);
+               setSize(800,
+                               (int) desc.getPreferredSize().getHeight() + 2
+                                               * desc.getBorderThickness());
 
-               Map<String, String> desc = BasicReader.getMetaDesc(meta);
-
-               Color trans = new Color(0, 0, 0, 1);
-               for (String key : desc.keySet()) {
-                       JTextArea jKey = new JTextArea(key);
-                       jKey.setFont(new Font(jKey.getFont().getFontName(), Font.BOLD, jKey
-                                       .getFont().getSize()));
-                       jKey.setEditable(false);
-                       jKey.setLineWrap(false);
-                       jKey.setBackground(trans);
-                       mainPanelKeys.add(jKey);
-
-                       JTextArea jValue = new JTextArea(desc.get(key));
-                       jValue.setEditable(false);
-                       jValue.setLineWrap(false);
-                       jValue.setBackground(trans);
-                       mainPanelValues.add(jValue);
-               }
-
-               // Image
-               JLabel imgLabel = new JLabel(img);
-               imgLabel.setVerticalAlignment(JLabel.TOP);
-
-               // Borders
-               mainPanelKeys.setBorder(BorderFactory.createEmptyBorder(top, space, 0,
-                               0));
-               mainPanelValues.setBorder(BorderFactory.createEmptyBorder(top, space,
-                               0, 0));
-               imgLabel.setBorder(BorderFactory.createEmptyBorder(0, space, 0, 0));
-
-               // Add all
-               add(imgLabel, BorderLayout.WEST);
-               add(mainPanel, BorderLayout.CENTER);
+               setLayout(new BorderLayout());
+               add(desc, BorderLayout.NORTH);
        }
 }
diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderPropertiesPane.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderPropertiesPane.java
new file mode 100644 (file)
index 0000000..2c9c7e7
--- /dev/null
@@ -0,0 +1,99 @@
+package be.nikiroo.fanfix.reader.ui;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Font;
+import java.util.Map;
+
+import javax.swing.BorderFactory;
+import javax.swing.BoxLayout;
+import javax.swing.ImageIcon;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+
+import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.reader.BasicReader;
+
+/**
+ * A panel displaying properties and other information of a {@link Story}.
+ * 
+ * @author niki
+ */
+public class GuiReaderPropertiesPane extends JPanel {
+       private static final long serialVersionUID = 1L;
+       private final int space = 10;
+
+       /**
+        * Create a new {@link GuiReaderPropertiesPane}.
+        * 
+        * @param lib
+        *            the library to use for the cover image
+        * @param meta
+        *            the meta to describe
+        */
+       public GuiReaderPropertiesPane(BasicLibrary lib, MetaData meta) {
+               // Image
+               ImageIcon img = GuiReaderCoverImager.generateCoverIcon(lib, meta);
+
+               setLayout(new BorderLayout());
+
+               // Main panel
+               JPanel mainPanel = new JPanel(new BorderLayout());
+               JPanel mainPanelKeys = new JPanel();
+               mainPanelKeys.setLayout(new BoxLayout(mainPanelKeys, BoxLayout.Y_AXIS));
+               JPanel mainPanelValues = new JPanel();
+               mainPanelValues.setLayout(new BoxLayout(mainPanelValues,
+                               BoxLayout.Y_AXIS));
+
+               mainPanel.add(mainPanelKeys, BorderLayout.WEST);
+               mainPanel.add(mainPanelValues, BorderLayout.CENTER);
+
+               Map<String, String> desc = BasicReader.getMetaDesc(meta);
+
+               Color trans = new Color(0, 0, 0, 1);
+               Color base = mainPanelValues.getBackground();
+               for (String key : desc.keySet()) {
+                       JTextArea jKey = new JTextArea(key);
+                       jKey.setFont(new Font(jKey.getFont().getFontName(), Font.BOLD, jKey
+                                       .getFont().getSize()));
+                       jKey.setEditable(false);
+                       jKey.setLineWrap(false);
+                       jKey.setBackground(trans);
+                       mainPanelKeys.add(jKey);
+
+                       final JTextArea jValue = new JTextArea(desc.get(key));
+                       jValue.setEditable(false);
+                       jValue.setLineWrap(false);
+                       jValue.setBackground(base);
+                       mainPanelValues.add(jValue);
+               }
+
+               // Image
+               JLabel imgLabel = new JLabel(img);
+               imgLabel.setVerticalAlignment(JLabel.TOP);
+
+               // Borders
+               mainPanelKeys.setBorder(BorderFactory.createEmptyBorder(space, space,
+                               space, space));
+               mainPanelValues.setBorder(BorderFactory.createEmptyBorder(space, space,
+                               space, space));
+               imgLabel.setBorder(BorderFactory.createEmptyBorder(0, space, space, 0));
+
+               // Add all
+               add(imgLabel, BorderLayout.WEST);
+               add(mainPanel, BorderLayout.CENTER);
+       }
+
+       /**
+        * The invisible border size (multiply by 2 if you need the total width or
+        * the total height).
+        * 
+        * @return the invisible border thickness
+        */
+       public int getBorderThickness() {
+               return space;
+       }
+}