code cleanup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBookInfo.java
CommitLineData
79a99506
NR
1package be.nikiroo.fanfix.reader.ui;
2
5bc9573b 3import be.nikiroo.fanfix.bundles.StringIdGui;
79a99506 4import be.nikiroo.fanfix.data.MetaData;
5bc9573b 5import be.nikiroo.fanfix.data.Story;
79a99506
NR
6import be.nikiroo.fanfix.library.BasicLibrary;
7import be.nikiroo.utils.Image;
596ed3d6 8import be.nikiroo.utils.StringUtils;
79a99506 9
5bc9573b
NR
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 */
79a99506
NR
17public 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
5bc9573b
NR
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 */
79a99506
NR
49 private GuiReaderBookInfo(Type type, String id, String value) {
50 this.type = type;
51 this.id = id;
52 this.value = value;
53 }
54
5bc9573b
NR
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 */
79a99506 65 public String getMainInfo() {
c349fd48
NR
66 if (meta != null) {
67 return meta.getTitle();
68 }
69
79a99506
NR
70 return value;
71 }
72
5bc9573b
NR
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 */
79a99506
NR
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
5bc9573b
NR
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 */
79a99506
NR
111 public MetaData getMeta() {
112 return meta;
113 }
114
5bc9573b
NR
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 */
79a99506
NR
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
5bc9573b
NR
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 */
79a99506
NR
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;
b4b7ac5c 158 info.count = StringUtils.formatNumber(meta.getWords());
5bc9573b
NR
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 }
79a99506
NR
164
165 return info;
166 }
167
5bc9573b
NR
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 */
79a99506
NR
179 static public GuiReaderBookInfo fromSource(BasicLibrary lib, String source) {
180 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_"
181 + source, source);
182
b4b7ac5c
NR
183 info.count = StringUtils.formatNumber(lib.getListBySource(source)
184 .size());
5bc9573b
NR
185 if (!info.count.isEmpty()) {
186 info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
187 info.count);
188 }
79a99506
NR
189
190 return info;
191 }
192
5bc9573b
NR
193 /**
194 * Create a new book describing the given author.
195 *
196 * @param lib
197 * the {@link BasicLibrary} to use to retrieve some more
198 * information about the author
199 * @param author
200 * the author name
201 *
202 * @return the book
203 */
79a99506
NR
204 static public GuiReaderBookInfo fromAuthor(BasicLibrary lib, String author) {
205 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_"
206 + author, author);
207
b4b7ac5c
NR
208 info.count = StringUtils.formatNumber(lib.getListByAuthor(author)
209 .size());
5bc9573b
NR
210 if (!info.count.isEmpty()) {
211 info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
212 info.count);
213 }
79a99506
NR
214
215 return info;
216 }
79a99506 217}