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 that
25 * 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 more
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.
55 * @param type the type of book
56 * @param id the main id, which must uniquely identify this book and will be
57 * used as a unique ID later on
58 * @param value the main value to show (see {@link BookInfo#getMainInfo()})
60 protected BookInfo(Type type
, String id
, String value
) {
67 * The type of {@link BookInfo}.
71 public Type
getType() {
76 * Get the main info to display for this book (a title, an author, a source/type
79 * Note that when {@link MetaData} about the book are present, the title inside
80 * is returned instead of the actual value (that way, we can update the
81 * {@link MetaData} and see the changes here).
83 * @return the main info, usually the title
85 public String
getMainInfo() {
87 return meta
.getTitle();
94 * Get the secondary info, of the given type.
96 * @param seeCount TRUE for word/image/story count, FALSE for author name
98 * @return the secondary info, never NULL
100 public String
getSecondaryInfo(boolean seeCount
) {
101 String author
= meta
== null ?
null : meta
.getAuthor();
102 String secondaryInfo
= seeCount ? count
: author
;
104 if (secondaryInfo
!= null && !secondaryInfo
.trim().isEmpty()) {
105 secondaryInfo
= "(" + secondaryInfo
+ ")";
110 return secondaryInfo
;
114 * A unique ID for this {@link BookInfo}.
116 * @return the unique ID
118 public String
getId() {
123 * This item library cache state.
125 * @return TRUE if it is present in the {@link CacheLibrary} cache
127 public boolean isCached() {
132 * This item library cache state.
134 * @param cached TRUE if it is present in the {@link CacheLibrary} cache
136 public void setCached(boolean cached
) {
137 this.cached
= cached
;
141 * The {@link MetaData} associated with this book, if this book is a
144 * Can be NULL for non-story books (authors or sources/types).
146 * @return the {@link MetaData} or NULL
148 public MetaData
getMeta() {
153 * Get the base image to use to represent this book.
155 * The image is <b>NOT</b> resized in any way, this is the original version.
157 * It can be NULL if no image can be found for this book.
159 * @param lib the {@link BasicLibrary} to use to fetch the image (can be NULL)
161 * @return the base image, or NULL if no library or no image
163 * @throws IOException in case of I/O error
165 public Image
getBaseImage(BasicLibrary lib
) throws IOException
{
169 if (meta
.getCover() != null) {
170 return meta
.getCover();
173 if (meta
.getLuid() != null) {
174 return lib
.getCover(meta
.getLuid());
179 return lib
.getSourceCover(value
);
181 return lib
.getAuthorCover(value
);
191 * Create a new book describing the given {@link Story}.
193 * @param lib the {@link BasicLibrary} to use to retrieve some more information
195 * @param meta the {@link MetaData} representing the {@link Story}
199 static public BookInfo
fromMeta(BasicLibrary lib
, MetaData meta
) {
200 String uid
= meta
.getUuid();
201 if (uid
== null || uid
.trim().isEmpty()) {
202 uid
= meta
.getLuid();
204 if (uid
== null || uid
.trim().isEmpty()) {
208 BookInfo info
= new BookInfo(Type
.STORY
, uid
, meta
.getTitle());
211 info
.count
= StringUtils
.formatNumber(meta
.getWords());
212 if (!info
.count
.isEmpty()) {
213 info
.count
= Instance
.getInstance().getTransGui().getString(
214 meta
.isImageDocument() ? StringIdGui
.BOOK_COUNT_IMAGES
: StringIdGui
.BOOK_COUNT_WORDS
,
215 new Object
[] { info
.count
});
218 if (lib
instanceof CacheLibrary
) {
219 info
.setCached(((CacheLibrary
) lib
).isCached(meta
.getLuid()));
221 info
.setCached(true);
228 * Create a new book describing the given source/type.
230 * @param lib the {@link BasicLibrary} to use to retrieve some more
231 * information about the source
232 * @param source the source name
236 static public BookInfo
fromSource(BasicLibrary lib
, String source
) {
237 BookInfo info
= new BookInfo(Type
.SOURCE
, "source_" + (source
== null ?
"" : source
), source
);
241 size
= lib
.getList().filter(source
, null, null).size();
242 } catch (IOException e
) {
245 info
.count
= StringUtils
.formatNumber(size
);
246 if (!info
.count
.isEmpty()) {
247 info
.count
= Instance
.getInstance().getTransGui().getString(StringIdGui
.BOOK_COUNT_STORIES
,
248 new Object
[] { info
.count
});
255 * Create a new book describing the given author.
257 * @param lib the {@link BasicLibrary} to use to retrieve some more
258 * information about the author
259 * @param author the author name
263 static public BookInfo
fromAuthor(BasicLibrary lib
, String author
) {
264 BookInfo info
= new BookInfo(Type
.AUTHOR
, "author_" + (author
== null ?
"" : author
), author
);
268 size
= lib
.getList().filter(null, author
, null).size();
269 } catch (IOException e
) {
272 info
.count
= StringUtils
.formatNumber(size
);
273 if (!info
.count
.isEmpty()) {
274 info
.count
= Instance
.getInstance().getTransGui().getString(StringIdGui
.BOOK_COUNT_STORIES
,
275 new Object
[] { info
.count
});
282 * Create a new book describing the given tag.
284 * @param lib the {@link BasicLibrary} to use to retrieve some more information
286 * @param tag the tag name
290 static public BookInfo
fromTag(BasicLibrary lib
, String tag
) {
291 BookInfo info
= new BookInfo(Type
.TAG
, "tag_" + (tag
== null ?
"" : tag
), tag
);
295 size
= lib
.getList().filter(null, null, tag
).size();
296 } catch (IOException e
) {
299 info
.count
= StringUtils
.formatNumber(size
);
300 if (!info
.count
.isEmpty()) {
301 info
.count
= Instance
.getInstance().getTransGui().getString(StringIdGui
.BOOK_COUNT_STORIES
,
302 new Object
[] { info
.count
});