72aee900d51893a25b31b8b1bacbc9878a2bd49a
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 * This {@link BookInfo} could have a cover (so we need to somehow represent
194 * @return TRUE if it does
196 public boolean supportsCover() {
197 return type
!= Type
.TAG
;
201 * Create a new book describing the given {@link Story}.
203 * @param lib the {@link BasicLibrary} to use to retrieve some more information
205 * @param meta the {@link MetaData} representing the {@link Story}
209 static public BookInfo
fromMeta(BasicLibrary lib
, MetaData meta
) {
210 String uid
= meta
.getUuid();
211 if (uid
== null || uid
.trim().isEmpty()) {
212 uid
= meta
.getLuid();
214 if (uid
== null || uid
.trim().isEmpty()) {
218 BookInfo info
= new BookInfo(Type
.STORY
, uid
, meta
.getTitle());
221 info
.count
= StringUtils
.formatNumber(meta
.getWords());
222 if (!info
.count
.isEmpty()) {
223 info
.count
= Instance
.getInstance().getTransGui().getString(
224 meta
.isImageDocument() ? StringIdGui
.BOOK_COUNT_IMAGES
: StringIdGui
.BOOK_COUNT_WORDS
,
225 new Object
[] { info
.count
});
228 if (lib
instanceof CacheLibrary
) {
229 info
.setCached(((CacheLibrary
) lib
).isCached(meta
.getLuid()));
231 info
.setCached(true);
238 * Create a new book describing the given source/type.
240 * @param lib the {@link BasicLibrary} to use to retrieve some more
241 * information about the source
242 * @param source the source name
246 static public BookInfo
fromSource(BasicLibrary lib
, String source
) {
247 BookInfo info
= new BookInfo(Type
.SOURCE
, "source_" + (source
== null ?
"" : source
), source
);
251 size
= lib
.getList().filter(source
, null, null).size();
252 } catch (IOException e
) {
255 info
.count
= StringUtils
.formatNumber(size
);
256 if (!info
.count
.isEmpty()) {
257 info
.count
= Instance
.getInstance().getTransGui().getString(StringIdGui
.BOOK_COUNT_STORIES
,
258 new Object
[] { info
.count
});
265 * Create a new book describing the given author.
267 * @param lib the {@link BasicLibrary} to use to retrieve some more
268 * information about the author
269 * @param author the author name
273 static public BookInfo
fromAuthor(BasicLibrary lib
, String author
) {
274 BookInfo info
= new BookInfo(Type
.AUTHOR
, "author_" + (author
== null ?
"" : author
), author
);
278 size
= lib
.getList().filter(null, author
, null).size();
279 } catch (IOException e
) {
282 info
.count
= StringUtils
.formatNumber(size
);
283 if (!info
.count
.isEmpty()) {
284 info
.count
= Instance
.getInstance().getTransGui().getString(StringIdGui
.BOOK_COUNT_STORIES
,
285 new Object
[] { info
.count
});
292 * Create a new book describing the given tag.
294 * @param lib the {@link BasicLibrary} to use to retrieve some more information
296 * @param tag the tag name
300 static public BookInfo
fromTag(BasicLibrary lib
, String tag
) {
301 BookInfo info
= new BookInfo(Type
.TAG
, "tag_" + (tag
== null ?
"" : tag
), tag
);
305 size
= lib
.getList().filter(null, null, tag
).size();
306 } catch (IOException e
) {
309 info
.count
= StringUtils
.formatNumber(size
);
310 if (!info
.count
.isEmpty()) {
311 info
.count
= Instance
.getInstance().getTransGui().getString(StringIdGui
.BOOK_COUNT_STORIES
,
312 new Object
[] { info
.count
});