Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
1 | package be.nikiroo.fanfix_swing.gui.book; |
2 | ||
3 | import java.awt.print.Book; | |
4 | import java.io.IOException; | |
5 | ||
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; | |
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 | */ | |
22 | public 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 | * | |
125 | * @return TRUE if it is present in the {@link GuiReader} cache | |
126 | */ | |
127 | public boolean isCached() { | |
128 | return cached; | |
129 | } | |
130 | ||
131 | /** | |
132 | * This item library cache state. | |
133 | * | |
134 | * @param cached TRUE if it is present in the {@link GuiReader} cache | |
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 | } | |
189 | ||
190 | /** | |
191 | * Create a new book describing the given {@link Story}. | |
192 | * | |
193 | * @param lib the {@link BasicLibrary} to use to retrieve some more information | |
194 | * about the source | |
195 | * @param meta the {@link MetaData} representing the {@link Story} | |
196 | * | |
197 | * @return the book | |
198 | */ | |
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(); | |
203 | } | |
204 | if (uid == null || uid.trim().isEmpty()) { | |
205 | uid = meta.getUrl(); | |
206 | } | |
207 | ||
208 | BookInfo info = new BookInfo(Type.STORY, uid, meta.getTitle()); | |
209 | ||
210 | info.meta = meta; | |
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 }); | |
216 | } | |
217 | ||
218 | if (lib instanceof CacheLibrary) { | |
219 | info.setCached(((CacheLibrary) lib).isCached(meta.getLuid())); | |
220 | } else { | |
221 | info.setCached(true); | |
222 | } | |
223 | ||
224 | return info; | |
225 | } | |
226 | ||
227 | /** | |
228 | * Create a new book describing the given source/type. | |
229 | * | |
230 | * @param lib the {@link BasicLibrary} to use to retrieve some more | |
231 | * information about the source | |
232 | * @param source the source name | |
233 | * | |
234 | * @return the book | |
235 | */ | |
236 | static public BookInfo fromSource(BasicLibrary lib, String source) { | |
237 | BookInfo info = new BookInfo(Type.SOURCE, "source_" + source, source); | |
238 | ||
239 | int size = 0; | |
240 | try { | |
241 | size = lib.getListBySource(source).size(); | |
242 | } catch (IOException e) { | |
243 | } | |
244 | ||
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 }); | |
249 | } | |
250 | ||
251 | return info; | |
252 | } | |
253 | ||
254 | /** | |
255 | * Create a new book describing the given author. | |
256 | * | |
257 | * @param lib the {@link BasicLibrary} to use to retrieve some more | |
258 | * information about the author | |
259 | * @param author the author name | |
260 | * | |
261 | * @return the book | |
262 | */ | |
263 | static public BookInfo fromAuthor(BasicLibrary lib, String author) { | |
264 | BookInfo info = new BookInfo(Type.AUTHOR, "author_" + author, author); | |
265 | ||
266 | int size = 0; | |
267 | try { | |
268 | size = lib.getListByAuthor(author).size(); | |
269 | } catch (IOException e) { | |
270 | } | |
271 | ||
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 }); | |
276 | } | |
277 | ||
278 | return info; | |
279 | } | |
280 | ||
281 | /** | |
282 | * Create a new book describing the given tag. | |
283 | * | |
284 | * @param lib the {@link BasicLibrary} to use to retrieve some more information | |
285 | * about the tag | |
286 | * @param tag the tag name | |
287 | * | |
288 | * @return the book | |
289 | */ | |
290 | static public BookInfo fromTag(BasicLibrary lib, String tag) { | |
291 | BookInfo info = new BookInfo(Type.TAG, "tag_" + tag, tag); | |
292 | ||
293 | int size = 0; | |
294 | try { | |
295 | for (MetaData meta : lib.getList()) { | |
296 | if (meta.getTags().contains(tag)) { | |
297 | size++; | |
298 | } | |
299 | } | |
300 | } catch (IOException e) { | |
301 | } | |
302 | ||
303 | info.count = StringUtils.formatNumber(size); | |
304 | if (!info.count.isEmpty()) { | |
305 | info.count = Instance.getInstance().getTransGui().getString(StringIdGui.BOOK_COUNT_STORIES, | |
306 | new Object[] { info.count }); | |
307 | } | |
308 | ||
309 | return info; | |
310 | } | |
311 | } |