Fix excess redraws
[nikiroo-utils.git] / src / jexer / bits / MnemonicString.java
CommitLineData
df8de03f
KL
1/**
2 * Jexer - Java Text User Interface
3 *
df8de03f
KL
4 * License: LGPLv3 or later
5 *
7b5261bc
KL
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.
df8de03f
KL
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
7b5261bc
KL
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
df8de03f
KL
30 */
31package 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 */
48e27807 39public final class MnemonicString {
df8de03f
KL
40
41 /**
7b5261bc 42 * Keyboard shortcut to activate this item.
df8de03f 43 */
7b5261bc 44 private char shortcut;
df8de03f 45
fca67db0
KL
46 /**
47 * Get the keyboard shortcut character.
48 *
49 * @return the highlighted character
50 */
51 public char getShortcut() {
52 return shortcut;
53 }
54
df8de03f 55 /**
7b5261bc 56 * Location of the highlighted character.
df8de03f 57 */
7b5261bc 58 private int shortcutIdx = -1;
df8de03f 59
48e27807
KL
60 /**
61 * Get location of the highlighted character.
62 *
63 * @return location of the highlighted character
64 */
65 public int getShortcutIdx() {
66 return shortcutIdx;
67 }
68
df8de03f 69 /**
7b5261bc 70 * The raw (uncolored) string.
df8de03f 71 */
7b5261bc 72 private String rawLabel;
df8de03f 73
48e27807
KL
74 /**
75 * Get the raw (uncolored) string.
76 *
77 * @return the raw (uncolored) string
78 */
79 public String getRawLabel() {
80 return rawLabel;
81 }
82
df8de03f 83 /**
7b5261bc 84 * Public constructor.
df8de03f
KL
85 *
86 * @param label widget label or title. Label must contain a keyboard
87 * shortcut, denoted by prefixing a letter with "&", e.g. "&File"
88 */
7b5261bc 89 public MnemonicString(final String label) {
df8de03f 90
7b5261bc
KL
91 // Setup the menu shortcut
92 String newLabel = "";
93 boolean foundAmp = false;
94 boolean foundShortcut = false;
95 int scanShortcutIdx = 0;
96 for (int i = 0; i < label.length(); i++) {
97 char c = label.charAt(i);
98 if (c == '&') {
99 if (foundAmp) {
100 newLabel += '&';
101 scanShortcutIdx++;
102 } else {
103 foundAmp = true;
104 }
105 } else {
106 newLabel += c;
107 if (foundAmp) {
108 if (!foundShortcut) {
109 shortcut = c;
110 foundAmp = false;
111 foundShortcut = true;
112 shortcutIdx = scanShortcutIdx;
113 }
114 } else {
115 scanShortcutIdx++;
116 }
117 }
118 }
119 this.rawLabel = newLabel;
df8de03f
KL
120 }
121}