Coverage Report - org.eclipse.swtbot.swt.finder.keyboard.Keystrokes
 
Classes in this File Line Coverage Branch Coverage Complexity
Keystrokes
91%
64/70
90%
18/20
3.75
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2009 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.keyboard;
 12  
 
 13  
 import java.util.Arrays;
 14  
 import java.util.LinkedHashSet;
 15  
 
 16  
 import org.eclipse.jface.bindings.keys.IKeyLookup;
 17  
 import org.eclipse.jface.bindings.keys.KeyLookupFactory;
 18  
 import org.eclipse.jface.bindings.keys.KeyStroke;
 19  
 import org.eclipse.swt.SWT;
 20  
 
 21  
 /**
 22  
  * A factory to convert 'complex' keystrokes to a chain of simple keystrokes.
 23  
  * <p>
 24  
  * For e.g. on a US keyboard converts a character '$' to two keystrokes SHIFT and 4, and a 'T' to a SHIFT and 'T'.
 25  
  * </p>
 26  
  * 
 27  
  * @author Ketan Padegaonkar &lt;KetanPadegaonkar [at] gmail [dot] com&gt;
 28  
  * @version $Id$
 29  
  */
 30  1
 public class Keystrokes {
 31  
 
 32  
         /** The SHIFT keystroke. */
 33  1
         public static final KeyStroke        SHIFT                                        = KeyStroke.getInstance(SWT.SHIFT, 0);
 34  
         /** The CTRL keystroke. */
 35  1
         public static final KeyStroke        CTRL                                        = KeyStroke.getInstance(SWT.CTRL, 0);
 36  
         /** The ALT keystroke. */
 37  1
         public static final KeyStroke        ALT                                                = KeyStroke.getInstance(SWT.ALT, 0);
 38  
         /** The COMMAND keystroke. */
 39  1
         public static final KeyStroke        COMMAND                                        = KeyStroke.getInstance(SWT.COMMAND, 0);
 40  
 
 41  
         /** The MOD1 keystroke. */
 42  1
         public static final KeyStroke        MOD1                                        = KeyStroke.getInstance(SWT.MOD1, 0);
 43  
         /** The MOD2 keystroke. */
 44  1
         public static final KeyStroke        MOD2                                        = KeyStroke.getInstance(SWT.MOD2, 0);
 45  
         /** The MOD3 keystroke. */
 46  1
         public static final KeyStroke        MOD3                                        = KeyStroke.getInstance(SWT.MOD3, 0);
 47  
         /** The MOD4 keystroke. */
 48  1
         public static final KeyStroke        MOD4                                        = KeyStroke.getInstance(SWT.MOD4, 0);
 49  
 
 50  
         /** The ESC keystroke. */
 51  1
         public static final KeyStroke        ESC                                                = KeyStroke.getInstance(0, SWT.ESC);
 52  
         /** The backspace keystroke. */
 53  1
         public static final KeyStroke        BS                                                = KeyStroke.getInstance(0, SWT.BS);
 54  
         /** The delete keystroke. */
 55  1
         public static final KeyStroke        DELETE                                        = KeyStroke.getInstance(0, SWT.DEL);
 56  
 
 57  
         /** The CR keystroke. */
 58  1
         public static final KeyStroke        CR                                                = KeyStroke.getInstance(0, SWT.CR);
 59  
         /** The LF keystroke. */
 60  1
         public static final KeyStroke        LF                                                = KeyStroke.getInstance(0, SWT.LF);
 61  
         /** The TAB keystroke. */
 62  1
         public static final KeyStroke        TAB                                                = KeyStroke.getInstance(0, SWT.TAB);
 63  
         /** The SPACE keystroke. */
 64  1
         public static final KeyStroke        SPACE                                        = KeyStroke.getInstance(0, ' ');
 65  
 
 66  
         /** The F1 keystroke. */
 67  1
         public static final KeyStroke        F1                                                = KeyStroke.getInstance(0, SWT.F1);
 68  
         /** The F2 keystroke. */
 69  1
         public static final KeyStroke        F2                                                = KeyStroke.getInstance(0, SWT.F2);
 70  
         /** The F3 keystroke. */
 71  1
         public static final KeyStroke        F3                                                = KeyStroke.getInstance(0, SWT.F3);
 72  
         /** The F4 keystroke. */
 73  1
         public static final KeyStroke        F4                                                = KeyStroke.getInstance(0, SWT.F4);
 74  
         /** The F5 keystroke. */
 75  1
         public static final KeyStroke        F5                                                = KeyStroke.getInstance(0, SWT.F5);
 76  
         /** The F6 keystroke. */
 77  1
         public static final KeyStroke        F6                                                = KeyStroke.getInstance(0, SWT.F6);
 78  
         /** The F7 keystroke. */
 79  1
         public static final KeyStroke        F7                                                = KeyStroke.getInstance(0, SWT.F7);
 80  
         /** The F8 keystroke. */
 81  1
         public static final KeyStroke        F8                                                = KeyStroke.getInstance(0, SWT.F8);
 82  
         /** The F9 keystroke. */
 83  1
         public static final KeyStroke        F9                                                = KeyStroke.getInstance(0, SWT.F9);
 84  
         /** The F10 keystroke. */
 85  1
         public static final KeyStroke        F10                                                = KeyStroke.getInstance(0, SWT.F10);
 86  
         /** The F11 keystroke. */
 87  1
         public static final KeyStroke        F11                                                = KeyStroke.getInstance(0, SWT.F11);
 88  
         /** The F12 keystroke. */
 89  1
         public static final KeyStroke        F12                                                = KeyStroke.getInstance(0, SWT.F12);
 90  
 
 91  
         /** The up keystroke. */
 92  1
         public static final KeyStroke        UP                                                = KeyStroke.getInstance(0, SWT.ARROW_UP);
 93  
         /** The down keystroke. */
 94  1
         public static final KeyStroke        DOWN                                        = KeyStroke.getInstance(0, SWT.ARROW_DOWN);
 95  
         /** The left keystroke. */
 96  1
         public static final KeyStroke        LEFT                                        = KeyStroke.getInstance(0, SWT.ARROW_LEFT);
 97  
         /** The right keystroke. */
 98  1
         public static final KeyStroke        RIGHT                                        = KeyStroke.getInstance(0, SWT.ARROW_RIGHT);
 99  
 
 100  
         /** The END keystroke. */
 101  1
         public static final KeyStroke        END                                                = KeyStroke.getInstance(0, SWT.END);
 102  
         /** The HOME keystroke. */
 103  1
         public static final KeyStroke        HOME                                        = KeyStroke.getInstance(0, SWT.HOME);
 104  
         /** The PAGE_UP keystroke. */
 105  1
         public static final KeyStroke        PAGE_UP                                        = KeyStroke.getInstance(0, SWT.PAGE_UP);
 106  
         /** The PAGE_DOWN keystroke. */
 107  1
         public static final KeyStroke        PAGE_DOWN                                = KeyStroke.getInstance(0, SWT.PAGE_DOWN);
 108  
 
 109  
         /** The keyboard layout to use for mapping characters. */
 110  1
         private static KeyboardLayout        defaultKeyboardLayout        = KeyboardLayout.getDefaultKeyboardLayout();
 111  
 
 112  
         /**
 113  
          * @param ch the character to convert to {@link KeyStroke}s.
 114  
          * @return the {@link KeyStroke}s corresponding to the character.
 115  
          */
 116  
         static KeyStroke[] create(char ch) {
 117  96
                 KeyStroke keyStroke = defaultKeyboardLayout.keyStrokeFor(ch);
 118  96
                 if (keyStroke.getModifierKeys() == KeyStroke.NO_KEY)
 119  73
                         return new KeyStroke[] { keyStroke };
 120  23
                 KeyStroke modifier = KeyStroke.getInstance(keyStroke.getModifierKeys(), 0);
 121  23
                 KeyStroke key = KeyStroke.getInstance(0, keyStroke.getNaturalKey());
 122  23
                 return new KeyStroke[] { modifier, key };
 123  
         }
 124  
 
 125  
         static char toCharacter(KeyStroke... keys) {
 126  0
                 int modifierKeys = 0;
 127  0
                 int ch = 0;
 128  0
                 for (KeyStroke keyStroke : keys) {
 129  0
                         modifierKeys |= keyStroke.getModifierKeys();
 130  0
                         ch |= keyStroke.getNaturalKey();
 131  
                 }
 132  
 
 133  0
                 return defaultKeyboardLayout.toCharacter(KeyStroke.getInstance(modifierKeys, ch));
 134  
         }
 135  
 
 136  
         /**
 137  
          * @param modificationKeys a combination of.
 138  
          * @param c the character to type.
 139  
          * @return the keystrokes corresponding to the modification keys and character.
 140  
          */
 141  
         public static KeyStroke[] toKeys(int modificationKeys, char c) {
 142  11
                 LinkedHashSet<KeyStroke> keys = new LinkedHashSet<KeyStroke>();
 143  11
                 if (modificationKeys != 0) {
 144  9
                         int[] sortModifierKeys = sortModifierKeys(modificationKeys);
 145  45
                         for (int modifierKey : sortModifierKeys) {
 146  36
                                 if (modifierKey != KeyStroke.NO_KEY)
 147  18
                                         keys.add(KeyStroke.getInstance(modifierKey, 0));
 148  
                         }
 149  
                 }
 150  11
                 if (c != 0)
 151  6
                         keys.addAll(Arrays.asList(Keystrokes.create(c)));
 152  11
                 return keys.toArray(new KeyStroke[] {});
 153  
         }
 154  
 
 155  
         // FIXME: performance improvement: put in a hash of input and output values.
 156  
         private static int[] sortModifierKeys(final int modifierKeys) {
 157  9
                 final IKeyLookup lookup = KeyLookupFactory.getDefault();
 158  9
                 final int[] sortedKeys = new int[4];
 159  9
                 int index = 0;
 160  
 
 161  9
                 if ((modifierKeys & lookup.getAlt()) != 0) {
 162  3
                         sortedKeys[index++] = lookup.getAlt();
 163  
                 }
 164  9
                 if ((modifierKeys & lookup.getCommand()) != 0) {
 165  1
                         sortedKeys[index++] = lookup.getCommand();
 166  
                 }
 167  9
                 if ((modifierKeys & lookup.getCtrl()) != 0) {
 168  7
                         sortedKeys[index++] = lookup.getCtrl();
 169  
                 }
 170  9
                 if ((modifierKeys & lookup.getShift()) != 0) {
 171  7
                         sortedKeys[index++] = lookup.getShift();
 172  
                 }
 173  
 
 174  9
                 return sortedKeys;
 175  
         }
 176  
 
 177  
 }