001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.gui.base;
017:
018: import javax.swing.AbstractButton;
019: import javax.swing.JLabel;
020:
021: /**
022: * This class contains utility methods to set text on buttons, checkboxes,
023: * menus, menuitems and labels with mnemonics. The mnemonics is to be specified
024: * using the & character in the display text just before the mnemonic character.
025: * <br>
026: * The first & character in the display text is used to define the mnemonic.
027: * Please be aware of this when trying to set display texts containing a &
028: * character.
029: *
030: * @author Karl Peder Olesen (karlpeder)
031: */
032: public class MnemonicSetter {
033: /**
034: * Sets the text of a menu, menuitem, button or checkbox. If a & character
035: * is found, it is used to define the mnemonic. Else the text is set just as
036: * if the setText method of the component was called.
037: *
038: * @param component
039: * Menu, menuitem, button or checkbox to handle
040: * @param text
041: * Displaytext, possibly including & for mnemonic specification
042: */
043: public static void setTextWithMnemonic(AbstractButton component,
044: String text) {
045: // search for mnemonic
046: int index = text.indexOf("&");
047:
048: if ((index != -1) && ((index + 1) < text.length())) {
049: // mnemonic found
050: // ...and not at the end of the string (which doesn't make sence)
051: char mnemonic = text.charAt(index + 1);
052:
053: StringBuffer buf = new StringBuffer();
054:
055: // if mnemonic is first character of this string
056: if (index == 0) {
057: buf.append(text.substring(1));
058: } else {
059: buf.append(text.substring(0, index));
060: buf.append(text.substring(index + 1));
061: }
062:
063: // set display text
064: component.setText(buf.toString());
065:
066: // set mnemonic
067: component.setMnemonic(mnemonic);
068: component.setDisplayedMnemonicIndex(index);
069: } else {
070: // no mnemonic found - just set the text on the menu item
071: component.setText(text);
072: }
073: }
074:
075: /**
076: * Sets the text of a label including mnemonic. <br>
077: * Same functionality as
078: *
079: * @see setTextWithMnemonic
080: *
081: * @param label
082: * Label to handle
083: * @param text
084: * Displaytext, possibly including & for mnemonic specification
085: */
086: public static void setTextWithMnemonic(JLabel label, String text) {
087: // search for mnemonic
088: int index = text.indexOf("&");
089:
090: if ((index != -1) && ((index + 1) < text.length())) {
091: // mnemonic found
092: // ...and not at the end of the string (which doesn't make sence)
093: char mnemonic = text.charAt(index + 1);
094:
095: StringBuffer buf = new StringBuffer();
096:
097: // if mnemonic is first character of this string
098: if (index == 0) {
099: buf.append(text.substring(1));
100: } else {
101: buf.append(text.substring(0, index));
102: buf.append(text.substring(index + 1));
103: }
104:
105: // set display text
106: label.setText(buf.toString());
107:
108: // set mnemonic
109: label.setDisplayedMnemonic(mnemonic);
110: label.setDisplayedMnemonicIndex(index);
111: } else {
112: // no mnemonic found - just set the text on the menu item
113: label.setText(text);
114: }
115: }
116: }
|