New launcher class to start all 3 modes:
[jvcard.git] / src / be / nikiroo / jvcard / tui / KeyAction.java
1 package be.nikiroo.jvcard.tui;
2
3 import java.io.File;
4
5 import be.nikiroo.jvcard.Card;
6 import be.nikiroo.jvcard.Contact;
7 import be.nikiroo.jvcard.Data;
8 import be.nikiroo.jvcard.launcher.Main;
9 import be.nikiroo.jvcard.resources.Trans.StringId;
10
11 import com.googlecode.lanterna.input.KeyStroke;
12 import com.googlecode.lanterna.input.KeyType;
13
14 /**
15 * This class represents a keybinding; it encapsulates data about the actual key
16 * to press and the associated action to take.
17 *
18 * You are expected to subclass it if you want to create a custom action.
19 *
20 * @author niki
21 *
22 */
23 public class KeyAction {
24 /**
25 * The keybinding mode that will be triggered by this action.
26 *
27 * @author niki
28 *
29 */
30 public enum Mode {
31 NONE, MOVE, BACK, HELP, FILE_LIST, CONTACT_LIST, CONTACT_DETAILS_RAW, CONTACT_DETAILS, ASK_USER, ASK_USER_KEY,
32 }
33
34 public enum DataType {
35 /**
36 * A list of Card {@link File}s.
37 */
38 CARD_FILES,
39 /**
40 * Contains a list of contacts.
41 */
42 CARD,
43 /**
44 * All the known informations about a specific contact person or
45 * company.
46 */
47 CONTACT,
48 /**
49 * An information about a contact.
50 */
51 DATA,
52 /**
53 * Empty.
54 */
55 NONE
56 }
57
58 private StringId id;
59 private KeyStroke key;
60 private Mode mode;
61
62 public KeyAction(Mode mode, KeyStroke key, StringId id) {
63 this.id = id;
64 this.key = key;
65 this.mode = mode;
66 }
67
68 public KeyAction(Mode mode, KeyType keyType, StringId id) {
69 this.id = id;
70 this.key = new KeyStroke(keyType);
71 this.mode = mode;
72 }
73
74 public KeyAction(Mode mode, char car, StringId id) {
75 this.id = id;
76 this.key = new KeyStroke(car, false, false);
77 this.mode = mode;
78 }
79
80 /**
81 * Return the key used to trigger this {@link KeyAction} or '\0' if none.
82 * Also check the special key ({@link KeyAction#getKkey}) if any.
83 *
84 * @return the shortcut character to use to invoke this {@link KeyAction} or
85 * '\0'
86 */
87 public KeyStroke getKey() {
88 return key;
89 }
90
91 // check if the given key should trigger this action
92 public boolean match(KeyStroke mkey) {
93 if (mkey == null || key == null)
94 return false;
95
96 if (mkey.getKeyType() == key.getKeyType()) {
97 if (mkey.getKeyType() != KeyType.Character)
98 return true;
99
100 return mkey.getCharacter() == key.getCharacter();
101 }
102
103 return false;
104 }
105
106 /**
107 * Return the kind of key this {@link KeyAction } is linked to. Will be
108 * {@link KeyType#NormalKey} if only normal keys can invoke this
109 * {@link KeyAction}. Also check the normal key ({@link KeyAction#getKey})
110 * if any.
111 *
112 * @return the special shortcut key to use to invoke this {@link KeyAction}
113 * or {@link KeyType#NormalKey}
114 */
115
116 /**
117 * The mode to change to when this action is completed.
118 *
119 * @return the new mode
120 */
121 public Mode getMode() {
122 return mode;
123 }
124
125 public StringId getStringId() {
126 return id;
127 }
128
129 public Card getCard() {
130 Object o = getObject();
131 if (o instanceof Card)
132 return (Card) o;
133 return null;
134 }
135
136 public Contact getContact() {
137 Object o = getObject();
138 if (o instanceof Contact)
139 return (Contact) o;
140 return null;
141 }
142
143 public Data getData() {
144 Object o = getObject();
145 if (o instanceof Data)
146 return (Data) o;
147 return null;
148 }
149
150 // override this one if needed, DO NOT process here as it will be call a lot
151 public Object getObject() {
152 return null;
153 }
154
155 /**
156 * The method which is called when the action is performed. You can subclass
157 * it if you want to customise the action (by default, it just accepts the
158 * mode change (see {@link KeyAction#getMode}).
159 *
160 * @return false to cancel mode change
161 */
162 public boolean onAction() {
163 return true;
164 }
165
166 /**
167 * Used to callback a function from the menu when the user has to introduce
168 * some text.
169 *
170 * @param answer
171 * the user answer
172 *
173 * @return an error message if any
174 */
175 public String callback(String answer) {
176 return null;
177 }
178
179 /**
180 * When asking a question to the user, return the question.
181 *
182 * @return the question
183 */
184 public String getQuestion() {
185 return null;
186 }
187
188 /**
189 * When asking a question to the user (not for one-key mode), return the
190 * default answer.
191 *
192 * @return the default answer
193 */
194 public String getDefaultAnswer() {
195 return null;
196 }
197
198 /**
199 * Translate the given {@link KeyStroke} into a user text {@link String} of
200 * size 3.
201 *
202 * @param key
203 * the key to translate
204 *
205 * @return the translated text
206 */
207 static public String trans(KeyStroke key) {
208 String keyTrans = "";
209
210 switch (key.getKeyType()) {
211 case Enter:
212 if (Main.isUnicode())
213 keyTrans = " ⤶ ";
214 else
215 keyTrans = Main.trans(StringId.KEY_ENTER);
216 break;
217 case Tab:
218 if (Main.isUnicode())
219 keyTrans = " ↹ ";
220 else
221 keyTrans = Main.trans(StringId.KEY_TAB);
222
223 break;
224 case Character:
225 keyTrans = " " + key.getCharacter() + " ";
226 break;
227 default:
228 keyTrans = "" + key.getKeyType();
229 int width = 3;
230 if (keyTrans.length() > width) {
231 keyTrans = keyTrans.substring(0, width);
232 } else if (keyTrans.length() < width) {
233 keyTrans = keyTrans
234 + new String(new char[width - keyTrans.length()])
235 .replace('\0', ' ');
236 }
237 break;
238 }
239
240 return keyTrans;
241 }
242 }