refresh sources/authors in popup when changes
authorNiki Roo <niki@nikiroo.be>
Sun, 26 Apr 2020 09:30:20 +0000 (11:30 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 26 Apr 2020 09:30:20 +0000 (11:30 +0200)
src/be/nikiroo/fanfix_swing/gui/BooksPanel.java
src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java

index 0eef128dd93355299743a77ffc4de963b0e0ff76..ac10a1a7427cd3edc74b384ea63a0d4bdb0b2712 100644 (file)
@@ -51,6 +51,7 @@ public class BooksPanel extends ListenerPanel {
 
        private Informer informer;
        private BooksPanelActions actions;
+       private BookPopup popup;
 
        private Object[] lastLoad = new Object[4];
 
@@ -146,6 +147,9 @@ public class BooksPanel extends ListenerPanel {
                        lastLoad = this.lastLoad.clone();
                }
 
+               // Reset the popup menu items for for sources/author
+               popup.reloadData();
+
                if (lastLoad[0] == null) {
                        return; // nothing was loaded yet
                }
@@ -208,10 +212,10 @@ public class BooksPanel extends ListenerPanel {
 
        private JList6<BookInfo> initList() {
                informer = initInformer();
+               popup = new BookPopup(Instance.getInstance().getLibrary(), informer);
                actions = new BooksPanelActions(this, informer);
                final JList6<BookInfo> list = new JList6<BookInfo>();
-               data = new ListModel<BookInfo>(list,
-                               new BookPopup(Instance.getInstance().getLibrary(), informer));
+               data = new ListModel<BookInfo>(list, popup);
 
                list.addMouseListener(new MouseAdapter() {
                        @Override
@@ -283,8 +287,8 @@ public class BooksPanel extends ListenerPanel {
 
                        @Override
                        public void invalidateCache() {
-                               // TODO: also reset the popup menu for sources/author
                                fireActionPerformed(INVALIDATE_CACHE);
+                               reloadData();
                        }
                };
        }
index d6499385a98e7b608a8739774f723b5774b5935c..d3f7e51bf91fc263b221aa54ef5f8f6fca673e8d 100644 (file)
@@ -27,6 +27,7 @@ import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.MetaResultList;
 import be.nikiroo.fanfix.library.BasicLibrary.Status;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix_swing.gui.BooksPanelActions;
@@ -69,10 +70,10 @@ public class BookPopup extends JPopupMenu {
                AUTHOR
        }
 
-       // be careful with that
        private BasicLibrary lib;
-
        private Informer informer;
+       private JMenuItem moveTo; // to update later
+       private JMenuItem setAuthor; // to update later
 
        public BookPopup(BasicLibrary lib, Informer informer) {
                this.lib = lib;
@@ -83,10 +84,7 @@ public class BookPopup extends JPopupMenu {
                addSeparator();
                add(createMenuItemExport());
                if (status.isWritable()) {
-                       // TODO: create a dedicated method to refresh those lists
-                       // TODO: call it when adding/removing things
-                       // TODO: call it deferred so not to slow startup
-                       add(createMenuItemMoveTo());
+                       moveTo = add(createMenuItemMoveTo(null));
                        add(createMenuItemSetCoverForSource());
                        add(createMenuItemSetCoverForAuthor());
                }
@@ -96,16 +94,50 @@ public class BookPopup extends JPopupMenu {
                        add(createMenuItemRedownload());
                        addSeparator();
                        add(createMenuItemRename());
-                       add(createMenuItemSetAuthor());
+                       setAuthor = add(createMenuItemSetAuthor(null));
                        addSeparator();
                        add(createMenuItemDelete());
                }
                addSeparator();
                add(createMenuItemProperties());
+
+               reloadMoveToSetAuthor();
        }
 
-       private String trans(StringIdGui id, Object... values) {
-               return Instance.getInstance().getTransGui().getString(id, values);
+       public void reloadData() {
+               reloadMoveToSetAuthor();
+       }
+
+       private void reloadMoveToSetAuthor() {
+               new SwingWorker<MetaResultList, Void>() {
+                       @Override
+                       protected MetaResultList doInBackground() throws Exception {
+                               return lib.getList();
+                       }
+
+                       @Override
+                       protected void done() {
+                               try {
+                                       MetaResultList list = get();
+
+                                       if (moveTo != null) {
+                                               remove(moveTo);
+                                       }
+                                       moveTo = add(
+                                                       createMenuItemMoveTo(list.getSourcesGrouped()));
+
+                                       if (setAuthor != null) {
+                                               remove(setAuthor);
+                                       }
+                                       setAuthor = add(
+                                                       createMenuItemSetAuthor(list.getAuthorsGrouped()));
+
+                               } catch (Exception e) {
+                                       UiHelper.error(BookPopup.this.getParent(),
+                                                       e.getLocalizedMessage(), "IOException", e);
+                               }
+                       }
+               }.execute();
        }
 
        /**
@@ -306,18 +338,15 @@ public class BookPopup extends JPopupMenu {
         * 
         * @return the item
         */
-       private JMenuItem createMenuItemMoveTo() {
+       private JMenuItem createMenuItemMoveTo(
+                       Map<String, List<String>> groupedSources) {
+               if (groupedSources == null) {
+                       groupedSources = new HashMap<String, List<String>>();
+               }
+
                JMenu changeTo = new JMenu(trans(StringIdGui.MENU_FILE_MOVE_TO));
                changeTo.setMnemonic(KeyEvent.VK_M);
 
-               Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
-               try {
-                       groupedSources = lib.getSourcesGrouped();
-               } catch (IOException e) {
-                       UiHelper.error(BookPopup.this.getParent(), e.getLocalizedMessage(),
-                                       "IOException", e);
-               }
-
                JMenuItem item = new JMenuItem(
                                trans(StringIdGui.MENU_FILE_MOVE_TO_NEW_TYPE));
                item.addActionListener(createMoveAction(ChangeAction.SOURCE, null));
@@ -358,7 +387,12 @@ public class BookPopup extends JPopupMenu {
         * 
         * @return the item
         */
-       private JMenuItem createMenuItemSetAuthor() {
+       private JMenuItem createMenuItemSetAuthor(
+                       Map<String, List<String>> groupedAuthors) {
+               if (groupedAuthors == null) {
+                       groupedAuthors = new HashMap<String, List<String>>();
+               }
+
                JMenu changeTo = new JMenu(trans(StringIdGui.MENU_FILE_SET_AUTHOR));
                changeTo.setMnemonic(KeyEvent.VK_A);
 
@@ -370,17 +404,6 @@ public class BookPopup extends JPopupMenu {
                newItem.addActionListener(createMoveAction(ChangeAction.AUTHOR, null));
 
                // Existing authors
-               Map<String, List<String>> groupedAuthors;
-
-               try {
-                       groupedAuthors = lib.getAuthorsGrouped();
-               } catch (IOException e) {
-                       UiHelper.error(BookPopup.this.getParent(), e.getLocalizedMessage(),
-                                       "IOException", e);
-                       groupedAuthors = new HashMap<String, List<String>>();
-
-               }
-
                if (groupedAuthors.size() > 1) {
                        for (String key : groupedAuthors.keySet()) {
                                JMenu group = new JMenu(key);
@@ -480,6 +503,10 @@ public class BookPopup extends JPopupMenu {
                                                                // simple fireElementChanged is not
                                                                // enough, we need to clear the whole cache (for
                                                                // BrowserPanel for instance)
+                                                               //
+                                                               // Note:
+                                                               // This will also reresh the authors/sources
+                                                               // lists here
                                                                if (what != ChangeAction.TITLE) {
                                                                        informer.invalidateCache();
                                                                }
@@ -490,9 +517,6 @@ public class BookPopup extends JPopupMenu {
                                                                        informer.fireElementChanged(book);
                                                                }
 
-                                                               // TODO: also refresh the
-                                                               // Sources/Authors(/Tags?) list
-
                                                                // Even if problems occurred, still invalidate
                                                                // the cache above
                                                                get();
@@ -728,4 +752,8 @@ public class BookPopup extends JPopupMenu {
 
                return open;
        }
+
+       static private String trans(StringIdGui id, Object... values) {
+               return Instance.getInstance().getTransGui().getString(id, values);
+       }
 }