ContactDetailsRaw: fix bad TYPES handling
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetailsRaw.java
index 08cf9b8b8ceb6d7b8f27231ef22e0ff59795c67d..2de99192050af84249affc547dbaa5a073e596d7 100644 (file)
@@ -8,11 +8,11 @@ import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.TypeInfo;
 import be.nikiroo.jvcard.launcher.Main;
 import be.nikiroo.jvcard.resources.StringUtils;
-import be.nikiroo.jvcard.resources.Trans;
+import be.nikiroo.jvcard.resources.enums.ColorOption;
+import be.nikiroo.jvcard.resources.enums.StringId;
 import be.nikiroo.jvcard.tui.KeyAction;
 import be.nikiroo.jvcard.tui.KeyAction.DataType;
 import be.nikiroo.jvcard.tui.KeyAction.Mode;
-import be.nikiroo.jvcard.tui.UiColors.Element;
 
 import com.googlecode.lanterna.input.KeyType;
 
@@ -36,15 +36,19 @@ public class ContactDetailsRaw extends MainContentList {
 
        @Override
        public List<KeyAction> getKeyBindings() {
-               // TODO Auto-generated method stub
                List<KeyAction> actions = new LinkedList<KeyAction>();
 
-               // TODO: add, remove
                actions.add(new KeyAction(Mode.ASK_USER, KeyType.Enter,
-                               Trans.StringId.DUMMY) {
+                               StringId.KEY_ACTION_EDIT_FIELD) {
                        @Override
                        public Object getObject() {
-                               return getSelectedData();
+                               Data data = getSelectedData();
+                               if (data != null && data.getB64Key() != -1) {
+                                       setMessage("Cannot modify binary values in RAW mode", true);
+                                       data = null;
+                               }
+
+                               return data;
                        }
 
                        @Override
@@ -79,7 +83,8 @@ public class ContactDetailsRaw extends MainContentList {
                                return "Cannot modify value";
                        }
                });
-               actions.add(new KeyAction(Mode.ASK_USER_KEY, 'd', Trans.StringId.DUMMY) {
+               actions.add(new KeyAction(Mode.ASK_USER_KEY, 'd',
+                               StringId.KEY_ACTION_DELETE) {
                        @Override
                        public Object getObject() {
                                return getSelectedData();
@@ -108,7 +113,7 @@ public class ContactDetailsRaw extends MainContentList {
                        }
                });
                // TODO: ui
-               actions.add(new KeyAction(Mode.ASK_USER, 'a', Trans.StringId.DUMMY) {
+               actions.add(new KeyAction(Mode.ASK_USER, 'a', StringId.KEY_ACTION_ADD) {
                        @Override
                        public Object getObject() {
                                return contact;
@@ -143,7 +148,7 @@ public class ContactDetailsRaw extends MainContentList {
                });
                // TODO: use a real UI for this, not a simple text box (a list or
                // something, maybe a whole new pane?)
-               actions.add(new KeyAction(Mode.ASK_USER, 't', Trans.StringId.DUMMY) {
+               actions.add(new KeyAction(Mode.ASK_USER, 't', StringId.DUMMY) {
                        private String previous;
 
                        @Override
@@ -186,7 +191,7 @@ public class ContactDetailsRaw extends MainContentList {
                                return "Cannot modify value";
                        }
                });
-               actions.add(new KeyAction(Mode.ASK_USER, 'g', Trans.StringId.DUMMY) {
+               actions.add(new KeyAction(Mode.ASK_USER, 'g', StringId.DUMMY) {
                        private String previous;
 
                        @Override
@@ -230,7 +235,7 @@ public class ContactDetailsRaw extends MainContentList {
                        }
                });
                actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
-                               Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
+                               StringId.KEY_ACTION_SWITCH_FORMAT) {
                        @Override
                        public boolean onAction() {
                                extMode = !extMode;
@@ -263,6 +268,7 @@ public class ContactDetailsRaw extends MainContentList {
        @Override
        protected List<TextPart> getLabel(int index, int width, boolean selected,
                        boolean focused) {
+
                // TODO: from ini file?
                int SIZE_COL_1 = 15;
                int SIZE_COL_2_OPT = 10;
@@ -278,12 +284,12 @@ public class ContactDetailsRaw extends MainContentList {
                if (data == null)
                        return parts;
 
-               Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED
-                               : Element.CONTACT_LINE;
-               Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED
-                               : Element.CONTACT_LINE_SEPARATOR;
-               Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED
-                               : Element.CONTACT_LINE_DIRTY;
+               ColorOption el = (focused && selected) ? ColorOption.CONTACT_LINE_SELECTED
+                               : ColorOption.CONTACT_LINE;
+               ColorOption elSep = (focused && selected) ? ColorOption.CONTACT_LINE_SEPARATOR_SELECTED
+                               : ColorOption.CONTACT_LINE_SEPARATOR;
+               ColorOption elDirty = (focused && selected) ? ColorOption.CONTACT_LINE_DIRTY_SELECTED
+                               : ColorOption.CONTACT_LINE_DIRTY;
 
                if (data.isDirty()) {
                        parts.add(new TextPart(" ", el));
@@ -297,7 +303,12 @@ public class ContactDetailsRaw extends MainContentList {
 
                StringBuilder valueBuilder = new StringBuilder(" ");
                if (!extMode) {
-                       valueBuilder.append(data.getValue().replaceAll("\n", "\\\\n"));
+                       if (data.getB64Key() != -1) {
+                               // TODO: i18n
+                               valueBuilder.append("<BKey " + data.getB64Key() + ">");
+                       } else {
+                               valueBuilder.append(data.getValue().replaceAll("\n", "\\\\n"));
+                       }
                        if (data.getGroup() != null && data.getGroup().length() > 0) {
                                valueBuilder.append("(");
                                valueBuilder.append(data.getGroup());
@@ -352,7 +363,7 @@ public class ContactDetailsRaw extends MainContentList {
         * @param data
         *            the {@link Data} from which to take the {@link TypeInfo}s
         * @param builder
-        *            an optional {@link StringBuilder} to append the serialized
+        *            an optional {@link StringBuilder} to append the serialised
         *            version to
         * 
         * @return the given {@link StringBuilder} or a new one if the given one is
@@ -398,12 +409,29 @@ public class ContactDetailsRaw extends MainContentList {
                        }
 
                        if (previous != '\\' && car == ',') {
-                               String[] tab = value.substring(done, index).split("\\:");
-                               infos.add(new TypeInfo( //
-                                               tab[0].replaceAll("\\,", ",").replaceAll("\\:", ":")
-                                                               .trim(), //
-                                               tab[1].replaceAll("\\,", ",").replaceAll("\\:", ":")
-                                                               .trim()));
+                               String subValue = value.substring(done, index);
+                               int indexColumn = subValue.indexOf(':');
+                               while (indexColumn > 0
+                                               && subValue.charAt(indexColumn - 1) == '\\') {
+                                       if (indexColumn == subValue.length() - 1) {
+                                               indexColumn = -1;
+                                       } else {
+                                               indexColumn = subValue.indexOf(':', indexColumn + 1);
+                                       }
+                               }
+
+                               String n = "";
+                               String v = "";
+                               if (indexColumn >= 0) {
+                                       n = subValue.substring(0, indexColumn).trim();
+                                       v = subValue.substring(indexColumn + 1)
+                                                       .replaceAll("\\\\:", ":").trim();
+                               } else {
+                                       n = subValue.trim();
+                               }
+
+                               infos.add(new TypeInfo(n, v));
+
                                done = index + 1;
                        }