From: Niki Roo Date: Sun, 26 Apr 2020 09:30:20 +0000 (+0200) Subject: refresh sources/authors in popup when changes X-Git-Tag: fanfix-swing-1.0.0~22 X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=866edfd9008355dc9c539180a2fc10977df8cc9e;p=fanfix-swing.git refresh sources/authors in popup when changes --- diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index 0eef128d..ac10a1a7 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -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 initList() { informer = initInformer(); + popup = new BookPopup(Instance.getInstance().getLibrary(), informer); actions = new BooksPanelActions(this, informer); final JList6 list = new JList6(); - data = new ListModel(list, - new BookPopup(Instance.getInstance().getLibrary(), informer)); + data = new ListModel(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(); } }; } diff --git a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java index d6499385..d3f7e51b 100644 --- a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java +++ b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java @@ -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() { + @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> groupedSources) { + if (groupedSources == null) { + groupedSources = new HashMap>(); + } + JMenu changeTo = new JMenu(trans(StringIdGui.MENU_FILE_MOVE_TO)); changeTo.setMnemonic(KeyEvent.VK_M); - Map> groupedSources = new HashMap>(); - 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> groupedAuthors) { + if (groupedAuthors == null) { + groupedAuthors = new HashMap>(); + } + 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> groupedAuthors; - - try { - groupedAuthors = lib.getAuthorsGrouped(); - } catch (IOException e) { - UiHelper.error(BookPopup.this.getParent(), e.getLocalizedMessage(), - "IOException", e); - groupedAuthors = new HashMap>(); - - } - 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); + } }