import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
private DelayWorker bookCoverUpdater;
private String filter = "";
+ private Informer informer;
+ private BooksPanelActions actions;
+
private Object[] lastLoad = new Object[4];
public BooksPanel(boolean listMode) {
}
private JList6<BookInfo> initList() {
+ informer = initInformer();
+ actions = new BooksPanelActions(this, informer);
final JList6<BookInfo> list = new JList6<BookInfo>();
- data = new ListModel<BookInfo>(list, new BookPopup(
- Instance.getInstance().getLibrary(), initInformer()));
+ data = new ListModel<BookInfo>(list,
+ new BookPopup(Instance.getInstance().getLibrary(), informer));
list.addMouseListener(new MouseAdapter() {
@Override
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
list.setSelectedIndex(index);
+ actions.openBook();
+ }
+ }
+ });
- final BookInfo book = data.get(index);
- BasicLibrary lib = Instance.getInstance().getLibrary();
-
- Actions.openBook(lib, book.getMeta(), BooksPanel.this,
- new Runnable() {
- @Override
- public void run() {
- book.setCached(true);
- data.fireElementChanged(book);
- }
- });
+ list.addKeyListener(new KeyAdapter() {
+ @Override
+ public void keyPressed(KeyEvent e) {
+ if (e.getKeyCode() == KeyEvent.VK_ENTER
+ || e.getKeyCode() == KeyEvent.VK_ACCEPT) {
+ actions.openBook();
+ e.consume();
+ } else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
+ actions.deleteBooks();
+ e.consume();
}
+ super.keyTyped(e);
}
});
private Informer initInformer() {
return new BookPopup.Informer() {
+ @Override
+ public BooksPanelActions getActions() {
+ return actions;
+ }
+
@Override
public void setCached(BookInfo book, boolean cached) {
book.setCached(cached);
--- /dev/null
+package be.nikiroo.fanfix_swing.gui;
+
+import java.awt.Container;
+import java.util.List;
+
+import javax.swing.JOptionPane;
+import javax.swing.SwingWorker;
+
+import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.bundles.StringIdGui;
+import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix_swing.Actions;
+import be.nikiroo.fanfix_swing.gui.book.BookInfo;
+import be.nikiroo.fanfix_swing.gui.book.BookPopup.Informer;
+import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
+
+public class BooksPanelActions {
+ private Container owner;
+ private Informer informer;
+
+ public BooksPanelActions(Container owner, Informer informer) {
+ this.owner = owner;
+ this.informer = informer;
+ }
+
+ /**
+ * Open the currently selected book if it is the only one selected.
+ *
+ * @return TRUE if a book was opened, FALSE if not (no book selected, or
+ * more than one book selected)
+ */
+ public boolean openBook() {
+ BasicLibrary lib = Instance.getInstance().getLibrary();
+ final BookInfo book = informer.getUniqueSelected();
+
+ if (book != null) {
+ Actions.openBook(lib, book.getMeta(), owner, new Runnable() {
+ @Override
+ public void run() {
+ book.setCached(true);
+ informer.fireElementChanged(book);
+ }
+ });
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public void deleteBooks() {
+ final List<BookInfo> selected = informer.getSelected();
+
+ // TODO: i18n is geared towards ONE item
+ if (selected.size() > 0) {
+ String one;
+ String two;
+ if (selected.size() == 1) {
+ MetaData meta = selected.get(0).getMeta();
+ one = meta.getLuid();
+ two = meta.getTitle();
+ } else {
+ one = "";
+ two = selected.size() + " stories";
+ }
+
+ int rep = JOptionPane.showConfirmDialog(owner,
+ trans(StringIdGui.SUBTITLE_DELETE, one, two),
+ trans(StringIdGui.TITLE_DELETE),
+ JOptionPane.OK_CANCEL_OPTION);
+
+ if (rep == JOptionPane.OK_OPTION) {
+ new SwingWorker<Void, BookInfo>() {
+
+ @Override
+ public Void doInBackground() throws Exception {
+ BasicLibrary lib = Instance.getInstance().getLibrary();
+
+ for (BookInfo info : selected) {
+ lib.delete(info.getMeta().getLuid());
+ publish(info);
+ }
+
+ return null;
+ }
+
+ @Override
+ protected void process(List<BookInfo> chunks) {
+ for (BookInfo info : chunks) {
+ informer.removeElement(info);
+ }
+ }
+
+ @Override
+ protected void done() {
+ try {
+ get();
+ } catch (Exception e) {
+ UiHelper.error(owner, e.getLocalizedMessage(),
+ "IOException", e);
+ }
+ }
+ }.execute();
+ }
+ }
+ }
+
+ static private String trans(StringIdGui id, Object... values) {
+ return Instance.getInstance().getTransGui().getString(id, values);
+ }
+}
import be.nikiroo.fanfix.library.BasicLibrary;
import be.nikiroo.fanfix.library.BasicLibrary.Status;
import be.nikiroo.fanfix.output.BasicOutput.OutputType;
-import be.nikiroo.fanfix_swing.Actions;
+import be.nikiroo.fanfix_swing.gui.BooksPanelActions;
import be.nikiroo.fanfix_swing.gui.PropertiesFrame;
import be.nikiroo.fanfix_swing.gui.utils.CoverImager;
import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
public class BookPopup extends JPopupMenu {
public abstract interface Informer {
+ public BooksPanelActions getActions();
+
// not null
public List<BookInfo> getSelected();
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- final List<BookInfo> selected = informer.getSelected();
-
- // TODO: i18n is geared towards ONE item
- if (selected.size() > 0) {
- String one;
- String two;
- if (selected.size() == 1) {
- MetaData meta = selected.get(0).getMeta();
- one = meta.getLuid();
- two = meta.getTitle();
- } else {
- one = "";
- two = selected.size() + " stories";
- }
-
- int rep = JOptionPane.showConfirmDialog(
- BookPopup.this.getParent(),
- trans(StringIdGui.SUBTITLE_DELETE, one, two),
- trans(StringIdGui.TITLE_DELETE),
- JOptionPane.OK_CANCEL_OPTION);
-
- if (rep == JOptionPane.OK_OPTION) {
- new SwingWorker<Void, BookInfo>() {
-
- @Override
- public Void doInBackground() throws Exception {
- for (BookInfo info : selected) {
- lib.delete(info.getMeta().getLuid());
- publish(info);
- }
-
- return null;
- }
-
- @Override
- protected void process(List<BookInfo> chunks) {
- for (BookInfo info : chunks) {
- informer.removeElement(info);
- }
- }
-
- @Override
- protected void done() {
- try {
- get();
- } catch (Exception e) {
- UiHelper.error(BookPopup.this.getParent(),
- e.getLocalizedMessage(),
- "IOException", e);
- }
- }
- }.execute();
- }
- }
+ informer.getActions().deleteBooks();
}
});
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- final BookInfo book = informer.getUniqueSelected();
- if (book != null) {
- Actions.openBook(lib, book.getMeta(),
- BookPopup.this.getParent(), new Runnable() {
- @Override
- public void run() {
- informer.setCached(book, true);
- }
- });
- }
+ informer.getActions().openBook();
}
});