Clover coverage report - Java Gui Builder - Code Coverage
Coverage timestamp: ven. juil. 11 2003 11:10:09 EDT
file stats: LOC: 73   Methods: 1
NCLOC: 31   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
KeyStrokeParser.java 91,7% 94,7% 100% 93,8%
coverage coverage
 1   
 /**
 2   
  * Java Gui Builder - A library to build GUIs using an XML file.
 3   
  * Copyright 2002, 2003 (C) François Beausoleil
 4   
  *
 5   
  * This library is free software; you can redistribute it and/or
 6   
  * modify it under the terms of the GNU Lesser General Public
 7   
  * License as published by the Free Software Foundation; either
 8   
  * version 2.1 of the License, or (at your option) any later version.
 9   
  *
 10   
  * This library is distributed in the hope that it will be useful,
 11   
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13   
  * Lesser General Public License for more details.
 14   
  *
 15   
  * You should have received a copy of the GNU Lesser General Public
 16   
  * License along with this library; if not, write to the Free Software
 17   
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 18   
  */
 19   
 
 20   
 package jgb.builder.utils;
 21   
 
 22   
 import javax.swing.*;
 23   
 
 24   
 /**
 25   
  * Implements a parser for returning {@link javax.swing.KeyStroke KeyStroke}
 26   
  * from a string representation of the key stroke.
 27   
  * <p>Java already provides such a facility but, unfortunately, the parsing
 28   
  * uses non-standard names for meta key names.</p>
 29   
  * <p>This class provides a parser that combines both parsers into one:
 30   
  * the passed key stroke string can be either in Java's parser format, or the
 31   
  * more standard "Ctrl+C" format.</p>
 32   
  * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a>
 33   
  */
 34   
 public class KeyStrokeParser {
 35   
     /**
 36   
      * Returns <code>null</code> if the key stroke string could not be parsed.
 37   
      */
 38  21
     public static KeyStroke getKeyStroke(final String keyStrokeString) {
 39  21
         KeyStroke stroke = KeyStroke.getKeyStroke(keyStrokeString);
 40  21
         if (stroke != null) {
 41  7
             return stroke;
 42   
         }
 43   
 
 44  14
         String upStroke = keyStrokeString.toUpperCase();
 45  14
         stroke = KeyStroke.getKeyStroke(upStroke);
 46  14
         if (stroke != null) {
 47  4
             return stroke;
 48   
         }
 49   
 
 50  10
         final StringBuffer newKeyStroke = new StringBuffer();
 51  10
         final String code = upStroke.substring(upStroke.lastIndexOf("+") + 1);
 52  10
         if (upStroke.indexOf("CTRL") != -1) {
 53  6
             newKeyStroke.append("control ");
 54   
         }
 55   
 
 56  10
         if (upStroke.indexOf("SHIFT") != -1) {
 57  3
             newKeyStroke.append("shift ");
 58   
         }
 59   
 
 60  10
         if (upStroke.indexOf("ALT") != -1) {
 61  2
             newKeyStroke.append("alt ");
 62   
         }
 63   
 
 64  10
         if (upStroke.indexOf("META") != -1) {
 65  0
             newKeyStroke.append(" meta");
 66   
         }
 67   
 
 68  10
         newKeyStroke.append(code);
 69   
 
 70  10
         return KeyStroke.getKeyStroke(newKeyStroke.toString());
 71   
     }
 72   
 }
 73