Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/SwingSet2/TextAndMnemonicUtils.java
41149 views
1
/*
2
*
3
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
7
*
8
* - Redistributions of source code must retain the above copyright notice, this
9
* list of conditions and the following disclaimer.
10
*
11
* - Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
*
15
* - Neither the name of Oracle nor the names of its contributors may be used to
16
* endorse or promote products derived from this software without specific prior
17
* written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
* POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
import java.io.IOException;
33
import java.util.Properties;
34
import java.util.ResourceBundle;
35
36
/**
37
* <code>TextAndMnemonicUtils</code> allows to extract text and mnemonic values
38
* from the unified text & mnemonic strings. For example:
39
* LafMenu.laf.labelAndMnemonic=&Look && Feel
40
* The extracted text is "Look & Feel" and the extracted mnemonic mnemonic is "L".
41
*
42
* There are several patterns for the text and mnemonic suffixes which are used
43
* in the resource file. The patterns format is:
44
* (resource key -> unified text & mnemonic resource key).
45
*
46
* Keys that have label suffixes:
47
* (xxx_label -> xxx.labelAndMnemonic)
48
*
49
* Keys that have mnemonic suffixes:
50
* (xxx_mnemonic -> xxx.labelAndMnemonic)
51
*
52
* Keys that do not have definite suffixes:
53
* (xxx -> xxx.labelAndMnemonic)
54
*
55
* @author Alexander Scherbatiy
56
*/
57
public class TextAndMnemonicUtils {
58
59
// Label suffix for the text & mnemonic resource
60
private static final String LABEL_SUFFIX = ".labelAndMnemonic";
61
62
// Resource bundle for internationalized and accessible text
63
private static ResourceBundle bundle = null;
64
65
// Resource properties for the mnemonic key defenition
66
private static Properties properties = null;
67
68
static {
69
bundle = ResourceBundle.getBundle("resources.swingset");
70
properties = new Properties();
71
try {
72
properties.load(TextAndMnemonicUtils.class.getResourceAsStream("resources/swingset.properties"));
73
} catch (IOException ex) {
74
System.out.println("java.io.IOException: Couldn't load properties from: resources/swingset.properties");
75
}
76
}
77
78
/**
79
* Returns accessible and internationalized strings or mnemonics from the
80
* resource bundle. The key is converted to the text & mnemonic key.
81
*
82
* The following patterns are checked:
83
* Keys that have label suffixes:
84
* (xxx_label -> xxx.labelAndMnemonic)
85
*
86
* Keys that have mnemonic suffixes:
87
* (xxx_mnemonic -> xxx.labelAndMnemonic)
88
*
89
* Keys that do not have definite suffixes:
90
* (xxx -> xxx.labelAndMnemonic)
91
*
92
* Properties class is used to check if a key created for mnemonic exists.
93
*/
94
public static String getTextAndMnemonicString(String key) {
95
96
if (key.endsWith("_label")) {
97
String compositeKey = composeKey(key, 6, LABEL_SUFFIX);
98
String textAndMnemonic = bundle.getString(compositeKey);
99
return getTextFromTextAndMnemonic(textAndMnemonic);
100
}
101
102
if (key.endsWith("_mnemonic")) {
103
104
String compositeKey = composeKey(key, 9, LABEL_SUFFIX);
105
Object value = properties.getProperty(compositeKey);
106
107
if (value != null) {
108
String textAndMnemonic = bundle.getString(compositeKey);
109
return getMnemonicFromTextAndMnemonic(textAndMnemonic);
110
}
111
112
}
113
114
String compositeKey = composeKey(key, 0, LABEL_SUFFIX);
115
Object value = properties.getProperty(compositeKey);
116
117
if (value != null) {
118
String textAndMnemonic = bundle.getString(compositeKey);
119
return getTextFromTextAndMnemonic(textAndMnemonic);
120
}
121
122
String textAndMnemonic = bundle.getString(key);
123
return getTextFromTextAndMnemonic(textAndMnemonic);
124
}
125
126
/**
127
* Convert the text & mnemonic string to text string
128
*
129
* The '&' symbol is treated as the mnemonic pointer
130
* The double "&&" symbols are treated as the single '&'
131
*
132
* For example the string "&Look && Feel" is converted to "Look & Feel"
133
*/
134
public static String getTextFromTextAndMnemonic(String text) {
135
136
StringBuilder sb = new StringBuilder();
137
138
int prevIndex = 0;
139
int nextIndex = text.indexOf('&');
140
int len = text.length();
141
142
while (nextIndex != -1) {
143
144
String s = text.substring(prevIndex, nextIndex);
145
sb.append(s);
146
147
nextIndex++;
148
149
if (nextIndex != len && text.charAt(nextIndex) == '&') {
150
sb.append('&');
151
nextIndex++;
152
}
153
154
prevIndex = nextIndex;
155
nextIndex = text.indexOf('&', nextIndex + 1);
156
}
157
158
sb.append(text.substring(prevIndex, text.length()));
159
return sb.toString();
160
}
161
162
/**
163
* Convert the text & mnemonic string to mnemonic
164
*
165
* The '&' symbol is treated the mnemonic pointer
166
* The double "&&" symbols are treated as the single '&'
167
*
168
* For example the string "&Look && Feel" is converted to "L"
169
*/
170
public static String getMnemonicFromTextAndMnemonic(String text) {
171
int len = text.length();
172
int index = text.indexOf('&');
173
174
while (0 <= index && index < text.length() - 1) {
175
index++;
176
if (text.charAt(index) == '&') {
177
index = text.indexOf('&', index + 1);
178
} else {
179
char c = text.charAt(index);
180
return String.valueOf(Character.toUpperCase(c));
181
}
182
}
183
184
return null;
185
}
186
187
/**
188
* Removes the last n characters and adds the suffix
189
*/
190
private static String composeKey(String key, int reduce, String sufix) {
191
return key.substring(0, key.length() - reduce) + sufix;
192
}
193
}
194
195