1347b0ee11698d94a18180142e28ea605c44b99c
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetailsRaw.java
1 package be.nikiroo.jvcard.tui.panes;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import be.nikiroo.jvcard.Contact;
7 import be.nikiroo.jvcard.Data;
8 import be.nikiroo.jvcard.TypeInfo;
9 import be.nikiroo.jvcard.i18n.Trans;
10 import be.nikiroo.jvcard.tui.KeyAction;
11 import be.nikiroo.jvcard.tui.KeyAction.DataType;
12 import be.nikiroo.jvcard.tui.KeyAction.Mode;
13 import be.nikiroo.jvcard.tui.StringUtils;
14 import be.nikiroo.jvcard.tui.UiColors;
15 import be.nikiroo.jvcard.tui.UiColors.Element;
16
17 import com.googlecode.lanterna.input.KeyType;
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.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.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 (int indexType = 0; indexType < data.size(); indexType++) {
71 TypeInfo type = data.get(indexType);
72 if (valueBuilder.length() > 1)
73 valueBuilder.append(", ");
74 valueBuilder.append(type.getName());
75 valueBuilder.append(": ");
76 valueBuilder.append(type.getValue());
77 }
78 break;
79 }
80 valueBuilder.append(" ");
81
82 value = valueBuilder.toString();
83
84 name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode());
85 value = StringUtils.sanitize(value, UiColors.getInstance().isUnicode());
86
87 name = StringUtils.padString(name, SIZE_COL_1);
88 value = StringUtils.padString(value, width - SIZE_COL_1
89 - getSeparator().length() - 2);
90
91 parts.add(new TextPart(name, el));
92 parts.add(new TextPart(getSeparator(), elSep));
93 parts.add(new TextPart(value, el));
94
95 return parts;
96 };
97
98 @Override
99 public DataType getDataType() {
100 return DataType.DATA;
101 }
102
103 @Override
104 public List<KeyAction> getKeyBindings() {
105 // TODO Auto-generated method stub
106 List<KeyAction> actions = new LinkedList<KeyAction>();
107
108 // TODO: add, remove
109 actions.add(new KeyAction(Mode.ASK_USER, KeyType.Enter,
110 Trans.StringId.DUMMY) {
111 @Override
112 public Object getObject() {
113 return contact.get(getSelectedIndex());
114 }
115
116 @Override
117 public String getQuestion() {
118 Data data = getData();
119 if (data != null) {
120 return data.getName();
121 }
122
123 return null;
124 }
125
126 @Override
127 public String getDefaultAnswer() {
128 Data data = getData();
129 if (data != null) {
130 return data.getValue();
131 }
132
133 return null;
134 }
135
136 @Override
137 public String callback(String answer) {
138 Data data = getData();
139 if (data != null) {
140 data.setValue(answer);
141 return null;
142 }
143
144 // TODO: i18n
145 return "Cannot modify value";
146 }
147 });
148 actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
149 Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
150 @Override
151 public boolean onAction() {
152 mode++;
153 if (mode > 1)
154 mode = 0;
155
156 return false;
157 }
158 });
159
160 return actions;
161 }
162
163 @Override
164 public String getTitle() {
165 String title = null;
166
167 if (contact != null) {
168 title = contact.getPreferredDataValue("FN");
169 if (title == null || title.length() == 0)
170 title = contact.getPreferredDataValue("N");
171 }
172
173 return title;
174 }
175
176 @Override
177 public String move(int x, int y) {
178 // TODO Auto-generated method stub
179 return null;
180 }
181 }