From 3634193b7a8927e68a3ae3d38fff4f6bd36c4ee5 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 26 Mar 2016 12:55:25 +0100 Subject: [PATCH] Refresh data on "Back", allow configuration of View + border --- src/be/nikiroo/jvcard/Contact.java | 10 +- src/be/nikiroo/jvcard/Data.java | 15 +++ .../nikiroo/jvcard/resources/StringUtils.java | 7 +- src/be/nikiroo/jvcard/resources/Trans.java | 2 + .../jvcard/resources/colors.properties | 2 + .../jvcard/resources/display.properties | 20 ++++ src/be/nikiroo/jvcard/tui/UiColors.java | 2 +- .../jvcard/tui/panes/ContactDetails.java | 112 +++++++++++++++--- src/be/nikiroo/jvcard/tui/panes/FileList.java | 4 + .../nikiroo/jvcard/tui/panes/MainContent.java | 5 + 10 files changed, 153 insertions(+), 26 deletions(-) diff --git a/src/be/nikiroo/jvcard/Contact.java b/src/be/nikiroo/jvcard/Contact.java index 08c2a51..1b88896 100644 --- a/src/be/nikiroo/jvcard/Contact.java +++ b/src/be/nikiroo/jvcard/Contact.java @@ -47,13 +47,9 @@ public class Contact extends BaseClass { 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; diff --git a/src/be/nikiroo/jvcard/Data.java b/src/be/nikiroo/jvcard/Data.java index 5c09a13..6336233 100644 --- a/src/be/nikiroo/jvcard/Data.java +++ b/src/be/nikiroo/jvcard/Data.java @@ -146,6 +146,21 @@ public class Data extends BaseClass { 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; diff --git a/src/be/nikiroo/jvcard/resources/StringUtils.java b/src/be/nikiroo/jvcard/resources/StringUtils.java index bfe0d3d..2009dce 100644 --- a/src/be/nikiroo/jvcard/resources/StringUtils.java +++ b/src/be/nikiroo/jvcard/resources/StringUtils.java @@ -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 size */ @@ -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 = ""; diff --git a/src/be/nikiroo/jvcard/resources/Trans.java b/src/be/nikiroo/jvcard/resources/Trans.java index 8dce6f1..c48d6af 100644 --- a/src/be/nikiroo/jvcard/resources/Trans.java +++ b/src/be/nikiroo/jvcard/resources/Trans.java @@ -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, // diff --git a/src/be/nikiroo/jvcard/resources/colors.properties b/src/be/nikiroo/jvcard/resources/colors.properties index b4cf41d..e1b7500 100644 --- a/src/be/nikiroo/jvcard/resources/colors.properties +++ b/src/be/nikiroo/jvcard/resources/colors.properties @@ -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 diff --git a/src/be/nikiroo/jvcard/resources/display.properties b/src/be/nikiroo/jvcard/resources/display.properties index d03d45b..106bdc3 100644 --- a/src/be/nikiroo/jvcard/resources/display.properties +++ b/src/be/nikiroo/jvcard/resources/display.properties @@ -10,3 +10,23 @@ # 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 diff --git a/src/be/nikiroo/jvcard/tui/UiColors.java b/src/be/nikiroo/jvcard/tui/UiColors.java index 3fa83c7..0a46968 100644 --- a/src/be/nikiroo/jvcard/tui/UiColors.java +++ b/src/be/nikiroo/jvcard/tui/UiColors.java @@ -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, // ; /** diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java index fc6a198..4cbbdf7 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java @@ -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 getKeyBindings() { List actions = new LinkedList(); - // 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; diff --git a/src/be/nikiroo/jvcard/tui/panes/FileList.java b/src/be/nikiroo/jvcard/tui/panes/FileList.java index 7065aaf..376aae9 100644 --- a/src/be/nikiroo/jvcard/tui/panes/FileList.java +++ b/src/be/nikiroo/jvcard/tui/panes/FileList.java @@ -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"); diff --git a/src/be/nikiroo/jvcard/tui/panes/MainContent.java b/src/be/nikiroo/jvcard/tui/panes/MainContent.java index 26cd7cb..0d873da 100644 --- a/src/be/nikiroo/jvcard/tui/panes/MainContent.java +++ b/src/be/nikiroo/jvcard/tui/panes/MainContent.java @@ -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. * + *

+ * By default, it will just refresh the data. + *

+ * * @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; } } -- 2.27.0