KeyAction management now more generic
[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 }
ae22c247 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
ae22c247
NR
108 actions.add(new KeyAction(Mode.ASK_USER , KeyType.Enter,
109 Trans.StringId.DUMMY) {
bcb54330
NR
110 @Override
111 public Object getObject() {
112 return contact.getContent().get(getSelectedIndex());
113 }
ae22c247
NR
114
115 @Override
116 public String getQuestion() {
117 Data data = getData();
118 if (data != null) {
119 return data.getName();
120 }
121
122 return null;
123 }
124
125 @Override
126 public String getDefaultAnswer() {
127 Data data = getData();
128 if (data != null) {
129 return data.getValue();
130 }
131
132 return null;
133 }
134
135 @Override
136 public String callback(String answer) {
137 Data data = getData();
138 if (data != null) {
139 data.setValue(answer);
140 return null;
141 }
142
143 // TODO: i18n
144 return "Cannot modify value";
145 }
bcb54330
NR
146 });
147 actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
148 Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
149 @Override
150 public boolean onAction() {
151 mode++;
152 if (mode > 1)
153 mode = 0;
154
155 return false;
156 }
157 });
158
159 return actions;
fae07ea7
NR
160 }
161
fae07ea7
NR
162 @Override
163 public String getTitle() {
0b0b2b0f
NR
164 String title = null;
165
166 if (contact != null) {
167 title = contact.getPreferredDataValue("FN");
168 if (title == null || title.length() == 0)
169 title = contact.getPreferredDataValue("N");
170 }
171
172 return title;
fae07ea7
NR
173 }
174
175 @Override
176 public String move(int x, int y) {
177 // TODO Auto-generated method stub
178 return null;
179 }
180}