View Javadoc

1   /*
2    * $Id: MnemonicUtils.java 1983 2010-02-23 06:46:51Z andrewinkler $
3    * ============================================================================
4    * Project swinger
5    * Copyright (c) 2004-2006 by Andre Winkler. All rights reserved.
6    * ============================================================================
7    *          GNU LESSER GENERAL PUBLIC LICENSE
8    *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9    *
10   *  This library is free software; you can redistribute it and/or
11   *  modify it under the terms of the GNU Lesser General Public
12   *  License as published by the Free Software Foundation; either
13   *  version 2.1 of the License, or (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   *  Lesser General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Lesser General Public
21   *  License along with this library; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   */
25  
26  package de.gluehloch.swinger.common;
27  
28  import org.apache.commons.lang.StringUtils;
29  
30  /**
31   * Support Klasse für das Generieren von Mnemonics.
32   * 
33   * @author by Andre Winkler, $LastChangedBy: andrewinkler $
34   * @version $LastChangedRevision: 1983 $ $LastChangedDate: 2010-02-23 07:46:51 +0100 (Di, 23 Feb 2010) $
35   */
36  public class MnemonicUtils {
37  
38      /** Kennzeichnung von Mnemonic Characters. */
39      private static final char MNEMONIC = '&';
40  
41      /** Der Mnemonic Character als String. */
42      private static final String MNEMONIC_STR = String.valueOf(MNEMONIC);
43  
44      /**
45       * Liefert den <code>KeyStroke</code> für ein mit '&' markiertes Zeichen
46       * aus <code>text</str>.
47       *
48       * @param text Aus diesem String wird der Mnemonic-KeyStroke ermittelt.
49       * @return Der ermittelte KeyStroke.
50       *
51       * @see #getMnemonicCharacter(String)
52       */
53      public static Integer mnemonic(final String text) {
54          Character mnemonic = getMnemonicCharacter(text);
55          if (mnemonic == null) {
56              return null;
57          } else {
58              char c = Character.toUpperCase(mnemonic.charValue());
59              return (new Integer(c));
60          }
61      }
62  
63       /**
64       * Durchforstet einen String nach '&' Zeichen und liefert das darauf
65       * folgende Zeichen zurück. Beispiel: '&Datei' liefert 'D' oder 'E&xit'
66       * liefert 'x'. Ist das darauf folgende Zeichen ebenfalls ein '&', so wird
67       * <code>null</code> zurück geliefert. Ebenso liefert diese Methode
68       * <code>null</code> zurück, wenn kein '&' gefunden wurde.
69       *
70       * @param text Der zu untersuchende Text.
71       * @return Das auf '&' folgende Zeichen.
72       */
73      public static Character getMnemonicCharacter(final String text) {
74          int index = StringUtils.indexOf(text, MNEMONIC);
75          if (index < 0) {
76              return null;
77          } else if (index >= text.length() - 1) {
78              return null;
79          } else {
80              char c = text.charAt(index + 1);
81              if (c == MNEMONIC) {
82                  return null;
83              } else {                
84                  return (new Character(text.charAt(index + 1)));
85              }
86          }
87      }
88  
89      /**
90       * Entfernt den Mnemonic Key aus einem String.
91       *
92       * @param text Der zu bearbeitende String. 
93       * @return Der String ohne Mnemonic.
94       */
95      public static String removeMnemonic(final String text) {
96          return text.replaceFirst(MNEMONIC_STR, "");
97      }
98  
99  }