KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > input > TextAreaInputHandler


1 /*
2  * ServiceManager.java - Handles services.xml files in plugins
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2006 Matthieu Casanova
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

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 JavaDoc;
34 import java.awt.*;
35
36 /**
37  * @author Matthieu Casanova
38  * @version $Id: FoldHandler.java 5568 2006-07-10 20:52:23Z kpouer $
39  */

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     /**
51      * Forwards key events directly to the input handler.
52      * This is slightly faster than using a KeyListener
53      * because some Swing overhead is avoided.
54      * @since 4.3pre7
55      */

56     public void processKeyEvent(KeyEvent JavaDoc 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         // Log.log(Log.DEBUG,this,view+".isFocused()="+view.isFocused()+'.',new Exception());
63
}
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             // if the user pressed eg C+e n n in the
80
// search bar we want focus to go back there
81
// after the prefix is done
82

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     } //}}}
112

113     //{{{ _preprocessKeyEvent() method
114
private KeyEvent JavaDoc _preprocessKeyEvent(KeyEvent JavaDoc evt)
115     {
116         Component focusOwner = textArea;
117         if (true /*Options.SIMPLIFIED_KEY_HANDLING*/)
118         {
119             /*
120                 It seems that the "else" path below does
121                 not work. Apparently, is is there to prevent
122                 some keyboard events to be "swallowed" by
123                 jEdit when the keyboard event in fact should
124                 be scheduled to swing for further handling.
125
126                 On some "key typed" events, the "return null;"
127                 is triggered. However, these key events
128                 actually do not seem to be handled elseewhere,
129                 so they are not handled at all.
130
131                 This behaviour exists with old keyboard handling
132                 as well as with new keyboard handling. However,
133                 the new keyboard handling is more sensitive
134                 about what kinds of key events it receives. It
135                 expects to see all "key typed" events,
136                 which is incompatible with the "return null;"
137                 below.
138
139                 This bug triggers jEdit bug 1493185 ( https://sourceforge.net/tracker/?func=detail&aid=1493185&group_id=588&atid=100588 ).
140
141                 Thus, we disable the possibility of
142                 key event swallowing for the new key event
143                 handling.
144
145             */

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 JavaDoc 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     } //}}}
175

176     //{{{ processKeyEventSub() method
177
private void processKeyEventSub(boolean focusOnTextArea)
178     {
179         // this is a weird hack.
180
// we don't want C+e a to insert 'a' in the
181
// search bar if the search bar has focus...
182
if (isPrefixActive() && focusOnTextArea)
183         {
184             textArea.requestFocus();
185         }
186     } //}}}
187

188     //{{{ handleKey() method
189
/**
190      * Handles the given keystroke.
191      * @param keyStroke The key stroke
192      * @param dryRun only calculate the return value, do not have any other effect
193      * @since jEdit 4.2pre5
194      */

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()) { // let user input be only local
234
userInput(input);
235                 }
236             } else {
237                 // this is retarded. excuse me while I drool
238
// and make stupid noises
239
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     } //}}}
265

266         //{{{ userInput() method
267
protected void userInput(char ch)
268     {
269         lastActionCount = 0;
270
271
272         if(repeatCount == 1)
273             textArea.userInput(ch);
274         else
275         {
276             // stop people doing dumb stuff like C+ENTER 100 C+n
277
/*if(repeatCount > REPEAT_COUNT_THRESHOLD)
278             {
279                 Object[] pp = { String.valueOf(ch),
280                     repeatCount };
281
282                 if(GUIUtilities.confirm(view,
283                     "large-repeat-count.user-input",pp,
284                     JOptionPane.WARNING_MESSAGE,
285                     JOptionPane.YES_NO_OPTION)
286                     != JOptionPane.YES_OPTION)
287                 {
288                     repeatCount = 1;
289                     view.getStatus().setMessage(null);
290                     return;
291                 }
292             }
293
294             JEditBuffer buffer = textArea.getBuffer();
295             try
296             {
297                 if(repeatCount != 1)
298                     buffer.beginCompoundEdit();
299                 for(int i = 0; i < repeatCount; i++)
300                     textArea.userInput(ch);
301             }
302             finally
303             {
304                 if(repeatCount != 1)
305                     buffer.endCompoundEdit();
306             } */

307         }
308
309         repeatCount = 1;
310     } //}}}
311

312     //{{{ invokeReadNextChar() method
313
protected void invokeReadNextChar(char ch)
314     {
315         String JavaDoc charStr = MiscUtilities.charsToEscapes(String.valueOf(ch));
316
317         // this might be a bit slow if __char__ occurs a lot
318
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     } //}}}
327

328 }
329
Popular Tags