| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * Version: $Id$ |
| 5 | * |
| 6 | * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a> |
| 7 | * |
| 8 | * License: LGPLv3 or later |
| 9 | * |
| 10 | * Copyright: This module is licensed under the GNU Lesser General |
| 11 | * Public License Version 3. Please see the file "COPYING" in this |
| 12 | * directory for more information about the GNU Lesser General Public |
| 13 | * License Version 3. |
| 14 | * |
| 15 | * Copyright (C) 2015 Kevin Lamonte |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU Lesser General Public License |
| 19 | * as published by the Free Software Foundation; either version 3 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, but |
| 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU Lesser General Public |
| 28 | * License along with this program; if not, see |
| 29 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 30 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 31 | * 02110-1301 USA |
| 32 | */ |
| 33 | package jexer.bits; |
| 34 | |
| 35 | /** |
| 36 | * MnemonicString is used to render a string like "&File" into a highlighted |
| 37 | * 'F' and the rest of 'ile'. To insert a literal '&', use two '&&' |
| 38 | * characters, e.g. "&File && Stuff" would be "File & Stuff" with the first |
| 39 | * 'F' highlighted. |
| 40 | */ |
| 41 | public class MnemonicString { |
| 42 | |
| 43 | /** |
| 44 | * Keyboard shortcut to activate this item |
| 45 | */ |
| 46 | public char shortcut; |
| 47 | |
| 48 | /** |
| 49 | * Location of the highlighted character |
| 50 | */ |
| 51 | public int shortcutIdx = -1; |
| 52 | |
| 53 | /** |
| 54 | * The raw (uncolored) string |
| 55 | */ |
| 56 | public String rawLabel; |
| 57 | |
| 58 | /** |
| 59 | * Public constructor |
| 60 | * |
| 61 | * @param label widget label or title. Label must contain a keyboard |
| 62 | * shortcut, denoted by prefixing a letter with "&", e.g. "&File" |
| 63 | */ |
| 64 | public MnemonicString(String label) { |
| 65 | |
| 66 | // Setup the menu shortcut |
| 67 | String newLabel = ""; |
| 68 | boolean foundAmp = false; |
| 69 | boolean foundShortcut = false; |
| 70 | int shortcutIdx = 0; |
| 71 | for (int i = 0; i < label.length(); i++) { |
| 72 | char c = label.charAt(i); |
| 73 | if (c == '&') { |
| 74 | if (foundAmp == true) { |
| 75 | newLabel += '&'; |
| 76 | shortcutIdx++; |
| 77 | } else { |
| 78 | foundAmp = true; |
| 79 | } |
| 80 | } else { |
| 81 | newLabel += c; |
| 82 | if (foundAmp == true) { |
| 83 | if (foundShortcut == false) { |
| 84 | shortcut = c; |
| 85 | foundAmp = false; |
| 86 | foundShortcut = true; |
| 87 | this.shortcutIdx = shortcutIdx; |
| 88 | } |
| 89 | } else { |
| 90 | shortcutIdx++; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | this.rawLabel = newLabel; |
| 95 | } |
| 96 | } |
| 97 | |