1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.KeyStroke ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.InputEvent ; 29 import java.awt.Toolkit ; 30 import java.util.Hashtable ; 31 import java.util.StringTokenizer ; 32 import org.gjt.sp.jedit.*; 33 import org.gjt.sp.util.Log; 34 import org.gjt.sp.jedit.msg.*; 35 import javax.swing.event.*; 36 38 44 public class DefaultInputHandler extends InputHandler 45 { 46 54 public DefaultInputHandler(View view, Hashtable bindings) 55 { 56 super(view); 57 58 if(bindings == null) 59 throw new NullPointerException (); 60 this.bindings = this.currentBindings = bindings; 61 } 63 68 public DefaultInputHandler(View view) 69 { 70 this(view,new Hashtable ()); 71 } 73 82 public DefaultInputHandler(View view, DefaultInputHandler copy) 83 { 84 this(view,copy.bindings); 85 } 87 98 public void addKeyBinding(String keyBinding, String action) 99 { 100 addKeyBinding(keyBinding,(Object )action); 101 } 103 113 public void addKeyBinding(String keyBinding, EditAction action) 114 { 115 addKeyBinding(keyBinding,(Object )action); 116 } 118 129 public void addKeyBinding(String keyBinding, Object action) 130 { 131 Hashtable current = bindings; 132 133 String prefixStr = null; 134 135 StringTokenizer st = new StringTokenizer (keyBinding); 136 while(st.hasMoreTokens()) 137 { 138 String keyCodeStr = st.nextToken(); 139 if(prefixStr == null) 140 prefixStr = keyCodeStr; 141 else 142 prefixStr = prefixStr + " " + keyCodeStr; 143 144 KeyEventTranslator.Key keyStroke = KeyEventTranslator.parseKey(keyCodeStr); 145 if(keyStroke == null) 146 return; 147 148 if(st.hasMoreTokens()) 149 { 150 Object o = current.get(keyStroke); 151 if(o instanceof Hashtable ) 152 current = (Hashtable )o; 153 else 154 { 155 Hashtable hash = new Hashtable (); 156 hash.put(PREFIX_STR,prefixStr); 157 o = hash; 158 current.put(keyStroke,o); 159 current = (Hashtable )o; 160 } 161 } 162 else 163 current.put(keyStroke,action); 164 } 165 } 167 173 public void removeKeyBinding(String keyBinding) 174 { 175 Hashtable current = bindings; 176 177 StringTokenizer st = new StringTokenizer (keyBinding); 178 while(st.hasMoreTokens()) 179 { 180 String keyCodeStr = st.nextToken(); 181 KeyEventTranslator.Key keyStroke = KeyEventTranslator.parseKey(keyCodeStr); 182 if(keyStroke == null) 183 return; 184 185 if(st.hasMoreTokens()) 186 { 187 Object o = current.get(keyStroke); 188 if(o instanceof Hashtable ) 189 current = ((Hashtable )o); 190 else if(o != null) 191 { 192 current.remove(keyStroke); 195 return; 196 } 197 else 198 { 199 return; 201 } 202 } 203 else 204 current.remove(keyStroke); 205 } 206 } 208 212 public void removeAllKeyBindings() 213 { 214 bindings.clear(); 215 } 217 224 public Object getKeyBinding(String keyBinding) 225 { 226 Hashtable current = bindings; 227 StringTokenizer st = new StringTokenizer (keyBinding); 228 229 while(st.hasMoreTokens()) 230 { 231 KeyEventTranslator.Key keyStroke = KeyEventTranslator.parseKey( 232 st.nextToken()); 233 if(keyStroke == null) 234 return null; 235 236 if(st.hasMoreTokens()) 237 { 238 Object o = current.get(keyStroke); 239 if(o instanceof Hashtable ) 240 { 241 if(!st.hasMoreTokens()) 242 return o; 243 else 244 current = (Hashtable )o; 245 } 246 else 247 return o; 248 } 249 else 250 { 251 return current.get(keyStroke); 252 } 253 } 254 255 return null; 256 } 258 262 public boolean isPrefixActive() 263 { 264 return bindings != currentBindings 265 || super.isPrefixActive(); 266 } 268 273 public void setBindings(Hashtable bindings) 274 { 275 this.bindings = this.currentBindings = bindings; 276 } 278 public void setCurrentBindings(Hashtable bindings) 280 { 281 view.getStatus().setMessage((String )bindings.get(PREFIX_STR)); 282 currentBindings = bindings; 283 } 285 292 public boolean handleKey(KeyEventTranslator.Key keyStroke,boolean dryRun) 293 { 294 char input = '\0'; 295 if(keyStroke.modifiers == null 296 || keyStroke.modifiers.equals("S")) 297 { 298 switch(keyStroke.key) 299 { 300 case '\n': 301 case '\t': 302 input = (char)keyStroke.key; 303 break; 304 default: 305 input = keyStroke.input; 306 break; 307 } 308 } 309 310 if(readNextChar != null) 311 { 312 if(input != '\0') 313 { 314 if (!dryRun) { 315 setCurrentBindings(bindings); 316 invokeReadNextChar(input); 317 repeatCount = 1; 318 } 319 return true; 320 } 321 else 322 { 323 if (!dryRun) { 324 readNextChar = null; 325 view.getStatus().setMessage(null); 326 } 327 } 328 } 329 330 Object o = currentBindings.get(keyStroke); 331 if(o == null) 332 { 333 if (!dryRun) { 334 if(currentBindings != bindings) 339 { 340 Toolkit.getDefaultToolkit().beep(); 341 repeatCount = 1; 344 setCurrentBindings(bindings); 345 } 346 else if(input != '\0') { 347 if (!keyStroke.isFromGlobalContext()) { userInput(input); 349 } 350 } else { 351 if(KeyEventWorkaround.isNumericKeypad(keyStroke.key)) 354 KeyEventWorkaround.numericKeypadKey(); 355 } 356 sendShortcutPrefixOff(); 357 } 358 } 359 else if(o instanceof Hashtable ) 360 { 361 if (!dryRun) { 362 setCurrentBindings((Hashtable )o); 363 ShortcutPrefixActiveEvent.firePrefixStateChange(currentBindings, true); 364 shortcutOn = true; 365 } 366 return true; 367 } 368 else if(o instanceof String ) 369 { 370 if (!dryRun) { 371 setCurrentBindings(bindings); 372 sendShortcutPrefixOff(); 373 invokeAction((String )o); 374 } 375 return true; 376 } 377 else if(o instanceof EditAction) 378 { 379 if (!dryRun) { 380 setCurrentBindings(bindings); 381 sendShortcutPrefixOff(); 382 invokeAction((EditAction)o); 383 } 384 return true; 385 } 386 if (!dryRun) { 387 sendShortcutPrefixOff(); 388 } 389 return false; 390 } 392 396 protected void sendShortcutPrefixOff() 397 { 398 if( shortcutOn == true ) 399 { 400 ShortcutPrefixActiveEvent.firePrefixStateChange(null, false); 401 shortcutOn = false; 402 } 403 } 405 protected boolean shortcutOn=false; 406 407 416 public static char getSymbolicModifierName(int mod) 417 { 418 return KeyEventTranslator.getSymbolicModifierName(mod); 419 } 421 430 public static String getModifierString(InputEvent evt) 431 { 432 return KeyEventTranslator.getModifierString(evt); 433 } 435 437 public static Object PREFIX_STR = "PREFIX_STR"; 439 440 private Hashtable bindings; 441 private Hashtable currentBindings; 442 444 } 445 | Popular Tags |