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;
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<BookInfo> {
+public class BooksPanel extends ListenerPanel {
+ private class ListModel extends DefaultListModel<BookInfo> {
public void fireElementChanged(BookInfo element) {
int index = indexOf(element);
if (index >= 0) {
}
}
+ static public final String INVALIDATE_CACHE = "invalidate_cache";
+
private List<BookInfo> bookInfos = new ArrayList<BookInfo>();
private Map<BookInfo, BookLine> books = new HashMap<BookInfo, BookLine>();
private boolean seeWordCount;
searchBar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- reload(searchBar.getText());
+ filter(searchBar.getText());
}
});
protected List<BookInfo> doInBackground() throws Exception {
List<BookInfo> bookInfos = new ArrayList<BookInfo>();
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));
}
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())) {
+ if (bookInfo.getMainInfo() == null || filter.isEmpty()
+ || bookInfo.getMainInfo().toLowerCase().contains(filter.toLowerCase())) {
data.addElement(bookInfo);
}
}
private JList<BookInfo> initList(boolean listMode) {
final JList<BookInfo> list = new JList<BookInfo>(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<BookInfo> getSelected() {
+ List<BookInfo> selected = new ArrayList<BookInfo>();
+ for (int index : list.getSelectedIndices()) {
+ selected.add(data.get(index));
}
+
+ return selected;
+ }
+
+ @Override
+ public BookInfo getUniqueSelected() {
+ List<BookInfo> 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);
}
});
Actions.openExternal(lib, book.getMeta(), BooksPanel.this, new Runnable() {
@Override
public void run() {
+ book.setCached(true);
data.fireElementChanged(book);
}
});