Make all classes in be.nikiroo.fanfix.data Serializable
[fanfix.git] / src / be / nikiroo / fanfix / data / Paragraph.java
index 273aca3af7884f4ed3de556b84837465b151015f..9adc51c420e815492858adbcffbd1b606dab7eda 100644 (file)
@@ -1,13 +1,17 @@
 package be.nikiroo.fanfix.data;
 
-import java.net.URL;
+import java.io.Serializable;
+
+import be.nikiroo.utils.Image;
 
 /**
  * A paragraph in a chapter of the story.
  * 
  * @author niki
  */
-public class Paragraph {
+public class Paragraph implements Cloneable, Serializable {
+       private static final long serialVersionUID = 1L;
+
        /**
         * A paragraph type, that will dictate how the paragraph will be handled.
         * 
@@ -23,11 +27,24 @@ public class Paragraph {
                /** Quotation (dialogue) */
                QUOTE,
                /** An image (no text) */
-               IMAGE,
+               IMAGE, ;
+
+               /**
+                * This paragraph type is of a text kind (quote or not).
+                * 
+                * @param allowEmpty
+                *            allow empty text as text, too (blanks, breaks...)
+                * @return TRUE if it is
+                */
+               public boolean isText(boolean allowEmpty) {
+                       return (this == NORMAL || this == QUOTE)
+                                       || (allowEmpty && (this == BLANK || this == BREAK));
+               }
        }
 
        private ParagraphType type;
        private String content;
+       private Image contentImage;
        private long words;
 
        /**
@@ -41,11 +58,12 @@ public class Paragraph {
        /**
         * Create a new {@link Paragraph} with the given image.
         * 
-        * @param imageUrl
-        *            the image as an URL
+        * @param contentImage
+        *            the image
         */
-       public Paragraph(URL imageUrl) {
-               this(ParagraphType.IMAGE, imageUrl.toString(), 1);
+       public Paragraph(Image contentImage) {
+               this(ParagraphType.IMAGE, null, 1);
+               this.contentImage = contentImage;
        }
 
        /**
@@ -84,7 +102,7 @@ public class Paragraph {
        }
 
        /**
-        * The content of this {@link Paragraph}.
+        * The content of this {@link Paragraph} if it is not an image.
         * 
         * @return the content
         */
@@ -102,6 +120,15 @@ public class Paragraph {
                this.content = content;
        }
 
+       /**
+        * The content of this {@link Paragraph} if it is an image.
+        * 
+        * @return the content
+        */
+       public Image getContentImage() {
+               return contentImage;
+       }
+
        /**
         * The number of words (or images) in this {@link Paragraph}.
         * 
@@ -126,6 +153,20 @@ public class Paragraph {
         */
        @Override
        public String toString() {
-               return String.format("%s: [%s]", "" + type, "" + content);
+               return String.format("%s: [%s]", "" + type, content == null ? "N/A"
+                               : content);
+       }
+
+       @Override
+       public Paragraph clone() {
+               Paragraph para = null;
+               try {
+                       para = (Paragraph) super.clone();
+               } catch (CloneNotSupportedException e) {
+                       // Did the clones rebel?
+                       System.err.println(e);
+               }
+
+               return para;
        }
 }