1 22 package org.gjt.sp.jedit.input; 23 24 import org.gjt.sp.jedit.textarea.TextArea; 25 import org.gjt.sp.jedit.gui.GrabKeyDialog; 26 import org.gjt.sp.jedit.gui.KeyEventWorkaround; 27 import org.gjt.sp.jedit.gui.KeyEventTranslator; 28 import org.gjt.sp.jedit.Debug; 29 import org.gjt.sp.jedit.MiscUtilities; 30 import org.gjt.sp.util.Log; 31 32 import javax.swing.*; 33 import java.awt.event.KeyEvent ; 34 import java.awt.*; 35 36 40 public class TextAreaInputHandler extends AbstractInputHandler 41 { 42 private TextArea textArea; 43 44 45 public TextAreaInputHandler(TextArea textArea) 46 { 47 this.textArea = textArea; 48 } 49 50 56 public void processKeyEvent(KeyEvent evt, int from, boolean global) 57 { 58 if(Debug.DUMP_KEY_EVENTS) 59 { 60 Log.log(Log.DEBUG,this,"Key event : " 61 + GrabKeyDialog.toString(evt) + " from " + from); 62 } 64 65 evt = _preprocessKeyEvent(evt); 66 if(evt == null) 67 return; 68 69 if(Debug.DUMP_KEY_EVENTS) 70 { 71 Log.log(Log.DEBUG,this,"Key event after workaround: " 72 + GrabKeyDialog.toString(evt) + " from " + from); 73 } 74 75 boolean focusOnTextArea = false; 76 switch(evt.getID()) 77 { 78 case KeyEvent.KEY_TYPED: 79 83 84 if(keyEventInterceptor != null) 85 keyEventInterceptor.keyTyped(evt); 86 else if(isPrefixActive() || textArea.hasFocus()) 87 { 88 processKeyEventKeyStrokeHandling(evt,from,"type ",global); 89 } 90 91 92 processKeyEventSub(focusOnTextArea); 93 94 break; 95 case KeyEvent.KEY_PRESSED: 96 if(keyEventInterceptor != null) 97 keyEventInterceptor.keyPressed(evt); 98 else if(KeyEventWorkaround.isBindable(evt.getKeyCode())) 99 { 100 processKeyEventKeyStrokeHandling(evt,from,"press",global); 101 102 processKeyEventSub(focusOnTextArea); 103 104 } 105 break; 106 case KeyEvent.KEY_RELEASED: 107 if(keyEventInterceptor != null) 108 keyEventInterceptor.keyReleased(evt); 109 break; 110 } 111 } 113 private KeyEvent _preprocessKeyEvent(KeyEvent evt) 115 { 116 Component focusOwner = textArea; 117 if (true ) 118 { 119 146 } 147 else 148 { 149 JComponent comp = (JComponent)focusOwner; 150 InputMap map = comp.getInputMap(); 151 ActionMap am = comp.getActionMap(); 152 153 if(map != null && am != null && comp.isEnabled()) 154 { 155 KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(evt); 156 Object binding = map.get(keyStroke); 157 if(binding != null && am.get(binding) != null) 158 { 159 return null; 160 } 161 } 162 } 163 164 if(evt.isConsumed()) 165 return null; 166 167 if(Debug.DUMP_KEY_EVENTS) 168 { 169 Log.log(Log.DEBUG,this,"Key event (preprocessing) : " 170 + GrabKeyDialog.toString(evt)); 171 } 172 173 return KeyEventWorkaround.processKeyEvent(evt); 174 } 176 private void processKeyEventSub(boolean focusOnTextArea) 178 { 179 if (isPrefixActive() && focusOnTextArea) 183 { 184 textArea.requestFocus(); 185 } 186 } 188 195 public boolean handleKey(KeyEventTranslator.Key keyStroke,boolean dryRun) 196 { 197 char input = '\0'; 198 if(keyStroke.modifiers == null 199 || keyStroke.modifiers.equals("S")) 200 { 201 switch(keyStroke.key) 202 { 203 case '\n': 204 case '\t': 205 input = (char)keyStroke.key; 206 break; 207 default: 208 input = keyStroke.input; 209 break; 210 } 211 } 212 213 if(readNextChar != null) 214 { 215 if(input != '\0') 216 { 217 if (!dryRun) { 218 invokeReadNextChar(input); 219 repeatCount = 1; 220 } 221 return true; 222 } 223 else 224 { 225 if (!dryRun) { 226 readNextChar = null; 227 } 228 } 229 } 230 if (!dryRun) 231 { 232 if(input != '\0') { 233 if (!keyStroke.isFromGlobalContext()) { userInput(input); 235 } 236 } else { 237 if(KeyEventWorkaround.isNumericKeypad(keyStroke.key)) 240 KeyEventWorkaround.numericKeypadKey(); 241 else 242 { 243 switch (keyStroke.key) 244 { 245 case KeyEvent.VK_LEFT: 246 textArea.goToPrevCharacter("S".equals(keyStroke.modifiers)); 247 break; 248 case KeyEvent.VK_RIGHT: 249 textArea.goToNextCharacter("S".equals(keyStroke.modifiers)); 250 break; 251 case KeyEvent.VK_UP: 252 textArea.goToPrevLine("S".equals(keyStroke.modifiers)); 253 break; 254 case KeyEvent.VK_DOWN: 255 textArea.goToNextLine("S".equals(keyStroke.modifiers)); 256 break; 257 258 } 259 } 260 261 } 262 } 263 return false; 264 } 266 protected void userInput(char ch) 268 { 269 lastActionCount = 0; 270 271 272 if(repeatCount == 1) 273 textArea.userInput(ch); 274 else 275 { 276 307 } 308 309 repeatCount = 1; 310 } 312 protected void invokeReadNextChar(char ch) 314 { 315 String charStr = MiscUtilities.charsToEscapes(String.valueOf(ch)); 316 317 int index; 319 while((index = readNextChar.indexOf("__char__")) != -1) 320 { 321 readNextChar = readNextChar.substring(0,index) 322 + '\'' + charStr + '\'' 323 + readNextChar.substring(index + 8); 324 } 325 readNextChar = null; 326 } 328 } 329 | Popular Tags |