Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sanity/client/lib/SwingSet2/src/TextAndMnemonicUtils.java
41161 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.util.Properties;
26
import java.util.ResourceBundle;
27
28
/**
29
* <code>TextAndMnemonicUtils</code> allows to extract text and mnemonic values
30
* from the unified text & mnemonic strings. For example:
31
* LafMenu.laf.labelAndMnemonic=&Look && Feel
32
* The extracted text is "Look & Feel" and the extracted mnemonic mnemonic is "L".
33
*
34
* There are several patterns for the text and mnemonic suffixes which are used
35
* in the resource file. The patterns format is:
36
* (resource key -> unified text & mnemonic resource key).
37
*
38
* Keys that have label suffixes:
39
* (xxx_label -> xxx.labelAndMnemonic)
40
*
41
* Keys that have mnemonic suffixes:
42
* (xxx_mnemonic -> xxx.labelAndMnemonic)
43
*
44
* Keys that do not have definite suffixes:
45
* (xxx -> xxx.labelAndMnemonic)
46
*
47
* @author Alexander Scherbatiy
48
*/
49
public class TextAndMnemonicUtils {
50
51
// Label suffix for the text & mnemonic resource
52
private static final String LABEL_SUFFIX = ".labelAndMnemonic";
53
54
// Resource bundle for internationalized and accessible text
55
private static ResourceBundle bundle = null;
56
57
// Resource properties for the mnemonic key defenition
58
private static Properties properties = null;
59
60
static {
61
bundle = ResourceBundle.getBundle("resources.swingset");
62
properties = new Properties();
63
try {
64
properties.load(TextAndMnemonicUtils.class.getResourceAsStream("resources/swingset.properties"));
65
} catch (IOException ex) {
66
System.out.println("java.io.IOException: Couldn't load properties from: resources/swingset.properties");
67
}
68
}
69
70
/**
71
* Returns accessible and internationalized strings or mnemonics from the
72
* resource bundle. The key is converted to the text & mnemonic key.
73
*
74
* The following patterns are checked:
75
* Keys that have label suffixes:
76
* (xxx_label -> xxx.labelAndMnemonic)
77
*
78
* Keys that have mnemonic suffixes:
79
* (xxx_mnemonic -> xxx.labelAndMnemonic)
80
*
81
* Keys that do not have definite suffixes:
82
* (xxx -> xxx.labelAndMnemonic)
83
*
84
* Properties class is used to check if a key created for mnemonic exists.
85
*/
86
public static String getTextAndMnemonicString(String key) {
87
88
if (key.endsWith("_label")) {
89
String compositeKey = composeKey(key, 6, LABEL_SUFFIX);
90
String textAndMnemonic = bundle.getString(compositeKey);
91
return getTextFromTextAndMnemonic(textAndMnemonic);
92
}
93
94
if (key.endsWith("_mnemonic")) {
95
96
String compositeKey = composeKey(key, 9, LABEL_SUFFIX);
97
Object value = properties.getProperty(compositeKey);
98
99
if (value != null) {
100
String textAndMnemonic = bundle.getString(compositeKey);
101
return getMnemonicFromTextAndMnemonic(textAndMnemonic);
102
}
103
104
}
105
106
String compositeKey = composeKey(key, 0, LABEL_SUFFIX);
107
Object value = properties.getProperty(compositeKey);
108
109
if (value != null) {
110
String textAndMnemonic = bundle.getString(compositeKey);
111
return getTextFromTextAndMnemonic(textAndMnemonic);
112
}
113
114
String textAndMnemonic = bundle.getString(key);
115
return getTextFromTextAndMnemonic(textAndMnemonic);
116
}
117
118
/**
119
* Convert the text & mnemonic string to text string
120
*
121
* The '&' symbol is treated as the mnemonic pointer
122
* The double "&&" symbols are treated as the single '&'
123
*
124
* For example the string "&Look && Feel" is converted to "Look & Feel"
125
*/
126
public static String getTextFromTextAndMnemonic(String text) {
127
128
StringBuilder sb = new StringBuilder();
129
130
int prevIndex = 0;
131
int nextIndex = text.indexOf('&');
132
int len = text.length();
133
134
while (nextIndex != -1) {
135
136
String s = text.substring(prevIndex, nextIndex);
137
sb.append(s);
138
139
nextIndex++;
140
141
if (nextIndex != len && text.charAt(nextIndex) == '&') {
142
sb.append('&');
143
nextIndex++;
144
}
145
146
prevIndex = nextIndex;
147
nextIndex = text.indexOf('&', nextIndex + 1);
148
}
149
150
sb.append(text.substring(prevIndex, text.length()));
151
return sb.toString();
152
}
153
154
/**
155
* Convert the text & mnemonic string to mnemonic
156
*
157
* The '&' symbol is treated the mnemonic pointer
158
* The double "&&" symbols are treated as the single '&'
159
*
160
* For example the string "&Look && Feel" is converted to "L"
161
*/
162
public static String getMnemonicFromTextAndMnemonic(String text) {
163
int index = text.indexOf('&');
164
165
while (0 <= index && index < text.length() - 1) {
166
index++;
167
if (text.charAt(index) == '&') {
168
index = text.indexOf('&', index + 1);
169
} else {
170
char c = text.charAt(index);
171
return String.valueOf(Character.toUpperCase(c));
172
}
173
}
174
175
return null;
176
}
177
178
/**
179
* Removes the last n characters and adds the suffix
180
*/
181
private static String composeKey(String key, int reduce, String sufix) {
182
return key.substring(0, key.length() - reduce) + sufix;
183
}
184
}
185