Initial commit
[jvcard.git] / src / be / nikiroo / jvcard / tui / KeyAction.java
1 package be.nikiroo.jvcard.tui;
2
3 import be.nikiroo.jvcard.Card;
4 import be.nikiroo.jvcard.Contact;
5 import be.nikiroo.jvcard.Data;
6 import be.nikiroo.jvcard.i18n.Trans.StringId;
7
8 import com.googlecode.lanterna.input.KeyStroke;
9 import com.googlecode.lanterna.input.KeyType;
10
11 /**
12 * This class represents a keybinding; it encapsulates data about the actual key
13 * to press and the associated action to take.
14 *
15 * You are expected to subclass it if you want to create a custom action.
16 *
17 * @author niki
18 *
19 */
20 public class KeyAction {
21 /**
22 * The keybinding mode that will be triggered by this action.
23 *
24 * @author niki
25 *
26 */
27 enum Mode {
28 NONE, MOVE, BACK, HELP, CONTACT_LIST, CONTACT_DETAILS, SWICTH_FORMAT,
29 }
30
31 enum DataType {
32 CONTACT, CARD, DATA, NONE
33 }
34
35 private StringId id;
36 private KeyStroke key;
37 private Mode mode;
38
39 public KeyAction(Mode mode, KeyStroke key, StringId id) {
40 this.id = id;
41 this.key = key;
42 this.mode = mode;
43 }
44
45 public KeyAction(Mode mode, KeyType keyType, StringId id) {
46 this.id = id;
47 this.key = new KeyStroke(keyType);
48 this.mode = mode;
49 }
50
51 public KeyAction(Mode mode, char car, StringId id) {
52 this.id = id;
53 this.key = new KeyStroke(car, false, false);
54 this.mode = mode;
55 }
56
57 /**
58 * Return the key used to trigger this {@link KeyAction} or '\0' if none.
59 * Also check the special key ({@link KeyAction#getKkey}) if any.
60 *
61 * @return the shortcut character to use to invoke this {@link KeyAction} or
62 * '\0'
63 */
64 public KeyStroke getKey() {
65 return key;
66 }
67
68 // check if the given key should trigger this action
69 public boolean match(KeyStroke mkey) {
70 if (mkey == null || key == null)
71 return false;
72
73 if (mkey.getKeyType() == key.getKeyType()) {
74 if (mkey.getKeyType() != KeyType.Character)
75 return true;
76
77 return mkey.getCharacter() == key.getCharacter();
78 }
79
80 return false;
81 }
82
83 /**
84 * Return the kind of key this {@link KeyAction } is linked to. Will be
85 * {@link KeyType#NormalKey} if only normal keys can invoke this
86 * {@link KeyAction}. Also check the normal key ({@link KeyAction#getKey})
87 * if any.
88 *
89 * @return the special shortcut key to use to invoke this {@link KeyAction}
90 * or {@link KeyType#NormalKey}
91 */
92
93 /**
94 * The mode to change to when this action is completed.
95 *
96 * @return the new mode
97 */
98 public Mode getMode() {
99 return mode;
100 }
101
102 public StringId getStringId() {
103 return id;
104 }
105
106 public Card getCard() {
107 Object o = getObject();
108 if (o instanceof Card)
109 return (Card) o;
110 return null;
111 }
112
113 public Contact getContact() {
114 Object o = getObject();
115 if (o instanceof Contact)
116 return (Contact) o;
117 return null;
118 }
119
120 public Data getData() {
121 Object o = getObject();
122 if (o instanceof Data)
123 return (Data) o;
124 return null;
125 }
126
127 // override this one if needed
128 public Object getObject() {
129 return null;
130 }
131
132 /**
133 * The method which is called when the action is performed. You can subclass
134 * it if you want to customize the action (by default, it just accepts the
135 * mode change (see {@link KeyAction#getMode}).
136 *
137 * @return false to cancel mode change
138 */
139 public boolean onAction() {
140 return true;
141 }
142 }