search: cleanup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBookInfo.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import be.nikiroo.fanfix.bundles.StringIdGui;
4 import be.nikiroo.fanfix.data.MetaData;
5 import be.nikiroo.fanfix.data.Story;
6 import be.nikiroo.fanfix.library.BasicLibrary;
7 import be.nikiroo.utils.Image;
8 import be.nikiroo.utils.StringUtils;
9
10 /**
11 * Some meta information related to a "book" (which can either be a
12 * {@link Story}, a fake-story grouping some authors or a fake-story grouping
13 * some sources/types).
14 *
15 * @author niki
16 */
17 public class GuiReaderBookInfo {
18 public enum Type {
19 /** A normal story, which can be "read". */
20 STORY,
21 /**
22 * A special, empty story that represents a source/type common to one or
23 * more normal stories.
24 */
25 SOURCE,
26 /** A special, empty story that represents an author. */
27 AUTHOR
28 }
29
30 private Type type;
31 private String id;
32 private String value;
33 private String count;
34
35 private MetaData meta;
36
37 /**
38 * For private use, see the "fromXXX" constructors instead for public use.
39 *
40 * @param type
41 * the type of book
42 * @param id
43 * the main id, which must uniquely identify this book and will
44 * be used as a unique ID later on
45 * @param value
46 * the main value to show (see
47 * {@link GuiReaderBookInfo#getMainInfo()})
48 */
49 private GuiReaderBookInfo(Type type, String id, String value) {
50 this.type = type;
51 this.id = id;
52 this.value = value;
53 }
54
55 /**
56 * Get the main info to display for this book (a title, an author, a
57 * source/type name...).
58 * <p>
59 * Note that when {@link MetaData} about the book are present, the title
60 * inside is returned instead of the actual value (that way, we can update
61 * the {@link MetaData} and see the changes here).
62 *
63 * @return the main info, usually the title
64 */
65 public String getMainInfo() {
66 if (meta != null) {
67 return meta.getTitle();
68 }
69
70 return value;
71 }
72
73 /**
74 * Get the secondary info, of the given type.
75 *
76 * @param seeCount
77 * TRUE for word/image/story count, FALSE for author name
78 *
79 * @return the secondary info
80 */
81 public String getSecondaryInfo(boolean seeCount) {
82 String author = meta == null ? null : meta.getAuthor();
83 String secondaryInfo = seeCount ? count : author;
84
85 if (secondaryInfo != null && !secondaryInfo.trim().isEmpty()) {
86 secondaryInfo = "(" + secondaryInfo + ")";
87 } else {
88 secondaryInfo = "";
89 }
90
91 return secondaryInfo;
92 }
93
94 /**
95 * A unique ID for this {@link GuiReaderBookInfo}.
96 *
97 * @return the unique ID
98 */
99 public String getId() {
100 return id;
101 }
102
103 /**
104 * The {@link MetaData} associated with this book, if this book is a
105 * {@link Story}.
106 * <p>
107 * Can be NULL for non-story books (authors or sources/types).
108 *
109 * @return the {@link MetaData} or NULL
110 */
111 public MetaData getMeta() {
112 return meta;
113 }
114
115 /**
116 * Get the base image to use to represent this book.
117 * <p>
118 * The image is <b>NOT</b> resized in any way, this is the original version.
119 * <p>
120 * It can be NULL if no image can be found for this book.
121 *
122 * @param lib
123 * the {@link BasicLibrary} to use to fetch the image
124 *
125 * @return the base image
126 */
127 public Image getBaseImage(BasicLibrary lib) {
128 switch (type) {
129 case STORY:
130 return lib.getCover(meta.getLuid());
131 case SOURCE:
132 return lib.getSourceCover(value);
133 case AUTHOR:
134 return lib.getAuthorCover(value);
135 }
136
137 return null;
138 }
139
140 /**
141 * Create a new book describing the given {@link Story}.
142 *
143 * @param meta
144 * the {@link MetaData} representing the {@link Story}
145 *
146 * @return the book
147 */
148 static public GuiReaderBookInfo fromMeta(MetaData meta) {
149 String uid = meta.getUuid();
150 if (uid == null || uid.trim().isEmpty()) {
151 uid = meta.getLuid();
152 }
153
154 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.STORY, uid,
155 meta.getTitle());
156
157 info.meta = meta;
158 info.count = formatNumber(meta.getWords());
159 if (!info.count.isEmpty()) {
160 info.count = GuiReader.trans(
161 meta.isImageDocument() ? StringIdGui.BOOK_COUNT_IMAGES
162 : StringIdGui.BOOK_COUNT_WORDS, info.count);
163 }
164
165 return info;
166 }
167
168 /**
169 * Create a new book describing the given source/type.
170 *
171 * @param lib
172 * the {@link BasicLibrary} to use to retrieve some more
173 * information about the source
174 * @param source
175 * the source name
176 *
177 * @return the book
178 */
179 static public GuiReaderBookInfo fromSource(BasicLibrary lib, String source) {
180 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_"
181 + source, source);
182
183 info.count = formatNumber(lib.getListBySource(source).size());
184 if (!info.count.isEmpty()) {
185 info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
186 info.count);
187 }
188
189 return info;
190 }
191
192 /**
193 * Create a new book describing the given author.
194 *
195 * @param lib
196 * the {@link BasicLibrary} to use to retrieve some more
197 * information about the author
198 * @param author
199 * the author name
200 *
201 * @return the book
202 */
203 static public GuiReaderBookInfo fromAuthor(BasicLibrary lib, String author) {
204 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_"
205 + author, author);
206
207 info.count = formatNumber(lib.getListByAuthor(author).size());
208 if (!info.count.isEmpty()) {
209 info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
210 info.count);
211 }
212
213 return info;
214 }
215
216 /**
217 * Format a number for display (use the "k" notation if higher or equal to
218 * 4000).
219 *
220 * @param number
221 * the number to parse
222 *
223 * @return the displayable version of the number
224 *
225 * @deprecated use {@link StringUtils} after update instead
226 */
227 @Deprecated
228 static private String formatNumber(long number) {
229 // TODO: replace with StringUtils after update
230 String displayNumber;
231 if (number >= 4000) {
232 displayNumber = "" + (number / 1000) + "k";
233 } else if (number > 0) {
234 displayNumber = "" + number;
235 } else {
236 displayNumber = "";
237 }
238
239 return displayNumber;
240 }
241 }