| 1 | package be.nikiroo.jvcard.tui.panes; |
| 2 | |
| 3 | import java.util.LinkedList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import com.googlecode.lanterna.input.KeyType; |
| 7 | |
| 8 | import be.nikiroo.jvcard.Contact; |
| 9 | import be.nikiroo.jvcard.Data; |
| 10 | import be.nikiroo.jvcard.TypeInfo; |
| 11 | import be.nikiroo.jvcard.i18n.Trans; |
| 12 | import be.nikiroo.jvcard.tui.KeyAction; |
| 13 | import be.nikiroo.jvcard.tui.UiColors; |
| 14 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
| 15 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
| 16 | import be.nikiroo.jvcard.tui.StringUtils; |
| 17 | import be.nikiroo.jvcard.tui.UiColors.Element; |
| 18 | |
| 19 | public class ContactDetailsRaw extends MainContentList { |
| 20 | private Contact contact; |
| 21 | private int mode; |
| 22 | |
| 23 | public ContactDetailsRaw(Contact contact) { |
| 24 | super(null, null); |
| 25 | |
| 26 | this.contact = contact; |
| 27 | this.mode = 0; |
| 28 | |
| 29 | for (int i = 0; i < contact.getContent().size(); i++) { |
| 30 | addItem("[detail line]"); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | protected List<TextPart> getLabel(int index, int width, boolean selected, |
| 36 | boolean focused) { |
| 37 | // TODO: from ini file? |
| 38 | int SIZE_COL_1 = 15; |
| 39 | |
| 40 | Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED |
| 41 | : Element.CONTACT_LINE; |
| 42 | Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED |
| 43 | : Element.CONTACT_LINE_SEPARATOR; |
| 44 | Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED |
| 45 | : Element.CONTACT_LINE_DIRTY; |
| 46 | |
| 47 | Data data = contact.getContent().get(index); |
| 48 | |
| 49 | List<TextPart> parts = new LinkedList<TextPart>(); |
| 50 | if (data.isDirty()) { |
| 51 | parts.add(new TextPart(" ", el)); |
| 52 | parts.add(new TextPart("*", elDirty)); |
| 53 | } else { |
| 54 | parts.add(new TextPart(" ", elSep)); |
| 55 | } |
| 56 | String name = " " + data.getName() + " "; |
| 57 | String value = null; |
| 58 | |
| 59 | StringBuilder valueBuilder = new StringBuilder(" "); |
| 60 | switch (mode) { |
| 61 | case 0: |
| 62 | valueBuilder.append(data.getValue()); |
| 63 | if (data.getGroup() != null && data.getGroup().length() > 0) { |
| 64 | valueBuilder.append("("); |
| 65 | valueBuilder.append(data.getGroup()); |
| 66 | valueBuilder.append(")"); |
| 67 | } |
| 68 | break; |
| 69 | case 1: |
| 70 | for (TypeInfo type : data.getTypes()) { |
| 71 | if (valueBuilder.length() > 1) |
| 72 | valueBuilder.append(", "); |
| 73 | valueBuilder.append(type.getName()); |
| 74 | valueBuilder.append(": "); |
| 75 | valueBuilder.append(type.getValue()); |
| 76 | } |
| 77 | break; |
| 78 | } |
| 79 | valueBuilder.append(" "); |
| 80 | |
| 81 | value = valueBuilder.toString(); |
| 82 | |
| 83 | name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode()); |
| 84 | value = StringUtils.sanitize(value, UiColors.getInstance().isUnicode()); |
| 85 | |
| 86 | name = StringUtils.padString(name, SIZE_COL_1); |
| 87 | value = StringUtils.padString(value, width - SIZE_COL_1 |
| 88 | - getSeparator().length() - 2); |
| 89 | |
| 90 | parts.add(new TextPart(name, el)); |
| 91 | parts.add(new TextPart(getSeparator(), elSep)); |
| 92 | parts.add(new TextPart(value, el)); |
| 93 | |
| 94 | return parts; |
| 95 | }; |
| 96 | |
| 97 | @Override |
| 98 | public DataType getDataType() { |
| 99 | return DataType.DATA; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public List<KeyAction> getKeyBindings() { |
| 104 | // TODO Auto-generated method stub |
| 105 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
| 106 | |
| 107 | // TODO: add, remove |
| 108 | actions.add(new KeyAction(Mode.EDIT_DETAIL, 'd', Trans.StringId.DUMMY) { |
| 109 | @Override |
| 110 | public Object getObject() { |
| 111 | return contact.getContent().get(getSelectedIndex()); |
| 112 | } |
| 113 | }); |
| 114 | actions.add(new KeyAction(Mode.NONE, KeyType.Tab, |
| 115 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { |
| 116 | @Override |
| 117 | public boolean onAction() { |
| 118 | mode++; |
| 119 | if (mode > 1) |
| 120 | mode = 0; |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | return actions; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String getTitle() { |
| 131 | String title = null; |
| 132 | |
| 133 | if (contact != null) { |
| 134 | title = contact.getPreferredDataValue("FN"); |
| 135 | if (title == null || title.length() == 0) |
| 136 | title = contact.getPreferredDataValue("N"); |
| 137 | } |
| 138 | |
| 139 | return title; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public String move(int x, int y) { |
| 144 | // TODO Auto-generated method stub |
| 145 | return null; |
| 146 | } |
| 147 | } |