much quicker BooksPanel display, set max title size for BookBlocks
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / book / BookInfo.java
CommitLineData
3cdf3fd8
NR
1package be.nikiroo.fanfix_swing.gui.book;
2
3import java.awt.print.Book;
4import java.io.IOException;
5
6import be.nikiroo.fanfix.Instance;
7import be.nikiroo.fanfix.bundles.StringIdGui;
8import be.nikiroo.fanfix.data.MetaData;
9import be.nikiroo.fanfix.data.Story;
10import be.nikiroo.fanfix.library.BasicLibrary;
11import be.nikiroo.fanfix.library.CacheLibrary;
12import be.nikiroo.utils.Image;
13import 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 */
22public class BookInfo {
23 /**
32ed6089
NR
24 * The type of {@link Book} (i.e., related to a story or to something else
25 * that can encompass stories).
3cdf3fd8
NR
26 *
27 * @author niki
28 */
29 public enum Type {
30 /** A normal story, which can be "read". */
31 STORY,
32 /**
32ed6089
NR
33 * A special, empty story that represents a source/type common to one or
34 * more normal stories.
3cdf3fd8
NR
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 *
32ed6089
NR
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()})
3cdf3fd8
NR
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 /**
32ed6089
NR
79 * Get the main info to display for this book (a title, an author, a
80 * source/type name...).
3cdf3fd8 81 * <p>
32ed6089
NR
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).
3cdf3fd8
NR
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 *
32ed6089
NR
99 * @param seeCount
100 * TRUE for word/image/story count, FALSE for author name
3cdf3fd8
NR
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 *
42797eff 129 * @return TRUE if it is present in the {@link CacheLibrary} cache
3cdf3fd8
NR
130 */
131 public boolean isCached() {
132 return cached;
133 }
134
135 /**
136 * This item library cache state.
137 *
32ed6089
NR
138 * @param cached
139 * TRUE if it is present in the {@link CacheLibrary} cache
3cdf3fd8
NR
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 *
32ed6089
NR
164 * @param lib
165 * the {@link BasicLibrary} to use to fetch the image (can be
166 * NULL)
3cdf3fd8
NR
167 *
168 * @return the base image, or NULL if no library or no image
169 *
32ed6089
NR
170 * @throws IOException
171 * in case of I/O error
3cdf3fd8
NR
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 }
32ed6089 197
bdded4b5
NR
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 }
3cdf3fd8
NR
207
208 /**
209 * Create a new book describing the given {@link Story}.
210 *
32ed6089
NR
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}
3cdf3fd8
NR
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(
32ed6089
NR
234 meta.isImageDocument() ? StringIdGui.BOOK_COUNT_IMAGES
235 : StringIdGui.BOOK_COUNT_WORDS,
3cdf3fd8
NR
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 *
32ed6089
NR
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
3cdf3fd8
NR
256 *
257 * @return the book
258 */
259 static public BookInfo fromSource(BasicLibrary lib, String source) {
32ed6089
NR
260 BookInfo info = new BookInfo(Type.SOURCE,
261 "source_" + (source == null ? "" : source), source);
3cdf3fd8
NR
262
263 int size = 0;
89c5b3e2
NR
264 if (lib != null) {
265 try {
266 size = lib.getList().filter(source, null, null).size();
267 } catch (IOException e) {
268 }
3cdf3fd8
NR
269 }
270
271 info.count = StringUtils.formatNumber(size);
272 if (!info.count.isEmpty()) {
32ed6089
NR
273 info.count = Instance.getInstance().getTransGui().getString(
274 StringIdGui.BOOK_COUNT_STORIES,
3cdf3fd8
NR
275 new Object[] { info.count });
276 }
277
278 return info;
279 }
280
281 /**
282 * Create a new book describing the given author.
283 *
32ed6089
NR
284 * @param lib
285 * the {@link BasicLibrary} to use to retrieve some more
286 * information about the author
287 * @param author
288 * the author name
3cdf3fd8
NR
289 *
290 * @return the book
291 */
292 static public BookInfo fromAuthor(BasicLibrary lib, String author) {
32ed6089
NR
293 BookInfo info = new BookInfo(Type.AUTHOR,
294 "author_" + (author == null ? "" : author), author);
3cdf3fd8
NR
295
296 int size = 0;
89c5b3e2
NR
297 if (lib != null) {
298 try {
299 size = lib.getList().filter(null, author, null).size();
300 } catch (IOException e) {
301 }
3cdf3fd8
NR
302 }
303
304 info.count = StringUtils.formatNumber(size);
305 if (!info.count.isEmpty()) {
32ed6089
NR
306 info.count = Instance.getInstance().getTransGui().getString(
307 StringIdGui.BOOK_COUNT_STORIES,
3cdf3fd8
NR
308 new Object[] { info.count });
309 }
310
311 return info;
312 }
313
314 /**
315 * Create a new book describing the given tag.
316 *
32ed6089
NR
317 * @param lib
318 * the {@link BasicLibrary} to use to retrieve some more
319 * information about the tag
320 * @param tag
321 * the tag name
3cdf3fd8
NR
322 *
323 * @return the book
324 */
325 static public BookInfo fromTag(BasicLibrary lib, String tag) {
32ed6089
NR
326 BookInfo info = new BookInfo(Type.TAG,
327 "tag_" + (tag == null ? "" : tag), tag);
3cdf3fd8
NR
328
329 int size = 0;
330 try {
42797eff 331 size = lib.getList().filter(null, null, tag).size();
3cdf3fd8
NR
332 } catch (IOException e) {
333 }
334
335 info.count = StringUtils.formatNumber(size);
336 if (!info.count.isEmpty()) {
32ed6089
NR
337 info.count = Instance.getInstance().getTransGui().getString(
338 StringIdGui.BOOK_COUNT_STORIES,
3cdf3fd8
NR
339 new Object[] { info.count });
340 }
341
342 return info;
343 }
344}