1 21 22 package org.armedbear.j; 23 24 import javax.swing.KeyStroke ; 25 26 public class KeyMapping implements Constants 27 { 28 private final char keyChar; 29 private final int keyCode; 30 private final int modifiers; 31 private final Object command; 32 33 public KeyMapping(int keyCode, int modifiers, Object command) 34 { 35 this.keyChar = 0; 36 this.keyCode = keyCode; 37 this.modifiers = modifiers; 38 if (command instanceof String ) 39 this.command = ((String )command).intern(); 40 else 41 this.command = command; 42 } 43 44 public KeyMapping(char keyChar, Object command) 45 { 46 this.keyChar = keyChar; 47 this.keyCode = 0; 48 this.modifiers = 0; 49 if (command instanceof String ) 50 this.command = ((String )command).intern(); 51 else 52 this.command = command; 53 } 54 55 private KeyMapping(KeyStroke keyStroke, String command) 56 { 57 char c = keyStroke.getKeyChar(); 58 keyChar = c == 0xffff ? 0 : c; 59 keyCode = keyStroke.getKeyCode(); 60 modifiers = keyStroke.getModifiers() & 0x0f; 62 if (command != null) 63 this.command = command.intern(); 64 else 65 this.command = null; 66 } 67 68 public static KeyMapping createKeyMapping(String s) 70 { 71 s = s.trim(); 72 String parameters = null; 73 int index = s.indexOf('('); 74 if (index >= 0) { 75 parameters = s.substring(index); 76 s = s.substring(0, index).trim(); 77 } 78 index = s.lastIndexOf(' '); 79 if (index < 0) 80 return null; 81 String keyText = s.substring(0, index).trim(); 82 String command = s.substring(index + 1).trim(); 83 if (parameters != null) 84 command += parameters; 85 return createKeyMapping(keyText, command); 86 } 87 88 private static KeyMapping createKeyMapping(String keyText, String command) 89 { 90 KeyStroke keyStroke = Utilities.getKeyStroke(keyText); 91 if (keyStroke == null) 92 return null; 93 if (command != null) 94 command = command.trim(); 95 return new KeyMapping(keyStroke, command); 96 } 97 98 public final char getKeyChar() 99 { 100 return keyChar; 101 } 102 103 public final int getKeyCode() 104 { 105 return keyCode; 106 } 107 108 public final int getModifiers() 109 { 110 return modifiers; 111 } 112 113 public final Object getCommand() 114 { 115 return command; 116 } 117 118 public String toString() 119 { 120 FastStringBuffer sb = new FastStringBuffer(64); 121 sb.append(Utilities.getKeyText(keyChar, keyCode, modifiers)); 122 if (command != null) { 123 while (sb.length() < 32) 124 sb.append(' '); 125 sb.append(command); 126 } 127 return sb.toString(); 128 } 129 130 public final String getKeyText() 131 { 132 return Utilities.getKeyText(keyChar, keyCode, modifiers); 133 } 134 } 135 | Popular Tags |