X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2Fpanes%2FContactDetails.java;h=aa838cb6d9339cc8d57a6e91dbefe743d827ae22;hb=30a4aa17f2141ad80a23447ee2e6303f6c9ef995;hp=fc6a198feda3dd316c77cd4c415d766a2e886af0;hpb=7da41ecd30228908bf2afcd07ff7943ab59d4c01;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java index fc6a198..aa838cb 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java @@ -1,16 +1,16 @@ package be.nikiroo.jvcard.tui.panes; import java.awt.Image; -import java.io.ByteArrayInputStream; import java.util.LinkedList; import java.util.List; - -import javax.imageio.ImageIO; -import javax.xml.bind.DatatypeConverter; +import java.util.MissingResourceException; +import java.util.ResourceBundle; 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 +20,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; @@ -35,7 +36,33 @@ public class ContactDetails extends MainContent { private Panel infoPanel; private Label note; + // from .properties file: + private int labelSize = -1; + private String infoFormat = ""; + // + public ContactDetails(Contact contact) { + // Get the .properties info: + ResourceBundle map = Bundles.getBundle("display"); + + try { + labelSize = Integer.parseInt(map + .getString("CONTACT_DETAILS_LABEL_WIDTH")); + + } catch (NumberFormatException e) { + e.printStackTrace(); + labelSize = -1; + } catch (MissingResourceException e) { + labelSize = -1; + } + + try { + infoFormat = map.getString("CONTACT_DETAILS_INFO"); + } catch (MissingResourceException e) { + e.printStackTrace(); + } + // + BorderLayout blayout = new BorderLayout(); setLayoutManager(blayout); @@ -57,8 +84,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); } /** @@ -76,36 +105,88 @@ 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"); - } - - // 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: + 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) + + data.toString())); + } else { + infoPanel + .addComponent(UiColors.Element.VIEW_CONTACT_NORMAL + .createLabel(StringUtils + .padString(label, + labelSize) + + data.toString())); + } + } + } else { + String val = contact.getPreferredDataValue(field); + if (val == null) + val = ""; + infoPanel.addComponent(el.createLabel(StringUtils + .padString(label, labelSize) + val)); + } + } 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("")); String notes = contact.getPreferredDataValue("NOTE"); 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; @@ -118,10 +199,7 @@ public class ContactDetails extends MainContent { && encoding.getValue().equalsIgnoreCase("b")) { try { - image = ImageIO.read(new ByteArrayInputStream( - DatatypeConverter.parseBase64Binary(photo - .getValue()))); - image.toString(); + image = StringUtils.toImage(photo.getValue()); } catch (Exception e) { System.err.println("Cannot parse image for contact: " + contact.getPreferredDataValue("UID")); @@ -142,7 +220,6 @@ public class ContactDetails extends MainContent { public List getKeyBindings() { List actions = new LinkedList(); - // TODO actions.add(new KeyAction(Mode.NONE, KeyType.Tab, Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { @Override @@ -174,9 +251,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; @@ -235,7 +312,7 @@ public class ContactDetails extends MainContent { } else { // TODO: configure size? int w = getSize().getColumns() - 40; - int h = getSize().getRows() - 5; + int h = getSize().getRows() - 9; if (w <= 0 || h <= 0) return null;