Fix PREF handling (was not correct relative to the RFC!)
authorNiki Roo <niki@nikiroo.be>
Sat, 2 Apr 2016 12:51:08 +0000 (14:51 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 2 Apr 2016 12:51:08 +0000 (14:51 +0200)
src/be/nikiroo/jvcard/Contact.java
src/be/nikiroo/jvcard/Data.java
src/be/nikiroo/jvcard/tui/panes/ContactDetails.java

index 9facbcd0f38cf45fe06c09f9d92f44e049c9d19d..d75d3380326a8bfde6c245f959dd6ff916b1f07f 100644 (file)
@@ -36,23 +36,26 @@ public class Contact extends BaseClass<Data> {
        }
 
        /**
-        * Return the preferred Data field with the given name, or NULL if none.
+        * Return the preferred Data field with the given name, the first one if
+        * none is preferred, or NULL if none at all.
         * 
         * @param name
         *            the name to look for
-        * @return the Data field, or NULL
+        * 
+        * @return the {@link Data} field, or NULL
         */
        public Data getPreferredData(String name) {
-               Data first = null;
+               Data pref = null;
+               int ipref = Integer.MAX_VALUE;
                for (Data data : getData(name)) {
-                       if (first == null)
-                               first = data;
+                       if (pref == null)
+                               pref = data;
 
-                       if (data.isPreferred())
-                               return data;
+                       if (data.getPreferred() < ipref)
+                               pref = data;
                }
 
-               return first;
+               return pref;
        }
 
        /**
index ba5fde5df13a9d86381b965aa1487d702ff61be8..4d3da7e4c7d37f0aa3bd1754065cfbcbdc0e4872 100644 (file)
@@ -186,18 +186,23 @@ public class Data extends BaseClass<TypeInfo> {
        }
 
        /**
-        * Check if this {@link Data} has the "preferred" flag.
+        * Return the preferred value of this {@link Data}, or
+        * {@link Integer#MAX_VALUE} if none.
         * 
-        * @return TRUE if it has
+        * @return the preferred value
         */
-       public boolean isPreferred() {
+       public int getPreferred() {
                for (TypeInfo type : this) {
-                       if (type.getName().equals("TYPE") && type.getValue().equals("pref")) {
-                               return true;
+                       if (type.getName().equals("PRE")) {
+                               try {
+                                       return Integer.parseInt(type.getValue());
+                               } catch (NumberFormatException e) {
+                                       e.printStackTrace();
+                               }
                        }
                }
 
-               return false;
+               return Integer.MAX_VALUE;
        }
 
        /**
index be2c2017a15a82aae009dea6643b55340a51c730..2ea8a9724b80cf6ac777cb37fc75ea08ae7f328c 100644 (file)
@@ -131,8 +131,9 @@ public class ContactDetails extends MainContent {
                                        String field = info.substring(index + 1);
 
                                        if (all) {
+                                               Data pref = contact.getPreferredData(field);
                                                for (Data data : contact.getData(field)) {
-                                                       if (data.isPreferred()) {
+                                                       if (data == pref) {
                                                                infoPanel.addComponent(UiColors.createLabel(el,
                                                                                StringUtils.padString(label, labelSize)
                                                                                                + data.toString()));