1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.component; |
16 | |
17 | import java.util.regex.Matcher; |
18 | import java.util.regex.Pattern; |
19 | |
20 | import javax.swing.AbstractButton; |
21 | |
22 | /** |
23 | * Provides miscellaneous utility functions for handling Swing components. |
24 | * There is no need to instantiate this class since it only provides |
25 | * static methods. |
26 | * @author Jean-Francois Poilpret |
27 | */ |
28 | public class ComponentTools |
29 | { |
30 | protected ComponentTools() |
31 | { |
32 | } |
33 | |
34 | public static void setMnemonic(AbstractButton component, String label) |
35 | { |
36 | // Find '_' in label and remove |
37 | Matcher matcher = _pattern.matcher(label); |
38 | if (!matcher.find()) |
39 | { |
40 | component.setText(label); |
41 | } |
42 | else |
43 | { |
44 | String realLabel = matcher.replaceFirst(""); |
45 | int index = matcher.start(); |
46 | component.setText(realLabel); |
47 | component.setMnemonic(realLabel.charAt(index)); |
48 | component.setDisplayedMnemonicIndex(index); |
49 | } |
50 | } |
51 | |
52 | static private final String MNEMONIC_MARKER = "_"; |
53 | static private final Pattern _pattern = Pattern.compile(MNEMONIC_MARKER); |
54 | } |