From bf251eb40de04a55e3fe008c7c3040c413180412 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 25 Apr 2020 17:53:05 +0200 Subject: [PATCH] enable redownload (do not delete old) --- .../nikiroo/fanfix_swing/gui/BooksPanel.java | 1 - .../fanfix_swing/gui/BooksPanelActions.java | 8 ++ .../nikiroo/fanfix_swing/gui/MainFrame.java | 43 ++++--- .../fanfix_swing/gui/book/BookPopup.java | 14 +-- .../gui/importer/ImporterFrame.java | 115 ++++++++++++++---- .../gui/importer/ImporterItem.java | 6 +- 6 files changed, 124 insertions(+), 63 deletions(-) diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index e2e3b127..b0009c17 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -22,7 +22,6 @@ import javax.swing.SwingWorker; import be.nikiroo.fanfix.Instance; 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.BookBlock; import be.nikiroo.fanfix_swing.gui.book.BookInfo; import be.nikiroo.fanfix_swing.gui.book.BookLine; diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java index 3c4fb8d0..daec0a30 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java @@ -106,6 +106,14 @@ public class BooksPanelActions { } } + public void redownload() { + MainFrame.getImporter().setVisible(true); + final List selected = informer.getSelected(); + for (BookInfo book : selected) { + MainFrame.getImporter().imprt(owner, book.getMeta().getUrl()); + } + } + static private String trans(StringIdGui id, Object... values) { return Instance.getInstance().getTransGui().getString(id, values); } diff --git a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java index 2df86cc5..06e85e06 100644 --- a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java @@ -32,11 +32,12 @@ import be.nikiroo.utils.Version; import be.nikiroo.utils.ui.ConfigEditor; public class MainFrame extends JFrame { + static private ImporterFrame importer; + private BooksPanel books; private DetailsPanel details; private BrowserPanel browser; private BreadCrumbsPanel goBack; - private ImporterFrame importer = new ImporterFrame(); private List modeItems = new ArrayList(); private boolean sidePanel; @@ -46,8 +47,24 @@ public class MainFrame extends JFrame { public MainFrame(boolean sidePanel, boolean detailsPanel) { super("Fanfix " + Version.getCurrentVersion()); + if (importer == null) { + importer = new ImporterFrame(); + } + + importer.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (e != null && ImporterFrame.IMPORTED + .equals(e.getActionCommand())) { + browser.reloadData(); + books.reloadData(); + details.setBook(browser.getHighlight()); + } + } + }); + browser = new BrowserPanel(); - books = new BooksPanel(true); + books = new BooksPanel(true); // TODO: very slow here!! details = new DetailsPanel(); goBack = new BreadCrumbsPanel(); @@ -117,6 +134,10 @@ public class MainFrame extends JFrame { setSize(800, 600); } + static public ImporterFrame getImporter() { + return importer; + } + public void setSidePanel(boolean sidePanel) { setMode(sidePanel, detailsPanel); } @@ -155,14 +176,7 @@ public class MainFrame extends JFrame { mnuDownload.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - importer.imprtUrl(MainFrame.this, new Runnable() { - @Override - public void run() { - browser.reloadData(); - books.reloadData(); - details.setBook(browser.getHighlight()); - } - }); + importer.imprtUrl(MainFrame.this); } }); @@ -170,14 +184,7 @@ public class MainFrame extends JFrame { mnuImprtFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - importer.imprtFile(MainFrame.this, new Runnable() { - @Override - public void run() { - browser.reloadData(); - books.reloadData(); - details.setBook(browser.getHighlight()); - } - }); + importer.imprtFile(MainFrame.this); } }); diff --git a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java index 8b62698e..46c6946b 100644 --- a/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java +++ b/src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java @@ -514,19 +514,7 @@ public class BookPopup extends JPopupMenu { refresh.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - // final GuiReaderBook selectedBook = - // mainPanel.getSelectedBook(); - // if (selectedBook != null) { - // final MetaData meta = selectedBook.getInfo().getMeta(); - // mainPanel.imprt(meta.getUrl(), new MetaDataRunnable() { - // @Override - // public void run(MetaData newMeta) { - // if (!newMeta.getSource().equals(meta.getSource())) { - // reader.changeSource(newMeta.getLuid(), meta.getSource()); - // } - // } - // }, trans(StringIdGui.PROGRESS_CHANGE_SOURCE)); - // } + informer.getActions().redownload(); } }); diff --git a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java index 6c0b9134..e73c9ea5 100644 --- a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java @@ -27,13 +27,20 @@ import be.nikiroo.utils.Progress; import be.nikiroo.utils.compat.JList6; import be.nikiroo.utils.ui.ListModel; import be.nikiroo.utils.ui.ListModel.Predicate; +import be.nikiroo.utils.ui.ListenerItem; +import be.nikiroo.utils.ui.ListenerPanel; -public class ImporterFrame extends JFrame { +public class ImporterFrame extends JFrame implements ListenerItem { + static public final String IMPORTED = "imported"; + + private ListenerPanel root = new ListenerPanel(); private ListModel data; private String filter = ""; public ImporterFrame() { setLayout(new BorderLayout()); + root.setLayout(new BorderLayout()); + this.add(root); JList6 list = new JList6(); data = new ListModel(list); @@ -43,7 +50,7 @@ public class ImporterFrame extends JFrame { list.setSelectedIndex(0); list.setVisibleRowCount(5); - this.add(list, BorderLayout.CENTER); + root.add(list, BorderLayout.CENTER); JPanel top = new JPanel(); top.setLayout(new BorderLayout()); @@ -78,7 +85,7 @@ public class ImporterFrame extends JFrame { } }); - this.add(top, BorderLayout.NORTH); + root.add(top, BorderLayout.NORTH); setSize(800, 600); } @@ -87,14 +94,14 @@ public class ImporterFrame extends JFrame { * Ask for and import an {@link URL} into the main {@link LocalLibrary}. *

