From: Niki Roo Date: Sat, 25 Apr 2020 11:55:39 +0000 (+0200) Subject: Enter/Delete keys support for books panel X-Git-Tag: fanfix-swing-0.0.2~6 X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=4ae6309c118e4f14d6f50c1d07ed75b4e9a54522;p=fanfix-swing.git Enter/Delete keys support for books panel --- diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index 3ccac1fe..e2e3b127 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -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 initList() { + informer = initInformer(); + actions = new BooksPanelActions(this, informer); final JList6 list = new JList6(); - data = new ListModel(list, new BookPopup( - Instance.getInstance().getLibrary(), initInformer())); + data = new ListModel(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 index 00000000..3c4fb8d0 --- /dev/null +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java @@ -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 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() { + + @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 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); + } +} diff --git a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java index c067cfb4..8b62698e 100644 --- a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java +++ b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java @@ -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 getSelected(); @@ -616,60 +618,7 @@ public class BookPopup extends JPopupMenu { delete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - final List 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() { - - @Override - public Void doInBackground() throws Exception { - for (BookInfo info : selected) { - lib.delete(info.getMeta().getLuid()); - publish(info); - } - - return null; - } - - @Override - protected void process(List 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(); } });