| 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.i18n.Trans.StringId; |
| 9 | |
| 10 | import com.googlecode.lanterna.input.KeyStroke; |
| 11 | import com.googlecode.lanterna.input.KeyType; |
| 12 | |
| 13 | /** |
| 14 | * This class represents a keybinding; it encapsulates data about the actual key |
| 15 | * to press and the associated action to take. |
| 16 | * |
| 17 | * You are expected to subclass it if you want to create a custom action. |
| 18 | * |
| 19 | * @author niki |
| 20 | * |
| 21 | */ |
| 22 | public class KeyAction { |
| 23 | /** |
| 24 | * The keybinding mode that will be triggered by this action. |
| 25 | * |
| 26 | * @author niki |
| 27 | * |
| 28 | */ |
| 29 | public enum Mode { |
| 30 | NONE, MOVE, BACK, HELP, FILE_LIST, CONTACT_LIST, CONTACT_DETAILS_RAW, CONTACT_DETAILS, ASK_USER, ASK_USER_KEY, |
| 31 | } |
| 32 | |
| 33 | public enum DataType { |
| 34 | /** |
| 35 | * A list of Card {@link File}s. |
| 36 | */ |
| 37 | CARD_FILES, |
| 38 | /** |
| 39 | * Contains a list of contacts. |
| 40 | */ |
| 41 | CARD, |
| 42 | /** |
| 43 | * All the known informations about a specific contact person or |
| 44 | * company. |
| 45 | */ |
| 46 | CONTACT, |
| 47 | /** |
| 48 | * An information about a contact. |
| 49 | */ |
| 50 | DATA, |
| 51 | /** |
| 52 | * Empty. |
| 53 | */ |
| 54 | NONE |
| 55 | } |
| 56 | |
| 57 | private StringId id; |
| 58 | private KeyStroke key; |
| 59 | private Mode mode; |
| 60 | |
| 61 | public KeyAction(Mode mode, KeyStroke key, StringId id) { |
| 62 | this.id = id; |
| 63 | this.key = key; |
| 64 | this.mode = mode; |
| 65 | } |
| 66 | |
| 67 | public KeyAction(Mode mode, KeyType keyType, StringId id) { |
| 68 | this.id = id; |
| 69 | this.key = new KeyStroke(keyType); |
| 70 | this.mode = mode; |
| 71 | } |
| 72 | |
| 73 | public KeyAction(Mode mode, char car, StringId id) { |
| 74 | this.id = id; |
| 75 | this.key = new KeyStroke(car, false, false); |
| 76 | this.mode = mode; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Return the key used to trigger this {@link KeyAction} or '\0' if none. |
| 81 | * Also check the special key ({@link KeyAction#getKkey}) if any. |
| 82 | * |
| 83 | * @return the shortcut character to use to invoke this {@link KeyAction} or |
| 84 | * '\0' |
| 85 | */ |
| 86 | public KeyStroke getKey() { |
| 87 | return key; |
| 88 | } |
| 89 | |
| 90 | // check if the given key should trigger this action |
| 91 | public boolean match(KeyStroke mkey) { |
| 92 | if (mkey == null || key == null) |
| 93 | return false; |
| 94 | |
| 95 | if (mkey.getKeyType() == key.getKeyType()) { |
| 96 | if (mkey.getKeyType() != KeyType.Character) |
| 97 | return true; |
| 98 | |
| 99 | return mkey.getCharacter() == key.getCharacter(); |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Return the kind of key this {@link KeyAction } is linked to. Will be |
| 107 | * {@link KeyType#NormalKey} if only normal keys can invoke this |
| 108 | * {@link KeyAction}. Also check the normal key ({@link KeyAction#getKey}) |
| 109 | * if any. |
| 110 | * |
| 111 | * @return the special shortcut key to use to invoke this {@link KeyAction} |
| 112 | * or {@link KeyType#NormalKey} |
| 113 | */ |
| 114 | |
| 115 | /** |
| 116 | * The mode to change to when this action is completed. |
| 117 | * |
| 118 | * @return the new mode |
| 119 | */ |
| 120 | public Mode getMode() { |
| 121 | return mode; |
| 122 | } |
| 123 | |
| 124 | public StringId getStringId() { |
| 125 | return id; |
| 126 | } |
| 127 | |
| 128 | public Card getCard() { |
| 129 | Object o = getObject(); |
| 130 | if (o instanceof Card) |
| 131 | return (Card) o; |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | public Contact getContact() { |
| 136 | Object o = getObject(); |
| 137 | if (o instanceof Contact) |
| 138 | return (Contact) o; |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | public Data getData() { |
| 143 | Object o = getObject(); |
| 144 | if (o instanceof Data) |
| 145 | return (Data) o; |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | // override this one if needed, DO NOT process here as it will be call a lot |
| 150 | public Object getObject() { |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * The method which is called when the action is performed. You can subclass |
| 156 | * it if you want to customise the action (by default, it just accepts the |
| 157 | * mode change (see {@link KeyAction#getMode}). |
| 158 | * |
| 159 | * @return false to cancel mode change |
| 160 | */ |
| 161 | public boolean onAction() { |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Used to callback a function from the menu when the user has to introduce |
| 167 | * some text. |
| 168 | * |
| 169 | * @param answer |
| 170 | * the user answer |
| 171 | * |
| 172 | * @return an error message if any |
| 173 | */ |
| 174 | public String callback(String answer) { |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * When asking a question to the user, return the question. |
| 180 | * |
| 181 | * @return the question |
| 182 | */ |
| 183 | public String getQuestion() { |
| 184 | return null; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * When asking a question to the user (not for one-key mode), return the |
| 189 | * default answer. |
| 190 | * |
| 191 | * @return the default answer |
| 192 | */ |
| 193 | public String getDefaultAnswer() { |
| 194 | return null; |
| 195 | } |
| 196 | } |