Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
df8de03f KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
df8de03f | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
df8de03f | 7 | * |
e16dda65 KL |
8 | * Permission is hereby granted, free of charge, to any person obtaining a |
9 | * copy of this software and associated documentation files (the "Software"), | |
10 | * to deal in the Software without restriction, including without limitation | |
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | * and/or sell copies of the Software, and to permit persons to whom the | |
13 | * Software is furnished to do so, subject to the following conditions: | |
df8de03f | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
df8de03f | 17 | * |
e16dda65 KL |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
24 | * DEALINGS IN THE SOFTWARE. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
df8de03f KL |
28 | */ |
29 | package jexer.bits; | |
30 | ||
31 | /** | |
32 | * MnemonicString is used to render a string like "&File" into a highlighted | |
33 | * 'F' and the rest of 'ile'. To insert a literal '&', use two '&&' | |
34 | * characters, e.g. "&File && Stuff" would be "File & Stuff" with the first | |
35 | * 'F' highlighted. | |
36 | */ | |
48e27807 | 37 | public final class MnemonicString { |
df8de03f KL |
38 | |
39 | /** | |
7b5261bc | 40 | * Keyboard shortcut to activate this item. |
df8de03f | 41 | */ |
7b5261bc | 42 | private char shortcut; |
df8de03f | 43 | |
fca67db0 KL |
44 | /** |
45 | * Get the keyboard shortcut character. | |
46 | * | |
47 | * @return the highlighted character | |
48 | */ | |
49 | public char getShortcut() { | |
50 | return shortcut; | |
51 | } | |
52 | ||
df8de03f | 53 | /** |
7b5261bc | 54 | * Location of the highlighted character. |
df8de03f | 55 | */ |
7b5261bc | 56 | private int shortcutIdx = -1; |
df8de03f | 57 | |
48e27807 KL |
58 | /** |
59 | * Get location of the highlighted character. | |
60 | * | |
61 | * @return location of the highlighted character | |
62 | */ | |
63 | public int getShortcutIdx() { | |
64 | return shortcutIdx; | |
65 | } | |
66 | ||
df8de03f | 67 | /** |
7b5261bc | 68 | * The raw (uncolored) string. |
df8de03f | 69 | */ |
7b5261bc | 70 | private String rawLabel; |
df8de03f | 71 | |
48e27807 KL |
72 | /** |
73 | * Get the raw (uncolored) string. | |
74 | * | |
75 | * @return the raw (uncolored) string | |
76 | */ | |
77 | public String getRawLabel() { | |
78 | return rawLabel; | |
79 | } | |
329fd62e | 80 | |
df8de03f | 81 | /** |
7b5261bc | 82 | * Public constructor. |
df8de03f KL |
83 | * |
84 | * @param label widget label or title. Label must contain a keyboard | |
85 | * shortcut, denoted by prefixing a letter with "&", e.g. "&File" | |
86 | */ | |
7b5261bc | 87 | public MnemonicString(final String label) { |
df8de03f | 88 | |
7b5261bc KL |
89 | // Setup the menu shortcut |
90 | String newLabel = ""; | |
91 | boolean foundAmp = false; | |
92 | boolean foundShortcut = false; | |
93 | int scanShortcutIdx = 0; | |
94 | for (int i = 0; i < label.length(); i++) { | |
95 | char c = label.charAt(i); | |
96 | if (c == '&') { | |
97 | if (foundAmp) { | |
98 | newLabel += '&'; | |
99 | scanShortcutIdx++; | |
100 | } else { | |
101 | foundAmp = true; | |
102 | } | |
103 | } else { | |
104 | newLabel += c; | |
105 | if (foundAmp) { | |
106 | if (!foundShortcut) { | |
107 | shortcut = c; | |
108 | foundAmp = false; | |
109 | foundShortcut = true; | |
110 | shortcutIdx = scanShortcutIdx; | |
111 | } | |
112 | } else { | |
113 | scanShortcutIdx++; | |
114 | } | |
115 | } | |
116 | } | |
117 | this.rawLabel = newLabel; | |
df8de03f KL |
118 | } |
119 | } |