1 19 20 package org.netbeans.modules.options.keymap; 21 import java.awt.event.InputEvent ; 22 import java.awt.event.KeyEvent ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.util.StringTokenizer ; 26 import javax.swing.KeyStroke ; 27 import org.openide.ErrorManager; 28 import org.openide.util.Utilities; 29 30 31 35 class Utils { 36 37 static KeyStroke [] stringToKeyStrokes2 (String key) { 38 StringTokenizer st = new StringTokenizer (key, " "); 39 List result = new ArrayList (); 40 key = null; 41 while (st.hasMoreTokens ()) { 42 String ks = st.nextToken ().trim (); 43 KeyStroke keyStroke = Utilities.stringToKey (ks); 44 if (keyStroke == null) { 46 if (System.getProperty ("org.netbeans.optionsDialog") != null) 47 System.out.println("no key stroke for:" + key); 48 return null; 49 } 50 result.add (keyStroke); 51 } 52 return (KeyStroke []) result.toArray 53 (new KeyStroke [result.size ()]); 54 } 55 56 static String getKeyStrokesAsText (KeyStroke [] keyStrokes, String delim) { 57 if (keyStrokes == null) return ""; 58 if (keyStrokes.length == 0) return ""; 59 StringBuffer sb = new StringBuffer (getKeyStrokeAsText (keyStrokes [0])); 60 int i, k = keyStrokes.length; 61 for (i = 1; i < k; i++) 62 sb.append (delim).append (getKeyStrokeAsText (keyStrokes [i])); 63 return new String (sb); 64 } 65 66 static KeyStroke getKeyStroke (String keyStroke) { 67 int modifiers = 0; 68 if (keyStroke.startsWith ("Ctrl+")) { 69 modifiers |= InputEvent.CTRL_DOWN_MASK; 70 keyStroke = keyStroke.substring (5); 71 } 72 if (keyStroke.startsWith ("Alt+")) { 73 modifiers |= InputEvent.ALT_DOWN_MASK; 74 keyStroke = keyStroke.substring (4); 75 } 76 if (keyStroke.startsWith ("Shift+")) { 77 modifiers |= InputEvent.SHIFT_DOWN_MASK; 78 keyStroke = keyStroke.substring (6); 79 } 80 if (keyStroke.startsWith ("Meta+")) { 81 modifiers |= InputEvent.META_DOWN_MASK; 82 keyStroke = keyStroke.substring (5); 83 } 84 KeyStroke ks = Utilities.stringToKey (keyStroke); 85 if (ks == null) 86 ErrorManager.getDefault ().notify ( 87 new IllegalArgumentException (keyStroke) 88 ); 89 KeyStroke result = KeyStroke.getKeyStroke (ks.getKeyCode (), modifiers); 90 return result; 91 } 92 93 static String getKeyStrokeAsText (KeyStroke keyStroke) { 94 int modifiers = keyStroke.getModifiers (); 95 StringBuffer sb = new StringBuffer (); 96 if ((modifiers & InputEvent.CTRL_DOWN_MASK) > 0) 97 sb.append ("Ctrl+"); 98 if ((modifiers & InputEvent.ALT_DOWN_MASK) > 0) 99 sb.append ("Alt+"); 100 if ((modifiers & InputEvent.SHIFT_DOWN_MASK) > 0) 101 sb.append ("Shift+"); 102 if ((modifiers & InputEvent.META_DOWN_MASK) > 0) 103 sb.append ("Meta+"); 104 if (keyStroke.getKeyCode () != KeyEvent.VK_SHIFT && 105 keyStroke.getKeyCode () != KeyEvent.VK_CONTROL && 106 keyStroke.getKeyCode () != KeyEvent.VK_META && 107 keyStroke.getKeyCode () != KeyEvent.VK_ALT && 108 keyStroke.getKeyCode () != KeyEvent.VK_ALT_GRAPH 109 ) 110 sb.append (Utilities.keyToString ( 111 KeyStroke.getKeyStroke (keyStroke.getKeyCode (), 0) 112 )); 113 return sb.toString (); 114 } 115 } 116 | Popular Tags |