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