gui: use submenus for subdirs
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderFrame.java
index d849a438388f139d2c236d9bc647c03b265c5c1c..b1ba90c9acde6cca6d23ad060eaf9526bac0a479 100644 (file)
@@ -307,11 +307,14 @@ class GuiReaderFrame extends JFrame {
                                popup.add(createMenuItemOpenBook());
                                popup.addSeparator();
                                popup.add(createMenuItemExport());
-                               popup.add(createMenuItemMove(true));
+                               popup.add(createMenuItemMoveTo(true));
                                popup.add(createMenuItemSetCover());
                                popup.add(createMenuItemClearCache());
                                popup.add(createMenuItemRedownload());
                                popup.addSeparator();
+                               popup.add(createMenuItemRename(true));
+                               popup.add(createMenuItemSetAuthor(true));
+                               popup.addSeparator();
                                popup.add(createMenuItemDelete());
                                popup.addSeparator();
                                popup.add(createMenuItemProperties());
@@ -394,11 +397,14 @@ class GuiReaderFrame extends JFrame {
 
                file.add(createMenuItemOpenBook());
                file.add(createMenuItemExport());
-               file.add(createMenuItemMove(libOk));
+               file.add(createMenuItemMoveTo(libOk));
                file.addSeparator();
                file.add(imprt);
                file.add(imprtF);
                file.addSeparator();
+               file.add(createMenuItemRename(libOk));
+               file.add(createMenuItemSetAuthor(libOk));
+               file.addSeparator();
                file.add(exit);
 
                bar.add(file);
@@ -440,26 +446,37 @@ class GuiReaderFrame extends JFrame {
                JMenu sources = new JMenu("Sources");
                sources.setMnemonic(KeyEvent.VK_S);
 
-               List<String> tt = new ArrayList<String>();
+               Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
                if (libOk) {
-                       tt.addAll(reader.getLibrary().getSources());
+                       groupedSources = reader.getLibrary().getSourcesGrouped();
                }
-               tt.add(0, null);
 
-               for (final String type : tt) {
-                       JMenuItem item = new JMenuItem(type == null ? "All" : type);
-                       item.addActionListener(new ActionListener() {
-                               @Override
-                               public void actionPerformed(ActionEvent e) {
-                                       removeBookPanes();
-                                       addBookPane(type, true);
-                                       refreshBooks();
-                               }
-                       });
-                       sources.add(item);
+               JMenuItem item = new JMenuItem("All");
+               item.addActionListener(getActionOpenSource(null));
+               sources.add(item);
+               sources.addSeparator();
+
+               for (final String type : groupedSources.keySet()) {
+                       List<String> list = groupedSources.get(type);
+                       if (list.size() == 1 && list.get(0).isEmpty()) {
+                               item = new JMenuItem(type);
+                               item.addActionListener(getActionOpenSource(type));
+                               sources.add(item);
+                       } else {
+                               JMenu dir = new JMenu(type);
+                               for (String sub : list) {
+                                       // " " instead of "" for the visual height
+                                       String itemName = sub.isEmpty() ? " " : sub;
+                                       String actualType = type;
+                                       if (!sub.isEmpty()) {
+                                               actualType += "/" + sub;
+                                       }
 
-                       if (type == null) {
-                               sources.addSeparator();
+                                       item = new JMenuItem(itemName);
+                                       item.addActionListener(getActionOpenSource(actualType));
+                                       dir.add(item);
+                               }
+                               sources.add(dir);
                        }
                }
 
@@ -504,6 +521,26 @@ class GuiReaderFrame extends JFrame {
                return bar;
        }
 
+       /**
+        * Return an {@link ActionListener} that will set the given source (type) as
+        * the selected/displayed one.
+        * 
+        * @param type
+        *            the type (source) to select
+        * 
+        * @return the {@link ActionListener}
+        */
+       private ActionListener getActionOpenSource(final String type) {
+               return new ActionListener() {
+                       @Override
+                       public void actionPerformed(ActionEvent e) {
+                               removeBookPanes();
+                               addBookPane(type, true);
+                               refreshBooks();
+                       }
+               };
+       }
+
        /**
         * Populate a list of authors as {@link JMenuItem}s into the given
         * {@link JMenu}.
@@ -711,74 +748,172 @@ class GuiReaderFrame extends JFrame {
        }
 
        /**
-        * Create the delete menu item.
+        * Create the "move to" menu item.
         * 
         * @param libOk
         *            the library can be queried
         * 
         * @return the item
         */
-       private JMenuItem createMenuItemMove(boolean libOk) {
-               JMenu moveTo = new JMenu("Move to...");
-               moveTo.setMnemonic(KeyEvent.VK_M);
+       private JMenuItem createMenuItemMoveTo(boolean libOk) {
+               JMenu changeTo = new JMenu("Move to");
+               changeTo.setMnemonic(KeyEvent.VK_M);
 
-               List<String> types = new ArrayList<String>();
-               types.add(null);
+               Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
                if (libOk) {
-                       types.addAll(reader.getLibrary().getSources());
+                       groupedSources = reader.getLibrary().getSourcesGrouped();
                }
 
-               for (String type : types) {
-                       JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
+               JMenuItem item = new JMenuItem("New type...");
+               item.addActionListener(createMoveAction("SOURCE", null));
+               changeTo.add(item);
+               changeTo.addSeparator();
+
+               for (final String type : groupedSources.keySet()) {
+                       List<String> list = groupedSources.get(type);
+                       if (list.size() == 1 && list.get(0).isEmpty()) {
+                               item = new JMenuItem(type);
+                               item.addActionListener(createMoveAction("SOURCE", type));
+                               changeTo.add(item);
+                       } else {
+                               JMenu dir = new JMenu(type);
+                               for (String sub : list) {
+                                       // " " instead of "" for the visual height
+                                       String itemName = sub.isEmpty() ? " " : sub;
+                                       String actualType = type;
+                                       if (!sub.isEmpty()) {
+                                               actualType += "/" + sub;
+                                       }
 
-                       moveTo.add(item);
-                       if (type == null) {
-                               moveTo.addSeparator();
+                                       item = new JMenuItem(itemName);
+                                       item.addActionListener(createMoveAction("SOURCE",
+                                                       actualType));
+                                       dir.add(item);
+                               }
+                               changeTo.add(dir);
                        }
+               }
 
-                       final String ftype = type;
-                       item.addActionListener(new ActionListener() {
-                               @Override
-                               public void actionPerformed(ActionEvent e) {
-                                       if (selectedBook != null) {
-                                               String type = ftype;
-                                               if (type == null) {
-                                                       Object rep = JOptionPane.showInputDialog(
-                                                                       GuiReaderFrame.this, "Move to:",
-                                                                       "Moving story",
-                                                                       JOptionPane.QUESTION_MESSAGE, null, null,
-                                                                       selectedBook.getMeta().getSource());
-
-                                                       if (rep == null) {
-                                                               return;
-                                                       }
-
-                                                       type = rep.toString();
-                                               }
+               return changeTo;
+       }
 
-                                               final String ftype = type;
-                                               outOfUi(null, new Runnable() {
-                                                       @Override
-                                                       public void run() {
-                                                               reader.changeSource(selectedBook.getMeta()
-                                                                               .getLuid(), ftype);
+       /**
+        * Create the "set author" menu item.
+        * 
+        * @param libOk
+        *            the library can be queried
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemSetAuthor(boolean libOk) {
+               JMenu changeTo = new JMenu("Set author");
+               changeTo.setMnemonic(KeyEvent.VK_A);
 
-                                                               selectedBook = null;
+               // New author
+               JMenuItem newItem = new JMenuItem("New author...");
+               changeTo.add(newItem);
+               changeTo.addSeparator();
+               newItem.addActionListener(createMoveAction("AUTHOR", null));
 
-                                                               SwingUtilities.invokeLater(new Runnable() {
-                                                                       @Override
-                                                                       public void run() {
-                                                                               setJMenuBar(createMenu(true));
-                                                                       }
-                                                               });
-                                                       }
-                                               });
+               // Existing authors
+               if (libOk) {
+                       List<Entry<String, List<String>>> authorGroups = reader
+                                       .getLibrary().getAuthorsGrouped();
+
+                       if (authorGroups.size() > 1) {
+                               for (Entry<String, List<String>> entry : authorGroups) {
+                                       JMenu group = new JMenu(entry.getKey());
+                                       for (String value : entry.getValue()) {
+                                               JMenuItem item = new JMenuItem(value);
+                                               item.addActionListener(createMoveAction("AUTHOR", value));
+                                               group.add(item);
                                        }
+                                       changeTo.add(group);
                                }
-                       });
+                       } else if (authorGroups.size() == 1) {
+                               for (String value : authorGroups.get(0).getValue()) {
+                                       JMenuItem item = new JMenuItem(value);
+                                       item.addActionListener(createMoveAction("AUTHOR", value));
+                                       changeTo.add(item);
+                               }
+                       }
                }
 
-               return moveTo;
+               return changeTo;
+       }
+
+       /**
+        * Create the "rename" menu item.
+        * 
+        * @param libOk
+        *            the library can be queried
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemRename(
+                       @SuppressWarnings("unused") boolean libOk) {
+               JMenuItem changeTo = new JMenuItem("Rename...");
+               changeTo.setMnemonic(KeyEvent.VK_R);
+               changeTo.addActionListener(createMoveAction("TITLE", null));
+               return changeTo;
+       }
+
+       private ActionListener createMoveAction(final String what, final String type) {
+               return new ActionListener() {
+                       @Override
+                       public void actionPerformed(ActionEvent e) {
+                               if (selectedBook != null) {
+                                       String changeTo = type;
+                                       if (type == null) {
+                                               String init = "";
+                                               if (what.equals("SOURCE")) {
+                                                       init = selectedBook.getMeta().getSource();
+                                               } else if (what.equals("TITLE")) {
+                                                       init = selectedBook.getMeta().getTitle();
+                                               } else if (what.equals("AUTHOR")) {
+                                                       init = selectedBook.getMeta().getAuthor();
+                                               }
+
+                                               Object rep = JOptionPane.showInputDialog(
+                                                               GuiReaderFrame.this, "Move to:",
+                                                               "Moving story", JOptionPane.QUESTION_MESSAGE,
+                                                               null, null, init);
+
+                                               if (rep == null) {
+                                                       return;
+                                               }
+
+                                               changeTo = rep.toString();
+                                       }
+
+                                       final String fChangeTo = changeTo;
+                                       outOfUi(null, new Runnable() {
+                                               @Override
+                                               public void run() {
+                                                       if (what.equals("SOURCE")) {
+                                                               reader.changeSource(selectedBook.getMeta()
+                                                                               .getLuid(), fChangeTo);
+                                                       } else if (what.equals("TITLE")) {
+                                                               reader.changeTitle(selectedBook.getMeta()
+                                                                               .getLuid(), fChangeTo);
+                                                       } else if (what.equals("AUTHOR")) {
+                                                               reader.changeAuthor(selectedBook.getMeta()
+                                                                               .getLuid(), fChangeTo);
+                                                       }
+
+                                                       selectedBook = null;
+
+                                                       SwingUtilities.invokeLater(new Runnable() {
+                                                               @Override
+                                                               public void run() {
+                                                                       setJMenuBar(createMenu(true));
+                                                               }
+                                                       });
+                                               }
+                                       });
+                               }
+                       }
+               };
        }
 
        /**