View Javadoc

1   package net.sf.ovanttasks.ovnative.win32;
2   
3   import java.util.StringTokenizer;
4   
5   /**
6    * A helper class that translates betwen Win hotkeys and the correnspondenting Keycode from {@link KeyEvent}.
7    * 
8    * @author arnep@users.sf.net
9    */
10  class HotKeyTools {
11  
12      private static final int HOTKEYF_SHIFT = 0x0100;
13      private static final int HOTKEYF_CONTROL = 0x0200;
14      private static final int HOTKEYF_ALT = 0x0400;
15      private static final int HOTKEYF_EXT = 0x0800;
16      private static final String SEPARATOR = " + ";
17      private static final String ALT = "Alt";
18      private static final String SHIFT = "Shift";
19      private static final String CTRL = "Ctrl";
20      private static final String EXT = "Ext";
21  
22      public static short parseString(String hotkey) {
23          int result = 0;
24          StringTokenizer st = new StringTokenizer(hotkey, SEPARATOR);
25          while (st.hasMoreTokens()) {
26              String token = st.nextToken();
27              if (CTRL.equalsIgnoreCase(token)) {
28                  result |= HOTKEYF_CONTROL;
29              } else if (SHIFT.equalsIgnoreCase(token)) {
30                  result |= HOTKEYF_SHIFT;
31              } else if (ALT.equalsIgnoreCase(token)) {
32                  result |= HOTKEYF_ALT;
33              } else if (EXT.equalsIgnoreCase(token)) {
34                  result |= HOTKEYF_EXT;
35              } else if (token.length() == 1) {
36                  result |= token.charAt(0);
37              } else {
38                  throw new IllegalArgumentException("Cant parse string");
39              }
40          }
41          return (short) result;
42      }
43  
44      /**
45       * Key scancode from 
46       * @param hotkey
47       * @return
48       */
49      public static String toString(short hotkey) {
50          StringBuilder sb = new StringBuilder();
51          if ((hotkey & HOTKEYF_CONTROL) != 0) {
52              sb.append(CTRL);
53          }
54          if ((hotkey & HOTKEYF_SHIFT) != 0) {
55              if (sb.length() > 0) {
56                  sb.append(SEPARATOR);
57              }
58              sb.append(SHIFT);
59          }
60          if ((hotkey & HOTKEYF_ALT) != 0) {
61              if (sb.length() > 0) {
62                  sb.append(SEPARATOR);
63              }
64              sb.append(ALT);
65          }
66          if ((hotkey & HOTKEYF_EXT) != 0) {
67              if (sb.length() > 0) {
68                  sb.append(SEPARATOR);
69              }
70              sb.append(EXT);
71          }
72          if (sb.length() > 0) {
73              sb.append(SEPARATOR);
74          }
75          sb.append((char) (hotkey & 0x00FF));
76          return sb.toString();
77      }
78  }