X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderBookInfo.java;h=c163834f493f875755ac0e3d15af12263433d7a2;hb=ca7d4e2fe5a5ae4df2c6b07e6dedf5640d5b8650;hp=93763f941092c3fbb8a4e05133f05d3d2ec22163;hpb=c349fd480d7ab36ad423e965434a6f1f418d60a5;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java index 93763f9..c163834 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBookInfo.java @@ -1,9 +1,21 @@ package be.nikiroo.fanfix.reader.ui; +import java.io.IOException; + +import be.nikiroo.fanfix.bundles.StringIdGui; import be.nikiroo.fanfix.data.MetaData; +import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.utils.Image; - +import be.nikiroo.utils.StringUtils; + +/** + * Some meta information related to a "book" (which can either be a + * {@link Story}, a fake-story grouping some authors or a fake-story grouping + * some sources/types). + * + * @author niki + */ public class GuiReaderBookInfo { public enum Type { /** A normal story, which can be "read". */ @@ -24,13 +36,43 @@ public class GuiReaderBookInfo { private MetaData meta; - // use the fromXXX methods + /** + * For private use, see the "fromXXX" constructors instead for public use. + * + * @param type + * the type of book + * @param id + * the main id, which must uniquely identify this book and will + * be used as a unique ID later on + * @param value + * the main value to show (see + * {@link GuiReaderBookInfo#getMainInfo()}) + */ private GuiReaderBookInfo(Type type, String id, String value) { this.type = type; this.id = id; this.value = value; } + + /** + * The type of {@link GuiReaderBookInfo}. + * + * @return the type + */ + public Type getType() { + return type; + } + /** + * Get the main info to display for this book (a title, an author, a + * source/type name...). + *

+ * Note that when {@link MetaData} about the book are present, the title + * inside is returned instead of the actual value (that way, we can update + * the {@link MetaData} and see the changes here). + * + * @return the main info, usually the title + */ public String getMainInfo() { if (meta != null) { return meta.getTitle(); @@ -39,6 +81,14 @@ public class GuiReaderBookInfo { return value; } + /** + * Get the secondary info, of the given type. + * + * @param seeCount + * TRUE for word/image/story count, FALSE for author name + * + * @return the secondary info + */ public String getSecondaryInfo(boolean seeCount) { String author = meta == null ? null : meta.getAuthor(); String secondaryInfo = seeCount ? count : author; @@ -61,15 +111,45 @@ public class GuiReaderBookInfo { return id; } - // can return null for non-books + /** + * The {@link MetaData} associated with this book, if this book is a + * {@link Story}. + *

+ * Can be NULL for non-story books (authors or sources/types). + * + * @return the {@link MetaData} or NULL + */ public MetaData getMeta() { return meta; } - public Image getBaseImage(BasicLibrary lib) { + /** + * Get the base image to use to represent this book. + *

+ * The image is NOT resized in any way, this is the original version. + *

+ * It can be NULL if no image can be found for this book. + * + * @param lib + * the {@link BasicLibrary} to use to fetch the image + * + * @return the base image + * + * @throws IOException + * in case of I/O error + */ + public Image getBaseImage(BasicLibrary lib) throws IOException { switch (type) { case STORY: - return lib.getCover(meta.getLuid()); + if (meta.getCover() != null) { + return meta.getCover(); + } + + if (meta.getLuid() != null) { + return lib.getCover(meta.getLuid()); + } + + return null; case SOURCE: return lib.getSourceCover(value); case AUTHOR: @@ -79,54 +159,94 @@ public class GuiReaderBookInfo { return null; } + /** + * Create a new book describing the given {@link Story}. + * + * @param meta + * the {@link MetaData} representing the {@link Story} + * + * @return the book + */ static public GuiReaderBookInfo fromMeta(MetaData meta) { String uid = meta.getUuid(); if (uid == null || uid.trim().isEmpty()) { uid = meta.getLuid(); } + if (uid == null || uid.trim().isEmpty()) { + uid = meta.getUrl(); + } GuiReaderBookInfo info = new GuiReaderBookInfo(Type.STORY, uid, meta.getTitle()); info.meta = meta; - info.count = formatNumber(meta.getWords(), - meta.isImageDocument() ? "images" : "words"); + info.count = StringUtils.formatNumber(meta.getWords()); + if (!info.count.isEmpty()) { + info.count = GuiReader.trans( + meta.isImageDocument() ? StringIdGui.BOOK_COUNT_IMAGES + : StringIdGui.BOOK_COUNT_WORDS, info.count); + } return info; } + /** + * Create a new book describing the given source/type. + * + * @param lib + * the {@link BasicLibrary} to use to retrieve some more + * information about the source + * @param source + * the source name + * + * @return the book + */ static public GuiReaderBookInfo fromSource(BasicLibrary lib, String source) { GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_" + source, source); - info.count = formatNumber(lib.getListBySource(source).size(), "stories"); + int size = 0; + try { + size = lib.getListBySource(source).size(); + } catch (IOException e) { + } + + info.count = StringUtils.formatNumber(size); + if (!info.count.isEmpty()) { + info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES, + info.count); + } return info; } + /** + * Create a new book describing the given author. + * + * @param lib + * the {@link BasicLibrary} to use to retrieve some more + * information about the author + * @param author + * the author name + * + * @return the book + */ static public GuiReaderBookInfo fromAuthor(BasicLibrary lib, String author) { GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_" + author, author); - info.count = formatNumber(lib.getListByAuthor(author).size(), "stories"); - - return info; - } - - static private String formatNumber(long number, String ofWhat) { - String displayNumber; - if (number >= 4000) { - displayNumber = "" + (number / 1000) + "k"; - } else if (number > 0) { - displayNumber = "" + number; - } else { - displayNumber = ""; + int size = 0; + try { + size = lib.getListByAuthor(author).size(); + } catch (IOException e) { } - if (!displayNumber.isEmpty()) { - displayNumber += " " + ofWhat; + info.count = StringUtils.formatNumber(size); + if (!info.count.isEmpty()) { + info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES, + info.count); } - return displayNumber; + return info; } }