Path: blob/master/test/jdk/sanity/client/lib/SwingSet2/src/TextAndMnemonicUtils.java
41161 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.util.Properties;25import java.util.ResourceBundle;2627/**28* <code>TextAndMnemonicUtils</code> allows to extract text and mnemonic values29* from the unified text & mnemonic strings. For example:30* LafMenu.laf.labelAndMnemonic=&Look && Feel31* The extracted text is "Look & Feel" and the extracted mnemonic mnemonic is "L".32*33* There are several patterns for the text and mnemonic suffixes which are used34* in the resource file. The patterns format is:35* (resource key -> unified text & mnemonic resource key).36*37* Keys that have label suffixes:38* (xxx_label -> xxx.labelAndMnemonic)39*40* Keys that have mnemonic suffixes:41* (xxx_mnemonic -> xxx.labelAndMnemonic)42*43* Keys that do not have definite suffixes:44* (xxx -> xxx.labelAndMnemonic)45*46* @author Alexander Scherbatiy47*/48public class TextAndMnemonicUtils {4950// Label suffix for the text & mnemonic resource51private static final String LABEL_SUFFIX = ".labelAndMnemonic";5253// Resource bundle for internationalized and accessible text54private static ResourceBundle bundle = null;5556// Resource properties for the mnemonic key defenition57private static Properties properties = null;5859static {60bundle = ResourceBundle.getBundle("resources.swingset");61properties = new Properties();62try {63properties.load(TextAndMnemonicUtils.class.getResourceAsStream("resources/swingset.properties"));64} catch (IOException ex) {65System.out.println("java.io.IOException: Couldn't load properties from: resources/swingset.properties");66}67}6869/**70* Returns accessible and internationalized strings or mnemonics from the71* resource bundle. The key is converted to the text & mnemonic key.72*73* The following patterns are checked:74* Keys that have label suffixes:75* (xxx_label -> xxx.labelAndMnemonic)76*77* Keys that have mnemonic suffixes:78* (xxx_mnemonic -> xxx.labelAndMnemonic)79*80* Keys that do not have definite suffixes:81* (xxx -> xxx.labelAndMnemonic)82*83* Properties class is used to check if a key created for mnemonic exists.84*/85public static String getTextAndMnemonicString(String key) {8687if (key.endsWith("_label")) {88String compositeKey = composeKey(key, 6, LABEL_SUFFIX);89String textAndMnemonic = bundle.getString(compositeKey);90return getTextFromTextAndMnemonic(textAndMnemonic);91}9293if (key.endsWith("_mnemonic")) {9495String compositeKey = composeKey(key, 9, LABEL_SUFFIX);96Object value = properties.getProperty(compositeKey);9798if (value != null) {99String textAndMnemonic = bundle.getString(compositeKey);100return getMnemonicFromTextAndMnemonic(textAndMnemonic);101}102103}104105String compositeKey = composeKey(key, 0, LABEL_SUFFIX);106Object value = properties.getProperty(compositeKey);107108if (value != null) {109String textAndMnemonic = bundle.getString(compositeKey);110return getTextFromTextAndMnemonic(textAndMnemonic);111}112113String textAndMnemonic = bundle.getString(key);114return getTextFromTextAndMnemonic(textAndMnemonic);115}116117/**118* Convert the text & mnemonic string to text string119*120* The '&' symbol is treated as the mnemonic pointer121* The double "&&" symbols are treated as the single '&'122*123* For example the string "&Look && Feel" is converted to "Look & Feel"124*/125public static String getTextFromTextAndMnemonic(String text) {126127StringBuilder sb = new StringBuilder();128129int prevIndex = 0;130int nextIndex = text.indexOf('&');131int len = text.length();132133while (nextIndex != -1) {134135String s = text.substring(prevIndex, nextIndex);136sb.append(s);137138nextIndex++;139140if (nextIndex != len && text.charAt(nextIndex) == '&') {141sb.append('&');142nextIndex++;143}144145prevIndex = nextIndex;146nextIndex = text.indexOf('&', nextIndex + 1);147}148149sb.append(text.substring(prevIndex, text.length()));150return sb.toString();151}152153/**154* Convert the text & mnemonic string to mnemonic155*156* The '&' symbol is treated the mnemonic pointer157* The double "&&" symbols are treated as the single '&'158*159* For example the string "&Look && Feel" is converted to "L"160*/161public static String getMnemonicFromTextAndMnemonic(String text) {162int index = text.indexOf('&');163164while (0 <= index && index < text.length() - 1) {165index++;166if (text.charAt(index) == '&') {167index = text.indexOf('&', index + 1);168} else {169char c = text.charAt(index);170return String.valueOf(Character.toUpperCase(c));171}172}173174return null;175}176177/**178* Removes the last n characters and adds the suffix179*/180private static String composeKey(String key, int reduce, String sufix) {181return key.substring(0, key.length() - reduce) + sufix;182}183}184185