KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > MnemonicSetter


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.gui.base;
17
18 import javax.swing.AbstractButton JavaDoc;
19 import javax.swing.JLabel JavaDoc;
20
21 /**
22  * This class contains utility methods to set text on buttons, checkboxes,
23  * menus, menuitems and labels with mnemonics. The mnemonics is to be specified
24  * using the & character in the display text just before the mnemonic character.
25  * <br>
26  * The first & character in the display text is used to define the mnemonic.
27  * Please be aware of this when trying to set display texts containing a &
28  * character.
29  *
30  * @author Karl Peder Olesen (karlpeder)
31  */

32 public class MnemonicSetter {
33     /**
34      * Sets the text of a menu, menuitem, button or checkbox. If a & character
35      * is found, it is used to define the mnemonic. Else the text is set just as
36      * if the setText method of the component was called.
37      *
38      * @param component
39      * Menu, menuitem, button or checkbox to handle
40      * @param text
41      * Displaytext, possibly including & for mnemonic specification
42      */

43     public static void setTextWithMnemonic(AbstractButton JavaDoc component, String JavaDoc text) {
44         // search for mnemonic
45
int index = text.indexOf("&");
46
47         if ((index != -1) && ((index + 1) < text.length())) {
48             // mnemonic found
49
// ...and not at the end of the string (which doesn't make sence)
50
char mnemonic = text.charAt(index + 1);
51
52             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
53
54             // if mnemonic is first character of this string
55
if (index == 0) {
56                 buf.append(text.substring(1));
57             } else {
58                 buf.append(text.substring(0, index));
59                 buf.append(text.substring(index + 1));
60             }
61
62             // set display text
63
component.setText(buf.toString());
64
65             // set mnemonic
66
component.setMnemonic(mnemonic);
67             component.setDisplayedMnemonicIndex(index);
68         } else {
69             // no mnemonic found - just set the text on the menu item
70
component.setText(text);
71         }
72     }
73
74     /**
75      * Sets the text of a label including mnemonic. <br>
76      * Same functionality as
77      *
78      * @see setTextWithMnemonic
79      *
80      * @param label
81      * Label to handle
82      * @param text
83      * Displaytext, possibly including & for mnemonic specification
84      */

85     public static void setTextWithMnemonic(JLabel JavaDoc label, String JavaDoc text) {
86         // search for mnemonic
87
int index = text.indexOf("&");
88
89         if ((index != -1) && ((index + 1) < text.length())) {
90             // mnemonic found
91
// ...and not at the end of the string (which doesn't make sence)
92
char mnemonic = text.charAt(index + 1);
93
94             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
95
96             // if mnemonic is first character of this string
97
if (index == 0) {
98                 buf.append(text.substring(1));
99             } else {
100                 buf.append(text.substring(0, index));
101                 buf.append(text.substring(index + 1));
102             }
103
104             // set display text
105
label.setText(buf.toString());
106
107             // set mnemonic
108
label.setDisplayedMnemonic(mnemonic);
109             label.setDisplayedMnemonicIndex(index);
110         } else {
111             // no mnemonic found - just set the text on the menu item
112
label.setText(text);
113         }
114     }
115 }
116
Popular Tags