Refresh data on "Back", allow configuration of View + border
[jvcard.git] / src / be / nikiroo / jvcard / Data.java
index f2bb4080f6d770776e1824e0cf7cfd7495c20433..63362332004fede408a44ee37d2fc3460fa0a64d 100644 (file)
@@ -1,7 +1,6 @@
 package be.nikiroo.jvcard;
 
 import java.security.InvalidParameterException;
-import java.util.LinkedList;
 import java.util.List;
 
 /**
@@ -11,7 +10,7 @@ import java.util.List;
  * @author niki
  *
  */
-public class Data {
+public class Data extends BaseClass<TypeInfo> {
        public enum DataPart {
                FN_FAMILY, FN_GIVEN, FN_ADDITIONAL, // Name
                FN_PRE, FN_POST, // Pre/Post
@@ -24,9 +23,6 @@ public class Data {
        private String value;
        private String group;
        private int b64; // -1 = no, 0 = still not ordered, the rest is order
-       private List<TypeInfo> types;
-       private boolean dirty;
-       private Contact parent;
 
        /**
         * Create a new {@link Data} with the given values.
@@ -34,25 +30,21 @@ public class Data {
         * @param types
         *            the types of this {@link Data}
         * @param name
-        *            its name
+        *            its name (<b>MUST NOT</b> be NULL)
         * @param value
-        *            its value
+        *            its value (<b>MUST NOT</b> be NULL)
         * @param group
-        *            its group if any
+        *            its group if any (or NULL if none)
         */
        public Data(List<TypeInfo> types, String name, String value, String group) {
-               if (types == null) {
-                       types = new LinkedList<TypeInfo>();
-               }
+               super(types);
 
-               this.types = types;
-               this.name = name;
-               this.value = value;
+               this.name = name.toUpperCase();
+               this.value = value.toString(); // crash NOW if null
                this.group = group;
 
                b64 = -1;
-               for (TypeInfo type : types) {
-                       type.setParent(this);
+               for (TypeInfo type : this) {
                        if (type.getName().equals("ENCODING")
                                        && type.getValue().equals("b")) {
                                b64 = 0;
@@ -61,30 +53,6 @@ public class Data {
                }
        }
 
-       /**
-        * Return the number of {@link TypeInfo} present in this {@link Data}.
-        * 
-        * @return the number of {@link TypeInfo}s
-        */
-       public int size() {
-               return types.size();
-       }
-
-       /**
-        * Return the {@link TypeInfo} at index <i>index</i>.
-        * 
-        * @param index
-        *            the index of the {@link TypeInfo} to find
-        * 
-        * @return the {@link TypeInfo}
-        * 
-        * @throws IndexOutOfBoundsException
-        *             if the index is < 0 or >= {@link Data#size()}
-        */
-       public TypeInfo get(int index) {
-               return types.get(index);
-       }
-
        /**
         * Return the name of this {@link Data}
         * 
@@ -126,6 +94,20 @@ public class Data {
                return group;
        }
 
+       /**
+        * Change the group of this {@link Data}
+        * 
+        * @param group
+        *            the new group
+        */
+       public void setGroup(String group) {
+               if ((group == null && this.group != null)
+                               || (group != null && !group.equals(this.group))) {
+                       this.group = group;
+                       setDirty();
+               }
+       }
+
        /**
         * Return the bkey number of this {@link Data} or -1 if it is not binary.
         * 
@@ -165,42 +147,27 @@ public class Data {
        }
 
        /**
-        * Check if this {@link Data} has unsaved changes.
+        * Check if this {@link Data} has the "preferred" flag.
         * 
         * @return TRUE if it has
         */
-       public boolean isDirty() {
-               return dirty;
-       }
+       public boolean isPreferred() {
+               for (TypeInfo type : this) {
+                       if (type.getName().equals("TYPE") && type.getValue().equals("pref")) {
+                               return true;
+                       }
+               }
 
-       /**
-        * Notify that this element has unsaved changes, and notify its parent of
-        * the same if any.
-        */
-       protected void setDirty() {
-               this.dirty = true;
-               if (this.parent != null)
-                       this.parent.setDirty();
+               return false;
        }
 
-       /**
-        * Notify this element <i>and all its descendants</i> that it is in pristine
-        * state (as opposed to dirty).
-        */
-       void setPristine() {
-               dirty = false;
-               for (TypeInfo type : types) {
-                       type.setPristine();
-               }
+       @Override
+       public String getId() {
+               return "" + name;
        }
 
-       /**
-        * Set the parent of this {@link Data}.
-        * 
-        * @param parent
-        *            the new parent
-        */
-       void setParent(Contact parent) {
-               this.parent = parent;
+       @Override
+       public String getState() {
+               return ("" + name + value + group).replace(' ', '_');
        }
 }