From 7cbd1407c87aa20dd2275229dfc6baea31e3d195 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 27 Apr 2020 22:38:34 +0200 Subject: [PATCH] fix Properties is last context menu item --- .../fanfix_swing/gui/book/BookPopup.java | 64 +++++++++---------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java index 10ba1eb6..6e515768 100644 --- a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java +++ b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java @@ -43,19 +43,26 @@ public class BookPopup extends JPopupMenu { private BasicLibrary lib; private Informer informer; - private JMenuItem moveTo; // to update later - private JMenuItem setAuthor; // to update later + private Map> groupedSources; + private Map> groupedAuthors; public BookPopup(BasicLibrary lib, Informer informer) { this.lib = lib; this.informer = informer; + initMenus(); + reloadData(); + } + + private void initMenus() { + removeAll(); + Status status = lib.getStatus(); add(createMenuItemOpenBook()); addSeparator(); add(createMenuItemExport()); if (status.isWritable()) { - moveTo = add(createMenuItemMoveTo(null)); + add(createMenuItemMoveTo(groupedSources)); add(createMenuItemSetCoverForSource()); add(createMenuItemSetCoverForAuthor()); } @@ -65,21 +72,17 @@ public class BookPopup extends JPopupMenu { add(createMenuItemRedownload()); addSeparator(); add(createMenuItemRename()); - setAuthor = add(createMenuItemSetAuthor(null)); + add(createMenuItemSetAuthor(groupedAuthors)); addSeparator(); add(createMenuItemDelete()); } addSeparator(); add(createMenuItemProperties()); - reloadMoveToSetAuthor(); + revalidate(); } public void reloadData() { - reloadMoveToSetAuthor(); - } - - private void reloadMoveToSetAuthor() { new SwingWorker() { @Override protected MetaResultList doInBackground() throws Exception { @@ -90,18 +93,9 @@ public class BookPopup extends JPopupMenu { 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())); - + groupedSources = list.getSourcesGrouped(); + groupedAuthors = list.getAuthorsGrouped(); + initMenus(); } catch (Exception e) { UiHelper.error(BookPopup.this.getParent(), e.getLocalizedMessage(), "IOException", e); @@ -170,8 +164,8 @@ public class BookPopup extends JPopupMenu { List list = groupedSources.get(type); if (list.size() == 1 && list.get(0).isEmpty()) { item = new JMenuItem(type); - item.addActionListener(createMoveAction(ChangeAction.SOURCE, - type)); + item.addActionListener( + createMoveAction(ChangeAction.SOURCE, type)); changeTo.add(item); } else { JMenu dir = new JMenu(type); @@ -184,8 +178,8 @@ public class BookPopup extends JPopupMenu { } item = new JMenuItem(itemName); - item.addActionListener(createMoveAction( - ChangeAction.SOURCE, actualType)); + item.addActionListener( + createMoveAction(ChangeAction.SOURCE, actualType)); dir.add(item); } changeTo.add(dir); @@ -221,22 +215,22 @@ public class BookPopup extends JPopupMenu { for (String key : groupedAuthors.keySet()) { JMenu group = new JMenu(key); for (String value : groupedAuthors.get(key)) { - JMenuItem item = new JMenuItem( - value.isEmpty() ? trans(StringIdGui.MENU_AUTHORS_UNKNOWN) - : value); - item.addActionListener(createMoveAction( - ChangeAction.AUTHOR, value)); + JMenuItem item = new JMenuItem(value.isEmpty() + ? trans(StringIdGui.MENU_AUTHORS_UNKNOWN) + : value); + item.addActionListener( + createMoveAction(ChangeAction.AUTHOR, value)); group.add(item); } changeTo.add(group); } } else if (groupedAuthors.size() == 1) { for (String value : groupedAuthors.values().iterator().next()) { - JMenuItem item = new JMenuItem( - value.isEmpty() ? trans(StringIdGui.MENU_AUTHORS_UNKNOWN) - : value); - item.addActionListener(createMoveAction(ChangeAction.AUTHOR, - value)); + JMenuItem item = new JMenuItem(value.isEmpty() + ? trans(StringIdGui.MENU_AUTHORS_UNKNOWN) + : value); + item.addActionListener( + createMoveAction(ChangeAction.AUTHOR, value)); changeTo.add(item); } } -- 2.27.0