f39b10324f0eeb8d3fcd62d44fc664a28e791e85
1 package be
.nikiroo
.jvcard
.tui
;
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
;
11 import com
.googlecode
.lanterna
.input
.KeyStroke
;
12 import com
.googlecode
.lanterna
.input
.KeyType
;
15 * This class represents a keybinding; it encapsulates data about the actual key
16 * to press and the associated action to take.
18 * You are expected to subclass it if you want to create a custom action.
23 public class KeyAction
{
25 * The keybinding mode that will be triggered by this action.
31 NONE
, MOVE
, BACK
, HELP
, FILE_LIST
, CONTACT_LIST
, CONTACT_DETAILS_RAW
, CONTACT_DETAILS
, ASK_USER
, ASK_USER_KEY
,
34 public enum DataType
{
36 * A list of Card {@link File}s.
40 * Contains a list of contacts.
44 * All the known informations about a specific contact person or
49 * An information about a contact.
59 private KeyStroke key
;
62 public KeyAction(Mode mode
, KeyStroke key
, StringId id
) {
68 public KeyAction(Mode mode
, KeyType keyType
, StringId id
) {
70 this.key
= new KeyStroke(keyType
);
74 public KeyAction(Mode mode
, char car
, StringId id
) {
76 this.key
= new KeyStroke(car
, false, false);
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.
84 * @return the shortcut character to use to invoke this {@link KeyAction} or
87 public KeyStroke
getKey() {
91 // check if the given key should trigger this action
92 public boolean match(KeyStroke mkey
) {
93 if (mkey
== null || key
== null)
96 if (mkey
.getKeyType() == key
.getKeyType()) {
97 if (mkey
.getKeyType() != KeyType
.Character
)
100 return mkey
.getCharacter() == key
.getCharacter();
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})
112 * @return the special shortcut key to use to invoke this {@link KeyAction}
113 * or {@link KeyType#NormalKey}
117 * The mode to change to when this action is completed.
119 * @return the new mode
121 public Mode
getMode() {
125 public StringId
getStringId() {
129 public Card
getCard() {
130 Object o
= getObject();
131 if (o
instanceof Card
)
136 public Contact
getContact() {
137 Object o
= getObject();
138 if (o
instanceof Contact
)
143 public Data
getData() {
144 Object o
= getObject();
145 if (o
instanceof Data
)
150 // override this one if needed, DO NOT process here as it will be call a lot
151 public Object
getObject() {
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}).
160 * @return false to cancel mode change
162 public boolean onAction() {
167 * Used to callback a function from the menu when the user has to introduce
173 * @return an error message if any
175 public String
callback(String answer
) {
180 * When asking a question to the user, return the question.
182 * @return the question
184 public String
getQuestion() {
189 * When asking a question to the user (not for one-key mode), return the
192 * @return the default answer
194 public String
getDefaultAnswer() {
199 * Translate the given {@link KeyStroke} into a user text {@link String} of
203 * the key to translate
205 * @return the translated text
207 static public String
trans(KeyStroke key
) {
208 String keyTrans
= "";
210 switch (key
.getKeyType()) {
212 if (Main
.isUnicode())
215 keyTrans
= Main
.trans(StringId
.KEY_ENTER
);
218 if (Main
.isUnicode())
221 keyTrans
= Main
.trans(StringId
.KEY_TAB
);
225 keyTrans
= " " + key
.getCharacter() + " ";
228 keyTrans
= "" + key
.getKeyType();
230 if (keyTrans
.length() > width
) {
231 keyTrans
= keyTrans
.substring(0, width
);
232 } else if (keyTrans
.length() < width
) {
234 + new String(new char[width
- keyTrans
.length()])