* Should be called inside the UI thread. + *

+ * Will fire {@link ImporterFrame#IMPORTED} if/when successful. * * @param parent * a container we can use to display the {@link URL} chooser and * to show error messages if any - * @param onSuccess - * Action to execute on success */ - public void imprtUrl(final Container parent, final Runnable onSuccess) { + public void imprtUrl(final Container parent) { String clipboard = ""; try { clipboard = ("" + Toolkit.getDefaultToolkit().getSystemClipboard() @@ -115,38 +122,24 @@ public class ImporterFrame extends JFrame { .getString(StringIdGui.TITLE_IMPORT_URL), JOptionPane.QUESTION_MESSAGE, null, null, clipboard); - Progress pg = new Progress(); - String basename = null; - try { - BasicSupport support = BasicSupport - .getSupport(BasicReader.getUrl(url.toString())); - basename = support.getType().getSourceName(); - } catch (Exception e) { - } - - add(pg, basename); // TODO: what when null? - if (url != null && !url.toString().isEmpty()) { - Actions.imprt(parent, url.toString(), pg, onSuccess); + imprt(parent, url.toString()); } - // TODO what when not ok? - - setVisible(true); } /** * Ask for and import a {@link File} into the main {@link LocalLibrary}. *

* Should be called inside the UI thread. + *

+ * Will fire {@link ImporterFrame#IMPORTED} if/when successful. * * @param parent * a container we can use to display the {@link File} chooser and * to show error messages if any - * @param onSuccess - * Action to execute on success */ - public void imprtFile(final Container parent, final Runnable onSuccess) { + public void imprtFile(final Container parent) { JFileChooser fc = new JFileChooser(); Progress pg = new Progress(); @@ -155,11 +148,56 @@ public class ImporterFrame extends JFrame { if (fc.showOpenDialog(parent) != JFileChooser.CANCEL_OPTION) { Object url = fc.getSelectedFile().getAbsolutePath(); if (url != null && !url.toString().isEmpty()) { - Actions.imprt(parent, url.toString(), pg, onSuccess); + Actions.imprt(parent, url.toString(), pg, new Runnable() { + @Override + public void run() { + fireActionPerformed(IMPORTED); + } + }); + + setVisible(true); } } + } + + /** + * Import an {@link URL} into the main {@link LocalLibrary}. + *

+ * Should be called inside the UI thread. + *

+ * Will fire {@link ImporterFrame#IMPORTED} if/when successful. + * + * @param parent + * a container we can use to display the {@link URL} chooser and + * to show error messages if any + * @param url + * the URL to import + */ + public void imprt(final Container parent, String url) { + Progress pg = new Progress(); + String basename = null; + try { + BasicSupport support = BasicSupport + .getSupport(BasicReader.getUrl(url)); + basename = support.getType().getSourceName(); + } catch (Exception e) { + basename = "unknown website"; + } + + add(pg, basename); // TODO: what when null? - setVisible(true); + if (url != null && !url.isEmpty()) { + Actions.imprt(parent, url, pg, new Runnable() { + @Override + public void run() { + fireActionPerformed(IMPORTED); + } + }); + + setVisible(true); + } + + // TODO what when not ok? } private void add(Progress pg, final String basename) { @@ -185,4 +223,29 @@ public class ImporterFrame extends JFrame { } }); } + + @Override + public boolean hasListeners() { + return root.hasListeners(); + } + + @Override + public int getWaitingEventCount() { + return root.getWaitingEventCount(); + } + + @Override + public void addActionListener(ActionListener listener) { + root.addActionListener(listener); + } + + @Override + public void removeActionListener(ActionListener listener) { + root.removeActionListener(listener); + } + + @Override + public void fireActionPerformed(String listenerCommand) { + root.fireActionPerformed(listenerCommand); + } } diff --git a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java index 532c5f63..ae81355d 100644 --- a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java +++ b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java @@ -2,22 +2,18 @@ package be.nikiroo.fanfix_swing.gui.importer; import java.awt.BorderLayout; import java.awt.Color; -import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.ListCellRenderer; import javax.swing.SwingUtilities; import be.nikiroo.fanfix_swing.gui.utils.CoverImager; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Progress.ProgressListener; -import be.nikiroo.utils.ui.ListModel; -import be.nikiroo.utils.ui.ListenerPanel; import be.nikiroo.utils.ui.ListModel.Hoverable; +import be.nikiroo.utils.ui.ListenerPanel; public class ImporterItem extends ListenerPanel implements Hoverable { static public final String CHANGE = "change"; -- 2.27.0