update nikiroo-utils
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / Paragraph.java
1 package be.nikiroo.fanfix.data;
2
3 import java.io.Serializable;
4
5 import be.nikiroo.utils.Image;
6
7 /**
8 * A paragraph in a chapter of the story.
9 *
10 * @author niki
11 */
12 public class Paragraph implements Cloneable, Serializable {
13 private static final long serialVersionUID = 1L;
14
15 /**
16 * A paragraph type, that will dictate how the paragraph will be handled.
17 *
18 * @author niki
19 */
20 public enum ParagraphType {
21 /** Normal paragraph (text) */
22 NORMAL,
23 /** Blank line */
24 BLANK,
25 /** A Break paragraph, i.e.: HR (Horizontal Line) or '* * *' or whatever */
26 BREAK,
27 /** Quotation (dialogue) */
28 QUOTE,
29 /** An image (no text) */
30 IMAGE, ;
31
32 /**
33 * This paragraph type is of a text kind (quote or not).
34 *
35 * @param allowEmpty
36 * allow empty text as text, too (blanks, breaks...)
37 * @return TRUE if it is
38 */
39 public boolean isText(boolean allowEmpty) {
40 return (this == NORMAL || this == QUOTE)
41 || (allowEmpty && (this == BLANK || this == BREAK));
42 }
43 }
44
45 private ParagraphType type;
46 private String content;
47 private Image contentImage;
48 private long words;
49
50 /**
51 * Empty constructor, not to use.
52 */
53 @SuppressWarnings("unused")
54 private Paragraph() {
55 // for serialisation purposes
56 }
57
58 /**
59 * Create a new {@link Paragraph} with the given image.
60 *
61 * @param contentImage
62 * the image
63 */
64 public Paragraph(Image contentImage) {
65 this(ParagraphType.IMAGE, null, 1);
66 this.contentImage = contentImage;
67 }
68
69 /**
70 * Create a new {@link Paragraph} with the given values.
71 *
72 * @param type
73 * the {@link ParagraphType}
74 * @param content
75 * the content of this paragraph
76 * @param words
77 * the number of words (or images)
78 */
79 public Paragraph(ParagraphType type, String content, long words) {
80 this.type = type;
81 this.content = content;
82 this.words = words;
83 }
84
85 /**
86 * The {@link ParagraphType}.
87 *
88 * @return the type
89 */
90 public ParagraphType getType() {
91 return type;
92 }
93
94 /**
95 * The {@link ParagraphType}.
96 *
97 * @param type
98 * the type to set
99 */
100 public void setType(ParagraphType type) {
101 this.type = type;
102 }
103
104 /**
105 * The content of this {@link Paragraph} if it is not an image.
106 *
107 * @return the content
108 */
109 public String getContent() {
110 return content;
111 }
112
113 /**
114 * The content of this {@link Paragraph}.
115 *
116 * @param content
117 * the content to set
118 */
119 public void setContent(String content) {
120 this.content = content;
121 }
122
123 /**
124 * The content of this {@link Paragraph} if it is an image.
125 *
126 * @return the content
127 */
128 public Image getContentImage() {
129 return contentImage;
130 }
131
132 /**
133 * The number of words (or images) in this {@link Paragraph}.
134 *
135 * @return the number of words
136 */
137 public long getWords() {
138 return words;
139 }
140
141 /**
142 * The number of words (or images) in this {@link Paragraph}.
143 *
144 * @param words
145 * the number of words to set
146 */
147 public void setWords(long words) {
148 this.words = words;
149 }
150
151 /**
152 * Display a DEBUG {@link String} representation of this object.
153 */
154 @Override
155 public String toString() {
156 return String.format("%s: [%s]", "" + type, content == null ? "N/A"
157 : content);
158 }
159
160 @Override
161 public Paragraph clone() {
162 Paragraph para = null;
163 try {
164 para = (Paragraph) super.clone();
165 } catch (CloneNotSupportedException e) {
166 // Did the clones rebel?
167 System.err.println(e);
168 }
169
170 return para;
171 }
172 }