forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaTextArea.java
More file actions
942 lines (810 loc) · 27.8 KB
/
JavaTextArea.java
File metadata and controls
942 lines (810 loc) · 27.8 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.mode.java.pdex;
import processing.mode.java.JavaInputHandler;
import processing.mode.java.JavaMode;
import processing.mode.java.JavaEditor;
import processing.mode.java.tweak.ColorControlBox;
import processing.mode.java.tweak.Handle;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.SwingWorker;
import processing.app.Base;
import processing.app.Mode;
import processing.app.syntax.JEditTextArea;
import processing.app.syntax.PdeTextAreaDefaults;
import processing.app.syntax.TextAreaDefaults;
public class JavaTextArea extends JEditTextArea {
protected PdeTextAreaDefaults defaults;
protected JavaEditor editor;
static final int LEFT_GUTTER = 59;
static final int RIGHT_GUTTER = 27;
static final int GUTTER_MARGIN = 3;
protected MouseListener[] mouseListeners; // cached mouselisteners, these are wrapped by MouseHandler
// contains line background colors
protected Map<Integer, Color> lineColors = new HashMap<Integer, Color>();
// [px] space added to the left and right of gutter chars
protected int gutterPadding; // = 3;
protected Color gutterBgColor; // = new Color(252, 252, 252); // gutter background color
protected Color gutterLineColor; // = new Color(233, 233, 233); // color of vertical separation line
/// the text marker for highlighting breakpoints in the gutter
public String breakpointMarker = "<>";
/// the text marker for highlighting the current line in the gutter
public String currentLineMarker = "->";
/// maps line index to gutter text
protected Map<Integer, String> gutterText = new HashMap<Integer, String>();
/// maps line index to gutter text color
protected Map<Integer, Color> gutterTextColors = new HashMap<Integer, Color>();
protected ErrorCheckerService errorCheckerService;
protected JavaTextAreaPainter getCustomPainter() {
return (JavaTextAreaPainter) painter;
}
//public JavaTextArea(TextAreaDefaults defaults, InputHandler inputHandler, JavaEditor editor) {
//public JavaTextArea(JavaEditor editor) {
public JavaTextArea(TextAreaDefaults defaults, JavaEditor editor) {
super(defaults, new JavaInputHandler(editor));
//super(defaults, inputHandler);
this.editor = editor;
// removed all this since we have the createPainter() method and we
// won't have to remove/re-add the custom painter object [fry 150122]
// // replace the painter:
// // first save listeners, these are package-private in JEditTextArea, so not accessible
// ComponentListener[] componentListeners = painter.getComponentListeners();
mouseListeners = painter.getMouseListeners();
// MouseMotionListener[] mouseMotionListeners = painter.getMouseMotionListeners();
//
// remove(painter);
// // set new painter
// customPainter = new TextAreaPainter(this, defaults);
// painter = customPainter;
//
// // set listeners
// for (ComponentListener cl : componentListeners) {
// painter.addComponentListener(cl);
// }
//
// for (MouseMotionListener mml : mouseMotionListeners) {
// painter.addMouseMotionListener(mml);
// }
// use a custom mouse handler instead of directly using mouseListeners
MouseHandler mouseHandler = new MouseHandler();
painter.addMouseListener(mouseHandler);
painter.addMouseMotionListener(mouseHandler);
//addCompletionPopupListner();
add(CENTER, painter);
// load settings from theme.txt
Mode mode = editor.getMode();
gutterBgColor = mode.getColor("gutter.bgcolor"); //, gutterBgColor);
gutterLineColor = mode.getColor("gutter.linecolor"); //, gutterLineColor);
gutterPadding = mode.getInteger("gutter.padding");
breakpointMarker = mode.getString("breakpoint.marker"); //, breakpointMarker);
currentLineMarker = mode.getString("currentline.marker"); //, currentLineMarker);
// TweakMode code
prevCompListeners = painter.getComponentListeners();
prevMouseListeners = painter.getMouseListeners();
prevMMotionListeners = painter.getMouseMotionListeners();
prevKeyListeners = editor.getKeyListeners();
interactiveMode = false;
addPrevListeners();
}
protected JavaTextAreaPainter createPainter(final TextAreaDefaults defaults) {
return new JavaTextAreaPainter(this, defaults);
}
/**
* Sets ErrorCheckerService and loads theme for TextArea(XQMode)
*
* @param ecs
* @param mode
*/
public void setECSandThemeforTextArea(ErrorCheckerService ecs,
JavaMode mode) {
errorCheckerService = ecs;
getCustomPainter().setECSandTheme(ecs, mode);
}
/**
* Handles KeyEvents for TextArea (code completion begins from here).
*/
public void processKeyEvent(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
if (suggestion != null){
if (suggestion.isVisible()){
Base.log("esc key");
hideSuggestion();
evt.consume();
return;
}
}
} else if (evt.getKeyCode() == KeyEvent.VK_ENTER &&
evt.getID() == KeyEvent.KEY_PRESSED) {
if (suggestion != null) {
if (suggestion.isVisible()) {
if (suggestion.insertSelection(CompletionPanel.KEYBOARD_COMPLETION)) {
//hideSuggestion(); // Kill it!
evt.consume();
// Still try to show suggestions after inserting if it's
// the case of overloaded methods. See #2755
if(suggestion.isVisible())
prepareSuggestions(evt);
return;
}
}
}
}
if (evt.getID() == KeyEvent.KEY_PRESSED) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
if (suggestion != null)
if (suggestion.isVisible()) {
//log("KeyDown");
suggestion.moveDown();
return;
}
break;
case KeyEvent.VK_UP:
if (suggestion != null)
if (suggestion.isVisible()) {
//log("KeyUp");
suggestion.moveUp();
return;
}
break;
case KeyEvent.VK_BACK_SPACE:
Base.log("BK Key");
break;
case KeyEvent.VK_SPACE:
if (suggestion != null)
if (suggestion.isVisible()) {
Base.log("Space bar, hide completion list");
suggestion.hide();
}
break;
default:
break;
}
}
super.processKeyEvent(evt);
// code completion disabled if java tabs
if (!editor.hasJavaTabs()) {
if (evt.getID() == KeyEvent.KEY_TYPED) {
processCompletionKeys(evt);
} else if (Base.isMacOS() && evt.getID() == KeyEvent.KEY_RELEASED) {
processControlSpace(evt);
}
}
}
// #2699 - Special case for OS X, where Ctrl-Space is not detected as Key_Typed -_-
private void processControlSpace(final KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_SPACE && event.isControlDown()) {
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
// Provide completions only if it's enabled
if (JavaMode.codeCompletionsEnabled) {
Base.log("[KeyEvent]" + KeyEvent.getKeyText(event.getKeyCode()) + " |Prediction started");
Base.log("Typing: " + fetchPhrase(event));
}
return null;
}
};
worker.execute();
}
}
private void processCompletionKeys(final KeyEvent event) {
char keyChar = event.getKeyChar();
if (keyChar == KeyEvent.VK_ENTER ||
keyChar == KeyEvent.VK_ESCAPE ||
keyChar == KeyEvent.VK_TAB ||
keyChar == KeyEvent.CHAR_UNDEFINED) {
return;
} else if (keyChar == ')') {
hideSuggestion(); // See #2741
return;
}
if (keyChar == '.') {
if (JavaMode.codeCompletionsEnabled) {
Base.log("[KeyEvent]" + KeyEvent.getKeyText(event.getKeyCode()) + " |Prediction started");
Base.log("Typing: " + fetchPhrase(event));
}
} else if (keyChar == ' ') { // Trigger on Ctrl-Space
if (!Base.isMacOS() && JavaMode.codeCompletionsEnabled &&
(event.isControlDown() || event.isMetaDown())) {
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
// Provide completions only if it's enabled
if (JavaMode.codeCompletionsEnabled) {
getDocument().remove(getCaretPosition() - 1, 1); // Remove the typed space
Base.log("[KeyEvent]" + event.getKeyChar() + " |Prediction started");
Base.log("Typing: " + fetchPhrase(event));
}
return null;
}
};
worker.execute();
} else {
hideSuggestion(); // hide on spacebar
}
} else {
if (JavaMode.codeCompletionsEnabled) {
prepareSuggestions(event);
}
}
}
/** Kickstart auto-complete suggestions */
private void prepareSuggestions(final KeyEvent evt){
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
// Provide completions only if it's enabled
if (JavaMode.codeCompletionsEnabled &&
(JavaMode.ccTriggerEnabled || suggestion.isVisible())) {
Base.log("[KeyEvent]" + evt.getKeyChar() + " |Prediction started");
Base.log("Typing: " + fetchPhrase(evt));
}
return null;
}
};
worker.execute();
}
/**
* Retrieves the word on which the mouse pointer is present
* @param evt - the MouseEvent which triggered this method
*/
private String fetchPhrase(MouseEvent evt) {
Base.log("--handle Mouse Right Click--");
int off = xyToOffset(evt.getX(), evt.getY());
if (off < 0)
return null;
int line = getLineOfOffset(off);
if (line < 0)
return null;
String s = getLineText(line);
if (s == null)
return null;
else if (s.length() == 0)
return null;
else {
int x = xToOffset(line, evt.getX()), x2 = x + 1, x1 = x - 1;
int xLS = off - getLineStartNonWhiteSpaceOffset(line);
Base.log("x=" + x);
if (x < 0 || x >= s.length())
return null;
String word = s.charAt(x) + "";
if (s.charAt(x) == ' ')
return null;
if (!(Character.isLetterOrDigit(s.charAt(x)) || s.charAt(x) == '_' || s
.charAt(x) == '$'))
return null;
int i = 0;
while (true) {
i++;
if (x1 >= 0 && x1 < s.length()) {
if (Character.isLetter(s.charAt(x1)) || s.charAt(x1) == '_') {
word = s.charAt(x1--) + word;
xLS--;
} else
x1 = -1;
} else
x1 = -1;
if (x2 >= 0 && x2 < s.length()) {
if (Character.isLetterOrDigit(s.charAt(x2)) || s.charAt(x2) == '_'
|| s.charAt(x2) == '$')
word = word + s.charAt(x2++);
else
x2 = -1;
} else
x2 = -1;
if (x1 < 0 && x2 < 0)
break;
if (i > 200) {
// time out!
break;
}
}
if (Character.isDigit(word.charAt(0)))
return null;
Base.log("Mouse click, word: " + word.trim());
errorCheckerService.getASTGenerator().setLastClickedWord(line, word, xLS);
return word.trim();
}
}
/**
* Retrieves the current word typed just before the caret.
* Then triggers code completion for that word.
*
* @param evt - the KeyEvent which triggered this method
* @return
*/
public String fetchPhrase(KeyEvent evt) {
int off = getCaretPosition();
Base.log("off " + off);
if (off < 0)
return null;
int line = getCaretLine();
if (line < 0)
return null;
String s = getLineText(line);
Base.log(" line " + line);
//log2(s + " len " + s.length());
int x = getCaretPosition() - getLineStartOffset(line) - 1, x1 = x - 1;
if(x >= s.length() || x < 0) {
//log("X is " + x + ". Returning null");
hideSuggestion();
return null; //TODO: Does this check cause problems? Verify.
}
Base.log(" x char: " + s.charAt(x));
if (!(Character.isLetterOrDigit(s.charAt(x)) || s.charAt(x) == '_'
|| s.charAt(x) == '(' || s.charAt(x) == '.')) {
//log("Char before caret isn't a letter/digit/_(. so no predictions");
hideSuggestion();
return null;
} else if (x > 0 && (s.charAt(x - 1) == ' ' || s.charAt(x - 1) == '(')
&& Character.isDigit(s.charAt(x))) {
//log("Char before caret isn't a letter, but ' ' or '(', so no predictions");
hideSuggestion(); // See #2755, Option 2 comment
return null;
} else if (x == 0){
//log("X is zero");
hideSuggestion();
return null;
}
//int xLS = off - getLineStartNonWhiteSpaceOffset(line);
String word = (x < s.length() ? s.charAt(x) : "") + "";
if (s.trim().length() == 1) {
// word = ""
// + (keyChar == KeyEvent.CHAR_UNDEFINED ? s.charAt(x - 1) : keyChar);
//word = (x < s.length()?s.charAt(x):"") + "";
word = word.trim();
if (word.endsWith("."))
word = word.substring(0, word.length() - 1);
errorCheckerService.getASTGenerator().preparePredictions(word, line
+ errorCheckerService.mainClassOffset,0);
return word;
}
int i = 0;
int closeB = 0;
while (true) {
i++;
//TODO: currently works on single line only. "a. <new line> b()" won't be detected
if (x1 >= 0) {
// if (s.charAt(x1) != ';' && s.charAt(x1) != ',' && s.charAt(x1) != '(')
if (Character.isLetterOrDigit(s.charAt(x1)) || s.charAt(x1) == '_'
|| s.charAt(x1) == '.' || s.charAt(x1) == ')' || s.charAt(x1) == ']') {
if (s.charAt(x1) == ')') {
word = s.charAt(x1--) + word;
closeB++;
while (x1 >= 0 && closeB > 0) {
word = s.charAt(x1) + word;
if (s.charAt(x1) == '(')
closeB--;
if (s.charAt(x1) == ')')
closeB++;
x1--;
}
}
else if (s.charAt(x1) == ']') {
word = s.charAt(x1--) + word;
closeB++;
while (x1 >= 0 && closeB > 0) {
word = s.charAt(x1) + word;
if (s.charAt(x1) == '[')
closeB--;
if (s.charAt(x1) == ']')
closeB++;
x1--;
}
}
else {
word = s.charAt(x1--) + word;
}
} else {
break;
}
} else {
break;
}
if (i > 200) {
// time out!
break;
}
}
if (Character.isDigit(word.charAt(0)))
return null;
word = word.trim();
// if (word.endsWith("."))
// word = word.substring(0, word.length() - 1);
int lineStartNonWSOffset = 0;
if (word.length() >= JavaMode.codeCompletionTriggerLength) {
errorCheckerService.getASTGenerator()
.preparePredictions(word, line + errorCheckerService.mainClassOffset,
lineStartNonWSOffset);
}
return word;
}
// /**
// * Retrieve the total width of the gutter area.
// * @return gutter width in pixels
// */
// protected int getGutterWidth() {
// if (!editor.isDebugToolbarEnabled()) {
// return 0;
// }
//
// FontMetrics fm = painter.getFontMetrics();
// int textWidth = Math.max(fm.stringWidth(breakpointMarker),
// fm.stringWidth(currentLineMarker));
// return textWidth + 2 * gutterPadding;
// }
//
//
// /**
// * Retrieve the width of margins applied to the left and right of the gutter
// * text.
// *
// * @return margins in pixels
// */
// protected int getGutterMargins() {
// if (!editor.isDebugToolbarEnabled()) {
// return 0;
// }
// return gutterPadding;
// }
/**
* Set the gutter text of a specific line.
*
* @param lineIdx
* the line index (0-based)
* @param text
* the text
*/
public void setGutterText(int lineIdx, String text) {
gutterText.put(lineIdx, text);
painter.invalidateLine(lineIdx);
}
/**
* Set the gutter text and color of a specific line.
*
* @param lineIdx
* the line index (0-based)
* @param text
* the text
* @param textColor
* the text color
*/
public void setGutterText(int lineIdx, String text, Color textColor) {
gutterTextColors.put(lineIdx, textColor);
setGutterText(lineIdx, text);
}
/**
* Clear the gutter text of a specific line.
*
* @param lineIdx
* the line index (0-based)
*/
public void clearGutterText(int lineIdx) {
gutterText.remove(lineIdx);
painter.invalidateLine(lineIdx);
}
/**
* Clear all gutter text.
*/
public void clearGutterText() {
for (int lineIdx : gutterText.keySet()) {
painter.invalidateLine(lineIdx);
}
gutterText.clear();
}
/**
* Retrieve the gutter text of a specific line.
*
* @param lineIdx
* the line index (0-based)
* @return the gutter text
*/
public String getGutterText(int lineIdx) {
return gutterText.get(lineIdx);
}
/**
* Retrieve the gutter text color for a specific line.
*
* @param lineIdx
* the line index
* @return the gutter text color
*/
public Color getGutterTextColor(int lineIdx) {
return gutterTextColors.get(lineIdx);
}
/**
* Set the background color of a line.
*
* @param lineIdx
* 0-based line number
* @param col
* the background color to set
*/
public void setLineBgColor(int lineIdx, Color col) {
lineColors.put(lineIdx, col);
painter.invalidateLine(lineIdx);
}
/**
* Clear the background color of a line.
*
* @param lineIdx
* 0-based line number
*/
public void clearLineBgColor(int lineIdx) {
lineColors.remove(lineIdx);
painter.invalidateLine(lineIdx);
}
/**
* Clear all line background colors.
*/
public void clearLineBgColors() {
for (int lineIdx : lineColors.keySet()) {
painter.invalidateLine(lineIdx);
}
lineColors.clear();
}
/**
* Get a lines background color.
*
* @param lineIdx
* 0-based line number
* @return the color or null if no color was set for the specified line
*/
public Color getLineBgColor(int lineIdx) {
return lineColors.get(lineIdx);
}
/**
* Convert a character offset to a horizontal pixel position inside the text
* area. Overridden to take gutter width into account.
*
* @param line
* the 0-based line number
* @param offset
* the character offset (0 is the first character on a line)
* @return the horizontal position
*/
@Override
public int _offsetToX(int line, int offset) {
return super._offsetToX(line, offset) + LEFT_GUTTER;
}
/**
* Convert a horizontal pixel position to a character offset. Overridden to
* take gutter width into account.
*
* @param line
* the 0-based line number
* @param x
* the horizontal pixel position
* @return he character offset (0 is the first character on a line)
*/
@Override
public int xToOffset(int line, int x) {
return super.xToOffset(line, x - LEFT_GUTTER);
}
/**
* Custom mouse handler. Implements double clicking in the gutter area to
* toggle breakpoints, sets default cursor (instead of text cursor) in the
* gutter area.
*/
protected class MouseHandler implements MouseListener, MouseMotionListener {
protected int lastX; // previous horizontal positon of the mouse cursor
@Override
public void mouseClicked(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseClicked(me);
}
}
@Override
public void mousePressed(MouseEvent me) {
// check if this happened in the gutter area
if (me.getX() < LEFT_GUTTER) {
if (me.getButton() == MouseEvent.BUTTON1 && me.getClickCount() == 2) {
int line = me.getY() / painter.getFontMetrics().getHeight()
+ firstLine;
if (line >= 0 && line <= getLineCount() - 1) {
editor.gutterDblClicked(line);
}
}
return;
}
if (me.getButton() == MouseEvent.BUTTON3) {
if (!editor.hasJavaTabs()) { // tooltips, etc disabled for java tabs
fetchPhrase(me);
}
}
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mousePressed(me);
}
}
@Override
public void mouseReleased(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseReleased(me);
}
}
@Override
public void mouseEntered(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseEntered(me);
}
}
@Override
public void mouseExited(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseExited(me);
}
}
@Override
public void mouseDragged(MouseEvent me) {
// No need to forward since the standard MouseMotionListeners are called anyway
// nop
}
@Override
public void mouseMoved(MouseEvent me) {
// No need to forward since the standard MouseMotionListeners are called anyway
if (me.getX() < LEFT_GUTTER) {
if (lastX >= LEFT_GUTTER) {
painter.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
} else {
if (lastX < LEFT_GUTTER) {
painter.setCursor(new Cursor(Cursor.TEXT_CURSOR));
}
}
lastX = me.getX();
}
}
private CompletionPanel suggestion;
// appears unused, removed when looking to change completion trigger [fry 140801]
/*
public void showSuggestionLater(final DefaultListModel defListModel, final String word) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
showSuggestion(defListModel,word);
}
});
}
*/
/**
* Calculates location of caret and displays the suggestion popup at the location.
*
* @param listModel
* @param subWord
*/
protected void showSuggestion(DefaultListModel<CompletionCandidate> listModel, String subWord) {
hideSuggestion();
if (listModel.size() == 0) {
Base.log("TextArea: No suggestions to show.");
} else {
int position = getCaretPosition();
Point location = new Point();
try {
location.x = offsetToX(getCaretLine(), position
- getLineStartOffset(getCaretLine()));
location.y = lineToY(getCaretLine())
+ getPainter().getFontMetrics().getHeight() + getPainter().getFontMetrics().getDescent();
//log("TA position: " + location);
} catch (Exception e2) {
e2.printStackTrace();
return;
}
if (subWord.length() < 2) {
return;
}
suggestion = new CompletionPanel(this, position, subWord,
listModel, location, editor);
requestFocusInWindow();
}
}
/** Hides suggestion popup */
public void hideSuggestion() {
if (suggestion != null) {
suggestion.hide();
//log("Suggestion hidden.");
suggestion = null;
}
}
// TweakMode code
// save input listeners to stop/start text edit
ComponentListener[] prevCompListeners;
MouseListener[] prevMouseListeners;
MouseMotionListener[] prevMMotionListeners;
KeyListener[] prevKeyListeners;
boolean interactiveMode;
/* remove all standard interaction listeners */
public void removeAllListeners()
{
ComponentListener[] componentListeners = painter
.getComponentListeners();
MouseListener[] mouseListeners = painter.getMouseListeners();
MouseMotionListener[] mouseMotionListeners = painter
.getMouseMotionListeners();
KeyListener[] keyListeners = editor.getKeyListeners();
for (ComponentListener cl : componentListeners)
painter.removeComponentListener(cl);
for (MouseListener ml : mouseListeners)
painter.removeMouseListener(ml);
for (MouseMotionListener mml : mouseMotionListeners)
painter.removeMouseMotionListener(mml);
for (KeyListener kl : keyListeners) {
editor.removeKeyListener(kl);
}
}
public void startInteractiveMode()
{
// ignore if we are already in interactiveMode
if (interactiveMode)
return;
removeAllListeners();
// add our private interaction listeners
getCustomPainter().startInterativeMode();
// customPainter.addMouseListener(customPainter);
// customPainter.addMouseMotionListener(customPainter);
// customPainter.startInterativeMode();
// customPainter.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
this.editable = false;
this.caretBlinks = false;
this.setCaretVisible(false);
interactiveMode = true;
}
public void stopInteractiveMode()
{
// ignore if we are not in interactive mode
if (!interactiveMode)
return;
removeAllListeners();
addPrevListeners();
getCustomPainter().stopInteractiveMode();
// customPainter.setCursor(new Cursor(Cursor.TEXT_CURSOR));
this.editable = true;
this.caretBlinks = true;
this.setCaretVisible(true);
interactiveMode = false;
}
public int getHorizontalScroll()
{
return horizontal.getValue();
}
private void addPrevListeners()
{
// add the original text-edit listeners
for (ComponentListener cl : prevCompListeners) {
painter.addComponentListener(cl);
}
for (MouseListener ml : prevMouseListeners) {
painter.addMouseListener(ml);
}
for (MouseMotionListener mml : prevMMotionListeners) {
painter.addMouseMotionListener(mml);
}
for (KeyListener kl : prevKeyListeners) {
editor.addKeyListener(kl);
}
}
//public void updateInterface(ArrayList<Handle> handles[], ArrayList<ColorControlBox> colorBoxes[]) {
public void updateInterface(List<List<Handle>> handles, List<List<ColorControlBox>> colorBoxes) {
getCustomPainter().updateInterface(handles, colorBoxes);
}
}