-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathInputHandler.java
More file actions
1183 lines (997 loc) · 34 KB
/
InputHandler.java
File metadata and controls
1183 lines (997 loc) · 34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* InputHandler.java - Manages key bindings and executes actions
* Copyright (C) 1999 Slava Pestov
*
* You may use and modify this package for any purpose. Redistribution is
* permitted, in both source and binary form, provided that this notice
* remains intact in all source distributions of this package.
*/
package processing.app.syntax;
import javax.swing.text.*;
import javax.swing.JPopupMenu;
import processing.app.Preferences;
import processing.data.StringDict;
import java.awt.event.*;
import java.awt.Component;
import java.util.*;
/**
* An input handler converts the user's key strokes into concrete actions.
* It also takes care of macro recording and action repetition.<p>
*
* This class provides all the necessary support code for an input
* handler, but doesn't actually do any key binding logic. It is up
* to the implementations of this class to do so.
*
* @author Slava Pestov
* @version $Id$
*/
public abstract class InputHandler extends KeyAdapter
{
/**
* If this client property is set to Boolean.TRUE on the text area,
* the home/end keys will support 'smart' BRIEF-like behaviour
* (one press = start/end of line, two presses = start/end of
* viewscreen, three presses = start/end of document). By default,
* this property is not set.
*/
public static final String SMART_HOME_END_PROPERTY = "InputHandler.homeEnd";
/**
* If this PDE property is set to Boolean.TRUE, the home/end keys will
* go to the first/last non-whitespace character of the line. If already at
* the that character, the keypress will move the cursor to the actual
* start/end of the line.
*
* SMART_HOME_END_PROPERTY takes precedence over this property.
*/
public static final String CONTEXT_AWARE_HOME_END =
"editor.keys.home_and_end_travel_smart";
public static final ActionListener BACKSPACE = new backspace();
public static final ActionListener BACKSPACE_WORD = new backspace_word();
public static final ActionListener DELETE = new delete();
public static final ActionListener DELETE_WORD = new delete_word();
public static final ActionListener END = new end(false);
public static final ActionListener DOCUMENT_END = new document_end(false);
public static final ActionListener SELECT_END = new end(true);
public static final ActionListener SELECT_DOC_END = new document_end(true);
public static final ActionListener INSERT_BREAK = new insert_break();
public static final ActionListener INSERT_TAB = new insert_tab();
public static final ActionListener HOME = new home(false);
public static final ActionListener DOCUMENT_HOME = new document_home(false);
public static final ActionListener SELECT_HOME = new home(true);
public static final ActionListener SELECT_DOC_HOME = new document_home(true);
public static final ActionListener NEXT_CHAR = new next_char(false);
public static final ActionListener NEXT_LINE = new next_line(false);
public static final ActionListener NEXT_PAGE = new next_page(false);
public static final ActionListener NEXT_WORD = new next_word(false);
public static final ActionListener SELECT_NEXT_CHAR = new next_char(true);
public static final ActionListener SELECT_NEXT_LINE = new next_line(true);
public static final ActionListener SELECT_NEXT_PAGE = new next_page(true);
public static final ActionListener SELECT_NEXT_WORD = new next_word(true);
public static final ActionListener OVERWRITE = new overwrite();
public static final ActionListener PREV_CHAR = new prev_char(false);
public static final ActionListener PREV_LINE = new prev_line(false);
public static final ActionListener PREV_PAGE = new prev_page(false);
public static final ActionListener PREV_WORD = new prev_word(false);
public static final ActionListener SELECT_PREV_CHAR = new prev_char(true);
public static final ActionListener SELECT_PREV_LINE = new prev_line(true);
public static final ActionListener SELECT_PREV_PAGE = new prev_page(true);
public static final ActionListener SELECT_PREV_WORD = new prev_word(true);
public static final ActionListener REPEAT = new repeat();
public static final ActionListener CLIPBOARD_CUT = new clipboard_cut(); // [fry]
public static final ActionListener CLIPBOARD_COPY = new clipboard_copy();
public static final ActionListener CLIPBOARD_PASTE = new clipboard_paste();
// Default action
public static final ActionListener INSERT_CHAR = new insert_char();
private static Map<String, ActionListener> actions;
private static final StringDict bracketsAndQuotesMap =
new StringDict(new String[][] {
{ "(", ")" }, { "{", "}" }, { "[", "]" }, { "\"", "\"" }, { "\'", "\'" }
});
static {
actions = new HashMap<String, ActionListener>();
actions.put("backspace",BACKSPACE);
actions.put("backspace-word",BACKSPACE_WORD);
actions.put("delete",DELETE);
actions.put("delete-word",DELETE_WORD);
actions.put("end",END);
actions.put("select-end",SELECT_END);
actions.put("document-end",DOCUMENT_END);
actions.put("select-doc-end",SELECT_DOC_END);
actions.put("insert-break",INSERT_BREAK);
actions.put("insert-tab",INSERT_TAB);
actions.put("home",HOME);
actions.put("select-home",SELECT_HOME);
actions.put("document-home",DOCUMENT_HOME);
actions.put("select-doc-home",SELECT_DOC_HOME);
actions.put("next-char",NEXT_CHAR);
actions.put("next-line",NEXT_LINE);
actions.put("next-page",NEXT_PAGE);
actions.put("next-word",NEXT_WORD);
actions.put("select-next-char",SELECT_NEXT_CHAR);
actions.put("select-next-line",SELECT_NEXT_LINE);
actions.put("select-next-page",SELECT_NEXT_PAGE);
actions.put("select-next-word",SELECT_NEXT_WORD);
actions.put("overwrite",OVERWRITE);
actions.put("prev-char",PREV_CHAR);
actions.put("prev-line",PREV_LINE);
actions.put("prev-page",PREV_PAGE);
actions.put("prev-word",PREV_WORD);
actions.put("select-prev-char",SELECT_PREV_CHAR);
actions.put("select-prev-line",SELECT_PREV_LINE);
actions.put("select-prev-page",SELECT_PREV_PAGE);
actions.put("select-prev-word",SELECT_PREV_WORD);
actions.put("repeat",REPEAT);
actions.put("insert-char",INSERT_CHAR);
actions.put("clipboard-cut",CLIPBOARD_CUT);
actions.put("clipboard-copy",CLIPBOARD_COPY);
actions.put("clipboard-paste",CLIPBOARD_PASTE);
}
/**
* Returns a named text area action.
* @param name The action name
*/
public static ActionListener getAction(String name) {
return actions.get(name);
}
/**
* Returns the name of the specified text area action.
* @param listener The action
*/
public static String getActionName(ActionListener listener) {
Set<String> set = getActions();
for (String name : set) {
ActionListener _listener = getAction(name);
if (_listener == listener) {
return name;
}
}
return null;
}
/**
* Returns an enumeration of all available actions.
*/
public static Set<String> getActions() {
return actions.keySet();
}
/**
* Adds the default key bindings to this input handler.
* This should not be called in the constructor of this
* input handler, because applications might load the
* key bindings from a file, etc.
*/
public abstract void addDefaultKeyBindings();
/**
* Adds a key binding to this input handler.
* @param keyBinding The key binding (the format of this is
* input-handler specific)
* @param action The action
*/
public abstract void addKeyBinding(String keyBinding, ActionListener action);
/**
* Removes a key binding from this input handler.
* @param keyBinding The key binding
*/
public abstract void removeKeyBinding(String keyBinding);
/**
* Removes all key bindings from this input handler.
*/
public abstract void removeAllKeyBindings();
/**
* Grabs the next key typed event and invokes the specified
* action with the key as a the action command.
*/
public void grabNextKeyStroke(ActionListener listener) {
grabAction = listener;
}
/**
* Returns if repeating is enabled. When repeating is enabled,
* actions will be executed multiple times. This is usually
* invoked with a special key stroke in the input handler.
*/
public boolean isRepeatEnabled() {
return repeat;
}
/**
* Enables repeating. When repeating is enabled, actions will be
* executed multiple times. Once repeating is enabled, the input
* handler should read a number from the keyboard.
*/
public void setRepeatEnabled(boolean repeat) {
this.repeat = repeat;
}
/**
* Returns the number of times the next action will be repeated.
*/
public int getRepeatCount() {
return (repeat ? Math.max(1,repeatCount) : 1);
}
/**
* Sets the number of times the next action will be repeated.
* @param repeatCount The repeat count
*/
public void setRepeatCount(int repeatCount) {
this.repeatCount = repeatCount;
}
/**
* Returns the macro recorder. If this is non-null, all executed
* actions should be forwarded to the recorder.
*/
public InputHandler.MacroRecorder getMacroRecorder() {
return recorder;
}
/**
* Sets the macro recorder. If this is non-null, all executed
* actions should be forwarded to the recorder.
* @param recorder The macro recorder
*/
public void setMacroRecorder(InputHandler.MacroRecorder recorder) {
this.recorder = recorder;
}
/**
* Returns a copy of this input handler that shares the same
* key bindings. Setting key bindings in the copy will also
* set them in the original.
*/
public abstract InputHandler copy();
/**
* Executes the specified action, repeating and recording it as
* necessary.
* @param listener The action listener
* @param source The event source
* @param actionCommand The action command
*/
public void executeAction(ActionListener listener, Object source,
String actionCommand) {
// create event
ActionEvent evt = new ActionEvent(source,
ActionEvent.ACTION_PERFORMED,
actionCommand);
// don't do anything if the action is a wrapper
// (like EditAction.Wrapper)
if (listener instanceof Wrapper) {
listener.actionPerformed(evt);
return;
}
// remember old values, in case action changes them
boolean _repeat = repeat;
int _repeatCount = getRepeatCount();
// execute the action
if (listener instanceof InputHandler.NonRepeatable) {
listener.actionPerformed(evt);
} else {
for (int i = 0; i < Math.max(1,repeatCount); i++)
listener.actionPerformed(evt);
}
// do recording. Notice that we do no recording whatsoever
// for actions that grab keys
if (grabAction == null) {
if (recorder != null) {
if (!(listener instanceof InputHandler.NonRecordable)) {
if (_repeatCount != 1) {
recorder.actionPerformed(REPEAT,String.valueOf(_repeatCount));
}
recorder.actionPerformed(listener,actionCommand);
}
}
// If repeat was true originally, clear it
// Otherwise it might have been set by the action, etc
if (_repeat) {
repeat = false;
repeatCount = 0;
}
}
}
/**
* Returns the text area that fired the specified event.
* @param evt The event
*/
public static JEditTextArea getTextArea(EventObject evt) {
if (evt != null) {
Object o = evt.getSource();
if (o instanceof Component)
{
// find the parent text area
Component c = (Component)o;
for(;;)
{
if (c instanceof JEditTextArea)
return (JEditTextArea)c;
else if (c == null)
break;
if (c instanceof JPopupMenu)
c = ((JPopupMenu)c)
.getInvoker();
else
c = c.getParent();
}
}
}
// this shouldn't happen
System.err.println("BUG: getTextArea() returning null");
System.err.println("Report this to Slava Pestov <sp@gjt.org>");
return null;
}
// protected members
/**
* If a key is being grabbed, this method should be called with
* the appropriate key event. It executes the grab action with
* the typed character as the parameter.
*/
protected void handleGrabAction(KeyEvent evt) {
// Clear it *before* it is executed so that executeAction()
// resets the repeat count
ActionListener _grabAction = grabAction;
grabAction = null;
executeAction(_grabAction,evt.getSource(),
String.valueOf(evt.getKeyChar()));
}
// protected members
protected ActionListener grabAction;
protected boolean repeat;
protected int repeatCount;
protected InputHandler.MacroRecorder recorder;
/**
* If an action implements this interface, it should not be repeated.
* Instead, it will handle the repetition itself.
*/
public interface NonRepeatable { }
/**
* If an action implements this interface, it should not be recorded
* by the macro recorder. Instead, it will do its own recording.
*/
public interface NonRecordable { }
/**
* For use by EditAction.Wrapper only.
* @since jEdit 2.2final
*/
public interface Wrapper { }
public interface MacroRecorder {
void actionPerformed(ActionListener listener, String actionCommand);
}
static public class backspace implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
if (!textArea.isEditable()) {
textArea.getToolkit().beep();
return;
}
if (textArea.getSelectionStart() != textArea.getSelectionStop()) {
textArea.setSelectedText("");
} else {
int caret = textArea.getCaretPosition();
if (caret == 0) {
textArea.getToolkit().beep();
return;
}
try {
textArea.getDocument().remove(caret - 1,1);
} catch(BadLocationException bl) {
bl.printStackTrace();
}
}
}
}
public static class backspace_word implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int start = textArea.getSelectionStart();
if (start != textArea.getSelectionStop())
{
textArea.setSelectedText("");
}
int line = textArea.getCaretLine();
int lineStart = textArea.getLineStartOffset(line);
int caret = start - lineStart;
String lineText = textArea.getLineText(textArea
.getCaretLine());
if (caret == 0)
{
if (lineStart == 0)
{
textArea.getToolkit().beep();
return;
}
caret--;
}
else
{
String noWordSep = (String)textArea.getDocument().getProperty("noWordSep");
caret = findWordStart(lineText,caret,noWordSep);
}
try
{
textArea.getDocument().remove(
caret + lineStart,
start - (caret + lineStart));
}
catch(BadLocationException bl)
{
bl.printStackTrace();
}
}
}
public static class delete implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
if (!textArea.isEditable()) {
textArea.getToolkit().beep();
return;
}
if (textArea.getSelectionStart() != textArea.getSelectionStop()) {
textArea.setSelectedText("");
} else {
int caret = textArea.getCaretPosition();
if (caret == textArea.getDocumentLength()) {
textArea.getToolkit().beep();
return;
}
try {
textArea.getDocument().remove(caret,1);
} catch(BadLocationException bl) {
bl.printStackTrace();
}
}
}
}
public static class delete_word implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
int start = textArea.getSelectionStart();
if (start != textArea.getSelectionStop()) {
textArea.setSelectedText("");
}
int line = textArea.getCaretLine();
int lineStart = textArea.getLineStartOffset(line);
int caret = start - lineStart;
String lineText =
textArea.getLineText(textArea.getCaretLine());
if (caret == lineText.length()) {
if (lineStart + caret == textArea.getDocumentLength()) {
textArea.getToolkit().beep();
return;
}
caret++;
} else {
String noWordSep = (String)textArea.getDocument().getProperty("noWordSep");
caret = findWordEnd(lineText,caret,noWordSep);
}
try {
textArea.getDocument().remove(start, (caret + lineStart) - start);
} catch(BadLocationException bl) {
bl.printStackTrace();
}
}
}
public static class end implements ActionListener {
private boolean select;
public end(boolean select) {
this.select = select;
}
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
int caret = textArea.getCaretPosition();
int caretLine = textArea.getCaretLine();
int lastOfLine = textArea.getLineStopOffset(caretLine) - 1;
int lastNonWhiteSpaceOfLine =
textArea.getLineStopNonWhiteSpaceOffset(caretLine) - 1;
int lastVisibleLine =
textArea.getFirstLine() + textArea.getVisibleLines();
if (lastVisibleLine >= textArea.getLineCount()) {
lastVisibleLine = Math.min(textArea.getLineCount() - 1,
lastVisibleLine);
} else {
lastVisibleLine -= (textArea.getElectricScroll() + 1);
}
int lastVisible = textArea.getLineStopOffset(lastVisibleLine) - 1;
int lastDocument = textArea.getDocumentLength();
if (caret == lastDocument &&
!Preferences.getBoolean(CONTEXT_AWARE_HOME_END)) {
textArea.getToolkit().beep();
return;
} else if (!Boolean.TRUE.equals(textArea.getClientProperty(SMART_HOME_END_PROPERTY))) {
if (!Preferences.getBoolean(CONTEXT_AWARE_HOME_END) || caret == lastNonWhiteSpaceOfLine) {
caret = lastOfLine;
} else {
caret = lastNonWhiteSpaceOfLine;
}
} else if (caret == lastVisible) {
caret = lastDocument;
} else if (caret == lastOfLine) {
caret = lastVisible;
} else {
caret = lastOfLine;
}
if (select) {
textArea.select(textArea.getMarkPosition(),caret);
} else {
textArea.setCaretPosition(caret);
}
}
}
public static class document_end implements ActionListener {
private boolean select;
public document_end(boolean select) {
this.select = select;
}
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
if (select) {
textArea.select(textArea.getMarkPosition(),
textArea.getDocumentLength());
} else {
textArea.setCaretPosition(textArea.getDocumentLength());
}
}
}
public static class home implements ActionListener {
private boolean select;
public home(boolean select) {
this.select = select;
}
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
int caret = textArea.getCaretPosition();
int firstLine = textArea.getFirstLine();
int caretLine = textArea.getCaretLine();
int firstOfLine = textArea.getLineStartOffset(caretLine);
int firstNonWhiteSpaceOfLine =
textArea.getLineStartNonWhiteSpaceOffset(caretLine);
int firstVisibleLine =
(firstLine == 0 ? 0 : firstLine + textArea.getElectricScroll());
int firstVisible = textArea.getLineStartOffset(firstVisibleLine);
if (caret == 0 && !Preferences.getBoolean(CONTEXT_AWARE_HOME_END)) {
textArea.getToolkit().beep();
return;
} else if (!Boolean.TRUE.equals(textArea.getClientProperty(SMART_HOME_END_PROPERTY))) {
if (!Preferences.getBoolean(CONTEXT_AWARE_HOME_END) || caret == firstNonWhiteSpaceOfLine) {
caret = firstOfLine;
} else {
caret = firstNonWhiteSpaceOfLine;
}
} else if (caret == firstVisible) {
caret = 0;
} else if (caret == firstOfLine) {
caret = firstVisible;
} else {
caret = firstOfLine;
}
if (select) {
textArea.select(textArea.getMarkPosition(),caret);
} else {
textArea.setCaretPosition(caret);
}
}
}
public static class document_home implements ActionListener {
private boolean select;
public document_home(boolean select) {
this.select = select;
}
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
if (select) {
textArea.select(textArea.getMarkPosition(),0);
} else {
textArea.setCaretPosition(0);
}
}
}
public static class insert_break implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);
if (!textArea.isEditable())
{
textArea.getToolkit().beep();
return;
}
textArea.setSelectedText("\n");
}
}
public static class insert_tab implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
if (!textArea.isEditable())
{
textArea.getToolkit().beep();
return;
}
textArea.overwriteSetSelectedText("\t");
}
}
public static class next_char implements ActionListener
{
private boolean select;
public next_char(boolean select)
{
this.select = select;
}
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int caret = textArea.getCaretPosition();
if (caret == textArea.getDocumentLength())
{
if (textArea.getSelectionStart() !=
textArea.getSelectionStop()) {
// just move to the end of the selection
textArea.select(caret, caret);
} else {
// beep at the user for being annoying
textArea.getToolkit().beep();
}
} else if (select) {
textArea.select(textArea.getMarkPosition(), caret+1);
} else {
int start = textArea.getSelectionStart();
int end = textArea.getSelectionStop();
if (start != end) {
textArea.select(end, end);
} else {
textArea.setCaretPosition(caret + 1);
}
}
}
}
public static class next_line implements ActionListener
{
private boolean select;
public next_line(boolean select)
{
this.select = select;
}
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int caret = textArea.getCaretPosition();
int line = textArea.getCaretLine();
if (line == textArea.getLineCount() - 1)
{
//textArea.getToolkit().beep();
int doc = textArea.getDocumentLength();
if (select) {
textArea.select(textArea.getMarkPosition(), doc);
} else {
textArea.setCaretPosition(doc);
}
return;
}
int magic = textArea.getMagicCaretPosition();
if (magic == -1)
{
magic = textArea.offsetToX(line,
caret - textArea.getLineStartOffset(line));
}
caret = textArea.getLineStartOffset(line + 1)
+ textArea.xToOffset(line + 1,magic);
if (select)
textArea.select(textArea.getMarkPosition(),caret);
else
textArea.setCaretPosition(caret);
textArea.setMagicCaretPosition(magic);
}
}
public static class next_page implements ActionListener
{
private boolean select;
public next_page(boolean select)
{
this.select = select;
}
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int lineCount = textArea.getLineCount();
int firstLine = textArea.getFirstLine();
int visibleLines = textArea.getVisibleLines();
int line = textArea.getCaretLine();
// Can't page down if there's nothing to visit
// https://github.com/processing/processing/issues/2990
if (lineCount > visibleLines) {
firstLine += visibleLines; // page down
if (firstLine + visibleLines >= lineCount - 1) {
// back up to the latest line we can go to
firstLine = lineCount - visibleLines;
}
textArea.setFirstLine(firstLine);
int caret = textArea.getLineStartOffset(Math.min(textArea.getLineCount() - 1, line + visibleLines));
if (select)
textArea.select(textArea.getMarkPosition(),caret);
else
textArea.setCaretPosition(caret);
}
}
}
public static class next_word implements ActionListener
{
private boolean select;
public next_word(boolean select)
{
this.select = select;
}
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int caret = textArea.getCaretPosition();
int line = textArea.getCaretLine();
int lineStart = textArea.getLineStartOffset(line);
caret -= lineStart;
String lineText = textArea.getLineText(textArea
.getCaretLine());
if (caret == lineText.length())
{
if (lineStart + caret == textArea.getDocumentLength())
{
textArea.getToolkit().beep();
return;
}
caret++;
}
else
{
String noWordSep = (String)textArea.getDocument().getProperty("noWordSep");
caret = findWordEnd(lineText,caret,noWordSep);
}
if (select)
textArea.select(textArea.getMarkPosition(),
lineStart + caret);
else
textArea.setCaretPosition(lineStart + caret);
}
}
public static class overwrite implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
textArea.setOverwriteEnabled(
!textArea.isOverwriteEnabled());
}
}
public static class prev_char implements ActionListener
{
private boolean select;
public prev_char(boolean select)
{
this.select = select;
}
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int caret = textArea.getCaretPosition();
if (caret == 0)
{
textArea.getToolkit().beep();
return;
}
if (select) {
textArea.select(textArea.getMarkPosition(), caret-1);
} else {
int start = textArea.getSelectionStart();
int end = textArea.getSelectionStop();
if (start != end) {
textArea.select(start, start);
} else {
textArea.setCaretPosition(caret - 1);
}
}
}
}
public static class prev_line implements ActionListener
{
private boolean select;
public prev_line(boolean select)
{
this.select = select;
}
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int caret = textArea.getCaretPosition();
int line = textArea.getCaretLine();
if (line == 0)
{
if (select) {
if (textArea.getSelectionStart() != 0) {
textArea.select(textArea.getMarkPosition(), 0);
}
} else {
textArea.setCaretPosition(0);
}
//textArea.getToolkit().beep();
return;
}
int magic = textArea.getMagicCaretPosition();
if (magic == -1)
{
magic = textArea.offsetToX(line,
caret - textArea.getLineStartOffset(line));
}
caret = textArea.getLineStartOffset(line - 1)
+ textArea.xToOffset(line - 1,magic);
if (select)
textArea.select(textArea.getMarkPosition(),caret);
else
textArea.setCaretPosition(caret);
textArea.setMagicCaretPosition(magic);
}
}
public static class prev_page implements ActionListener
{
private boolean select;
public prev_page(boolean select)
{
this.select = select;
}
public void actionPerformed(ActionEvent evt)
{
JEditTextArea textArea = getTextArea(evt);
int firstLine = textArea.getFirstLine();
int visibleLines = textArea.getVisibleLines();
int line = textArea.getCaretLine();
if (firstLine < visibleLines)
firstLine = visibleLines;
textArea.setFirstLine(firstLine - visibleLines);
int caret = textArea.getLineStartOffset(
Math.max(0,line - visibleLines));
if (select)
textArea.select(textArea.getMarkPosition(),caret);
else
textArea.setCaretPosition(caret);