| CWLabel.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2007
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: org.eclipse.jdt.ui.prefs 1178 2006-11-06 12:48:02Z dmsmith $
21 */
22 package org.crosswire.common.swing;
23
24 import javax.swing.JLabel;
25
26 import org.crosswire.common.util.OSType;
27
28 /**
29 * CWLabel is a utility class to create JLabels from text with an optional
30 * mnemonic indicator. A preceding '_' indicates a mnemonic.
31 *
32 * @see gnu.lgpl.License for license details.<br>
33 * The copyright to this program is held by it's authors.
34 * @author DM Smith [dmsmith555 at yahoo dot com]
35 */
36 public class CWLabel {
37
38 /**
39 * Utility class. Prevent instantiation.
40 */
41 private CWLabel() {
42 }
43
44 /**
45 * Construct a JLabel from text. A preceding '_' indicates a mnemonic.
46 * Mnemonics are ignored on MacOS X.
47 *
48 * @param text
49 * the text of the label, with an optional mnemonic indicator
50 * @return a JLabel
51 */
52 public static JLabel createJLabel(String text) {
53 String label = text;
54
55 // A Mnemonic can be specified by a preceding _ in the name
56 char mnemonic = '\0';
57 int pos = label.indexOf('_');
58 int len = label.length();
59 if (pos == len - 1) {
60 // There is nothing following the _. Just remove it.
61 label = label.substring(0, len - 1);
62 } else if (pos >= 0 && pos < len - 1) {
63 // Remove the _
64 StringBuilder buffer = new StringBuilder(label.length() - 1);
65 if (pos > 0) {
66 buffer.append(label.substring(0, pos));
67 }
68 buffer.append(label.substring(pos + 1));
69
70 label = buffer.toString();
71
72 // the mnemonic is now at the position that the _ was.
73 mnemonic = label.charAt(pos);
74 }
75
76 if (label.length() == 0) {
77 label = "?";
78 }
79
80 JLabel theLabel = new JLabel();
81 theLabel.setText(label);
82
83 // Mac's don't have mnemonics
84 if (mnemonic != '\0' && !OSType.MAC.equals(OSType.getOSType())) {
85 theLabel.setDisplayedMnemonic(mnemonic);
86 }
87
88 return theLabel;
89 }
90 }
91