Refresh data on "Back", allow configuration of View + border
authorNiki Roo <niki@nikiroo.be>
Sat, 26 Mar 2016 11:55:25 +0000 (12:55 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 26 Mar 2016 11:55:25 +0000 (12:55 +0100)
src/be/nikiroo/jvcard/Contact.java
src/be/nikiroo/jvcard/Data.java
src/be/nikiroo/jvcard/resources/StringUtils.java
src/be/nikiroo/jvcard/resources/Trans.java
src/be/nikiroo/jvcard/resources/colors.properties
src/be/nikiroo/jvcard/resources/display.properties
src/be/nikiroo/jvcard/tui/UiColors.java
src/be/nikiroo/jvcard/tui/panes/ContactDetails.java
src/be/nikiroo/jvcard/tui/panes/FileList.java
src/be/nikiroo/jvcard/tui/panes/MainContent.java

index 08c2a515e563cdfb39aadc7ab84795b1ec1153c6..1b888961466737bace2f6f647ee5d9d21b2b739b 100644 (file)
@@ -47,13 +47,9 @@ public class Contact extends BaseClass<Data> {
                for (Data data : getData(name)) {
                        if (first == null)
                                first = data;
-                       for (int index = 0; index < data.size(); index++) {
-                               TypeInfo type = data.get(index);
-                               if (type.getName().equals("TYPE")
-                                               && type.getValue().equals("pref")) {
-                                       return data;
-                               }
-                       }
+
+                       if (data.isPreferred())
+                               return data;
                }
 
                return first;
index 5c09a13419c1177167f4b2b1cb7bcea1fab68dbb..63362332004fede408a44ee37d2fc3460fa0a64d 100644 (file)
@@ -146,6 +146,21 @@ public class Data extends BaseClass<TypeInfo> {
                return b64 >= 0;
        }
 
+       /**
+        * Check if this {@link Data} has the "preferred" flag.
+        * 
+        * @return TRUE if it has
+        */
+       public boolean isPreferred() {
+               for (TypeInfo type : this) {
+                       if (type.getName().equals("TYPE") && type.getValue().equals("pref")) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
        @Override
        public String getId() {
                return "" + name;
index bfe0d3da44782e82db58416549c1b11a3f74a33e..2009dce628ef48864d48cf51c31bab5e6eae9813 100644 (file)
@@ -22,8 +22,7 @@ public class StringUtils {
         * @param text
         *            the {@link String} to fix
         * @param width
-        *            the size of the resulting {@link String} if the text fits or
-        *            if cut is TRUE
+        *            the size of the resulting {@link String} or -1 for a noop
         * 
         * @return the resulting {@link String} of size <i>size</i>
         */
@@ -39,7 +38,7 @@ public class StringUtils {
         *            the {@link String} to fix
         * @param width
         *            the size of the resulting {@link String} if the text fits or
-        *            if cut is TRUE
+        *            if cut is TRUE or -1 for a noop
         * @param cut
         *            cut the {@link String} shorter if needed
         * @param align
@@ -50,7 +49,7 @@ public class StringUtils {
         */
        static public String padString(String text, int width, boolean cut,
                        Alignment align) {
-
+               
                if (width >= 0) {
                        if (text == null)
                                text = "";
index 8dce6f1f9eb575744a552280864f7d1f1fd5efb0..c48d6af7907407d0036113c294ee07d11f6a0588 100644 (file)
@@ -294,6 +294,8 @@ public class Trans {
                @Meta(what = "", where = "", format = "", info = "")
                KEY_ACTION_EDIT_CONTACT, //
                @Meta(what = "", where = "", format = "", info = "")
+               KEY_ACTION_EDIT_CONTACT_RAW, //
+               @Meta(what = "", where = "", format = "", info = "")
                KEY_ACTION_SAVE_CARD, //
                @Meta(what = "", where = "", format = "", info = "")
                KEY_ACTION_DELETE_CONTACT, //
index b4cf41d8149e73071c0530dc1fffb19f4fd1143b..e1b750061cc788dc34355b944df939f15556d938 100644 (file)
@@ -43,6 +43,8 @@ VIEW_CONTACT_NAME_FG = BLACK
 VIEW_CONTACT_NAME_BG = WHITE
 VIEW_CONTACT_NORMAL_FG = WHITE
 VIEW_CONTACT_NORMAL_BG = BLACK
+VIEW_CONTACT_HIGHLIGHT_FG = RED
+VIEW_CONTACT_HIGHLIGHT_BG = BLACK
 VIEW_CONTACT_NOTES_TITLE_FG = BLACK
 VIEW_CONTACT_NOTES_TITLE_BG = WHITE
 CONTACT_LINE_DIRTY_SELECTED_FG = BLACK
index d03d45b343b7de26710d74a9536b6ba48e58dc4e..106bdc32237e953d6d28b02d87cb0689d12f1fb9 100644 (file)
 # You can cycle through them if you have more than one
 # (in this case, separate them with a comma (',')
 CONTACT_LIST_FORMAT = NICKNAME@10|FN@+|EMAIL@30|PHOTO@x,FN@+|EMAIL@40
+
+# The list of details to show in View Contact mode:
+# - Each detail (separated by a pipe "|" character) is visible on its own line
+# - It is made up of two parts: the label and the linked VCF field (optional),
+#              separated by an equal "=", sharp "#", plus "+" or asterisk "*" sign
+# - "=FIELD" will take the preferred value for this field
+# - "+FIELD" will take the preferred value for this field and highlight it
+# - "#FIELD" will take all the values with this field's name
+# - "*FIELD" will take all the values with this field's name, highlighting the preferred one
+#
+# Example:
+#      CONTACT_DETAILS_INFO = Phone:=TEL|eMail:=EMAIL
+#
+# This will print two lines in View mode:
+#      Phone: +32 888 88 88 88
+#      eMail: nobody@nowhere.com
+CONTACT_DETAILS_INFO = Phone:=TEL|eMail:=EMAIL
+
+# The size of the details' labels
+CONTACT_DETAILS_LABEL_WIDTH = 12
index 3fa83c7b9477f8f0fdde1b9a453add835dbbaa34..0a4696888e5e37278f07443da336690d0ce10038 100644 (file)
@@ -41,7 +41,7 @@ public class UiColors {
                ACTION_KEY, ACTION_DESC, //
                LINE_MESSAGE, LINE_MESSAGE_ERR, LINE_MESSAGE_QUESTION, LINE_MESSAGE_ANS, //
                CONTACT_LINE, CONTACT_LINE_SEPARATOR, CONTACT_LINE_SELECTED, CONTACT_LINE_SEPARATOR_SELECTED, CONTACT_LINE_DIRTY, CONTACT_LINE_DIRTY_SELECTED, //
-               VIEW_CONTACT_NAME, VIEW_CONTACT_NORMAL, VIEW_CONTACT_NOTES_TITLE, //
+               VIEW_CONTACT_NAME, VIEW_CONTACT_NORMAL, VIEW_CONTACT_HIGHLIGHT, VIEW_CONTACT_NOTES_TITLE, //
                ;
 
                /**
index fc6a198feda3dd316c77cd4c415d766a2e886af0..4cbbdf7d38e2e27a6264880123c647d3fd2f7410 100644 (file)
@@ -4,6 +4,8 @@ import java.awt.Image;
 import java.io.ByteArrayInputStream;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
 
 import javax.imageio.ImageIO;
 import javax.xml.bind.DatatypeConverter;
@@ -11,6 +13,8 @@ import javax.xml.bind.DatatypeConverter;
 import be.nikiroo.jvcard.Contact;
 import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.TypeInfo;
+import be.nikiroo.jvcard.resources.Bundles;
+import be.nikiroo.jvcard.resources.StringUtils;
 import be.nikiroo.jvcard.resources.Trans;
 import be.nikiroo.jvcard.tui.ImageTextControl;
 import be.nikiroo.jvcard.tui.KeyAction;
@@ -20,6 +24,7 @@ import be.nikiroo.jvcard.tui.UiColors;
 
 import com.googlecode.lanterna.TerminalSize;
 import com.googlecode.lanterna.gui2.BorderLayout;
+import com.googlecode.lanterna.gui2.Borders;
 import com.googlecode.lanterna.gui2.Direction;
 import com.googlecode.lanterna.gui2.Label;
 import com.googlecode.lanterna.gui2.LinearLayout;
@@ -34,8 +39,11 @@ public class ContactDetails extends MainContent {
        private boolean fullscreenImage;
        private Panel infoPanel;
        private Label note;
+       ResourceBundle map;
 
        public ContactDetails(Contact contact) {
+               map = Bundles.getBundle("display");
+
                BorderLayout blayout = new BorderLayout();
                setLayoutManager(blayout);
 
@@ -57,8 +65,10 @@ public class ContactDetails extends MainContent {
 
                setContact(contact);
 
-               addComponent(top, BorderLayout.Location.TOP);
-               addComponent(notePanel, BorderLayout.Location.CENTER);
+               addComponent(top.withBorder(Borders.doubleLineBevel()),
+                               BorderLayout.Location.TOP);
+               addComponent(notePanel.withBorder(Borders.singleLineBevel()),
+                               BorderLayout.Location.CENTER);
        }
 
        /**
@@ -81,18 +91,93 @@ public class ContactDetails extends MainContent {
                                name = contact.getPreferredDataValue("N");
                        }
 
-                       // TODO: i18n + do it properly
                        infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NAME
                                        .createLabel(name));
-
                        infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
                                        .createLabel(""));
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel("Phone:    "
-                                                       + contact.getPreferredDataValue("TEL")));
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel("eMail:    "
-                                                       + contact.getPreferredDataValue("EMAIL")));
+
+                       // List of infos:
+                       int labelSize = -1;
+                       try {
+                               labelSize = Integer.parseInt(map
+                                               .getString("CONTACT_DETAILS_LABEL_WIDTH"));
+
+                       } catch (NumberFormatException e) {
+                               e.printStackTrace();
+                               labelSize = -1;
+                       } catch (MissingResourceException e) {
+                               labelSize = -1;
+                       }
+
+                       String infoFormat = "";
+                       try {
+                               infoFormat = map.getString("CONTACT_DETAILS_INFO");
+                       } catch (MissingResourceException e) {
+                               e.printStackTrace();
+                       }
+
+                       String[] infos = infoFormat.split("\\|");
+                       for (String info : infos) {
+                               // # - "=FIELD" will take the preferred value for this field
+                               // # - "+FIELD" will take the preferred value for this field and
+                               // highlight it
+                               // # - "#FIELD" will take all the values with this field's name
+                               // # - "*FIELD" will take all the values with this field's name,
+                               // highlighting the preferred one
+                               // #
+
+                               boolean hl = false;
+                               boolean all = false;
+                               if (info.contains("+") || info.contains("#"))
+                                       hl = true;
+                               if (info.contains("*") || info.contains("#"))
+                                       all = true;
+
+                               if (all || hl || info.contains("=")) {
+                                       UiColors.Element el = hl ? UiColors.Element.VIEW_CONTACT_HIGHLIGHT
+                                                       : UiColors.Element.VIEW_CONTACT_NORMAL;
+
+                                       int index = info.indexOf('=');
+                                       if (index < 0)
+                                               index = info.indexOf('+');
+                                       if (index < 0)
+                                               index = info.indexOf('#');
+                                       if (index < 0)
+                                               index = info.indexOf('*');
+
+                                       String label = info.substring(0, index);
+                                       String field = info.substring(index + 1);
+
+                                       if (all) {
+                                               for (Data data : contact.getData(field)) {
+                                                       if (data.isPreferred()) {
+                                                               infoPanel
+                                                                               .addComponent(el.createLabel(StringUtils
+                                                                                               .padString(label, labelSize)
+                                                                                               + contact
+                                                                                                               .getPreferredDataValue(field)));
+                                                       } else {
+                                                               infoPanel
+                                                                               .addComponent(UiColors.Element.VIEW_CONTACT_NORMAL.createLabel(StringUtils
+                                                                                               .padString(label, labelSize)
+                                                                                               + contact
+                                                                                                               .getPreferredDataValue(field)));
+                                                       }
+                                               }
+                                       } else {
+                                               infoPanel.addComponent(el.createLabel(StringUtils
+                                                               .padString(label, labelSize)
+                                                               + contact.getPreferredDataValue(field)));
+                                       }
+                               } else {
+                                       String label = info;
+                                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
+                                                       .createLabel(StringUtils
+                                                                       .padString(label, labelSize)));
+                               }
+                       }
+                       // end of list
+
                        infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
                                        .createLabel(""));
 
@@ -142,7 +227,6 @@ public class ContactDetails extends MainContent {
        public List<KeyAction> getKeyBindings() {
                List<KeyAction> actions = new LinkedList<KeyAction>();
 
-               // TODO
                actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
                                Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
                        @Override
@@ -174,9 +258,9 @@ public class ContactDetails extends MainContent {
                                return false;
                        }
                });
-               // TODO: add "normal" edit and remove this one into RAW edit
-               actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'e',
-                               Trans.StringId.KEY_ACTION_EDIT_CONTACT) {
+               // TODO: add "normal" edit
+               actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'r',
+                               Trans.StringId.KEY_ACTION_EDIT_CONTACT_RAW) {
                        @Override
                        public Object getObject() {
                                return contact;
index 7065aaf679a6a0290c1ec7de401a716dd7a736bf..376aae9feb503e7c8c492fa3c5d3ca2c2fa74c90 100644 (file)
@@ -211,6 +211,10 @@ public class FileList extends MainContentList {
 
        @Override
        public String wakeup() throws IOException {
+               String s = super.wakeup();
+               if (s != null)
+                       return s;
+
                if (merger != null) {
                        if (!merger.mergeTargetFile.exists()) {
                                throw new IOException("Merge cancelled");
index 26cd7cb8ae330797e8394f27eb831762480241ac..0d873daa26287010a46183d84c001544b3601e93 100644 (file)
@@ -101,6 +101,10 @@ abstract public class MainContent extends Panel {
         * Wake up call when the content is popped-back into view. You should call
         * this method when you exit a previous content and come back to this one.
         * 
+        * <p>
+        * By default, it will just refresh the data.
+        * </p>
+        * 
         * @return a message to display, or NULL
         * 
         * @throws IOException
@@ -108,6 +112,7 @@ abstract public class MainContent extends Panel {
         *             be displayed to the user)
         */
        public String wakeup() throws IOException {
+               refreshData();
                return null;
        }
 }