popup: add title in undecorated mode and add creation date
authorNiki Roo <niki@nikiroo.be>
Sat, 2 May 2020 11:14:33 +0000 (13:14 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 2 May 2020 11:14:33 +0000 (13:14 +0200)
src/be/nikiroo/fanfix_swing/Actions.java
src/be/nikiroo/fanfix_swing/gui/BooksPanel.java
src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java
src/be/nikiroo/fanfix_swing/gui/PropertiesFrame.java
src/be/nikiroo/fanfix_swing/gui/PropertiesPanel.java
src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java

index 54a3f10ff51ac98c45982a5727a817d8425ab817..c7be0f541883968b17a5fe8786435898d089b3ed 100644 (file)
@@ -5,18 +5,17 @@ import java.awt.Container;
 import java.awt.Window;
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import javax.swing.ImageIcon;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
 import javax.swing.SwingWorker;
 
-import org.jsoup.helper.DataUtil;
-import org.jsoup.nodes.Document;
-import org.jsoup.nodes.Element;
-
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.StringIdGui;
 import be.nikiroo.fanfix.bundles.UiConfig;
@@ -24,12 +23,12 @@ import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.fanfix.library.LocalLibrary;
-import be.nikiroo.fanfix.reader.BasicReader;
 import be.nikiroo.fanfix_swing.gui.book.BookInfo;
 import be.nikiroo.fanfix_swing.gui.utils.CoverImager;
 import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
 import be.nikiroo.fanfix_swing.gui.viewer.Viewer;
 import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.StringUtils;
 
 public class Actions {
        static public void openBook(final BasicLibrary lib, MetaData meta,
@@ -248,9 +247,7 @@ public class Actions {
                                        pg = new Progress();
 
                                try {
-                                       Instance.getInstance().getLibrary()
-                                                       .imprt(BasicReader.getUrl(url), pg);
-
+                                       Instance.getInstance().getLibrary().imprt(getUrl(url), pg);
                                        pg.done();
                                        if (onSuccess != null) {
                                                onSuccess.run();
@@ -280,4 +277,77 @@ public class Actions {
                        }
                }.execute();
        }
+
+       /**
+        * Return an {@link URL} from this {@link String}, be it a file path or an
+        * actual {@link URL}.
+        * 
+        * @param sourceString
+        *            the source
+        * 
+        * @return the corresponding {@link URL}
+        * 
+        * @throws MalformedURLException
+        *             if this is neither a file nor a conventional {@link URL}
+        */
+       static public URL getUrl(String sourceString) throws MalformedURLException {
+               if (sourceString == null || sourceString.isEmpty()) {
+                       throw new MalformedURLException("Empty url");
+               }
+
+               URL source = null;
+               try {
+                       source = new URL(sourceString);
+               } catch (MalformedURLException e) {
+                       File sourceFile = new File(sourceString);
+                       source = sourceFile.toURI().toURL();
+               }
+
+               return source;
+       }
+
+       /**
+        * Describe a {@link Story} from its {@link MetaData} and return a list of
+        * title/value that represent this {@link Story}.
+        * 
+        * @param meta
+        *            the {@link MetaData} to represent
+        * 
+        * @return the information
+        */
+       static public Map<String, String> getMetaDesc(MetaData meta) {
+               Map<String, String> metaDesc = new LinkedHashMap<String, String>();
+
+               // TODO: i18n
+
+               StringBuilder tags = new StringBuilder();
+               for (String tag : meta.getTags()) {
+                       if (tags.length() > 0) {
+                               tags.append(", ");
+                       }
+                       tags.append(tag);
+               }
+
+               // TODO: i18n
+               metaDesc.put("Author", meta.getAuthor());
+               metaDesc.put("Published on", meta.getPublisher());
+               metaDesc.put("Publication date", meta.getDate());
+               metaDesc.put("Creation date", meta.getCreationDate());
+               String count = "";
+               if (meta.getWords() > 0) {
+                       count = StringUtils.formatNumber(meta.getWords());
+               }
+               if (meta.isImageDocument()) {
+                       metaDesc.put("Number of images", count);
+               } else {
+                       metaDesc.put("Number of words", count);
+               }
+               metaDesc.put("Source", meta.getSource());
+               metaDesc.put("Subject", meta.getSubject());
+               metaDesc.put("Language", meta.getLang());
+               metaDesc.put("Tags", tags.toString());
+               metaDesc.put("URL", meta.getUrl());
+
+               return metaDesc;
+       }
 }
index 93eb361c6f6c078bc0f067fe8056f4762bbb55b9..ad889c9962e4643584c92d03f84ab060fdcd816e 100644 (file)
@@ -256,8 +256,8 @@ public class BooksPanel extends ListenerPanel {
                                                MetaData meta = book == null ? null : book.getMeta();
                                                if (meta != null) {
                                                        PropertiesFrame tooltip = new PropertiesFrame(
-                                                                       Instance.getInstance().getLibrary(), meta);
-                                                       tooltip.setUndecorated(undecorated);
+                                                                       Instance.getInstance().getLibrary(), meta,
+                                                                       undecorated);
                                                        return tooltip;
                                                }
 
index bf4e12856037332ed2bbb37a40e1bb945e0e5db2..2495ca09d7a810ddbc910a7026391d6c13792e37 100644 (file)
@@ -40,7 +40,7 @@ public class BooksPanelActions {
                /** Change its author. */
                AUTHOR
        }
-       
+
        private Container owner;
        private Informer informer;
 
@@ -138,7 +138,7 @@ public class BooksPanelActions {
                        MainFrame.getImporter().imprt(owner, book.getMeta().getUrl());
                }
        }
-       
+
        public void export() {
                // TODO: allow dir for multiple selection?
 
@@ -193,7 +193,7 @@ public class BooksPanelActions {
                        }
                }
        }
-       
+
        /**
         * Create a {@link FileFilter} that accepts all files and return the given
         * description.
@@ -216,7 +216,7 @@ public class BooksPanelActions {
                        }
                };
        }
-       
+
        public void clearCache() {
                final List<BookInfo> selected = informer.getSelected();
                if (!selected.isEmpty()) {
@@ -239,9 +239,8 @@ public class BooksPanelActions {
                                                        informer.setCached(book, false);
                                                }
                                        } catch (Exception e) {
-                                               UiHelper.error(owner,
-                                                               e.getLocalizedMessage(), "IOException",
-                                                               e);
+                                               UiHelper.error(owner, e.getLocalizedMessage(),
+                                                               "IOException", e);
                                        }
                                }
                        }.execute();
@@ -266,8 +265,7 @@ public class BooksPanelActions {
                                        }
                                }
 
-                               Object rep = JOptionPane.showInputDialog(
-                                               owner,
+                               Object rep = JOptionPane.showInputDialog(owner,
                                                trans(StringIdGui.SUBTITLE_MOVE_TO),
                                                trans(StringIdGui.TITLE_MOVE_TO),
                                                JOptionPane.QUESTION_MESSAGE, null, null, init);
@@ -323,15 +321,14 @@ public class BooksPanelActions {
                                                // the cache above
                                                get();
                                        } catch (Exception e) {
-                                               UiHelper.error(owner,
-                                                               e.getLocalizedMessage(), "IOException",
-                                                               e);
+                                               UiHelper.error(owner, e.getLocalizedMessage(),
+                                                               "IOException", e);
                                        }
                                }
                        }.execute();
                }
        }
-       
+
        public void prefetch() {
                final List<BookInfo> selected = informer.getSelected();
 
@@ -346,8 +343,8 @@ public class BooksPanelActions {
                                                luid = book.getMeta().getLuid();
                                                break;
                                        case SOURCE:
-                                               for (MetaData meta : lib.getList().filter(
-                                                               book.getMainInfo(), null, null)) {
+                                               for (MetaData meta : lib.getList()
+                                                               .filter(book.getMainInfo(), null, null)) {
                                                        luid = meta.getLuid();
                                                }
                                                break;
@@ -358,8 +355,8 @@ public class BooksPanelActions {
                                                }
                                                break;
                                        case TAG:
-                                               for (MetaData meta : lib.getList().filter(null,
-                                                               null, book.getMainInfo())) {
+                                               for (MetaData meta : lib.getList().filter(null, null,
+                                                               book.getMainInfo())) {
                                                        luid = meta.getLuid();
                                                }
                                                break;
@@ -386,22 +383,22 @@ public class BooksPanelActions {
                                try {
                                        get();
                                } catch (Exception e) {
-                                       UiHelper.error(owner,
-                                                       e.getLocalizedMessage(), "IOException", e);
+                                       UiHelper.error(owner, e.getLocalizedMessage(),
+                                                       "IOException", e);
                                }
                        }
                }.execute();
        }
-       
+
        public void properties() {
                BasicLibrary lib = Instance.getInstance().getLibrary();
                BookInfo selected = informer.getUniqueSelected();
                if (selected != null) {
-                       new PropertiesFrame(lib, selected.getMeta())
+                       new PropertiesFrame(lib, selected.getMeta(), false)
                                        .setVisible(true);
                }
        }
-       
+
        public void setCoverFor(final ChangeAction what) {
                final BookInfo book = informer.getUniqueSelected();
                if (book != null) {
@@ -441,7 +438,7 @@ public class BooksPanelActions {
                        }.execute();
                }
        }
-       
+
        static private String trans(StringIdGui id, Object... values) {
                return Instance.getInstance().getTransGui().getString(id, values);
        }
index cc16115a49403e4d3942a0fcafbc60a67d09a065..bca3ecfa680418bb9149167d168f3933f6298965 100644 (file)
@@ -27,15 +27,20 @@ public class PropertiesFrame extends JDialog {
         *            the library to use for the cover image
         * @param meta
         *            the meta to describe
+        * @param undecorated
+        *            do not draw the usual window decorations
+        *            (close/minimize/maximize)
         */
-       public PropertiesFrame(BasicLibrary lib, MetaData meta) {
+       public PropertiesFrame(BasicLibrary lib, MetaData meta,
+                       boolean undecorated) {
                setTitle(MainFrame.trans(StringIdGui.TITLE_STORY, meta.getLuid(),
                                meta.getTitle()));
 
-               desc = new PropertiesPanel(lib, meta);
+               desc = new PropertiesPanel(lib, meta, undecorated);
                setLayout(new BorderLayout());
                add(desc, BorderLayout.NORTH);
 
+               this.setUndecorated(undecorated);
                this.setSize(600, desc.getHeight() + 0);
        }
 
index 1a3bdca83a0d65186f3111b1312b4248bbed6bcc..70d2402ae9367deeb3ac40026257509458642a7a 100644 (file)
@@ -15,11 +15,13 @@ import javax.swing.ImageIcon;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextArea;
+import javax.swing.border.EmptyBorder;
 
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.fanfix.reader.BasicReader;
+import be.nikiroo.fanfix_swing.Actions;
 import be.nikiroo.fanfix_swing.gui.book.BookInfo;
 import be.nikiroo.fanfix_swing.gui.utils.CoverImager;
 import be.nikiroo.utils.ui.UIUtils;
@@ -43,16 +45,43 @@ public class PropertiesPanel extends JPanel {
         *            the library to use for the cover image
         * @param meta
         *            the meta to describe
+        * @param includeTitle
+        *            TRUE to include the title on top
         */
-       public PropertiesPanel(BasicLibrary lib, MetaData meta) {
+       public PropertiesPanel(BasicLibrary lib, MetaData meta,
+                       boolean includeTitle) {
                listenables = new ArrayList<Component>();
 
+               Color trans = new Color(0, 0, 0, 1);
+
                // Image
                ImageIcon img = new ImageIcon(CoverImager.generateCoverImage(lib,
                                BookInfo.fromMeta(lib, meta)));
 
                setLayout(new BorderLayout());
 
+               // Title
+               JPanel title = null;
+               if (includeTitle) {
+                       title = new JPanel(new BorderLayout());
+                       JTextArea titleLabel = new JTextArea(
+                                       meta.getLuid() + ": " + meta.getTitle());
+                       titleLabel.setEditable(false);
+                       titleLabel.setLineWrap(true);
+                       titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
+                       titleLabel.setOpaque(false);
+                       titleLabel.setFocusable(false);
+                       titleLabel.setBorder(new EmptyBorder(3, 3, 3, 3));
+                       titleLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
+                       title.add(titleLabel);
+                       Color fg = new JLabel("dummy").getForeground();
+                       Color bg = title.getBackground();
+                       title.setForeground(bg);
+                       title.setBackground(fg);
+                       titleLabel.setForeground(bg);
+                       titleLabel.setBackground(trans);
+               }
+
                // Main panel
                JPanel mainPanel = new JPanel(new BorderLayout());
                JPanel mainPanelKeys = new JPanel();
@@ -65,9 +94,7 @@ public class PropertiesPanel extends JPanel {
                mainPanel.add(UIUtils.scroll(mainPanelValues, true, false),
                                BorderLayout.CENTER);
 
-               Map<String, String> desc = BasicReader.getMetaDesc(meta);
-
-               Color trans = new Color(0, 0, 0, 1);
+               Map<String, String> desc = Actions.getMetaDesc(meta);
                for (String key : desc.keySet()) {
                        JTextArea jKey = new JTextArea(key);
                        jKey.setFont(new Font(jKey.getFont().getFontName(), Font.BOLD,
@@ -100,6 +127,8 @@ public class PropertiesPanel extends JPanel {
                                BorderFactory.createEmptyBorder(0, space, space + hscroll, 0));
 
                // Add all
+               if (includeTitle)
+                       add(title, BorderLayout.NORTH);
                add(imgLabel, BorderLayout.WEST);
                add(mainPanel, BorderLayout.CENTER);
 
index 329c42a16f9373518428c42c02bea8fc4d378e0d..fed028020020690d45d6672ba768dfa0ede2e3a3 100644 (file)
@@ -176,8 +176,7 @@ public class ImporterFrame extends JFrame implements ListenerItem {
                Progress pg = new Progress();
                String basename = null;
                try {
-                       BasicSupport support = BasicSupport
-                                       .getSupport(BasicReader.getUrl(url));
+                       BasicSupport support = BasicSupport.getSupport(Actions.getUrl(url));
                        basename = support.getType().getSourceName();
                } catch (Exception e) {
                        basename = "unknown website";