enable redownload (do not delete old)
authorNiki Roo <niki@nikiroo.be>
Sat, 25 Apr 2020 15:53:05 +0000 (17:53 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 25 Apr 2020 15:53:05 +0000 (17:53 +0200)
src/be/nikiroo/fanfix_swing/gui/BooksPanel.java
src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java
src/be/nikiroo/fanfix_swing/gui/MainFrame.java
src/be/nikiroo/fanfix_swing/gui/book/BookPopup.java
src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java
src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java

index e2e3b1277094139389287e9d343352d81dc3e85e..b0009c17ae64f987789d48c3013e778c14790738 100644 (file)
@@ -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;
index 3c4fb8d0f37ace44f736ec1f653a08dc51053328..daec0a30945707684bc009c18becdbc30771f68b 100644 (file)
@@ -106,6 +106,14 @@ public class BooksPanelActions {
                }
        }
 
+       public void redownload() {
+               MainFrame.getImporter().setVisible(true);
+               final List<BookInfo> 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);
        }
index 2df86cc580ed6f46b7ee52c3fa1115c46e105c6f..06e85e063b13c443075996d365357a30f4479578 100644 (file)
@@ -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<JComponent> modeItems = new ArrayList<JComponent>();
        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);
                        }
                });
 
index 8b62698ef09859f2fd2674c9dac4862815b23229..46c6946b672772055d9e46fa6fc36aed32fcca17 100644 (file)
@@ -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();
                        }
                });
 
index 6c0b9134f789a62173b5037ee926d0a2779f9a12..e73c9ea5ebaf56ccf4d4ae053444362fdba1aec8 100644 (file)
@@ -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<ImporterItem> data;
        private String filter = "";
 
        public ImporterFrame() {
                setLayout(new BorderLayout());
+               root.setLayout(new BorderLayout());
+               this.add(root);
 
                JList6<ImporterItem> list = new JList6<ImporterItem>();
                data = new ListModel<ImporterItem>(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}.
         * <p>
         * Should be called inside the UI thread.
+        * <p>
+        * 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}.
         * <p>
         * Should be called inside the UI thread.
+        * <p>
+        * 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}.
+        * <p>
+        * Should be called inside the UI thread.
+        * <p>
+        * 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);
+       }
 }
index 532c5f63f40aec7c6c11dc079898b6ecb2d9d8c2..ae81355db874e189708874b9d846ee8c7a5b90e2 100644 (file)
@@ -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";