LICENSE CHANGED TO MIT
[nikiroo-utils.git] / src / jexer / bits / MnemonicString.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2016 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
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 */
37 public final class MnemonicString {
38
39 /**
40 * Keyboard shortcut to activate this item.
41 */
42 private char shortcut;
43
44 /**
45 * Get the keyboard shortcut character.
46 *
47 * @return the highlighted character
48 */
49 public char getShortcut() {
50 return shortcut;
51 }
52
53 /**
54 * Location of the highlighted character.
55 */
56 private int shortcutIdx = -1;
57
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
67 /**
68 * The raw (uncolored) string.
69 */
70 private String rawLabel;
71
72 /**
73 * Get the raw (uncolored) string.
74 *
75 * @return the raw (uncolored) string
76 */
77 public String getRawLabel() {
78 return rawLabel;
79 }
80
81 /**
82 * Public constructor.
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 */
87 public MnemonicString(final String label) {
88
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;
118 }
119 }