KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui > AnnotatedString


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004,2005 Rohan Lloyd <findbugs@rohanl.com>
4  * Copyright (C) 2004,2005 University of Maryland
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20
21 /*
22  * AnnotatedString.java
23  *
24  * Created on September 14, 2004
25  */

26
27 package edu.umd.cs.findbugs.gui;
28
29 import java.awt.FlowLayout JavaDoc;
30 import java.awt.event.KeyEvent JavaDoc;
31
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JFrame JavaDoc;
34
35 import edu.umd.cs.findbugs.SystemProperties;
36
37 /**
38  * Class to handle Strings annotated with embedded mnemonics
39  *
40  * Note: Since the human interface guidelines for Mac OS X say never
41  * to use mnemonics, this class behaves as if no mnemonics are set
42  * when run on Mac OS X.
43  */

44 public class AnnotatedString {
45
46     private static final boolean MAC_OS_X =
47         SystemProperties.getProperty("os.name").toLowerCase().startsWith("mac os x");
48
49     private final String JavaDoc myAnnotatedString;
50
51     public AnnotatedString(String JavaDoc s) {
52         myAnnotatedString = s;
53     }
54
55     /*
56      * Return the string minus any annotation
57      */

58     @Override JavaDoc
59     public String JavaDoc toString() {
60         if (MAC_OS_X) {
61             // Support annotations like "File(&F)"
62
if (myAnnotatedString.matches("[^&]+\\(&\\p{Alnum}\\)")) {
63                 int endIndex = myAnnotatedString.length() - "(&X)".length();
64
65                 return myAnnotatedString.substring(0, endIndex);
66             }
67
68             // Support annotations like "File(&F)..."
69
if (myAnnotatedString.matches("[^&]+\\(&\\p{Alnum}\\)\\.\\.\\.")) {
70                 int startIndex = myAnnotatedString.length() - "(&X)...".length();
71                 int endIndex = startIndex + "(&X)".length();
72
73                 return new StringBuffer JavaDoc(myAnnotatedString).delete(startIndex, endIndex).toString();
74             }
75         }
76         return myAnnotatedString.replaceFirst("&", "");
77     }
78
79     /**
80      * Return the appropriate mnemonic character for this string. If no mnemonic
81      * should be displayed, KeyEvent.VK_UNDEFINED is returned.
82      *
83      * @return the Mnemonic character, or VK_UNDEFINED if no mnemonic should
84      * be set
85      */

86     public int getMnemonic() {
87         int mnemonic = KeyEvent.VK_UNDEFINED;
88         if (!MAC_OS_X) {
89             int index = getMnemonicIndex();
90             if ((index >= 0) && ((index + 1) < myAnnotatedString.length())) {
91                 mnemonic = Character.toUpperCase(myAnnotatedString.charAt(index + 1));
92             }
93         }
94         return mnemonic;
95     }
96     
97     /**
98      * @return the index in the plain string at which the mnemonic should be
99      * displayed, or -1 if no mnemonic should be set
100      */

101     public int getMnemonicIndex() {
102         int index = -1;
103         if (!MAC_OS_X) {
104             index = myAnnotatedString.indexOf('&');
105             if (index + 1 >= myAnnotatedString.length()) {
106                 index = -1;
107             }
108         }
109         return index;
110     }
111
112     
113     /*
114      * Some test code
115      */

116     private static void addButton(JFrame JavaDoc frame, String JavaDoc s) {
117         AnnotatedString as = new AnnotatedString(s);
118         JButton JavaDoc button = new JButton JavaDoc(as.toString());
119         button.setMnemonic(as.getMnemonic());
120         button.setDisplayedMnemonicIndex(as.getMnemonicIndex());
121         frame.getContentPane().add(button);
122
123         System.out.println("\"" + s + "\" \"" + as + "\" '" +
124         as.getMnemonic() + "' " + as.getMnemonicIndex());
125     }
126
127     public static void main(String JavaDoc[] args) {
128     // Some basic tests
129

130         JFrame JavaDoc frame = new JFrame JavaDoc();
131         frame.getContentPane().setLayout(new FlowLayout JavaDoc());
132
133         addButton(frame, "&File");
134         addButton(frame, "S&ave As...");
135         addButton(frame, "Save &As...");
136         addButton(frame, "Fo&o");
137         addButton(frame, "Foo");
138
139         // Error cases - make sure we handle them sensibly
140
addButton(frame, "");
141         addButton(frame, "&");
142         addButton(frame, "Foo&");
143         addButton(frame, "Cat & Dog");
144         addButton(frame, "Cat && Dog");
145     
146         frame.pack();
147         frame.setVisible(true);
148     }
149     
150 }
151
Popular Tags