gui: separate authors into subgroups
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderFrame.java
index b98bed22cd8c42b0d290639015814f56b0d29dda..d849a438388f139d2c236d9bc647c03b265c5c1c 100644 (file)
@@ -2,6 +2,7 @@ package be.nikiroo.fanfix.reader.ui;
 
 import java.awt.BorderLayout;
 import java.awt.Color;
+import java.awt.Font;
 import java.awt.Frame;
 import java.awt.Toolkit;
 import java.awt.datatransfer.DataFlavor;
@@ -15,12 +16,15 @@ import java.io.IOException;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import javax.swing.BorderFactory;
 import javax.swing.BoxLayout;
+import javax.swing.ImageIcon;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
@@ -31,6 +35,7 @@ import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JPopupMenu;
 import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
 import javax.swing.SwingConstants;
 import javax.swing.SwingUtilities;
 import javax.swing.filechooser.FileFilter;
@@ -184,7 +189,7 @@ class GuiReaderFrame extends JFrame {
                                                break;
 
                                        case UNAVAILABLE:
-                                               err += "Library currently unavilable";
+                                               err += "Library currently unavailable";
                                                break;
 
                                        default:
@@ -308,6 +313,8 @@ class GuiReaderFrame extends JFrame {
                                popup.add(createMenuItemRedownload());
                                popup.addSeparator();
                                popup.add(createMenuItemDelete());
+                               popup.addSeparator();
+                               popup.add(createMenuItemProperties());
                                popup.show(e.getComponent(), e.getX(), e.getY());
                        }
 
@@ -330,7 +337,6 @@ class GuiReaderFrame extends JFrame {
 
        /**
         * Refresh the list of {@link GuiReaderBook}s from disk.
-        * 
         */
        private void refreshBooks() {
                for (GuiReaderGroup group : booksByType.keySet()) {
@@ -462,27 +468,29 @@ class GuiReaderFrame extends JFrame {
                JMenu authors = new JMenu("Authors");
                authors.setMnemonic(KeyEvent.VK_A);
 
-               List<String> aa = new ArrayList<String>();
-               if (libOk) {
-                       aa.addAll(reader.getLibrary().getAuthors());
-               }
-               aa.add(0, null);
-               for (final String author : aa) {
-                       JMenuItem item = new JMenuItem(author == null ? "All"
-                                       : author.isEmpty() ? "[unknown]" : author);
-                       item.addActionListener(new ActionListener() {
-                               @Override
-                               public void actionPerformed(ActionEvent e) {
-                                       removeBookPanes();
-                                       addBookPane(author, false);
-                                       refreshBooks();
-                               }
-                       });
-                       authors.add(item);
+               List<Entry<String, List<String>>> authorGroups = reader.getLibrary()
+                               .getAuthorsGrouped();
+               if (authorGroups.size() > 1) {
+                       // Multiple groups
 
-                       if (author == null || author.isEmpty()) {
-                               authors.addSeparator();
+                       // null -> "All" authors special item
+                       populateMenuAuthorList(authors, Arrays.asList((String) null));
+
+                       for (Entry<String, List<String>> group : authorGroups) {
+                               JMenu thisGroup = new JMenu(group.getKey());
+                               populateMenuAuthorList(thisGroup, group.getValue());
+                               authors.add(thisGroup);
                        }
+               } else {
+                       // Only one group
+
+                       // null -> "All" authors special item
+                       List<String> authorNames = new ArrayList<String>();
+                       authorNames.add(null);
+                       if (authorGroups.size() > 0) {
+                               authorNames.addAll(authorGroups.get(0).getValue());
+                       }
+                       populateMenuAuthorList(authors, authorNames);
                }
 
                bar.add(authors);
@@ -496,6 +504,37 @@ class GuiReaderFrame extends JFrame {
                return bar;
        }
 
+       /**
+        * Populate a list of authors as {@link JMenuItem}s into the given
+        * {@link JMenu}.
+        * <p>
+        * Each item will select the author when clicked.
+        * 
+        * @param authors
+        *            the parent {@link JMenuItem}
+        * @param names
+        *            the authors' names
+        */
+       private void populateMenuAuthorList(JMenu authors, List<String> names) {
+               for (final String name : names) {
+                       JMenuItem item = new JMenuItem(name == null ? "All"
+                                       : name.isEmpty() ? "[unknown]" : name);
+                       item.addActionListener(new ActionListener() {
+                               @Override
+                               public void actionPerformed(ActionEvent e) {
+                                       removeBookPanes();
+                                       addBookPane(name, false);
+                                       refreshBooks();
+                               }
+                       });
+                       authors.add(item);
+
+                       if (name == null || name.isEmpty()) {
+                               authors.addSeparator();
+                       }
+               }
+       }
+
        /**
         * Create the Fanfix Configuration menu item.
         * 
@@ -654,7 +693,8 @@ class GuiReaderFrame extends JFrame {
                                                        reader.clearLocalReaderCache(selectedBook.getMeta()
                                                                        .getLuid());
                                                        selectedBook.setCached(false);
-                                                       GuiReaderBook.clearIcon(selectedBook.getMeta());
+                                                       GuiReaderCoverImager.clearIcon(selectedBook
+                                                                       .getMeta());
                                                        SwingUtilities.invokeLater(new Runnable() {
                                                                @Override
                                                                public void run() {
@@ -720,7 +760,7 @@ class GuiReaderFrame extends JFrame {
                                                outOfUi(null, new Runnable() {
                                                        @Override
                                                        public void run() {
-                                                               reader.changeType(selectedBook.getMeta()
+                                                               reader.changeSource(selectedBook.getMeta()
                                                                                .getLuid(), ftype);
 
                                                                selectedBook = null;
@@ -760,7 +800,7 @@ class GuiReaderFrame extends JFrame {
                                                        GuiReaderFrame.this.selectedBook = null;
                                                        MetaData newMeta = story.getMeta();
                                                        if (!newMeta.getSource().equals(meta.getSource())) {
-                                                               reader.changeType(newMeta.getLuid(),
+                                                               reader.changeSource(newMeta.getLuid(),
                                                                                meta.getSource());
                                                        }
                                                }
@@ -797,6 +837,110 @@ class GuiReaderFrame extends JFrame {
                return delete;
        }
 
+       /**
+        * Create the properties menu item.
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemProperties() {
+               JMenuItem delete = new JMenuItem("Properties", KeyEvent.VK_P);
+               delete.addActionListener(new ActionListener() {
+                       @Override
+                       public void actionPerformed(ActionEvent e) {
+                               if (selectedBook != null) {
+                                       outOfUi(null, new Runnable() {
+                                               @Override
+                                               public void run() {
+                                                       final MetaData meta = selectedBook.getMeta();
+                                                       new JFrame() {
+                                                               private static final long serialVersionUID = 1L;
+                                                               @SuppressWarnings("unused")
+                                                               private Object init = init();
+
+                                                               private Object init() {
+                                                                       // Borders
+                                                                       int top = 20;
+                                                                       int space = 10;
+
+                                                                       // Image
+                                                                       ImageIcon img = GuiReaderCoverImager
+                                                                                       .generateCoverIcon(
+                                                                                                       reader.getLibrary(), meta);
+
+                                                                       // frame
+                                                                       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);
+
+                                                                       List<Entry<String, String>> infos = BasicReader
+                                                                                       .getMetaDesc(meta);
+
+                                                                       Color trans = new Color(0, 0, 0, 1);
+                                                                       for (Entry<String, String> info : infos) {
+                                                                               JTextArea key = new JTextArea(info
+                                                                                               .getKey());
+                                                                               key.setFont(new Font(key.getFont()
+                                                                                               .getFontName(), Font.BOLD, key
+                                                                                               .getFont().getSize()));
+                                                                               key.setEditable(false);
+                                                                               key.setLineWrap(false);
+                                                                               key.setBackground(trans);
+                                                                               mainPanelKeys.add(key);
+
+                                                                               JTextArea value = new JTextArea(info
+                                                                                               .getValue());
+                                                                               value.setEditable(false);
+                                                                               value.setLineWrap(false);
+                                                                               value.setBackground(trans);
+                                                                               mainPanelValues.add(value);
+                                                                       }
+
+                                                                       // 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);
+
+                                                                       return null;
+                                                               }
+
+                                                       }.setVisible(true);
+                                               }
+                                       });
+                               }
+                       }
+               });
+
+               return delete;
+       }
+
        /**
         * Create the open menu item for a book or a source (no LUID).
         * 
@@ -839,7 +983,7 @@ class GuiReaderFrame extends JFrame {
                                                        selectedBook.getMeta().getLuid());
                                        MetaData source = selectedBook.getMeta().clone();
                                        source.setLuid(null);
-                                       GuiReaderBook.clearIcon(source);
+                                       GuiReaderCoverImager.clearIcon(source);
                                }
                        }
                });
@@ -859,7 +1003,7 @@ class GuiReaderFrame extends JFrame {
                        @Override
                        public void run() {
                                try {
-                                       reader.read(book.getMeta().getLuid(), pg);
+                                       reader.read(book.getMeta().getLuid(), false, pg);
                                        SwingUtilities.invokeLater(new Runnable() {
                                                @Override
                                                public void run() {