update to latest nikiroo-utils
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetails.java
index 4cbbdf7d38e2e27a6264880123c647d3fd2f7410..2137bd885b1588312590eb15183037a554d6a801 100644 (file)
@@ -1,34 +1,32 @@
 package be.nikiroo.jvcard.tui.panes;
 
 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;
 
 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.resources.ColorOption;
+import be.nikiroo.jvcard.resources.DisplayBundle;
+import be.nikiroo.jvcard.resources.DisplayOption;
+import be.nikiroo.jvcard.resources.StringId;
 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 be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.ui.ImageUtilsAwt;
 
 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;
 import com.googlecode.lanterna.gui2.Panel;
+import com.googlecode.lanterna.gui2.TextBox;
+import com.googlecode.lanterna.gui2.TextBox.Style;
 import com.googlecode.lanterna.input.KeyType;
 
 public class ContactDetails extends MainContent {
@@ -38,11 +36,21 @@ public class ContactDetails extends MainContent {
        private Image image;
        private boolean fullscreenImage;
        private Panel infoPanel;
-       private Label note;
-       ResourceBundle map;
+       private TextBox note;
+
+       // from .properties file:
+       private int labelSize = -1;
+       private String infoFormat = "";
+
+       //
 
        public ContactDetails(Contact contact) {
-               map = Bundles.getBundle("display");
+               // Get the .properties info:
+               DisplayBundle map = new DisplayBundle();
+               labelSize = map.getInteger(DisplayOption.CONTACT_DETAILS_LABEL_WIDTH,
+                               -1);
+               infoFormat = map.getString(DisplayOption.CONTACT_DETAILS_INFO);
+               //
 
                BorderLayout blayout = new BorderLayout();
                setLayoutManager(blayout);
@@ -58,10 +66,14 @@ public class ContactDetails extends MainContent {
                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(UiColors.createLabel(
+                               ColorOption.VIEW_CONTACT_NOTES_TITLE, "Notes:"));
+               // 10000x10000 is probably enough or "max"
+               note = new TextBox(new TerminalSize(10000, 10000), Style.MULTI_LINE);
+               note.setReadOnly(true);
                notePanel.addComponent(note);
+               note.setVerticalFocusSwitching(false);
+               note.setHorizontalFocusSwitching(false);
 
                setContact(contact);
 
@@ -86,36 +98,12 @@ public class ContactDetails extends MainContent {
                        infoPanel.removeAllComponents();
 
                        String name = contact.getPreferredDataValue("FN");
-                       if (name == null || name.length() == 0) {
-                               // TODO format it ourself
-                               name = contact.getPreferredDataValue("N");
-                       }
-
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NAME
-                                       .createLabel(name));
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel(""));
+                       infoPanel.addComponent(UiColors.createLabel(
+                                       ColorOption.VIEW_CONTACT_NAME, name));
+                       infoPanel.addComponent(UiColors.createLabel(
+                                       ColorOption.VIEW_CONTACT_NORMAL, ""));
 
                        // 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
@@ -128,69 +116,75 @@ public class ContactDetails extends MainContent {
 
                                boolean hl = false;
                                boolean all = false;
-                               if (info.contains("+") || info.contains("#"))
+                               if (info.contains("+") || info.contains("#")) {
                                        hl = true;
-                               if (info.contains("*") || info.contains("#"))
+                               }
+                               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;
+                                       ColorOption el = hl ? ColorOption.VIEW_CONTACT_HIGHLIGHT
+                                                       : ColorOption.VIEW_CONTACT_NORMAL;
 
                                        int index = info.indexOf('=');
-                                       if (index < 0)
+                                       if (index < 0) {
                                                index = info.indexOf('+');
-                                       if (index < 0)
+                                       }
+                                       if (index < 0) {
                                                index = info.indexOf('#');
-                                       if (index < 0)
+                                       }
+                                       if (index < 0) {
                                                index = info.indexOf('*');
+                                       }
 
                                        String label = info.substring(0, index);
                                        String field = info.substring(index + 1);
 
                                        if (all) {
+                                               Data pref = contact.getPreferredData(field);
                                                for (Data data : contact.getData(field)) {
-                                                       if (data.isPreferred()) {
-                                                               infoPanel
-                                                                               .addComponent(el.createLabel(StringUtils
-                                                                                               .padString(label, labelSize)
-                                                                                               + contact
-                                                                                                               .getPreferredDataValue(field)));
+                                                       if (data == pref) {
+                                                               infoPanel.addComponent(UiColors.createLabel(el,
+                                                                               StringUtils.padString(label, labelSize)
+                                                                                               + data.toString()));
                                                        } else {
-                                                               infoPanel
-                                                                               .addComponent(UiColors.Element.VIEW_CONTACT_NORMAL.createLabel(StringUtils
-                                                                                               .padString(label, labelSize)
-                                                                                               + contact
-                                                                                                               .getPreferredDataValue(field)));
+                                                               infoPanel.addComponent(UiColors.createLabel(
+                                                                               ColorOption.VIEW_CONTACT_NORMAL,
+                                                                               StringUtils.padString(label, labelSize)
+                                                                                               + data.toString()));
                                                        }
                                                }
                                        } else {
-                                               infoPanel.addComponent(el.createLabel(StringUtils
-                                                               .padString(label, labelSize)
-                                                               + contact.getPreferredDataValue(field)));
+                                               String val = contact.getPreferredDataValue(field);
+                                               if (val == null) {
+                                                       val = "";
+                                               }
+                                               infoPanel.addComponent(UiColors.createLabel(el,
+                                                               StringUtils.padString(label, labelSize) + val));
                                        }
                                } else {
                                        String label = info;
-                                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                                       .createLabel(StringUtils
-                                                                       .padString(label, labelSize)));
+                                       infoPanel.addComponent(UiColors.createLabel(
+                                                       ColorOption.VIEW_CONTACT_NORMAL,
+                                                       StringUtils.padString(label, labelSize)));
                                }
                        }
                        // end of list
 
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel(""));
+                       infoPanel.addComponent(UiColors.createLabel(
+                                       ColorOption.VIEW_CONTACT_NORMAL, ""));
 
                        String notes = contact.getPreferredDataValue("NOTE");
-                       if (notes == null)
+                       if (notes == null) {
                                notes = "";
-                       note.setText(notes.replaceAll("\\\\n", "\n"));
+                       }
+                       note.setText(notes);
 
                        Data photo = contact.getPreferredData("PHOTO");
                        if (photo != null) {
                                TypeInfo encoding = null;
-                               for (int index = 0; index < photo.size(); index++) {
-                                       TypeInfo info = photo.get(index);
+                               for (TypeInfo info : photo) {
                                        if (info.getName() != null) {
                                                if (info.getName().equalsIgnoreCase("ENCODING"))
                                                        encoding = info;
@@ -201,12 +195,14 @@ public class ContactDetails extends MainContent {
 
                                if (encoding != null && encoding.getValue() != null
                                                && encoding.getValue().equalsIgnoreCase("b")) {
-
                                        try {
-                                               image = ImageIO.read(new ByteArrayInputStream(
-                                                               DatatypeConverter.parseBase64Binary(photo
-                                                                               .getValue())));
-                                               image.toString();
+                                               be.nikiroo.utils.Image img = new be.nikiroo.utils.Image(
+                                                               photo.getValue());
+                                               try {
+                                                       image = ImageUtilsAwt.fromImage(img);
+                                               } finally {
+                                                       img.close();
+                                               }
                                        } catch (Exception e) {
                                                System.err.println("Cannot parse image for contact: "
                                                                + contact.getPreferredDataValue("UID"));
@@ -228,7 +224,7 @@ public class ContactDetails extends MainContent {
                List<KeyAction> actions = new LinkedList<KeyAction>();
 
                actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
-                               Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
+                               StringId.KEY_ACTION_SWITCH_FORMAT) {
                        @Override
                        public boolean onAction() {
                                if (txtImage != null) {
@@ -238,8 +234,7 @@ public class ContactDetails extends MainContent {
                                return false;
                        }
                });
-               actions.add(new KeyAction(Mode.NONE, 'i',
-                               Trans.StringId.KEY_ACTION_INVERT) {
+               actions.add(new KeyAction(Mode.NONE, 'i', StringId.KEY_ACTION_INVERT) {
                        @Override
                        public boolean onAction() {
                                if (txtImage != null) {
@@ -250,7 +245,7 @@ public class ContactDetails extends MainContent {
                        }
                });
                actions.add(new KeyAction(Mode.NONE, 'f',
-                               Trans.StringId.KEY_ACTION_FULLSCREEN) {
+                               StringId.KEY_ACTION_FULLSCREEN) {
                        @Override
                        public boolean onAction() {
                                fullscreenImage = !fullscreenImage;
@@ -260,7 +255,7 @@ public class ContactDetails extends MainContent {
                });
                // TODO: add "normal" edit
                actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'r',
-                               Trans.StringId.KEY_ACTION_EDIT_CONTACT_RAW) {
+                               StringId.KEY_ACTION_EDIT_CONTACT_RAW) {
                        @Override
                        public Object getObject() {
                                return contact;
@@ -287,15 +282,17 @@ public class ContactDetails extends MainContent {
        private void setImage(Image image) {
                this.image = image;
 
-               if (txtImage != null && top.containsComponent(txtImage))
+               if (txtImage != null && top.containsComponent(txtImage)) {
                        top.removeComponent(txtImage);
+               }
 
                TerminalSize size = getTxtSize();
                if (size != null) {
-                       if (txtImage != null)
+                       if (txtImage != null) {
                                txtImage.setSize(size);
-                       else
+                       } else {
                                txtImage = new ImageTextControl(image, size);
+                       }
                }
 
                if (size != null) {
@@ -316,15 +313,16 @@ public class ContactDetails extends MainContent {
                                && getSize().getRows() > 0) {
                        if (fullscreenImage) {
                                return getSize();
-                       } else {
-                               // 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);
+                       // TODO: configure size?
+                       int w = getSize().getColumns() - 40;
+                       int h = getSize().getRows() - 9;
+                       if (w <= 0 || h <= 0) {
+                               return null;
                        }
+
+                       return new TerminalSize(w, h);
                }
 
                return null;