Fix --noutf, fix onAction being called to many times, lot of small fixes
[jvcard.git] / src / be / nikiroo / jvcard / i18n / Trans.java
1 package be.nikiroo.jvcard.i18n;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import com.googlecode.lanterna.input.KeyStroke;
7
8 import be.nikiroo.jvcard.tui.UiColors;
9
10 /**
11 * This class manages the translation of {@link Trans#StringId}s into
12 * user-understandable text.
13 *
14 * @author niki
15 *
16 */
17 public class Trans {
18 static private Object lock = new Object();
19 static private Trans instance = null;
20
21 private Map<StringId, String> map = null;
22
23 /**
24 * An enum representing information to be translated to the user.
25 *
26 * @author niki
27 *
28 */
29 public enum StringId {
30 DUMMY, // <-- TODO : remove
31 KEY_ACTION_BACK, KEY_ACTION_HELP, // MainWindow
32 KEY_ACTION_VIEW_CARD, // FileList
33 KEY_ACTION_VIEW_CONTACT, KEY_ACTION_EDIT_CONTACT, KEY_ACTION_SAVE_CARD, KEY_ACTION_DELETE_CONTACT, KEY_ACTION_SWITCH_FORMAT, // ContactList
34 DEAULT_FIELD_SEPARATOR, DEAULT_FIELD_SEPARATOR_NOUTF, // MainContentList
35 NULL; // Special usage
36
37 public String trans() {
38 return Trans.getInstance().trans(this);
39 }
40 };
41
42 /**
43 * Get the (unique) instance of this class.
44 *
45 * @return the (unique) instance
46 */
47 static public Trans getInstance() {
48 synchronized (lock) {
49 if (instance == null)
50 instance = new Trans();
51 }
52
53 return instance;
54 }
55
56 /**
57 * Translate the given {@link StringId} into user text.
58 *
59 * @param stringId
60 * the ID to translate
61 *
62 * @return the translated text
63 */
64 public String trans(StringId stringId) {
65 StringId id = stringId;
66 if (!UiColors.getInstance().isUnicode()) {
67 try {
68 id = StringId.valueOf(stringId.toString() + "_NOUTF");
69 } catch (IllegalArgumentException iae) {
70 // no special _NOUTF version found
71 }
72 }
73
74 if (map.containsKey(id)) {
75 return map.get(id);
76 }
77
78 return id.toString();
79 }
80
81 /**
82 * Translate the given {@link KeyStroke} into a user text {@link String} of
83 * size 3.
84 *
85 * @param key
86 * the key to translate
87 *
88 * @return the translated text
89 */
90 public String trans(KeyStroke key) {
91 String keyTrans = "";
92
93 switch (key.getKeyType()) {
94 case Enter:
95 if (UiColors.getInstance().isUnicode())
96 keyTrans = " ⤶ ";
97 else
98 keyTrans = "ENT";
99 break;
100 case Tab:
101 if (UiColors.getInstance().isUnicode())
102 keyTrans = " ↹ ";
103 else
104 keyTrans = "TAB";
105
106 break;
107 case Character:
108 keyTrans = " " + key.getCharacter() + " ";
109 break;
110 default:
111 keyTrans = "" + key.getKeyType();
112 int width = 3;
113 if (keyTrans.length() > width) {
114 keyTrans = keyTrans.substring(0, width);
115 } else if (keyTrans.length() < width) {
116 keyTrans = keyTrans
117 + new String(new char[width - keyTrans.length()])
118 .replace('\0', ' ');
119 }
120 break;
121 }
122
123 return keyTrans;
124 }
125
126 private Trans() {
127 map = new HashMap<StringId, String>();
128
129 // TODO: get from a file instead?
130 map.put(StringId.NULL, "");
131 map.put(StringId.DUMMY, "[dummy]");
132 // we could use: " ", "┃", "│"...
133 map.put(StringId.DEAULT_FIELD_SEPARATOR, "┃");
134 map.put(StringId.DEAULT_FIELD_SEPARATOR_NOUTF, "|");
135 map.put(StringId.KEY_ACTION_BACK, "Back");
136 map.put(StringId.KEY_ACTION_HELP, "Help");
137 map.put(StringId.KEY_ACTION_VIEW_CONTACT, "Open");
138 map.put(StringId.KEY_ACTION_VIEW_CARD, "Open");
139 map.put(StringId.KEY_ACTION_EDIT_CONTACT, "Edit");
140 map.put(StringId.KEY_ACTION_DELETE_CONTACT, "Delete");
141 map.put(StringId.KEY_ACTION_SWITCH_FORMAT, "Change view");
142 }
143 }