working on ui refresh for books
[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 /**
24 * The type of {@link Book} (i.e., related to a story or to something else that
25 * 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 more
34 * 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 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()})
59 */
60 protected BookInfo(Type type, String id, String value) {
61 this.type = type;
62 this.id = id;
63 this.value = value;
64 }
65
66 /**
67 * The type of {@link BookInfo}.
68 *
69 * @return the type
70 */
71 public Type getType() {
72 return type;
73 }
74
75 /**
76 * Get the main info to display for this book (a title, an author, a source/type
77 * name...).
78 * <p>
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).
82 *
83 * @return the main info, usually the title
84 */
85 public String getMainInfo() {
86 if (meta != null) {
87 return meta.getTitle();
88 }
89
90 return value;
91 }
92
93 /**
94 * Get the secondary info, of the given type.
95 *
96 * @param seeCount TRUE for word/image/story count, FALSE for author name
97 *
98 * @return the secondary info, never NULL
99 */
100 public String getSecondaryInfo(boolean seeCount) {
101 String author = meta == null ? null : meta.getAuthor();
102 String secondaryInfo = seeCount ? count : author;
103
104 if (secondaryInfo != null && !secondaryInfo.trim().isEmpty()) {
105 secondaryInfo = "(" + secondaryInfo + ")";
106 } else {
107 secondaryInfo = "";
108 }
109
110 return secondaryInfo;
111 }
112
113 /**
114 * A unique ID for this {@link BookInfo}.
115 *
116 * @return the unique ID
117 */
118 public String getId() {
119 return id;
120 }
121
122 /**
123 * This item library cache state.
124 *
42797eff 125 * @return TRUE if it is present in the {@link CacheLibrary} cache
3cdf3fd8
NR
126 */
127 public boolean isCached() {
128 return cached;
129 }
130
131 /**
132 * This item library cache state.
133 *
42797eff 134 * @param cached TRUE if it is present in the {@link CacheLibrary} cache
3cdf3fd8
NR
135 */
136 public void setCached(boolean cached) {
137 this.cached = cached;
138 }
139
140 /**
141 * The {@link MetaData} associated with this book, if this book is a
142 * {@link Story}.
143 * <p>
144 * Can be NULL for non-story books (authors or sources/types).
145 *
146 * @return the {@link MetaData} or NULL
147 */
148 public MetaData getMeta() {
149 return meta;
150 }
151
152 /**
153 * Get the base image to use to represent this book.
154 * <p>
155 * The image is <b>NOT</b> resized in any way, this is the original version.
156 * <p>
157 * It can be NULL if no image can be found for this book.
158 *
159 * @param lib the {@link BasicLibrary} to use to fetch the image (can be NULL)
160 *
161 * @return the base image, or NULL if no library or no image
162 *
163 * @throws IOException in case of I/O error
164 */
165 public Image getBaseImage(BasicLibrary lib) throws IOException {
166 if (lib != null) {
167 switch (type) {
168 case STORY:
169 if (meta.getCover() != null) {
170 return meta.getCover();
171 }
172
173 if (meta.getLuid() != null) {
174 return lib.getCover(meta.getLuid());
175 }
176
177 return null;
178 case SOURCE:
179 return lib.getSourceCover(value);
180 case AUTHOR:
181 return lib.getAuthorCover(value);
182 case TAG:
183 return null;
184 }
185 }
186
187 return null;
188 }
bdded4b5
NR
189
190 /**
191 * This {@link BookInfo} could have a cover (so we need to somehow represent
192 * that to the user).
193 *
194 * @return TRUE if it does
195 */
196 public boolean supportsCover() {
197 return type != Type.TAG;
198 }
3cdf3fd8
NR
199
200 /**
201 * Create a new book describing the given {@link Story}.
202 *
203 * @param lib the {@link BasicLibrary} to use to retrieve some more information
204 * about the source
205 * @param meta the {@link MetaData} representing the {@link Story}
206 *
207 * @return the book
208 */
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();
213 }
214 if (uid == null || uid.trim().isEmpty()) {
215 uid = meta.getUrl();
216 }
217
218 BookInfo info = new BookInfo(Type.STORY, uid, meta.getTitle());
219
220 info.meta = meta;
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 });
226 }
227
228 if (lib instanceof CacheLibrary) {
229 info.setCached(((CacheLibrary) lib).isCached(meta.getLuid()));
230 } else {
231 info.setCached(true);
232 }
233
234 return info;
235 }
236
237 /**
238 * Create a new book describing the given source/type.
239 *
240 * @param lib the {@link BasicLibrary} to use to retrieve some more
241 * information about the source
242 * @param source the source name
243 *
244 * @return the book
245 */
246 static public BookInfo fromSource(BasicLibrary lib, String source) {
d6c8579c 247 BookInfo info = new BookInfo(Type.SOURCE, "source_" + (source == null ? "" : source), source);
3cdf3fd8
NR
248
249 int size = 0;
250 try {
42797eff 251 size = lib.getList().filter(source, null, null).size();
3cdf3fd8
NR
252 } catch (IOException e) {
253 }
254
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 });
259 }
260
261 return info;
262 }
263
264 /**
265 * Create a new book describing the given author.
266 *
267 * @param lib the {@link BasicLibrary} to use to retrieve some more
268 * information about the author
269 * @param author the author name
270 *
271 * @return the book
272 */
273 static public BookInfo fromAuthor(BasicLibrary lib, String author) {
d6c8579c 274 BookInfo info = new BookInfo(Type.AUTHOR, "author_" + (author == null ? "" : author), author);
3cdf3fd8
NR
275
276 int size = 0;
277 try {
42797eff 278 size = lib.getList().filter(null, author, null).size();
3cdf3fd8
NR
279 } catch (IOException e) {
280 }
281
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 });
286 }
287
288 return info;
289 }
290
291 /**
292 * Create a new book describing the given tag.
293 *
294 * @param lib the {@link BasicLibrary} to use to retrieve some more information
295 * about the tag
296 * @param tag the tag name
297 *
298 * @return the book
299 */
300 static public BookInfo fromTag(BasicLibrary lib, String tag) {
d6c8579c 301 BookInfo info = new BookInfo(Type.TAG, "tag_" + (tag == null ? "" : tag), tag);
3cdf3fd8
NR
302
303 int size = 0;
304 try {
42797eff 305 size = lib.getList().filter(null, null, tag).size();
3cdf3fd8
NR
306 } catch (IOException e) {
307 }
308
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 });
313 }
314
315 return info;
316 }
317}