001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.gravy;
043:
044: import java.util.MissingResourceException;
045: import java.util.ResourceBundle;
046: import org.netbeans.jemmy.JemmyException;
047: import org.openide.util.NbBundle;
048:
049: /** Helper class to get strings from NetBeans Bundle.properties files.
050: * <br>
051: * Everytime someone wants to identify a component by its title, label, caption or whatever,
052: * he should not use hard coded string in his test case but he should use
053: * <code>Bundle.getString(bundleName, key)</code> to obtain string from bundle.
054: * Then test cases can be executed on different than English locale because
055: * <code>getString()</code> methods returns string according to current locale.
056: * <br><br>
057: * Usage:
058: * <br><pre>
059: * // "OK"
060: * Bundle.getString("org.netbeans.core.Bundle", "OK_OPTION_CAPTION");
061: * // "Properties of AnObject"
062: * Bundle.getString("org.netbeans.core.Bundle", "CTL_FMT_LocalProperties", new Object[] {new Integer(1), "AnObject"});
063: * // "View"
064: * Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/View");
065: * </pre>
066: */
067: public class Bundle {
068:
069: /** Placeholder to disallow creating of instances. */
070: private Bundle() {
071: throw new Error("Bundle is just a container for static methods");
072: }
073:
074: /** Returns ResourceBundle from specified path.
075: * @param bundle path to bundle (e.g. "org.netbeans.core.Bundle")
076: * @return ResourceBundle instance
077: */
078: public static ResourceBundle getBundle(String bundle) {
079: try {
080: return NbBundle.getBundle(bundle);
081: } catch (NullPointerException e) {
082: throw new JemmyException("\"" + bundle
083: + "\" bundle has not been found", e);
084: } catch (MissingResourceException e) {
085: throw new JemmyException("\"" + bundle
086: + "\" bundle has not been found", e);
087: }
088: }
089:
090: /** Gets string from specified ResourceBundle.
091: * @param bundle instance of ResourceBundle
092: * @param key key of requested string
093: * @return string from bundle in current locale
094: */
095: public static String getString(ResourceBundle bundle, String key) {
096: try {
097: return bundle.getString(key);
098: } catch (MissingResourceException e) {
099: throw new JemmyException("\"" + key
100: + "\" key has not been found", e);
101: } catch (NullPointerException npe) {
102: throw new JemmyException("Cannot accept null parameter.",
103: npe);
104: }
105: }
106:
107: /** Gets string from bundle specified by path to bundle and format it.
108: * @param bundle path to bundle (e.g. "org.netbeans.core.Bundle")
109: * @param key key of requested string
110: * @param params parameters to be formatted
111: * @return string from bundle in current locale with formatted parameters
112: */
113: public static String getString(ResourceBundle bundle, String key,
114: Object[] params) {
115: return java.text.MessageFormat.format(getString(bundle, key),
116: params);
117: }
118:
119: /** Gets string from bundle specified by path to bundle.
120: * @param bundle path to bundle (e.g. "org.netbeans.core.Bundle")
121: * @param key key of requested string
122: * @return string from bundle in current locale
123: */
124: public static String getString(String bundle, String key) {
125: return getString(getBundle(bundle), key);
126: }
127:
128: /** Gets string from bundle, removes mnemonic (i.e. '&' or '(&X)') from it
129: * and cuts parameters like {0} from the end.
130: * @param bundle path to bundle (e.g. "org.netbeans.core.Bundle")
131: * @param key key of requested string
132: * @return string from bundle in current locale. Mnemonic (i.e. '&' or '(&X)')
133: * is removed and parameter patterns are also removed starting by first '{'.
134: */
135: public static String getStringTrimmed(String bundle, String key) {
136: return trim(getString(getBundle(bundle), key));
137: }
138:
139: /** Gets string from bundle specified by path to bundle and format it.
140: * @param bundle path to bundle (e.g. "org.netbeans.core.Bundle")
141: * @param key key of requested string
142: * @param params parameter to be formatted
143: * @return string from bundle in current locale with formatted parameters
144: */
145: public static String getString(String bundle, String key,
146: Object[] params) {
147: return java.text.MessageFormat.format(getString(bundle, key),
148: params);
149: }
150:
151: /** Gets string from bundle and formats it. It removes mnemonic (i.e. '&' or '(&X)')
152: * from it and cuts parameters like {0} from the end if any.
153: * @param bundle path to bundle (e.g. "org.netbeans.core.Bundle")
154: * @param key key of requested string
155: * @param params parameter to be formatted
156: * @return string from bundle in current locale. Mnemonic and parameters
157: * like {0} removed from the end.
158: */
159: public static String getStringTrimmed(String bundle, String key,
160: Object[] params) {
161: return trim(getString(getBundle(bundle), key, params));
162: }
163:
164: /** Removes mnemonic (i.e. '&' or '(&X)') and cut parameters like {0} from the end.
165: * @param value string to modify
166: * @return string with removed mnemonic and parameters like {0} from the end.
167: */
168: private static String trim(String value) {
169: // remove mnemonic, i.e. '&' or '(&X)'
170: value = cutAmpersand(value);
171: // cut parameters like {0} from string
172: if (value.indexOf('{') != -1) {
173: value = value.substring(0, value.indexOf('{'));
174: }
175: return value;
176: }
177:
178: /**
179: * Removes an ampersand from a text string; commonly used to strip out unneeded mnemonics.
180: * Replaces the first occurence of <samp>&?</samp> by <samp>?</samp> or <samp>(&??</samp> by the empty string
181: * where <samp>?</samp> is a wildcard for any character.
182: * <samp>&?</samp> is a shortcut in English locale.
183: * <samp>(&?)</samp> is a shortcut in Japanese locale.
184: * Used to remove shortcuts from workspace names (or similar) when shortcuts are not supported.
185: * <p>The current implementation behaves in the same way regardless of locale.
186: * In case of a conflict it would be necessary to change the
187: * behavior based on the current locale.
188: * @param text a localized label that may have mnemonic information in it
189: * @return string without first <samp>&</samp> if there was any
190: */
191: private static String cutAmpersand(String text) {
192: // modified code of org.openide.awt.Actions.cutAmpersand
193: // see also org.openide.awt.Mnemonics
194: int i;
195: String result = text;
196: /* First check of occurence of '(&'. If not found check
197: * for '&' itself.
198: * If '(&' is found then remove '(&??' and rest of line.
199: */
200: i = text.indexOf("(&"); // NOI18N
201: if (i >= 0 && i + 3 < text.length()
202: && /* #31093 */text.charAt(i + 3) == ')') { // NOI18N
203: result = text.substring(0, i);
204: } else {
205: //Sequence '(&?)' not found look for '&' itself
206: i = text.indexOf('&');
207: if (i < 0) {
208: //No ampersand
209: result = text;
210: } else if (i == (text.length() - 1)) {
211: //Ampersand is last character, wrong shortcut but we remove it anyway
212: result = text.substring(0, i);
213: } else {
214: //Remove ampersand from middle of string
215: result = text.substring(0, i) + text.substring(i + 1);
216: }
217: }
218: return result;
219: }
220: }
|