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
;
61 private String message
;
62 private boolean error
;
64 public KeyAction(Mode mode
, KeyStroke key
, StringId id
) {
70 public KeyAction(Mode mode
, KeyType keyType
, StringId id
) {
72 this.key
= new KeyStroke(keyType
);
76 public KeyAction(Mode mode
, char car
, StringId id
) {
78 this.key
= new KeyStroke(car
, false, false);
83 * Return the key used to trigger this {@link KeyAction}.
85 * @return the shortcut {@link KeyStroke} to use to invoke this
88 public KeyStroke
getKey() {
93 * Return the associated message if any.
95 * @return the associated message or NULL
97 public String
getMessage() {
102 * Set a message to display to the user. This message will be get after
103 * {@link KeyAction#getObject()} has been called.
108 * TRUE for an error message, FALSE for information
110 public void setMessage(String message
, boolean error
) {
111 this.message
= message
;
116 * Check if the included message ({@link KeyAction#getMessage()}) is an
117 * error message or an information message.
119 * @return TRUE for error, FALSE for information
121 public boolean isError() {
126 * Check if the given {@link KeyStroke} should trigger this action.
129 * the {@link KeyStroke} to check against
131 * @return TRUE if it should
133 public boolean match(KeyStroke mkey
) {
134 if (mkey
== null || key
== null)
137 if (mkey
.getKeyType() == key
.getKeyType()) {
138 if (mkey
.getKeyType() != KeyType
.Character
)
141 return mkey
.getCharacter() == key
.getCharacter();
148 * The mode to change to when this action is completed.
150 * @return the new mode
152 public Mode
getMode() {
157 * Get the associated {@link StringId} or NULL if the action must not be
158 * displayed in the action bar.
160 * @return the {@link StringId} or NULL
162 public StringId
getStringId() {
167 * Get the associated object as a {@link Card} if it is a {@link Card}.
169 * @return the associated {@link Card} or NULL
171 public Card
getCard() {
172 Object o
= getObject();
173 if (o
instanceof Card
)
179 * Get the associated object as a {@link Contact} if it is a {@link Contact}
182 * @return the associated {@link Contact} or NULL
184 public Contact
getContact() {
185 Object o
= getObject();
186 if (o
instanceof Contact
)
192 * Get the associated object as a {@link Data} if it is a {@link Data}.
194 * @return the associated {@link Data} or NULL
196 public Data
getData() {
197 Object o
= getObject();
198 if (o
instanceof Data
)
204 * Return the associated target object. You should use
205 * {@link KeyAction#getCard()}, {@link KeyAction#getContact()} or
206 * {@link KeyAction#getData()} instead if you know the kind of object it is.
210 * You are expected to override this method to return your object, the 3
211 * afore-mentioned methods will use this one as the source.
215 * <b>DO NOT</b> process data here, this method will be called often; this
216 * should only be a <b>getter</b> method.
218 * @return the associated object
220 public Object
getObject() {
225 * The method which is called when the action is performed. You can subclass
226 * it if you want to customise the action (by default, it just accepts the
227 * mode change (see {@link KeyAction#getMode}).
229 * @return false to cancel mode change
231 public boolean onAction() {
236 * Used to callback a function from the menu when the user has to introduce
242 * @return an error message if any
244 public String
callback(String answer
) {
249 * When asking a question to the user, return the question.
251 * @return the question
253 public String
getQuestion() {
258 * When asking a question to the user (not for one-key mode), return the
261 * @return the default answer
263 public String
getDefaultAnswer() {
268 * Translate the given {@link KeyStroke} into a user text {@link String} of
272 * the key to translate
274 * @return the translated text
276 static public String
trans(KeyStroke key
) {
277 String keyTrans
= "";
279 switch (key
.getKeyType()) {
281 if (Main
.isUnicode())
284 keyTrans
= Main
.trans(StringId
.KEY_ENTER
);
287 if (Main
.isUnicode())
290 keyTrans
= Main
.trans(StringId
.KEY_TAB
);
294 keyTrans
= " " + key
.getCharacter() + " ";
297 keyTrans
= "" + key
.getKeyType();
299 if (keyTrans
.length() > width
) {
300 keyTrans
= keyTrans
.substring(0, width
);
301 } else if (keyTrans
.length() < width
) {
303 + new String(new char[width
- keyTrans
.length()])