1 19 20 package org.netbeans.modules.form.editors; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.beans.PropertyEditorSupport ; 25 import java.lang.reflect.*; 26 import java.util.*; 27 import javax.swing.*; 28 29 import org.netbeans.modules.form.NamedPropertyEditor; 30 31 import org.openide.awt.Mnemonics; 32 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 33 import org.openide.util.NbBundle; 34 35 public class KeyStrokeEditor extends PropertyEditorSupport 36 implements XMLPropertyEditor, NamedPropertyEditor { 37 private String TXT_CTRL; 38 private String TXT_ALT; 39 private String TXT_SHIFT; 40 41 public KeyStrokeEditor() { 42 ResourceBundle bundle = 43 org.openide.util.NbBundle.getBundle(KeyStrokeEditor.class); 44 TXT_CTRL = bundle.getString("CTL_CtrlAsText"); TXT_SHIFT = bundle.getString("CTL_ShiftAsText"); TXT_ALT = bundle.getString("CTL_AltAsText"); } 48 49 public String getJavaInitializationString() { 50 KeyStroke key =(KeyStroke) getValue(); 51 int mods = key.getModifiers(); 52 StringBuffer modsText = new StringBuffer (); 53 54 if (0 !=(mods 55 &(InputEvent.ALT_MASK | InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK))) { 56 if (0 !=(mods & InputEvent.ALT_MASK)) 57 modsText.append("java.awt.event.InputEvent.ALT_MASK"); if (0 !=(mods & InputEvent.SHIFT_MASK)) { 59 if (modsText.length() > 0) 60 modsText.append(" | "); modsText.append("java.awt.event.InputEvent.SHIFT_MASK"); } 63 if (0 !=(mods & InputEvent.CTRL_MASK)) { 64 if (modsText.length() > 0) 65 modsText.append(" | "); modsText.append("java.awt.event.InputEvent.CTRL_MASK"); } 68 } 69 else 70 modsText.append("0"); 72 return "javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent." + getVirtualkeyName(key.getKeyCode()) + ", " + modsText.toString() + ")"; } 75 76 public String getAsText() { 77 KeyStroke key = (KeyStroke) getValue(); 78 return key != null ? keyStrokeAsString(key, true) : "null"; } 80 81 public void setAsText(String text) throws IllegalArgumentException { 82 if (text == null || "".equals(text) || "null".equals(text)) { setValue(null); 84 return; 85 } 86 87 KeyStroke key = keyStrokeFromString(text, true); 88 if (key == null) 89 throw new IllegalArgumentException ("Unrecognized key: " + text); else 91 setValue(key); 92 } 93 94 private static String getVirtualkeyName(int keycode) { 95 Field[] fields = KeyEvent.class.getDeclaredFields(); 96 for (int i = 0; i < fields.length; i++) { 97 Field f = fields[i]; 98 int modifiers = f.getModifiers(); 99 if (Modifier.isPublic(modifiers) 100 && Modifier.isStatic(modifiers) 101 && Modifier.isFinal(modifiers) 102 && f.getType() == Integer.TYPE 103 && f.getName().startsWith("VK_")) { try { 105 if (f.getInt(KeyEvent.class) == keycode) { 106 return f.getName(); 107 } 108 } 109 catch (IllegalAccessException ex) { 110 ex.printStackTrace(); } 112 } 113 } 114 return null; 115 } 116 117 private KeyStroke keyStrokeFromString(String s, boolean i18ned) { 118 StringTokenizer st = new StringTokenizer(s, "+"); String token; 120 int mods = 0; 121 int keycode = 0; 122 123 String alt = i18ned ? TXT_ALT : "Alt"; String shift = i18ned ? TXT_SHIFT : "Shift"; String ctrl = i18ned ? TXT_CTRL : "Ctrl"; 127 while (st.hasMoreTokens() &&(token = st.nextToken()) != null) { 128 if (alt.equalsIgnoreCase(token)) 129 mods |= InputEvent.ALT_MASK; 130 else if (shift.equalsIgnoreCase(token)) 131 mods |= InputEvent.SHIFT_MASK; 132 else if (ctrl.equalsIgnoreCase(token)) 133 mods |= InputEvent.CTRL_MASK; 134 else { 135 String keycodeName = "VK_" + token.toUpperCase(); try { 137 keycode = KeyEvent.class.getField(keycodeName).getInt(KeyEvent.class); 138 } 139 catch (Exception e) { 140 } 142 } 143 } 144 if (keycode != 0) 145 return KeyStroke.getKeyStroke(keycode, mods); 146 else 147 return null; 148 } 149 150 private String keyStrokeAsString(KeyStroke key, boolean i18ned) { 151 String alt = i18ned ? TXT_ALT : "Alt"; String shift = i18ned ? TXT_SHIFT : "Shift"; String ctrl = i18ned ? TXT_CTRL : "Ctrl"; 155 StringBuffer buf = new StringBuffer (); 156 int mods = key.getModifiers(); 157 int modMasks[] = { InputEvent.SHIFT_MASK, InputEvent.CTRL_MASK, 158 InputEvent.ALT_MASK, }; 159 String modMaskStrings[] = { shift, ctrl, alt, }; 160 161 for (int i = 0; i < modMasks.length; i++) { 162 if ((mods & modMasks[i]) != 0) { 163 buf.append(modMaskStrings[i]); 164 buf.append("+"); } 166 } 167 String keyName = getVirtualkeyName(key.getKeyCode()); 168 if (keyName != null) { 169 buf.append(keyName.substring(3)); 170 } 171 return buf.toString(); 172 } 173 174 178 public static final String XML_KEYSTROKE = "KeyStroke"; public static final String ATTR_KEY = "key"; 181 public void readFromXML(org.w3c.dom.Node element) throws java.io.IOException { 182 if (!XML_KEYSTROKE.equals(element.getNodeName())) { 183 throw new java.io.IOException (); 184 } 185 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 186 try { 187 String value = attributes.getNamedItem(ATTR_KEY).getNodeValue(); 188 KeyStroke key = keyStrokeFromString(value, false); 189 if (key == null) 190 throw new IllegalArgumentException (); 191 else 192 setValue(key); 193 } 194 catch (Exception e) { 195 throw new java.io.IOException (); 196 } 197 } 198 199 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 200 KeyStroke key = (KeyStroke) getValue(); 201 String str = key != null ? keyStrokeAsString(key, false) : "null"; 203 org.w3c.dom.Element el = doc.createElement(XML_KEYSTROKE); 204 el.setAttribute(ATTR_KEY, str); 205 return el; 206 } 207 208 212 public boolean supportsCustomEditor() { 213 return true; 214 } 215 216 public java.awt.Component getCustomEditor() { 217 return new CustomEditor(); 218 } 219 220 public String getDisplayName() { 222 return NbBundle.getBundle(getClass()).getString("CTL_KeyStrokeEditor_DisplayName"); } 224 225 private static String [] _virtualKeys; 226 227 private class CustomEditor extends JPanel 228 { 229 private KeyGrabberField _keyGrabber; 230 private JCheckBox _ctrl, _alt, _shift; 231 private JComboBox _virtualKey; 232 233 CustomEditor() { 234 setLayout(new GridBagLayout()); 235 236 ResourceBundle bundle = 237 org.openide.util.NbBundle.getBundle(KeyStrokeEditor.class); 238 239 JLabel virtualKeyLabel = new JLabel(); 240 Mnemonics.setLocalizedText(virtualKeyLabel, bundle.getString("CTL_VirtualKey")); 242 GridBagConstraints gbc = new GridBagConstraints(); 243 gbc.gridx = 0; 244 gbc.gridy = 0; 245 gbc.gridwidth = 1; 246 gbc.anchor = gbc.WEST; 247 gbc.fill = gbc.NONE; 248 gbc.weightx = 0; 249 gbc.weighty = 0; 250 gbc.insets = new Insets(12, 12, 5, 12); 251 add(virtualKeyLabel, gbc); 252 253 gbc = new GridBagConstraints(); 254 gbc.gridx = 1; 255 gbc.gridy = 0; 256 gbc.gridwidth = 1; 257 gbc.fill = gbc.HORIZONTAL; 258 gbc.weightx = 1.0; 259 gbc.weighty = 0; 260 gbc.insets = new Insets(12, 0, 5, 11); 261 add(_virtualKey = new JComboBox(), gbc); 262 _virtualKey.getAccessibleContext().setAccessibleDescription( 263 bundle.getString("ACSD_VirtualKey")); 265 JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 0)); 266 _ctrl = new JCheckBox(); 267 Mnemonics.setLocalizedText(_ctrl, bundle.getString("CTL_Ctrl")); _ctrl.getAccessibleContext().setAccessibleDescription( 269 bundle.getString("ACSD_CtrlKey")); panel.add(_ctrl); 271 _alt = new JCheckBox(); 272 Mnemonics.setLocalizedText(_alt, bundle.getString("CTL_Alt")); _alt.getAccessibleContext().setAccessibleDescription( 274 bundle.getString("ACSD_AltKey")); panel.add(_alt); 276 _shift = new JCheckBox(); 277 Mnemonics.setLocalizedText(_shift, bundle.getString("CTL_Shift")); _shift.getAccessibleContext().setAccessibleDescription( 279 bundle.getString("ACSD_ShiftKey")); panel.add(_shift); 281 virtualKeyLabel.setLabelFor(_virtualKey); 282 283 gbc = new GridBagConstraints(); 284 gbc.gridx = 2; 285 gbc.gridy = 0; 286 gbc.gridwidth = 1; 287 gbc.fill = gbc.NONE; 288 gbc.weightx = 0; 289 gbc.weighty = 0; 290 gbc.insets = new Insets(12, 0, 5, 12); 291 add(panel, gbc); 292 293 JLabel keyStrokeLabel = new JLabel(); 294 Mnemonics.setLocalizedText(keyStrokeLabel, bundle.getString("CTL_KeyStroke")); 296 gbc = new GridBagConstraints(); 297 gbc.gridx = 0; 298 gbc.gridy = 1; 299 gbc.gridwidth = 1; 300 gbc.anchor = gbc.WEST; 301 gbc.fill = gbc.NONE; 302 gbc.weightx = 0; 303 gbc.weighty = 0; 304 gbc.insets = new Insets(0, 12, 0, 12); 305 add(keyStrokeLabel, gbc); 306 307 gbc = new GridBagConstraints(); 308 gbc.gridx = 1; 309 gbc.gridy = 1; 310 gbc.gridwidth = 2; 311 gbc.fill = gbc.HORIZONTAL; 312 gbc.weightx = 1.0; 313 gbc.weighty = 0; 314 gbc.insets = new Insets(0, 0, 0, 11); 315 add(_keyGrabber = new KeyGrabberField(), gbc); 316 _keyGrabber.getAccessibleContext().setAccessibleDescription( 317 bundle.getString("ACSD_KeyStroke")); keyStrokeLabel.setLabelFor(_keyGrabber); 319 320 _keyGrabber.addActionListener(new ActionListener() { 321 public void actionPerformed(ActionEvent ev) { 322 setAsText(_keyGrabber.getText()); 323 } 324 }); 325 326 getAccessibleContext().setAccessibleDescription( 327 bundle.getString("ACSD_KeyStrokeCustomEditor")); 329 331 if (_virtualKeys == null) { 332 java.util.List list = new ArrayList(); 333 334 Field[] fields = KeyEvent.class.getDeclaredFields(); 335 for (int i = 0; i < fields.length; i++) { 336 Field f = fields[i]; 337 int modifiers = f.getModifiers(); 338 if (Modifier.isPublic(modifiers) 339 && Modifier.isStatic(modifiers) 340 && Modifier.isFinal(modifiers) 341 && f.getType() == Integer.TYPE 342 && f.getName().startsWith("VK_")) { list.add(f.getName()); 344 } 345 } 346 _virtualKeys = new String [list.size()]; 347 for (int i = 0; i < list.size(); i++) { 348 _virtualKeys[i] =(String ) list.get(i); 349 } 350 } 351 _virtualKey.addItem(""); for (int i = 0; i < _virtualKeys.length; i++) 353 _virtualKey.addItem(_virtualKeys[i]); 354 355 KeyStroke key =(KeyStroke) getValue(); 356 if (key != null) 357 setKeyStroke(key); 358 359 361 ItemListener il = new ItemListener() { 362 public void itemStateChanged(ItemEvent e) { 363 virtualKeyChanged(); 364 } 365 }; 366 _virtualKey.addItemListener(il); 367 _ctrl.addItemListener(il); 368 _alt.addItemListener(il); 369 _shift.addItemListener(il); 370 } 371 372 java.awt.Component getKeyGrabber() { 373 return _keyGrabber; 374 } 375 376 private void setKeyStroke(KeyStroke key) { 377 _ctrl.setSelected(0 !=(InputEvent.CTRL_MASK & key.getModifiers())); 378 _alt.setSelected(0 !=(InputEvent.ALT_MASK & key.getModifiers())); 379 _shift.setSelected(0 !=(InputEvent.SHIFT_MASK & key.getModifiers())); 380 381 int keycode = key.getKeyCode(); 382 String keyName = getVirtualkeyName(keycode); 383 if (keyName != null) { 384 _virtualKey.setSelectedItem(keyName); 385 _keyGrabber.setText(getAsText()); 386 } 387 } 388 389 private void virtualKeyChanged() { 390 String keyName =(String ) _virtualKey.getSelectedItem(); 391 if ("".equals(keyName)) { _keyGrabber.setText(""); setValue(null); 394 return; 395 } 396 397 try { 398 Field f = KeyEvent.class.getDeclaredField(keyName); 399 int keycode = f.getInt(KeyEvent.class); 400 int mods = 0; 401 if (_ctrl.isSelected()) 402 mods |= InputEvent.CTRL_MASK; 403 if (_shift.isSelected()) 404 mods |= InputEvent.SHIFT_MASK; 405 if (_alt.isSelected()) 406 mods |= InputEvent.ALT_MASK; 407 408 setValue(KeyStroke.getKeyStroke(keycode, mods)); 409 _keyGrabber.setText(getAsText()); 410 } 411 catch (NoSuchFieldException ex) { 412 ex.printStackTrace(); } 414 catch (IllegalAccessException ex) { 415 ex.printStackTrace(); } 417 } 418 419 private class KeyGrabberField extends JTextField { 420 protected void processKeyEvent(KeyEvent e) { 421 if (e.getKeyCode() == KeyEvent.VK_TAB) 422 super.processKeyEvent(e); 423 else if (e.getID() == KeyEvent.KEY_PRESSED) { 424 int keycode = e.getKeyCode(); 425 if (keycode != KeyEvent.VK_CONTROL 426 && keycode != KeyEvent.VK_ALT 427 && keycode != KeyEvent.VK_SHIFT) { 428 KeyStroke key = KeyStroke.getKeyStroke(keycode, e.getModifiers()); 429 setKeyStroke(key); 430 } 431 e.consume(); 432 } 433 } 434 } 435 } 436 } 437 | Popular Tags |