From: Niki Roo Date: Sat, 2 May 2020 11:14:33 +0000 (+0200) Subject: popup: add title in undecorated mode and add creation date X-Git-Tag: fanfix-swing-1.1.0~8 X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=ab786f7f0b6d5b1a873d29d7d348f75f9774fc8c;p=fanfix-swing.git popup: add title in undecorated mode and add creation date --- diff --git a/src/be/nikiroo/fanfix_swing/Actions.java b/src/be/nikiroo/fanfix_swing/Actions.java index 54a3f10f..c7be0f54 100644 --- a/src/be/nikiroo/fanfix_swing/Actions.java +++ b/src/be/nikiroo/fanfix_swing/Actions.java @@ -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 getMetaDesc(MetaData meta) { + Map metaDesc = new LinkedHashMap(); + + // 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; + } } diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index 93eb361c..ad889c99 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -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; } diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java index bf4e1285..2495ca09 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanelActions.java @@ -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 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 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); } diff --git a/src/be/nikiroo/fanfix_swing/gui/PropertiesFrame.java b/src/be/nikiroo/fanfix_swing/gui/PropertiesFrame.java index cc16115a..bca3ecfa 100644 --- a/src/be/nikiroo/fanfix_swing/gui/PropertiesFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/PropertiesFrame.java @@ -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); } diff --git a/src/be/nikiroo/fanfix_swing/gui/PropertiesPanel.java b/src/be/nikiroo/fanfix_swing/gui/PropertiesPanel.java index 1a3bdca8..70d2402a 100644 --- a/src/be/nikiroo/fanfix_swing/gui/PropertiesPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/PropertiesPanel.java @@ -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(); + 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 desc = BasicReader.getMetaDesc(meta); - - Color trans = new Color(0, 0, 0, 1); + Map 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); diff --git a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java index 329c42a1..fed02802 100644 --- a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java @@ -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";