713ee203564f8ab90af62335e178faebb43fde1b
1 package be
.nikiroo
.fanfix_swing
.gui
.book
;
3 import java
.awt
.print
.Book
;
4 import java
.io
.IOException
;
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
;
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).
22 public class BookInfo
{
24 * The type of {@link Book} (i.e., related to a story or to something else
25 * that can encompass stories).
30 /** A normal story, which can be "read". */
33 * A special, empty story that represents a source/type common to one or
34 * more normal stories.
37 /** A special, empty story that represents an author. */
39 /** A special, empty story that represents a tag. **/
48 private boolean cached
;
50 private MetaData meta
;
53 * For private use; see the "fromXXX" constructors instead for public use.
58 * the main id, which must uniquely identify this book and will
59 * be used as a unique ID later on
61 * the main value to show (see {@link BookInfo#getMainInfo()})
63 protected BookInfo(Type type
, String id
, String value
) {
70 * The type of {@link BookInfo}.
74 public Type
getType() {
79 * Get the main info to display for this book (a title, an author, a
80 * source/type name...).
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).
86 * @return the main info, usually the title
88 public String
getMainInfo() {
90 return meta
.getTitle();
97 * Get the secondary info, of the given type.
100 * TRUE for word/image/story count, FALSE for author name
102 * @return the secondary info, never NULL
104 public String
getSecondaryInfo(boolean seeCount
) {
105 String author
= meta
== null ?
null : meta
.getAuthor();
106 String secondaryInfo
= seeCount ? count
: author
;
108 if (secondaryInfo
!= null && !secondaryInfo
.trim().isEmpty()) {
109 secondaryInfo
= "(" + secondaryInfo
+ ")";
114 return secondaryInfo
;
118 * A unique ID for this {@link BookInfo}.
120 * @return the unique ID
122 public String
getId() {
127 * This item library cache state.
129 * @return TRUE if it is present in the {@link CacheLibrary} cache
131 public boolean isCached() {
136 * This item library cache state.
139 * TRUE if it is present in the {@link CacheLibrary} cache
141 public void setCached(boolean cached
) {
142 this.cached
= cached
;
146 * The {@link MetaData} associated with this book, if this book is a
149 * Can be NULL for non-story books (authors or sources/types).
151 * @return the {@link MetaData} or NULL
153 public MetaData
getMeta() {
158 * Get the base image to use to represent this book.
160 * The image is <b>NOT</b> resized in any way, this is the original version.
162 * It can be NULL if no image can be found for this book.
165 * the {@link BasicLibrary} to use to fetch the image (can be
168 * @return the base image, or NULL if no library or no image
170 * @throws IOException
171 * in case of I/O error
173 public Image
getBaseImage(BasicLibrary lib
) throws IOException
{
177 if (meta
.getCover() != null) {
178 return meta
.getCover();
181 if (meta
.getLuid() != null) {
182 return lib
.getCover(meta
.getLuid());
187 return lib
.getSourceCover(value
);
189 return lib
.getAuthorCover(value
);
199 * This {@link BookInfo} could have a cover (so we need to somehow represent
202 * @return TRUE if it does
204 public boolean supportsCover() {
205 return type
!= Type
.TAG
;
209 * Create a new book describing the given {@link Story}.
212 * the {@link BasicLibrary} to use to retrieve some more
213 * information about the source
215 * the {@link MetaData} representing the {@link Story}
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();
224 if (uid
== null || uid
.trim().isEmpty()) {
228 BookInfo info
= new BookInfo(Type
.STORY
, uid
, meta
.getTitle());
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
});
239 if (lib
instanceof CacheLibrary
) {
240 info
.setCached(((CacheLibrary
) lib
).isCached(meta
.getLuid()));
242 info
.setCached(true);
249 * Create a new book describing the given source/type.
252 * the {@link BasicLibrary} to use to retrieve some more
253 * information about the source
259 static public BookInfo
fromSource(BasicLibrary lib
, String source
) {
260 BookInfo info
= new BookInfo(Type
.SOURCE
,
261 "source_" + (source
== null ?
"" : source
), source
);
266 size
= lib
.getList().filter(source
, null, null).size();
267 } catch (IOException e
) {
271 info
.count
= StringUtils
.formatNumber(size
);
272 if (!info
.count
.isEmpty()) {
273 info
.count
= Instance
.getInstance().getTransGui().getString(
274 StringIdGui
.BOOK_COUNT_STORIES
,
275 new Object
[] { info
.count
});
282 * Create a new book describing the given author.
285 * the {@link BasicLibrary} to use to retrieve some more
286 * information about the author
292 static public BookInfo
fromAuthor(BasicLibrary lib
, String author
) {
293 BookInfo info
= new BookInfo(Type
.AUTHOR
,
294 "author_" + (author
== null ?
"" : author
), author
);
299 size
= lib
.getList().filter(null, author
, null).size();
300 } catch (IOException e
) {
304 info
.count
= StringUtils
.formatNumber(size
);
305 if (!info
.count
.isEmpty()) {
306 info
.count
= Instance
.getInstance().getTransGui().getString(
307 StringIdGui
.BOOK_COUNT_STORIES
,
308 new Object
[] { info
.count
});
315 * Create a new book describing the given tag.
318 * the {@link BasicLibrary} to use to retrieve some more
319 * information about the tag
325 static public BookInfo
fromTag(BasicLibrary lib
, String tag
) {
326 BookInfo info
= new BookInfo(Type
.TAG
,
327 "tag_" + (tag
== null ?
"" : tag
), tag
);
331 size
= lib
.getList().filter(null, null, tag
).size();
332 } catch (IOException e
) {
335 info
.count
= StringUtils
.formatNumber(size
);
336 if (!info
.count
.isEmpty()) {
337 info
.count
= Instance
.getInstance().getTransGui().getString(
338 StringIdGui
.BOOK_COUNT_STORIES
,
339 new Object
[] { info
.count
});