Add text-image control and separate Edit/View contact
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetailsRaw.java
CommitLineData
fae07ea7
NR
1package be.nikiroo.jvcard.tui.panes;
2
bcb54330 3import java.util.LinkedList;
fae07ea7
NR
4import java.util.List;
5
bcb54330
NR
6import com.googlecode.lanterna.input.KeyType;
7
fae07ea7
NR
8import be.nikiroo.jvcard.Contact;
9import be.nikiroo.jvcard.Data;
bcb54330
NR
10import be.nikiroo.jvcard.TypeInfo;
11import be.nikiroo.jvcard.i18n.Trans;
fae07ea7 12import be.nikiroo.jvcard.tui.KeyAction;
296a0b75 13import be.nikiroo.jvcard.tui.UiColors;
fae07ea7
NR
14import be.nikiroo.jvcard.tui.KeyAction.DataType;
15import be.nikiroo.jvcard.tui.KeyAction.Mode;
bcb54330
NR
16import be.nikiroo.jvcard.tui.StringUtils;
17import be.nikiroo.jvcard.tui.UiColors.Element;
fae07ea7 18
296a0b75 19public class ContactDetailsRaw extends MainContentList {
fae07ea7 20 private Contact contact;
bcb54330 21 private int mode;
fae07ea7 22
296a0b75 23 public ContactDetailsRaw(Contact contact) {
bcb54330 24 super(null, null);
fae07ea7
NR
25
26 this.contact = contact;
bcb54330 27 this.mode = 0;
fae07ea7 28
bcb54330
NR
29 for (int i = 0; i < contact.getContent().size(); i++) {
30 addItem("[detail line]");
fae07ea7
NR
31 }
32 }
33
bcb54330
NR
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
296a0b75
NR
83 name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode());
84 value = StringUtils.sanitize(value, UiColors.getInstance().isUnicode());
85
bcb54330
NR
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
fae07ea7
NR
97 @Override
98 public DataType getDataType() {
bcb54330 99 return DataType.DATA;
fae07ea7 100 }
f04d8b1c 101
fae07ea7
NR
102 @Override
103 public List<KeyAction> getKeyBindings() {
104 // TODO Auto-generated method stub
bcb54330
NR
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;
fae07ea7
NR
127 }
128
fae07ea7
NR
129 @Override
130 public String getTitle() {
0b0b2b0f
NR
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;
fae07ea7
NR
140 }
141
142 @Override
143 public String move(int x, int y) {
144 // TODO Auto-generated method stub
145 return null;
146 }
147}