X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix_swing%2Fgui%2FBooksPanel.java;h=8a882136e3803e856ea0bd9cc36f862dc9118a04;hb=42797eff5b7bf96a93bcd79ffa2ce62d256ef35d;hp=5a2b9949325775e451a9652cbee1f7963aecd438;hpb=3cdf3fd8a60d22a592e1cd0634cb108faa1f5f9f;p=fanfix.git diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index 5a2b994..8a88213 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -18,8 +18,6 @@ import java.util.concurrent.ExecutionException; import javax.swing.DefaultListModel; import javax.swing.JList; -import javax.swing.JMenuItem; -import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.ListCellRenderer; import javax.swing.ListSelectionModel; @@ -33,10 +31,12 @@ import be.nikiroo.fanfix_swing.Actions; import be.nikiroo.fanfix_swing.gui.book.BookBlock; import be.nikiroo.fanfix_swing.gui.book.BookInfo; import be.nikiroo.fanfix_swing.gui.book.BookLine; +import be.nikiroo.fanfix_swing.gui.book.BookPopup; +import be.nikiroo.fanfix_swing.gui.utils.ListenerPanel; import be.nikiroo.fanfix_swing.gui.utils.UiHelper; -public class BooksPanel extends JPanel { - class ListModel extends DefaultListModel { +public class BooksPanel extends ListenerPanel { + private class ListModel extends DefaultListModel { public void fireElementChanged(BookInfo element) { int index = indexOf(element); if (index >= 0) { @@ -45,6 +45,8 @@ public class BooksPanel extends JPanel { } } + static public final String INVALIDATE_CACHE = "invalidate_cache"; + private List bookInfos = new ArrayList(); private Map books = new HashMap(); private boolean seeWordCount; @@ -68,7 +70,7 @@ public class BooksPanel extends JPanel { searchBar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - reload(searchBar.getText()); + filter(searchBar.getText()); } }); @@ -127,7 +129,7 @@ public class BooksPanel extends JPanel { protected List doInBackground() throws Exception { List bookInfos = new ArrayList(); BasicLibrary lib = Instance.getInstance().getLibrary(); - for (MetaData meta : lib.getList(null).filter(sources, authors, tags)) { + for (MetaData meta : lib.getList().filter(sources, authors, tags)) { bookInfos.add(BookInfo.fromMeta(lib, meta)); } @@ -155,11 +157,11 @@ public class BooksPanel extends JPanel { updateBookQueue.clear(); } - reload(searchBar.getText()); + filter(searchBar.getText()); } // cannot be NULL - private void reload(String filter) { + private void filter(String filter) { data.clear(); for (BookInfo bookInfo : bookInfos) { if (filter.isEmpty() || bookInfo.getMainInfo().toLowerCase().contains(filter.toLowerCase())) { @@ -198,22 +200,40 @@ public class BooksPanel extends JPanel { private JList initList(boolean listMode) { final JList list = new JList(data); - final JPopupMenu popup = new JPopupMenu(); - JMenuItem open = popup.add("Open"); - open.addActionListener(new ActionListener() { + final JPopupMenu popup = new BookPopup(Instance.getInstance().getLibrary(), new BookPopup.Informer() { @Override - public void actionPerformed(ActionEvent e) { - int[] selected = list.getSelectedIndices(); - if (selected.length == 1) { - final BookInfo book = data.get(selected[0]); - BasicLibrary lib = Instance.getInstance().getLibrary(); - Actions.openExternal(lib, book.getMeta(), BooksPanel.this, new Runnable() { - @Override - public void run() { - data.fireElementChanged(book); - } - }); + public void setCached(BookInfo book, boolean cached) { + book.setCached(cached); + fireElementChanged(book); + } + + public void fireElementChanged(BookInfo book) { + data.fireElementChanged(book); + } + + @Override + public List getSelected() { + List selected = new ArrayList(); + for (int index : list.getSelectedIndices()) { + selected.add(data.get(index)); } + + return selected; + } + + @Override + public BookInfo getUniqueSelected() { + List selected = getSelected(); + if (selected.size() == 1) { + return selected.get(0); + } + return null; + } + + @Override + public void invalidateCache() { + // TODO: also reset the popup menu for sources/author + fireActionPerformed(INVALIDATE_CACHE); } }); @@ -266,6 +286,7 @@ public class BooksPanel extends JPanel { Actions.openExternal(lib, book.getMeta(), BooksPanel.this, new Runnable() { @Override public void run() { + book.setCached(true); data.fireElementChanged(book); } });