Enter/Delete keys support for books panel
authorNiki Roo <niki@nikiroo.be>
Sat, 25 Apr 2020 11:55:39 +0000 (13:55 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 25 Apr 2020 11:56:06 +0000 (13:56 +0200)
src/be/nikiroo/fanfix_swing/gui/BooksPanel.java
src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java [new file with mode: 0644]
src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java

index 3ccac1fea6ad8a27e9c1a1afb44caa0dec6c5a4d..e2e3b1277094139389287e9d343352d81dc3e85e 100644 (file)
@@ -6,6 +6,8 @@ import java.awt.Dimension;
 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;
@@ -46,6 +48,9 @@ public class BooksPanel extends ListenerPanel {
        private DelayWorker bookCoverUpdater;
        private String filter = "";
 
+       private Informer informer;
+       private BooksPanelActions actions;
+
        private Object[] lastLoad = new Object[4];
 
        public BooksPanel(boolean listMode) {
@@ -195,9 +200,11 @@ public class BooksPanel extends ListenerPanel {
        }
 
        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
@@ -206,19 +213,23 @@ public class BooksPanel extends ListenerPanel {
                                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);
                        }
                });
 
@@ -232,6 +243,11 @@ public class BooksPanel extends ListenerPanel {
 
        private Informer initInformer() {
                return new BookPopup.Informer() {
+                       @Override
+                       public BooksPanelActions getActions() {
+                               return actions;
+                       }
+
                        @Override
                        public void setCached(BookInfo book, boolean cached) {
                                book.setCached(cached);
diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java
new file mode 100644 (file)
index 0000000..3c4fb8d
--- /dev/null
@@ -0,0 +1,112 @@
+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);
+       }
+}
index c067cfb446e7b1420d01c95bc80febdb020d215b..8b62698ef09859f2fd2674c9dac4862815b23229 100644 (file)
@@ -29,7 +29,7 @@ import be.nikiroo.fanfix.data.Story;
 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;
@@ -39,6 +39,8 @@ import be.nikiroo.utils.ui.ConfigEditor;
 public class BookPopup extends JPopupMenu {
        public abstract interface Informer {
 
+               public BooksPanelActions getActions();
+
                // not null
                public List<BookInfo> getSelected();
 
@@ -616,60 +618,7 @@ public class BookPopup extends JPopupMenu {
                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();
                        }
                });
 
@@ -709,16 +658,7 @@ public class BookPopup extends JPopupMenu {
                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();
                        }
                });