KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > syntax > jedit > InputHandler


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * InputHandler.java
28  *
29  */

30
31 package org.syntax.jedit;
32
33 import javax.swing.text.*;
34 import javax.swing.JPopupMenu JavaDoc;
35 import java.awt.event.*;
36 import java.awt.Component JavaDoc;
37 import java.util.*;
38
39 /**
40  * An input handler converts the user's key strokes into concrete actions.
41  * It also takes care of macro recording and action repetition.<p>
42  *
43  * This class provides all the necessary support code for an input
44  * handler, but doesn't actually do any key binding logic. It is up
45  * to the implementations of this class to do so.
46  *
47  * @author Slava Pestov
48  * @version $Id: InputHandler.java 932 2006-10-20 09:32:45Z gtoffoli $
49  * @see org.syntax.jedit.DefaultInputHandler
50  *
51  * 08/12/2002 Clipboard actions (Oliver Henning)
52  */

53 public abstract class InputHandler extends KeyAdapter
54 {
55     /**
56      * If this client property is set to Boolean.TRUE on the text area,
57      * the home/end keys will support 'smart' BRIEF-like behaviour
58      * (one press = start/end of line, two presses = start/end of
59      * viewscreen, three presses = start/end of document). By default,
60      * this property is not set.
61      */

62     public static final String JavaDoc SMART_HOME_END_PROPERTY = "InputHandler.homeEnd";
63
64     public static final ActionListener BACKSPACE = new backspace();
65     public static final ActionListener BACKSPACE_WORD = new backspace_word();
66     public static final ActionListener DELETE = new delete();
67     public static final ActionListener DELETE_WORD = new delete_word();
68     public static final ActionListener END = new end(false);
69     public static final ActionListener DOCUMENT_END = new document_end(false);
70     public static final ActionListener SELECT_ALL = new select_all();
71     public static final ActionListener SELECT_END = new end(true);
72     public static final ActionListener SELECT_DOC_END = new document_end(true);
73     public static final ActionListener INSERT_BREAK = new insert_break();
74     public static final ActionListener INSERT_TAB = new insert_tab();
75     public static final ActionListener HOME = new home(false);
76     public static final ActionListener DOCUMENT_HOME = new document_home(false);
77     public static final ActionListener SELECT_HOME = new home(true);
78     public static final ActionListener SELECT_DOC_HOME = new document_home(true);
79     public static final ActionListener NEXT_CHAR = new next_char(false);
80     public static final ActionListener NEXT_LINE = new next_line(false);
81     public static final ActionListener NEXT_PAGE = new next_page(false);
82     public static final ActionListener NEXT_WORD = new next_word(false);
83     public static final ActionListener SELECT_NEXT_CHAR = new next_char(true);
84     public static final ActionListener SELECT_NEXT_LINE = new next_line(true);
85     public static final ActionListener SELECT_NEXT_PAGE = new next_page(true);
86     public static final ActionListener SELECT_NEXT_WORD = new next_word(true);
87     public static final ActionListener OVERWRITE = new overwrite();
88     public static final ActionListener PREV_CHAR = new prev_char(false);
89     public static final ActionListener PREV_LINE = new prev_line(false);
90     public static final ActionListener PREV_PAGE = new prev_page(false);
91     public static final ActionListener PREV_WORD = new prev_word(false);
92     public static final ActionListener SELECT_PREV_CHAR = new prev_char(true);
93     public static final ActionListener SELECT_PREV_LINE = new prev_line(true);
94     public static final ActionListener SELECT_PREV_PAGE = new prev_page(true);
95     public static final ActionListener SELECT_PREV_WORD = new prev_word(true);
96     public static final ActionListener REPEAT = new repeat();
97     public static final ActionListener TOGGLE_RECT = new toggle_rect();
98     // Clipboard
99
public static final ActionListener CLIP_COPY = new clip_copy();
100     public static final ActionListener CLIP_PASTE = new clip_paste();
101     public static final ActionListener CLIP_CUT = new clip_cut();
102         public static final ActionListener UNDO = new undo();
103         public static final ActionListener REDO = new redo();
104         
105     // Default action
106
public static final ActionListener INSERT_CHAR = new insert_char();
107
108     private static Hashtable actions;
109
110     static
111     {
112         actions = new Hashtable();
113         actions.put("backspace",BACKSPACE);
114         actions.put("backspace-word",BACKSPACE_WORD);
115         actions.put("delete",DELETE);
116         actions.put("delete-word",DELETE_WORD);
117         actions.put("end",END);
118         actions.put("select-all",SELECT_ALL);
119         actions.put("select-end",SELECT_END);
120         actions.put("document-end",DOCUMENT_END);
121         actions.put("select-doc-end",SELECT_DOC_END);
122         actions.put("insert-break",INSERT_BREAK);
123         actions.put("insert-tab",INSERT_TAB);
124         actions.put("home",HOME);
125         actions.put("select-home",SELECT_HOME);
126         actions.put("document-home",DOCUMENT_HOME);
127         actions.put("select-doc-home",SELECT_DOC_HOME);
128         actions.put("next-char",NEXT_CHAR);
129         actions.put("next-line",NEXT_LINE);
130         actions.put("next-page",NEXT_PAGE);
131         actions.put("next-word",NEXT_WORD);
132         actions.put("select-next-char",SELECT_NEXT_CHAR);
133         actions.put("select-next-line",SELECT_NEXT_LINE);
134         actions.put("select-next-page",SELECT_NEXT_PAGE);
135         actions.put("select-next-word",SELECT_NEXT_WORD);
136         actions.put("overwrite",OVERWRITE);
137         actions.put("prev-char",PREV_CHAR);
138         actions.put("prev-line",PREV_LINE);
139         actions.put("prev-page",PREV_PAGE);
140         actions.put("prev-word",PREV_WORD);
141         actions.put("select-prev-char",SELECT_PREV_CHAR);
142         actions.put("select-prev-line",SELECT_PREV_LINE);
143         actions.put("select-prev-page",SELECT_PREV_PAGE);
144         actions.put("select-prev-word",SELECT_PREV_WORD);
145         actions.put("repeat",REPEAT);
146         actions.put("toggle-rect",TOGGLE_RECT);
147         actions.put("insert-char",INSERT_CHAR);
148         actions.put("clipboard-copy",CLIP_COPY);
149         actions.put("clipboard-paste",CLIP_PASTE);
150         actions.put("clipboard-cut",CLIP_CUT);
151                 actions.put("undo",UNDO);
152                 actions.put("redo",REDO);
153     }
154
155     /**
156      * Returns a named text area action.
157      * @param name The action name
158      */

159     public static ActionListener getAction(String JavaDoc name)
160     {
161         return (ActionListener)actions.get(name);
162     }
163
164     /**
165      * Returns the name of the specified text area action.
166      * @param listener The action
167      */

168     public static String JavaDoc getActionName(ActionListener listener)
169     {
170         Enumeration e = getActions();
171         while(e.hasMoreElements())
172         {
173             String JavaDoc name = (String JavaDoc)e.nextElement();
174             ActionListener _listener = getAction(name);
175             if(_listener == listener)
176                 return name;
177         }
178         return null;
179     }
180
181     /**
182      * Returns an enumeration of all available actions.
183      */

184     public static Enumeration getActions()
185     {
186         return actions.keys();
187     }
188
189     /**
190      * Adds the default key bindings to this input handler.
191      * This should not be called in the constructor of this
192      * input handler, because applications might load the
193      * key bindings from a file, etc.
194      */

195     public abstract void addDefaultKeyBindings();
196
197     /**
198      * Adds a key binding to this input handler.
199      * @param keyBinding The key binding (the format of this is
200      * input-handler specific)
201      * @param action The action
202      */

203     public abstract void addKeyBinding(String JavaDoc keyBinding, ActionListener action);
204
205     /**
206      * Removes a key binding from this input handler.
207      * @param keyBinding The key binding
208      */

209     public abstract void removeKeyBinding(String JavaDoc keyBinding);
210
211     /**
212      * Removes all key bindings from this input handler.
213      */

214     public abstract void removeAllKeyBindings();
215
216     /**
217      * Grabs the next key typed event and invokes the specified
218      * action with the key as a the action command.
219      * @param action The action
220      */

221     public void grabNextKeyStroke(ActionListener listener)
222     {
223         grabAction = listener;
224     }
225
226     /**
227      * Returns if repeating is enabled. When repeating is enabled,
228      * actions will be executed multiple times. This is usually
229      * invoked with a special key stroke in the input handler.
230      */

231     public boolean isRepeatEnabled()
232     {
233         return repeat;
234     }
235
236     /**
237      * Enables repeating. When repeating is enabled, actions will be
238      * executed multiple times. Once repeating is enabled, the input
239      * handler should read a number from the keyboard.
240      */

241     public void setRepeatEnabled(boolean repeat)
242     {
243         this.repeat = repeat;
244     }
245
246     /**
247      * Returns the number of times the next action will be repeated.
248      */

249     public int getRepeatCount()
250     {
251         return (repeat ? Math.max(1,repeatCount) : 1);
252     }
253
254     /**
255      * Sets the number of times the next action will be repeated.
256      * @param repeatCount The repeat count
257      */

258     public void setRepeatCount(int repeatCount)
259     {
260         this.repeatCount = repeatCount;
261     }
262
263     /**
264      * Returns the macro recorder. If this is non-null, all executed
265      * actions should be forwarded to the recorder.
266      */

267     public InputHandler.MacroRecorder getMacroRecorder()
268     {
269         return recorder;
270     }
271
272     /**
273      * Sets the macro recorder. If this is non-null, all executed
274      * actions should be forwarded to the recorder.
275      * @param recorder The macro recorder
276      */

277     public void setMacroRecorder(InputHandler.MacroRecorder recorder)
278     {
279         this.recorder = recorder;
280     }
281
282     /**
283      * Returns a copy of this input handler that shares the same
284      * key bindings. Setting key bindings in the copy will also
285      * set them in the original.
286      */

287     public abstract InputHandler copy();
288
289     /**
290      * Executes the specified action, repeating and recording it as
291      * necessary.
292      * @param listener The action listener
293      * @param source The event source
294      * @param actionCommand The action command
295      */

296     public void executeAction(ActionListener listener, Object JavaDoc source,
297         String JavaDoc actionCommand)
298     {
299         // create event
300
ActionEvent evt = new ActionEvent(source,
301             ActionEvent.ACTION_PERFORMED,
302             actionCommand);
303
304         // don't do anything if the action is a wrapper
305
// (like EditAction.Wrapper)
306
if(listener instanceof Wrapper)
307         {
308             listener.actionPerformed(evt);
309             return;
310         }
311
312         // remember old values, in case action changes them
313
boolean _repeat = repeat;
314         int _repeatCount = getRepeatCount();
315
316         // execute the action
317
if(listener instanceof InputHandler.NonRepeatable)
318             listener.actionPerformed(evt);
319         else
320         {
321             for(int i = 0; i < Math.max(1,repeatCount); i++)
322                 listener.actionPerformed(evt);
323         }
324
325         // do recording. Notice that we do no recording whatsoever
326
// for actions that grab keys
327
if(grabAction == null)
328         {
329             if(recorder != null)
330             {
331                 if(!(listener instanceof InputHandler.NonRecordable))
332                 {
333                     if(_repeatCount != 1)
334                         recorder.actionPerformed(REPEAT,String.valueOf(_repeatCount));
335
336                     recorder.actionPerformed(listener,actionCommand);
337                 }
338             }
339
340             // If repeat was true originally, clear it
341
// Otherwise it might have been set by the action, etc
342
if(_repeat)
343             {
344                 repeat = false;
345                 repeatCount = 0;
346             }
347         }
348     }
349
350     /**
351      * Returns the text area that fired the specified event.
352      * @param evt The event
353      */

354     public static JEditTextArea getTextArea(EventObject evt)
355     {
356         if(evt != null)
357         {
358             Object JavaDoc o = evt.getSource();
359             if(o instanceof Component JavaDoc)
360             {
361                 // find the parent text area
362
Component JavaDoc c = (Component JavaDoc)o;
363                 for(;;)
364                 {
365                     if(c instanceof JEditTextArea)
366                         return (JEditTextArea)c;
367                     else if(c == null)
368                         break;
369                     if(c instanceof JPopupMenu JavaDoc)
370                         c = ((JPopupMenu JavaDoc)c)
371                             .getInvoker();
372                     else
373                         c = c.getParent();
374                 }
375             }
376         }
377
378         // this shouldn't happen
379
System.err.println("BUG: getTextArea() returning null");
380         System.err.println("Report this to Slava Pestov <sp@gjt.org>");
381         return null;
382     }
383
384     // protected members
385

386     /**
387      * If a key is being grabbed, this method should be called with
388      * the appropriate key event. It executes the grab action with
389      * the typed character as the parameter.
390      */

391     protected void handleGrabAction(KeyEvent evt)
392     {
393         // Clear it *before* it is executed so that executeAction()
394
// resets the repeat count
395
ActionListener _grabAction = grabAction;
396         grabAction = null;
397         executeAction(_grabAction,evt.getSource(),
398             String.valueOf(evt.getKeyChar()));
399     }
400
401     // protected members
402
protected ActionListener grabAction;
403     protected boolean repeat;
404     protected int repeatCount;
405     protected InputHandler.MacroRecorder recorder;
406
407     /**
408      * If an action implements this interface, it should not be repeated.
409      * Instead, it will handle the repetition itself.
410      */

411     public interface NonRepeatable {}
412
413     /**
414      * If an action implements this interface, it should not be recorded
415      * by the macro recorder. Instead, it will do its own recording.
416      */

417     public interface NonRecordable {}
418
419     /**
420      * For use by EditAction.Wrapper only.
421      * @since jEdit 2.2final
422      */

423     public interface Wrapper {}
424
425     /**
426      * Macro recorder.
427      */

428     public interface MacroRecorder
429     {
430         void actionPerformed(ActionListener listener,
431             String JavaDoc actionCommand);
432     }
433
434     public static class backspace implements ActionListener
435     {
436         public void actionPerformed(ActionEvent evt)
437         {
438             JEditTextArea textArea = getTextArea(evt);
439
440             if(!textArea.isEditable())
441             {
442                 textArea.getToolkit().beep();
443                 return;
444             }
445
446             if(textArea.getSelectionStart()
447                != textArea.getSelectionEnd())
448             {
449                 textArea.setSelectedText("");
450             }
451             else
452             {
453                 int caret = textArea.getCaretPosition();
454                 if(caret == 0)
455                 {
456                     textArea.getToolkit().beep();
457                     return;
458                 }
459                 try
460                 {
461                     textArea.getDocument().remove(caret - 1,1);
462                 }
463                 catch(BadLocationException bl)
464                 {
465                     bl.printStackTrace();
466                 }
467             }
468         }
469     }
470
471     public static class backspace_word implements ActionListener
472     {
473         public void actionPerformed(ActionEvent evt)
474         {
475             JEditTextArea textArea = getTextArea(evt);
476             int start = textArea.getSelectionStart();
477             if(start != textArea.getSelectionEnd())
478             {
479                 textArea.setSelectedText("");
480             }
481
482             int line = textArea.getCaretLine();
483             int lineStart = textArea.getLineStartOffset(line);
484             int caret = start - lineStart;
485
486             String JavaDoc lineText = textArea.getLineText(textArea
487                 .getCaretLine());
488
489             if(caret == 0)
490             {
491                 if(lineStart == 0)
492                 {
493                     textArea.getToolkit().beep();
494                     return;
495                 }
496                 caret--;
497             }
498             else
499             {
500                 String JavaDoc noWordSep = (String JavaDoc)textArea.getDocument().getProperty("noWordSep");
501                 caret = TextUtilities.findWordStart(lineText,caret,noWordSep);
502             }
503
504             try
505             {
506                 textArea.getDocument().remove(
507                         caret + lineStart,
508                         start - (caret + lineStart));
509             }
510             catch(BadLocationException bl)
511             {
512                 bl.printStackTrace();
513             }
514         }
515     }
516
517     public static class delete implements ActionListener
518     {
519         public void actionPerformed(ActionEvent evt)
520         {
521             JEditTextArea textArea = getTextArea(evt);
522
523             if(!textArea.isEditable())
524             {
525                 textArea.getToolkit().beep();
526                 return;
527             }
528
529             if(textArea.getSelectionStart()
530                != textArea.getSelectionEnd())
531             {
532                 textArea.setSelectedText("");
533             }
534             else
535             {
536                 int caret = textArea.getCaretPosition();
537                 if(caret == textArea.getDocumentLength())
538                 {
539                     textArea.getToolkit().beep();
540                     return;
541                 }
542                 try
543                 {
544                     textArea.getDocument().remove(caret,1);
545                 }
546                 catch(BadLocationException bl)
547                 {
548                     bl.printStackTrace();
549                 }
550             }
551         }
552     }
553
554     public static class delete_word implements ActionListener
555     {
556         public void actionPerformed(ActionEvent evt)
557         {
558             JEditTextArea textArea = getTextArea(evt);
559             int start = textArea.getSelectionStart();
560             if(start != textArea.getSelectionEnd())
561             {
562                 textArea.setSelectedText("");
563             }
564
565             int line = textArea.getCaretLine();
566             int lineStart = textArea.getLineStartOffset(line);
567             int caret = start - lineStart;
568
569             String JavaDoc lineText = textArea.getLineText(textArea
570                 .getCaretLine());
571
572             if(caret == lineText.length())
573             {
574                 if(lineStart + caret == textArea.getDocumentLength())
575                 {
576                     textArea.getToolkit().beep();
577                     return;
578                 }
579                 caret++;
580             }
581             else
582             {
583                 String JavaDoc noWordSep = (String JavaDoc)textArea.getDocument().getProperty("noWordSep");
584                 caret = TextUtilities.findWordEnd(lineText,caret,noWordSep);
585             }
586
587             try
588             {
589                 textArea.getDocument().remove(start,
590                     (caret + lineStart) - start);
591             }
592             catch(BadLocationException bl)
593             {
594                 bl.printStackTrace();
595             }
596         }
597     }
598
599     public static class end implements ActionListener
600     {
601         private boolean select;
602
603         public end(boolean select)
604         {
605             this.select = select;
606         }
607
608         public void actionPerformed(ActionEvent evt)
609         {
610             JEditTextArea textArea = getTextArea(evt);
611
612             int caret = textArea.getCaretPosition();
613
614             int lastOfLine = textArea.getLineEndOffset(
615                 textArea.getCaretLine()) - 1;
616             int lastVisibleLine = textArea.getFirstLine()
617                 + textArea.getVisibleLines();
618             if(lastVisibleLine >= textArea.getLineCount())
619             {
620                 lastVisibleLine = Math.min(textArea.getLineCount() - 1,
621                     lastVisibleLine);
622             }
623             else
624                 lastVisibleLine -= (textArea.getElectricScroll() + 1);
625
626             int lastVisible = textArea.getLineEndOffset(lastVisibleLine) - 1;
627             int lastDocument = textArea.getDocumentLength();
628
629             if(caret == lastDocument)
630             {
631                 textArea.getToolkit().beep();
632                 return;
633             }
634             else if(!Boolean.TRUE.equals(textArea.getClientProperty(
635                 SMART_HOME_END_PROPERTY)))
636                 caret = lastOfLine;
637             else if(caret == lastVisible)
638                 caret = lastDocument;
639             else if(caret == lastOfLine)
640                 caret = lastVisible;
641             else
642                 caret = lastOfLine;
643
644             if(select)
645                 textArea.select(textArea.getMarkPosition(),caret);
646             else
647                 textArea.setCaretPosition(caret);
648         }
649     }
650
651     public static class select_all implements ActionListener {
652         public void actionPerformed(ActionEvent evt)
653         {
654             JEditTextArea textArea = getTextArea(evt);
655             textArea.selectAll();
656         }
657     }
658     
659     public static class document_end implements ActionListener
660     {
661         private boolean select;
662
663         public document_end(boolean select)
664         {
665             this.select = select;
666         }
667
668         public void actionPerformed(ActionEvent evt)
669         {
670             JEditTextArea textArea = getTextArea(evt);
671             if(select)
672                 textArea.select(textArea.getMarkPosition(),
673                     textArea.getDocumentLength());
674             else
675                 textArea.setCaretPosition(textArea
676                     .getDocumentLength());
677         }
678     }
679
680     public static class home implements ActionListener
681     {
682         private boolean select;
683
684         public home(boolean select)
685         {
686             this.select = select;
687         }
688
689         public void actionPerformed(ActionEvent evt)
690         {
691             JEditTextArea textArea = getTextArea(evt);
692
693             int caret = textArea.getCaretPosition();
694
695             int firstLine = textArea.getFirstLine();
696
697             int firstOfLine = textArea.getLineStartOffset(
698                 textArea.getCaretLine());
699             int firstVisibleLine = (firstLine == 0 ? 0 :
700                 firstLine + textArea.getElectricScroll());
701             int firstVisible = textArea.getLineStartOffset(
702                 firstVisibleLine);
703
704             if(caret == 0)
705             {
706                 textArea.getToolkit().beep();
707                 return;
708             }
709             else if(!Boolean.TRUE.equals(textArea.getClientProperty(
710                 SMART_HOME_END_PROPERTY)))
711                 caret = firstOfLine;
712             else if(caret == firstVisible)
713                 caret = 0;
714             else if(caret == firstOfLine)
715                 caret = firstVisible;
716             else
717                 caret = firstOfLine;
718
719             if(select)
720                 textArea.select(textArea.getMarkPosition(),caret);
721             else
722                 textArea.setCaretPosition(caret);
723         }
724     }
725
726     public static class document_home implements ActionListener
727     {
728         private boolean select;
729
730         public document_home(boolean select)
731         {
732             this.select = select;
733         }
734
735         public void actionPerformed(ActionEvent evt)
736         {
737             JEditTextArea textArea = getTextArea(evt);
738             if(select)
739                 textArea.select(textArea.getMarkPosition(),0);
740             else
741                 textArea.setCaretPosition(0);
742         }
743     }
744
745     public static class insert_break implements ActionListener
746     {
747         public void actionPerformed(ActionEvent evt)
748         {
749             JEditTextArea textArea = getTextArea(evt);
750
751             if(!textArea.isEditable())
752             {
753                 textArea.getToolkit().beep();
754                 return;
755             }
756
757             textArea.setSelectedText("\n");
758         }
759     }
760
761     public static class insert_tab implements ActionListener
762     {
763         public void actionPerformed(ActionEvent evt)
764         {
765             JEditTextArea textArea = getTextArea(evt);
766
767             if(!textArea.isEditable())
768             {
769                 textArea.getToolkit().beep();
770                 return;
771             }
772
773             textArea.overwriteSetSelectedText("\t");
774         }
775     }
776
777     public static class next_char implements ActionListener
778     {
779         private boolean select;
780
781         public next_char(boolean select)
782         {
783             this.select = select;
784         }
785
786         public void actionPerformed(ActionEvent evt)
787         {
788             JEditTextArea textArea = getTextArea(evt);
789             int caret = textArea.getCaretPosition();
790             if(caret == textArea.getDocumentLength())
791             {
792                 textArea.getToolkit().beep();
793                 return;
794             }
795
796             if(select)
797                 textArea.select(textArea.getMarkPosition(),
798                     caret + 1);
799             else
800                 textArea.setCaretPosition(caret + 1);
801         }
802     }
803
804     public static class next_line implements ActionListener
805     {
806         private boolean select;
807
808         public next_line(boolean select)
809         {
810             this.select = select;
811         }
812
813         public void actionPerformed(ActionEvent evt)
814         {
815             JEditTextArea textArea = getTextArea(evt);
816             int caret = textArea.getCaretPosition();
817             int line = textArea.getCaretLine();
818
819             if(line == textArea.getLineCount() - 1)
820             {
821                 textArea.getToolkit().beep();
822                 return;
823             }
824
825             int magic = textArea.getMagicCaretPosition();
826             if(magic == -1)
827             {
828                 magic = textArea.offsetToX(line,
829                     caret - textArea.getLineStartOffset(line));
830             }
831
832             caret = textArea.getLineStartOffset(line + 1)
833                 + textArea.xToOffset(line + 1,magic);
834             if(select)
835                 textArea.select(textArea.getMarkPosition(),caret);
836             else
837                 textArea.setCaretPosition(caret);
838             textArea.setMagicCaretPosition(magic);
839         }
840     }
841
842     public static class next_page implements ActionListener
843     {
844         private boolean select;
845
846         public next_page(boolean select)
847         {
848             this.select = select;
849         }
850
851         public void actionPerformed(ActionEvent evt)
852         {
853             JEditTextArea textArea = getTextArea(evt);
854             int lineCount = textArea.getLineCount();
855             int firstLine = textArea.getFirstLine();
856             int visibleLines = textArea.getVisibleLines();
857             int line = textArea.getCaretLine();
858
859             firstLine += visibleLines;
860
861             if(firstLine + visibleLines >= lineCount - 1)
862                 firstLine = lineCount - visibleLines;
863
864             textArea.setFirstLine(firstLine);
865
866             int caret = textArea.getLineStartOffset(
867                 Math.min(textArea.getLineCount() - 1,
868                 line + visibleLines));
869             if(select)
870                 textArea.select(textArea.getMarkPosition(),caret);
871             else
872                 textArea.setCaretPosition(caret);
873         }
874     }
875
876     public static class next_word implements ActionListener
877     {
878         private boolean select;
879
880         public next_word(boolean select)
881         {
882             this.select = select;
883         }
884
885         public void actionPerformed(ActionEvent evt)
886         {
887             JEditTextArea textArea = getTextArea(evt);
888             int caret = textArea.getCaretPosition();
889             int line = textArea.getCaretLine();
890             int lineStart = textArea.getLineStartOffset(line);
891             caret -= lineStart;
892
893             String JavaDoc lineText = textArea.getLineText(textArea
894                 .getCaretLine());
895
896             if(caret == lineText.length())
897             {
898                 if(lineStart + caret == textArea.getDocumentLength())
899                 {
900                     textArea.getToolkit().beep();
901                     return;
902                 }
903                 caret++;
904             }
905             else
906             {
907                 String JavaDoc noWordSep = (String JavaDoc)textArea.getDocument().getProperty("noWordSep");
908                 caret = TextUtilities.findWordEnd(lineText,caret,noWordSep);
909             }
910
911             if(select)
912                 textArea.select(textArea.getMarkPosition(),
913                     lineStart + caret);
914             else
915                 textArea.setCaretPosition(lineStart + caret);
916         }
917     }
918
919     public static class overwrite implements ActionListener
920     {
921         public void actionPerformed(ActionEvent evt)
922         {
923             JEditTextArea textArea = getTextArea(evt);
924             textArea.setOverwriteEnabled(
925                 !textArea.isOverwriteEnabled());
926         }
927     }
928
929     public static class prev_char implements ActionListener
930     {
931         private boolean select;
932
933         public prev_char(boolean select)
934         {
935             this.select = select;
936         }
937
938         public void actionPerformed(ActionEvent evt)
939         {
940             JEditTextArea textArea = getTextArea(evt);
941             int caret = textArea.getCaretPosition();
942             if(caret == 0)
943             {
944                 textArea.getToolkit().beep();
945                 return;
946             }
947
948             if(select)
949                 textArea.select(textArea.getMarkPosition(),
950                     caret - 1);
951             else
952                 textArea.setCaretPosition(caret - 1);
953         }
954     }
955
956     public static class prev_line implements ActionListener
957     {
958         private boolean select;
959
960         public prev_line(boolean select)
961         {
962             this.select = select;
963         }
964
965         public void actionPerformed(ActionEvent evt)
966         {
967             JEditTextArea textArea = getTextArea(evt);
968             int caret = textArea.getCaretPosition();
969             int line = textArea.getCaretLine();
970
971             if(line == 0)
972             {
973                 textArea.getToolkit().beep();
974                 return;
975             }
976
977             int magic = textArea.getMagicCaretPosition();
978             if(magic == -1)
979             {
980                 magic = textArea.offsetToX(line,
981                     caret - textArea.getLineStartOffset(line));
982             }
983
984             caret = textArea.getLineStartOffset(line - 1)
985                 + textArea.xToOffset(line - 1,magic);
986             if(select)
987                 textArea.select(textArea.getMarkPosition(),caret);
988             else
989                 textArea.setCaretPosition(caret);
990             textArea.setMagicCaretPosition(magic);
991         }
992     }
993
994     public static class prev_page implements ActionListener
995     {
996         private boolean select;
997
998         public prev_page(boolean select)
999         {
1000            this.select = select;
1001        }
1002
1003        public void actionPerformed(ActionEvent evt)
1004        {
1005            JEditTextArea textArea = getTextArea(evt);
1006            int firstLine = textArea.getFirstLine();
1007            int visibleLines = textArea.getVisibleLines();
1008            int line = textArea.getCaretLine();
1009
1010            if(firstLine < visibleLines)
1011                firstLine = visibleLines;
1012
1013            textArea.setFirstLine(firstLine - visibleLines);
1014
1015            int caret = textArea.getLineStartOffset(
1016                Math.max(0,line - visibleLines));
1017            if(select)
1018                textArea.select(textArea.getMarkPosition(),caret);
1019            else
1020                textArea.setCaretPosition(caret);
1021        }
1022    }
1023
1024    public static class prev_word implements ActionListener
1025    {
1026        private boolean select;
1027
1028        public prev_word(boolean select)
1029        {
1030            this.select = select;
1031        }
1032
1033        public void actionPerformed(ActionEvent evt)
1034        {
1035            JEditTextArea textArea = getTextArea(evt);
1036            int caret = textArea.getCaretPosition();
1037            int line = textArea.getCaretLine();
1038            int lineStart = textArea.getLineStartOffset(line);
1039            caret -= lineStart;
1040
1041            String JavaDoc lineText = textArea.getLineText(textArea
1042                .getCaretLine());
1043
1044            if(caret == 0)
1045            {
1046                if(lineStart == 0)
1047                {
1048                    textArea.getToolkit().beep();
1049                    return;
1050                }
1051                caret--;
1052            }
1053            else
1054            {
1055                String JavaDoc noWordSep = (String JavaDoc)textArea.getDocument().getProperty("noWordSep");
1056                caret = TextUtilities.findWordStart(lineText,caret,noWordSep);
1057            }
1058
1059            if(select)
1060                textArea.select(textArea.getMarkPosition(),
1061                    lineStart + caret);
1062            else
1063                textArea.setCaretPosition(lineStart + caret);
1064        }
1065    }
1066
1067    public static class repeat implements ActionListener,
1068        InputHandler.NonRecordable
1069    {
1070        public void actionPerformed(ActionEvent evt)
1071        {
1072            JEditTextArea textArea = getTextArea(evt);
1073            textArea.getInputHandler().setRepeatEnabled(true);
1074            String JavaDoc actionCommand = evt.getActionCommand();
1075            if(actionCommand != null)
1076            {
1077                textArea.getInputHandler().setRepeatCount(
1078                    Integer.parseInt(actionCommand));
1079            }
1080        }
1081    }
1082
1083    public static class toggle_rect implements ActionListener
1084    {
1085        public void actionPerformed(ActionEvent evt)
1086        {
1087            JEditTextArea textArea = getTextArea(evt);
1088            textArea.setSelectionRectangular(
1089                !textArea.isSelectionRectangular());
1090        }
1091    }
1092
1093    public static class insert_char implements ActionListener,
1094        InputHandler.NonRepeatable
1095    {
1096        public void actionPerformed(ActionEvent evt)
1097        {
1098            JEditTextArea textArea = getTextArea(evt);
1099            String JavaDoc str = evt.getActionCommand();
1100            int repeatCount = textArea.getInputHandler().getRepeatCount();
1101
1102            if(textArea.isEditable())
1103            {
1104                StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1105                for(int i = 0; i < repeatCount; i++)
1106                    buf.append(str);
1107                textArea.overwriteSetSelectedText(buf.toString());
1108            }
1109            else
1110            {
1111                textArea.getToolkit().beep();
1112            }
1113        }
1114    }
1115    
1116    public static class clip_copy implements ActionListener {
1117        public void actionPerformed(ActionEvent evt)
1118        {
1119            JEditTextArea textArea = getTextArea(evt);
1120            textArea.copy();
1121        }
1122    }
1123    
1124    public static class clip_paste implements ActionListener {
1125        public void actionPerformed(ActionEvent evt)
1126        {
1127            JEditTextArea textArea = getTextArea(evt);
1128            textArea.paste();
1129        }
1130    }
1131    
1132    public static class clip_cut implements ActionListener {
1133        public void actionPerformed(ActionEvent evt)
1134        {
1135            JEditTextArea textArea = getTextArea(evt);
1136            textArea.cut();
1137        }
1138    }
1139        
1140        public static class undo implements ActionListener {
1141        public void actionPerformed(ActionEvent evt)
1142        {
1143            JEditTextArea textArea = getTextArea(evt);
1144                        if (textArea.getDocument().getUM().canUndo())
1145                        {
1146                            textArea.getDocument().getUM().undo();
1147                        }
1148        }
1149    }
1150        
1151        public static class redo implements ActionListener {
1152        public void actionPerformed(ActionEvent evt)
1153        {
1154            JEditTextArea textArea = getTextArea(evt);
1155                        if (textArea.getDocument().getUM().canRedo() )
1156                        {
1157                            textArea.getDocument().getUM().redo();
1158                        }
1159        }
1160    }
1161}
1162    
1163
Popular Tags