| 1 | package be.nikiroo.fanfix_swing.gui.book; |
| 2 | |
| 3 | import java.awt.print.Book; |
| 4 | import java.io.IOException; |
| 5 | |
| 6 | import be.nikiroo.fanfix.Instance; |
| 7 | import be.nikiroo.fanfix.bundles.StringIdGui; |
| 8 | import be.nikiroo.fanfix.data.MetaData; |
| 9 | import be.nikiroo.fanfix.data.Story; |
| 10 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 11 | import be.nikiroo.fanfix.library.CacheLibrary; |
| 12 | import be.nikiroo.utils.Image; |
| 13 | import be.nikiroo.utils.StringUtils; |
| 14 | |
| 15 | /** |
| 16 | * Some meta information related to a "book" (which can either be a |
| 17 | * {@link Story}, a fake-story grouping some authors or a fake-story grouping |
| 18 | * some sources/types). |
| 19 | * |
| 20 | * @author niki |
| 21 | */ |
| 22 | public class BookInfo { |
| 23 | /** |
| 24 | * The type of {@link Book} (i.e., related to a story or to something else |
| 25 | * that can encompass stories). |
| 26 | * |
| 27 | * @author niki |
| 28 | */ |
| 29 | public enum Type { |
| 30 | /** A normal story, which can be "read". */ |
| 31 | STORY, |
| 32 | /** |
| 33 | * A special, empty story that represents a source/type common to one or |
| 34 | * more normal stories. |
| 35 | */ |
| 36 | SOURCE, |
| 37 | /** A special, empty story that represents an author. */ |
| 38 | AUTHOR, |
| 39 | /** A special, empty story that represents a tag. **/ |
| 40 | TAG |
| 41 | } |
| 42 | |
| 43 | private Type type; |
| 44 | private String id; |
| 45 | private String value; |
| 46 | private String count; |
| 47 | |
| 48 | private boolean cached; |
| 49 | |
| 50 | private MetaData meta; |
| 51 | |
| 52 | /** |
| 53 | * For private use; see the "fromXXX" constructors instead for public use. |
| 54 | * |
| 55 | * @param type |
| 56 | * the type of book |
| 57 | * @param id |
| 58 | * the main id, which must uniquely identify this book and will |
| 59 | * be used as a unique ID later on |
| 60 | * @param value |
| 61 | * the main value to show (see {@link BookInfo#getMainInfo()}) |
| 62 | */ |
| 63 | protected BookInfo(Type type, String id, String value) { |
| 64 | this.type = type; |
| 65 | this.id = id; |
| 66 | this.value = value; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * The type of {@link BookInfo}. |
| 71 | * |
| 72 | * @return the type |
| 73 | */ |
| 74 | public Type getType() { |
| 75 | return type; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the main info to display for this book (a title, an author, a |
| 80 | * source/type name...). |
| 81 | * <p> |
| 82 | * Note that when {@link MetaData} about the book are present, the title |
| 83 | * inside is returned instead of the actual value (that way, we can update |
| 84 | * the {@link MetaData} and see the changes here). |
| 85 | * |
| 86 | * @return the main info, usually the title |
| 87 | */ |
| 88 | public String getMainInfo() { |
| 89 | if (meta != null) { |
| 90 | return meta.getTitle(); |
| 91 | } |
| 92 | |
| 93 | return value; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the secondary info, of the given type. |
| 98 | * |
| 99 | * @param seeCount |
| 100 | * TRUE for word/image/story count, FALSE for author name |
| 101 | * |
| 102 | * @return the secondary info, never NULL |
| 103 | */ |
| 104 | public String getSecondaryInfo(boolean seeCount) { |
| 105 | String author = meta == null ? null : meta.getAuthor(); |
| 106 | String secondaryInfo = seeCount ? count : author; |
| 107 | |
| 108 | if (secondaryInfo != null && !secondaryInfo.trim().isEmpty()) { |
| 109 | secondaryInfo = "(" + secondaryInfo + ")"; |
| 110 | } else { |
| 111 | secondaryInfo = ""; |
| 112 | } |
| 113 | |
| 114 | return secondaryInfo; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * A unique ID for this {@link BookInfo}. |
| 119 | * |
| 120 | * @return the unique ID |
| 121 | */ |
| 122 | public String getId() { |
| 123 | return id; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * This item library cache state. |
| 128 | * |
| 129 | * @return TRUE if it is present in the {@link CacheLibrary} cache |
| 130 | */ |
| 131 | public boolean isCached() { |
| 132 | return cached; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * This item library cache state. |
| 137 | * |
| 138 | * @param cached |
| 139 | * TRUE if it is present in the {@link CacheLibrary} cache |
| 140 | */ |
| 141 | public void setCached(boolean cached) { |
| 142 | this.cached = cached; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * The {@link MetaData} associated with this book, if this book is a |
| 147 | * {@link Story}. |
| 148 | * <p> |
| 149 | * Can be NULL for non-story books (authors or sources/types). |
| 150 | * |
| 151 | * @return the {@link MetaData} or NULL |
| 152 | */ |
| 153 | public MetaData getMeta() { |
| 154 | return meta; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get the base image to use to represent this book. |
| 159 | * <p> |
| 160 | * The image is <b>NOT</b> resized in any way, this is the original version. |
| 161 | * <p> |
| 162 | * It can be NULL if no image can be found for this book. |
| 163 | * |
| 164 | * @param lib |
| 165 | * the {@link BasicLibrary} to use to fetch the image (can be |
| 166 | * NULL) |
| 167 | * |
| 168 | * @return the base image, or NULL if no library or no image |
| 169 | * |
| 170 | * @throws IOException |
| 171 | * in case of I/O error |
| 172 | */ |
| 173 | public Image getBaseImage(BasicLibrary lib) throws IOException { |
| 174 | if (lib != null) { |
| 175 | switch (type) { |
| 176 | case STORY: |
| 177 | if (meta.getCover() != null) { |
| 178 | return meta.getCover(); |
| 179 | } |
| 180 | |
| 181 | if (meta.getLuid() != null) { |
| 182 | return lib.getCover(meta.getLuid()); |
| 183 | } |
| 184 | |
| 185 | return null; |
| 186 | case SOURCE: |
| 187 | return lib.getSourceCover(value); |
| 188 | case AUTHOR: |
| 189 | return lib.getAuthorCover(value); |
| 190 | case TAG: |
| 191 | return null; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * This {@link BookInfo} could have a cover (so we need to somehow represent |
| 200 | * that to the user). |
| 201 | * |
| 202 | * @return TRUE if it does |
| 203 | */ |
| 204 | public boolean supportsCover() { |
| 205 | return type != Type.TAG; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Create a new book describing the given {@link Story}. |
| 210 | * |
| 211 | * @param lib |
| 212 | * the {@link BasicLibrary} to use to retrieve some more |
| 213 | * information about the source |
| 214 | * @param meta |
| 215 | * the {@link MetaData} representing the {@link Story} |
| 216 | * |
| 217 | * @return the book |
| 218 | */ |
| 219 | static public BookInfo fromMeta(BasicLibrary lib, MetaData meta) { |
| 220 | String uid = meta.getUuid(); |
| 221 | if (uid == null || uid.trim().isEmpty()) { |
| 222 | uid = meta.getLuid(); |
| 223 | } |
| 224 | if (uid == null || uid.trim().isEmpty()) { |
| 225 | uid = meta.getUrl(); |
| 226 | } |
| 227 | |
| 228 | BookInfo info = new BookInfo(Type.STORY, uid, meta.getTitle()); |
| 229 | |
| 230 | info.meta = meta; |
| 231 | info.count = StringUtils.formatNumber(meta.getWords()); |
| 232 | if (!info.count.isEmpty()) { |
| 233 | info.count = Instance.getInstance().getTransGui().getString( |
| 234 | meta.isImageDocument() ? StringIdGui.BOOK_COUNT_IMAGES |
| 235 | : StringIdGui.BOOK_COUNT_WORDS, |
| 236 | new Object[] { info.count }); |
| 237 | } |
| 238 | |
| 239 | if (lib instanceof CacheLibrary) { |
| 240 | info.setCached(((CacheLibrary) lib).isCached(meta.getLuid())); |
| 241 | } else { |
| 242 | info.setCached(true); |
| 243 | } |
| 244 | |
| 245 | return info; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Create a new book describing the given source/type. |
| 250 | * |
| 251 | * @param lib |
| 252 | * the {@link BasicLibrary} to use to retrieve some more |
| 253 | * information about the source |
| 254 | * @param source |
| 255 | * the source name |
| 256 | * |
| 257 | * @return the book |
| 258 | */ |
| 259 | static public BookInfo fromSource(BasicLibrary lib, String source) { |
| 260 | BookInfo info = new BookInfo(Type.SOURCE, |
| 261 | "source_" + (source == null ? "" : source), source); |
| 262 | |
| 263 | int size = 0; |
| 264 | try { |
| 265 | size = lib.getList().filter(source, null, null).size(); |
| 266 | } catch (IOException e) { |
| 267 | } |
| 268 | |
| 269 | info.count = StringUtils.formatNumber(size); |
| 270 | if (!info.count.isEmpty()) { |
| 271 | info.count = Instance.getInstance().getTransGui().getString( |
| 272 | StringIdGui.BOOK_COUNT_STORIES, |
| 273 | new Object[] { info.count }); |
| 274 | } |
| 275 | |
| 276 | return info; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Create a new book describing the given author. |
| 281 | * |
| 282 | * @param lib |
| 283 | * the {@link BasicLibrary} to use to retrieve some more |
| 284 | * information about the author |
| 285 | * @param author |
| 286 | * the author name |
| 287 | * |
| 288 | * @return the book |
| 289 | */ |
| 290 | static public BookInfo fromAuthor(BasicLibrary lib, String author) { |
| 291 | BookInfo info = new BookInfo(Type.AUTHOR, |
| 292 | "author_" + (author == null ? "" : author), author); |
| 293 | |
| 294 | int size = 0; |
| 295 | try { |
| 296 | size = lib.getList().filter(null, author, null).size(); |
| 297 | } catch (IOException e) { |
| 298 | } |
| 299 | |
| 300 | info.count = StringUtils.formatNumber(size); |
| 301 | if (!info.count.isEmpty()) { |
| 302 | info.count = Instance.getInstance().getTransGui().getString( |
| 303 | StringIdGui.BOOK_COUNT_STORIES, |
| 304 | new Object[] { info.count }); |
| 305 | } |
| 306 | |
| 307 | return info; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Create a new book describing the given tag. |
| 312 | * |
| 313 | * @param lib |
| 314 | * the {@link BasicLibrary} to use to retrieve some more |
| 315 | * information about the tag |
| 316 | * @param tag |
| 317 | * the tag name |
| 318 | * |
| 319 | * @return the book |
| 320 | */ |
| 321 | static public BookInfo fromTag(BasicLibrary lib, String tag) { |
| 322 | BookInfo info = new BookInfo(Type.TAG, |
| 323 | "tag_" + (tag == null ? "" : tag), tag); |
| 324 | |
| 325 | int size = 0; |
| 326 | try { |
| 327 | size = lib.getList().filter(null, null, tag).size(); |
| 328 | } catch (IOException e) { |
| 329 | } |
| 330 | |
| 331 | info.count = StringUtils.formatNumber(size); |
| 332 | if (!info.count.isEmpty()) { |
| 333 | info.count = Instance.getInstance().getTransGui().getString( |
| 334 | StringIdGui.BOOK_COUNT_STORIES, |
| 335 | new Object[] { info.count }); |
| 336 | } |
| 337 | |
| 338 | return info; |
| 339 | } |
| 340 | } |