Show some information in ContacTView (FN/N, email, phone, notes)
authorNiki Roo <niki@nikiroo.be>
Tue, 1 Mar 2016 17:10:42 +0000 (18:10 +0100)
committerNiki Roo <niki@nikiroo.be>
Tue, 1 Mar 2016 17:10:42 +0000 (18:10 +0100)
src/be/nikiroo/jvcard/tui/UiColors.java
src/be/nikiroo/jvcard/tui/panes/ContactDetails.java
src/be/nikiroo/jvcard/tui/panes/ContactDetailsRaw.java

index 589579a6281caebdcb61d04ea04d46ff251c8fcf..eadcd2985ca0546af2ab39126d4cb048ac5181ae 100644 (file)
@@ -39,7 +39,9 @@ public class UiColors {
                TITLE_MAIN, TITLE_VARIABLE, TITLE_COUNT, //
                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;
+               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, //
+               ;
 
                /**
                 * Get the foreground colour of this element.
@@ -88,6 +90,9 @@ public class UiColors {
        }
 
        private Label createLabel(Element el, String text) {
+               if (text == null)
+                       text = "";
+
                Label lbl = new Label(text);
                themeLabel(el, lbl);
                return lbl;
@@ -139,6 +144,12 @@ public class UiColors {
                addEl(Element.TITLE_MAIN, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE);
                addEl(Element.TITLE_VARIABLE, TextColor.ANSI.GREEN, TextColor.ANSI.BLUE);
                addEl(Element.TITLE_COUNT, TextColor.ANSI.RED, TextColor.ANSI.BLUE);
+               addEl(Element.VIEW_CONTACT_NAME, TextColor.ANSI.BLACK,
+                               TextColor.ANSI.WHITE);
+               addEl(Element.VIEW_CONTACT_NORMAL, TextColor.ANSI.WHITE,
+                               TextColor.ANSI.BLACK);
+               addEl(Element.VIEW_CONTACT_NOTES_TITLE, TextColor.ANSI.BLACK,
+                               TextColor.ANSI.WHITE);
        }
 
        private void addEl(Element el, TextColor fore, TextColor back) {
index 0814ed06a7db6ed207a5b1c38a9f3a4d808a4093..8aeac19f27002419035c58ba6218cf36a692bc82 100644 (file)
@@ -15,35 +15,94 @@ import be.nikiroo.jvcard.tui.ImageTextControl;
 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;
 
 import com.googlecode.lanterna.TerminalSize;
 import com.googlecode.lanterna.gui2.BorderLayout;
+import com.googlecode.lanterna.gui2.Direction;
+import com.googlecode.lanterna.gui2.Label;
+import com.googlecode.lanterna.gui2.LinearLayout;
 import com.googlecode.lanterna.gui2.Panel;
 import com.googlecode.lanterna.input.KeyType;
 
 public class ContactDetails extends MainContent {
        private Contact contact;
        private Panel top;
-       private ImageTextControl txt;
+       private ImageTextControl txtImage;
        private Image image;
        private boolean fullscreenImage;
+       private Panel infoPanel;
+       private Label note;
 
        public ContactDetails(Contact contact) {
-               this.contact = contact;
-
                BorderLayout blayout = new BorderLayout();
                setLayoutManager(blayout);
 
                top = new Panel();
+               blayout = new BorderLayout();
+               top.setLayoutManager(blayout);
+
+               infoPanel = new Panel();
+               infoPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
+               top.addComponent(infoPanel, BorderLayout.Location.CENTER);
+
+               Panel notePanel = new Panel();
+               notePanel.setLayoutManager(new LinearLayout(Direction.HORIZONTAL));
+
+               notePanel.addComponent(UiColors.Element.VIEW_CONTACT_NOTES_TITLE
+                               .createLabel("Notes:"));
+               note = UiColors.Element.VIEW_CONTACT_NORMAL.createLabel("");
+               notePanel.addComponent(note);
+
                setContact(contact);
+
                addComponent(top, BorderLayout.Location.TOP);
+               addComponent(notePanel, BorderLayout.Location.CENTER);
        }
 
+       /**
+        * Change the enclosed {@link Contact} from this {@link ContactDetails}.
+        * 
+        * @param contact
+        *            the new {@link Contact}
+        */
        public void setContact(Contact contact) {
-               Image img = null;
+               if (this.contact == contact)
+                       return;
+
                this.contact = contact;
 
-               if (contact != null) {
+               if (contact == null) {
+                       image = null;
+               } else {
+                       infoPanel.removeAllComponents();
+
+                       String name = contact.getPreferredDataValue("FN");
+                       if (name == null || name.length() == 0) {
+                               // TODO format it ourself
+                               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")));
+                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
+                                       .createLabel(""));
+
+                       String notes = contact.getPreferredDataValue("NOTE");
+                       if (notes == null)
+                               notes = "";
+                       note.setText(notes.replaceAll("\\\\n", "\n"));
+
                        Data photo = contact.getPreferredData("PHOTO");
                        if (photo != null) {
                                TypeInfo encoding = null;
@@ -61,13 +120,13 @@ public class ContactDetails extends MainContent {
                                if (encoding != null && encoding.getValue() != null
                                                && encoding.getValue().equalsIgnoreCase("b")) {
 
-                                       img = new ImageIcon(Base64.getDecoder().decode(
+                                       image = new ImageIcon(Base64.getDecoder().decode(
                                                        photo.getValue())).getImage();
                                }
                        }
                }
 
-               setImage(img);
+               setImage(image);
        }
 
        @Override
@@ -84,8 +143,8 @@ public class ContactDetails extends MainContent {
                                Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
                        @Override
                        public boolean onAction() {
-                               if (txt != null) {
-                                       txt.switchMode();
+                               if (txtImage != null) {
+                                       txtImage.switchMode();
                                }
 
                                return false;
@@ -95,8 +154,8 @@ public class ContactDetails extends MainContent {
                                Trans.StringId.KEY_ACTION_INVERT) {
                        @Override
                        public boolean onAction() {
-                               if (txt != null) {
-                                       txt.invertColor();
+                               if (txtImage != null) {
+                                       txtImage.invertColor();
                                }
 
                                return false;
@@ -123,7 +182,8 @@ public class ContactDetails extends MainContent {
        }
 
        /**
-        * Set the {@link Image} to render.
+        * Set the {@link Image} to render and refresh it to the current size
+        * constraints.
         * 
         * @param image
         *            the new {@link Image}
@@ -131,19 +191,22 @@ public class ContactDetails extends MainContent {
        private void setImage(Image image) {
                this.image = image;
 
+               if (txtImage != null && top.containsComponent(txtImage))
+                       top.removeComponent(txtImage);
+
                TerminalSize size = getTxtSize();
                if (size != null) {
-                       if (txt != null)
-                               txt.setSize(size);
+                       if (txtImage != null)
+                               txtImage.setSize(size);
                        else
-                               txt = new ImageTextControl(image, size);
+                               txtImage = new ImageTextControl(image, size);
                }
 
-               if (top.getChildCount() > 0)
-                       top.removeAllComponents();
+               if (size != null) {
+                       top.addComponent(txtImage, BorderLayout.Location.LEFT);
+               }
 
-               if (size != null)
-                       top.addComponent(txt);
+               invalidate();
        }
 
        /**
@@ -158,8 +221,13 @@ public class ContactDetails extends MainContent {
                        if (fullscreenImage) {
                                return getSize();
                        } else {
-                               // TODO:
-                               return new TerminalSize(40, 20);
+                               // TODO: configure size?
+                               int w = getSize().getColumns() - 40;
+                               int h = getSize().getRows() - 5;
+                               if (w <= 0 || h <= 0)
+                                       return null;
+
+                               return new TerminalSize(w, h);
                        }
                }
 
index 1347b0ee11698d94a18180142e28ea605c44b99c..47b5e8addb103c25c3f63f4658f70751609fd5a1 100644 (file)
@@ -31,70 +31,6 @@ 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;
-
-               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;
-
-               Data data = contact.get(index);
-
-               List<TextPart> parts = new LinkedList<TextPart>();
-               if (data.isDirty()) {
-                       parts.add(new TextPart(" ", el));
-                       parts.add(new TextPart("*", elDirty));
-               } else {
-                       parts.add(new TextPart("  ", elSep));
-               }
-               String name = " " + data.getName() + " ";
-               String value = null;
-
-               StringBuilder valueBuilder = new StringBuilder(" ");
-               switch (mode) {
-               case 0:
-                       valueBuilder.append(data.getValue());
-                       if (data.getGroup() != null && data.getGroup().length() > 0) {
-                               valueBuilder.append("(");
-                               valueBuilder.append(data.getGroup());
-                               valueBuilder.append(")");
-                       }
-                       break;
-               case 1:
-                       for (int indexType = 0; indexType < data.size(); indexType++) {
-                               TypeInfo type = data.get(indexType);
-                               if (valueBuilder.length() > 1)
-                                       valueBuilder.append(", ");
-                               valueBuilder.append(type.getName());
-                               valueBuilder.append(": ");
-                               valueBuilder.append(type.getValue());
-                       }
-                       break;
-               }
-               valueBuilder.append(" ");
-
-               value = valueBuilder.toString();
-
-               name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode());
-               value = StringUtils.sanitize(value, UiColors.getInstance().isUnicode());
-
-               name = StringUtils.padString(name, SIZE_COL_1);
-               value = StringUtils.padString(value, width - SIZE_COL_1
-                               - getSeparator().length() - 2);
-
-               parts.add(new TextPart(name, el));
-               parts.add(new TextPart(getSeparator(), elSep));
-               parts.add(new TextPart(value, el));
-
-               return parts;
-       };
-
        @Override
        public DataType getDataType() {
                return DataType.DATA;
@@ -178,4 +114,69 @@ public class ContactDetailsRaw extends MainContentList {
                // TODO Auto-generated method stub
                return null;
        }
+
+       @Override
+       protected List<TextPart> getLabel(int index, int width, boolean selected,
+                       boolean focused) {
+               // TODO: from ini file?
+               int SIZE_COL_1 = 15;
+
+               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;
+
+               Data data = contact.get(index);
+
+               List<TextPart> parts = new LinkedList<TextPart>();
+               if (data.isDirty()) {
+                       parts.add(new TextPart(" ", el));
+                       parts.add(new TextPart("*", elDirty));
+               } else {
+                       parts.add(new TextPart("  ", elSep));
+               }
+               String name = " " + data.getName() + " ";
+               String value = null;
+
+               StringBuilder valueBuilder = new StringBuilder(" ");
+               switch (mode) {
+               case 0:
+                       valueBuilder.append(data.getValue());
+                       if (data.getGroup() != null && data.getGroup().length() > 0) {
+                               valueBuilder.append("(");
+                               valueBuilder.append(data.getGroup());
+                               valueBuilder.append(")");
+                       }
+                       break;
+               case 1:
+                       for (int indexType = 0; indexType < data.size(); indexType++) {
+                               TypeInfo type = data.get(indexType);
+                               if (valueBuilder.length() > 1)
+                                       valueBuilder.append(", ");
+                               valueBuilder.append(type.getName());
+                               valueBuilder.append(": ");
+                               valueBuilder.append(type.getValue());
+                       }
+                       break;
+               }
+               valueBuilder.append(" ");
+
+               value = valueBuilder.toString();
+
+               name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode());
+               value = StringUtils.sanitize(value, UiColors.getInstance().isUnicode());
+
+               name = StringUtils.padString(name, SIZE_COL_1);
+               value = StringUtils.padString(value, width - SIZE_COL_1
+                               - getSeparator().length() - 2);
+
+               parts.add(new TextPart(name, el));
+               parts.add(new TextPart(getSeparator(), elSep));
+               parts.add(new TextPart(value, el));
+
+               return parts;
+       };
+
 }