1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.*; 27 import javax.swing.text.JTextComponent ; 28 29 import org.gjt.sp.jedit.textarea.JEditTextArea; 30 import org.gjt.sp.jedit.*; 31 import org.gjt.sp.jedit.buffer.JEditBuffer; 32 import org.gjt.sp.jedit.input.AbstractInputHandler; 33 import org.gjt.sp.util.Log; 34 35 import java.awt.event.KeyEvent ; 36 import java.awt.*; 37 39 51 public abstract class InputHandler extends AbstractInputHandler 52 { 53 58 public InputHandler(View view) 59 { 60 super(); 61 this.view = view; 62 } 64 71 public abstract void addKeyBinding(String keyBinding, String action); 72 74 81 public abstract void addKeyBinding(String keyBinding, EditAction action); 82 84 89 public abstract void removeKeyBinding(String keyBinding); 90 92 96 public abstract void removeAllKeyBindings(); 97 99 105 public final boolean handleKey(KeyEventTranslator.Key keyStroke) 106 { 107 return handleKey(keyStroke, false); 108 } 109 110 116 public void processKeyEvent(KeyEvent evt, int from, boolean global) 117 { 118 if(Debug.DUMP_KEY_EVENTS) 119 { 120 Log.log(Log.DEBUG,this,"Key event : " 121 + GrabKeyDialog.toString(evt) + " from " + from); 122 Log.log(Log.DEBUG,this,view+".isFocused()="+view.isFocused()+'.',new Exception ()); 123 } 124 125 if(view.getTextArea().hasFocus() && from == View.VIEW) 126 return; 127 128 evt = _preprocessKeyEvent(evt); 129 if(evt == null) 130 return; 131 132 if(Debug.DUMP_KEY_EVENTS) 133 { 134 Log.log(Log.DEBUG,this,"Key event after workaround: " 135 + GrabKeyDialog.toString(evt) + " from " + from); 136 } 137 138 Component prefixFocusOwner = view.getPrefixFocusOwner(); 139 boolean focusOnTextArea = false; 140 switch(evt.getID()) 141 { 142 case KeyEvent.KEY_TYPED: 143 if(prefixFocusOwner != null) 147 { 148 if(prefixFocusOwner.isShowing()) 149 { 150 prefixFocusOwner.requestFocus(); 151 focusOnTextArea = true; 152 } 153 } 154 155 if(keyEventInterceptor != null) 156 keyEventInterceptor.keyTyped(evt); 157 else if(from == View.ACTION_BAR 158 || (Debug.GLOBAL_SHORTCUTS_FOR_DOCKED_DOCKABLES && 159 Options.SIMPLIFIED_KEY_HANDLING) 160 || isPrefixActive() 161 || view.getTextArea().hasFocus()) 162 { 163 processKeyEventKeyStrokeHandling(evt,from,"type ",global); 164 } 165 166 167 processKeyEventSub(focusOnTextArea); 168 169 break; 170 case KeyEvent.KEY_PRESSED: 171 if(keyEventInterceptor != null) 172 keyEventInterceptor.keyPressed(evt); 173 else if(KeyEventWorkaround.isBindable(evt.getKeyCode())) 174 { 175 if(prefixFocusOwner != null) 176 { 177 if(prefixFocusOwner.isShowing()) 178 { 179 prefixFocusOwner.requestFocus(); 180 focusOnTextArea = true; 181 } 182 view.setPrefixFocusOwner(null); 183 } 184 185 processKeyEventKeyStrokeHandling(evt,from,"press",global); 186 187 processKeyEventSub(focusOnTextArea); 188 189 } 190 break; 191 case KeyEvent.KEY_RELEASED: 192 if(keyEventInterceptor != null) 193 keyEventInterceptor.keyReleased(evt); 194 break; 195 } 196 } 198 private KeyEvent _preprocessKeyEvent(KeyEvent evt) 200 { 201 if(view.isClosed()) 202 return null; 203 Component focusOwner = view.getFocusOwner(); 204 if (Options.SIMPLIFIED_KEY_HANDLING) 205 { 206 233 } 234 else 235 { 236 if(focusOwner instanceof JComponent) 237 { 238 JComponent comp = (JComponent)focusOwner; 239 InputMap map = comp.getInputMap(); 240 ActionMap am = comp.getActionMap(); 241 242 if(map != null && am != null && comp.isEnabled()) 243 { 244 KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(evt); 245 Object binding = map.get(keyStroke); 246 if(binding != null && am.get(binding) != null) 247 { 248 return null; 249 } 250 } 251 } 252 } 253 254 if(focusOwner instanceof JTextComponent ) 255 { 256 if(evt.getID() == KeyEvent.KEY_PRESSED) 259 { 260 switch(evt.getKeyCode()) 261 { 262 case KeyEvent.VK_ENTER: 263 case KeyEvent.VK_TAB: 264 case KeyEvent.VK_BACK_SPACE: 265 case KeyEvent.VK_SPACE: 266 return null; 267 } 268 } 269 } 270 271 if(evt.isConsumed()) 272 return null; 273 274 if(Debug.DUMP_KEY_EVENTS) 275 { 276 Log.log(Log.DEBUG,this,"Key event (preprocessing) : " 277 + GrabKeyDialog.toString(evt)); 278 } 279 280 return KeyEventWorkaround.processKeyEvent(evt); 281 } 283 private void processKeyEventSub(boolean focusOnTextArea) 285 { 286 if(view.isClosed()) 289 return; 290 291 if(isPrefixActive()) 295 { 296 Component focusOwner = view.getFocusOwner(); 297 if(focusOwner instanceof JTextComponent ) 298 { 299 view.setPrefixFocusOwner(focusOwner); 300 view.getTextArea().requestFocus(); 301 } 302 else if(focusOnTextArea) 303 { 304 view.getTextArea().requestFocus(); 305 } 306 else 307 { 308 view.setPrefixFocusOwner(null); 309 } 310 } 311 else 312 { 313 view.setPrefixFocusOwner(null); 314 } 315 } 316 318 322 public int getRepeatCount() 323 { 324 return repeatCount; 325 } 327 332 public void setRepeatCount(int repeatCount) 333 { 334 int oldRepeatCount = this.repeatCount; 335 this.repeatCount = repeatCount; 336 if(oldRepeatCount != repeatCount) 337 view.getStatus().setMessage(null); 338 } 340 345 public EditAction getLastAction() 346 { 347 return lastAction; 348 } 350 358 public void readNextChar(String msg, String code) 359 { 360 view.getStatus().setMessage(msg); 361 readNextChar = code; 362 } 364 368 public void readNextChar(String code) 369 { 370 readNextChar = code; 371 } 373 380 public void invokeAction(String action) 381 { 382 invokeAction(jEdit.getAction(action)); 383 } 385 391 public void invokeAction(EditAction action) 392 { 393 JEditBuffer buffer = view.getBuffer(); 394 395 397 398 if(!action.noRememberLast()) 400 { 401 HistoryModel.getModel("action").addItem(action.getName()); 402 if(lastAction == action) 403 lastActionCount++; 404 else 405 { 406 lastAction = action; 407 lastActionCount = 1; 408 } 409 } 410 411 int _repeatCount = repeatCount; 413 414 if(action.noRepeat() || _repeatCount == 1) 416 action.invoke(view); 417 else 418 { 419 if(_repeatCount > REPEAT_COUNT_THRESHOLD) 421 { 422 String label = action.getLabel(); 423 if(label == null) 424 label = action.getName(); 425 else 426 label = GUIUtilities.prettifyMenuLabel(label); 427 428 Object [] pp = { label, _repeatCount }; 429 430 if(GUIUtilities.confirm(view,"large-repeat-count",pp, 431 JOptionPane.WARNING_MESSAGE, 432 JOptionPane.YES_NO_OPTION) 433 != JOptionPane.YES_OPTION) 434 { 435 repeatCount = 1; 436 view.getStatus().setMessage(null); 437 return; 438 } 439 } 440 441 try 442 { 443 buffer.beginCompoundEdit(); 444 445 for(int i = 0; i < _repeatCount; i++) 446 action.invoke(view); 447 } 448 finally 449 { 450 buffer.endCompoundEdit(); 451 } 452 } 453 454 Macros.Recorder recorder = view.getMacroRecorder(); 455 456 if(recorder != null && !action.noRecord()) 457 recorder.record(_repeatCount,action.getCode()); 458 459 if(_repeatCount != 1) 462 { 463 if(readNextChar != null) 466 return; 467 468 repeatCount = 1; 469 view.getStatus().setMessage(null); 470 } 471 } 473 public void invokeLastAction() 475 { 476 if(lastAction == null) 477 view.getToolkit().beep(); 478 else 479 invokeAction(lastAction); 480 } 482 483 protected View view; 485 486 protected EditAction lastAction; 487 488 490 protected void userInput(char ch) 492 { 493 lastActionCount = 0; 494 495 JEditTextArea textArea = view.getTextArea(); 496 497 500 501 if(repeatCount == 1) 502 textArea.userInput(ch); 503 else 504 { 505 if(repeatCount > REPEAT_COUNT_THRESHOLD) 507 { 508 Object [] pp = { String.valueOf(ch), 509 repeatCount }; 510 511 if(GUIUtilities.confirm(view, 512 "large-repeat-count.user-input",pp, 513 JOptionPane.WARNING_MESSAGE, 514 JOptionPane.YES_NO_OPTION) 515 != JOptionPane.YES_OPTION) 516 { 517 repeatCount = 1; 518 view.getStatus().setMessage(null); 519 return; 520 } 521 } 522 523 JEditBuffer buffer = view.getBuffer(); 524 try 525 { 526 if(repeatCount != 1) 527 buffer.beginCompoundEdit(); 528 for(int i = 0; i < repeatCount; i++) 529 textArea.userInput(ch); 530 } 531 finally 532 { 533 if(repeatCount != 1) 534 buffer.endCompoundEdit(); 535 } 536 } 537 538 Macros.Recorder recorder = view.getMacroRecorder(); 539 540 if(recorder != null) 541 { 542 recorder.recordInput(repeatCount,ch, 543 textArea.isOverwriteEnabled()); 544 } 545 546 repeatCount = 1; 547 } 549 protected void invokeReadNextChar(char ch) 551 { 552 JEditBuffer buffer = view.getBuffer(); 553 554 556 557 String charStr = MiscUtilities.charsToEscapes(String.valueOf(ch)); 558 559 int index; 561 while((index = readNextChar.indexOf("__char__")) != -1) 562 { 563 readNextChar = readNextChar.substring(0,index) 564 + '\'' + charStr + '\'' 565 + readNextChar.substring(index + 8); 566 } 567 568 Macros.Recorder recorder = view.getMacroRecorder(); 569 if(recorder != null) 570 recorder.record(getRepeatCount(),readNextChar); 571 572 view.getStatus().setMessage(null); 573 574 if(getRepeatCount() != 1) 575 { 576 try 577 { 578 buffer.beginCompoundEdit(); 579 580 BeanShell.eval(view,BeanShell.getNameSpace(), 581 "for(int i = 1; i < " 582 + getRepeatCount() + "; i++)\n{\n" 583 + readNextChar + "\n}"); 584 } 585 finally 586 { 587 buffer.endCompoundEdit(); 588 } 589 } 590 else 591 BeanShell.eval(view,BeanShell.getNameSpace(),readNextChar); 592 593 readNextChar = null; 594 } 596 } 598 | Popular Tags |