FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/app/src/processing/app/syntax/JEditTextArea.java at master · orgicus/processing · GitHub
orgicus
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
syntax
/
JEditTextArea.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
2600 lines (2178 loc) · 70 KB
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
syntax
/
JEditTextArea.java
Copy path
File metadata and controls
2600 lines (2178 loc) · 70 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* JEditTextArea.java - jEdit's text component
* 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
processing
.
app
.*;
import
java
.
awt
.*;
import
java
.
awt
.
datatransfer
.*;
import
java
.
awt
.
event
.*;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Enumeration
;
import
java
.
util
.
Vector
;
import
javax
.
swing
.
event
.*;
import
javax
.
swing
.
text
.*;
import
javax
.
swing
.
undo
.*;
import
javax
.
swing
.*;
import
java
.
awt
.
im
.
InputMethodRequests
;
import
java
.
awt
.
print
.
Printable
;
import
processing
.
app
.
syntax
.
im
.
InputMethodSupport
;
import
processing
.
core
.
PApplet
;
/**
* The text area component from the JEdit Syntax (syntax.jedit.org) project.
* This is a very early version of what later was completely rewritten and
* become jEdit (jedit.org). Over the years we've also added minor features
* for use with Processing (notably mouse wheel support and copyAsHTML). [fry]
* <p>
* jEdit's text area component. It is more suited for editing program
* source code than JEditorPane, because it drops the unnecessary features
* (images, variable-width lines, and so on) and adds a whole bunch of
* useful goodies such as:
* <ul>
* <li>More flexible key binding scheme
* <li>Supports macro recorders
* <li>Rectangular selection
* <li>Bracket highlighting
* <li>Syntax highlighting
* <li>Command repetition
* <li>Block caret can be enabled
* </ul>
* It is also faster and doesn't have as many problems. It can be used
* in other applications; the only other part of jEdit it depends on is
* the syntax package.
* <p>
* To use it in your app, treat it like any other component, for example:
* <pre>JEditTextArea ta = new JEditTextArea();
* ta.setTokenMarker(new JavaTokenMarker());
* ta.setText("public class Test {\n"
* + " public static void main(String[] args) {\n"
* + " System.out.println(\"Hello World\");\n"
* + " }\n"
* + "}");</pre>
*
* @author Slava Pestov
*/
public
class
JEditTextArea
extends
JComponent
{
/**
* Adding components with this name to the text area will place
* them left of the horizontal scroll bar. In jEdit, the status
* bar is added this way.
*/
public
static
String
LEFT_OF_SCROLLBAR
=
"los"
;
/** The size of the offset between the leftmost padding and the code */
public
static
final
int
leftHandGutter
=
6
;
private
InputMethodSupport
inputMethodSupport
=
null
;
private
Brackets
bracketHelper
=
new
Brackets
();
/**
* Creates a new JEditTextArea with the specified settings.
* @param defaults The default settings
*/
public
JEditTextArea
(
TextAreaDefaults
defaults
,
InputHandler
inputHandler
) {
// Enable the necessary events
enableEvents
(
AWTEvent
.
KEY_EVENT_MASK
);
if
(!
DISABLE_CARET
) {
caretTimer
=
new
Timer
(
500
,
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
e
) {
if
(
hasFocus
()) {
blinkCaret
();
}
}
});
caretTimer
.
setInitialDelay
(
500
);
caretTimer
.
start
();
}
// Initialize some misc. stuff
painter
=
createPainter
(
defaults
);
documentHandler
=
new
DocumentHandler
();
eventListenerList
=
new
EventListenerList
();
caretEvent
=
new
MutableCaretEvent
();
lineSegment
=
new
Segment
();
bracketLine
=
bracketPosition
= -
1
;
blink
=
true
;
// Initialize the GUI
setLayout
(
new
ScrollLayout
());
add
(
CENTER
,
painter
);
add
(
RIGHT
,
vertical
=
new
JScrollBar
(
Adjustable
.
VERTICAL
));
add
(
BOTTOM
,
horizontal
=
new
JScrollBar
(
Adjustable
.
HORIZONTAL
));
// Add some event listeners
vertical
.
addAdjustmentListener
(
new
AdjustHandler
());
horizontal
.
addAdjustmentListener
(
new
AdjustHandler
());
painter
.
addComponentListener
(
new
ComponentHandler
());
painter
.
addMouseListener
(
new
MouseHandler
());
painter
.
addMouseMotionListener
(
new
DragHandler
());
addFocusListener
(
new
FocusHandler
());
// send tab keys through to the text area
// http://dev.processing.org/bugs/show_bug.cgi?id=1267
setFocusTraversalKeysEnabled
(
false
);
// Load the defaults
setInputHandler
(
inputHandler
);
setDocument
(
defaults
.
document
);
// editable = defaults.editable;
caretVisible
=
defaults
.
caretVisible
;
caretBlinks
=
defaults
.
caretBlinks
;
electricScroll
=
defaults
.
electricScroll
;
// We don't seem to get the initial focus event?
// focusedComponent = this;
addMouseWheelListener
(
new
MouseWheelListener
() {
@
Override
public
void
mouseWheelMoved
(
MouseWheelEvent
e
) {
if
(
scrollBarsInitialized
) {
if
(
e
.
getScrollType
() ==
MouseWheelEvent
.
WHEEL_UNIT_SCROLL
) {
int
scrollAmount
=
e
.
getUnitsToScroll
();
// System.out.println("rot/amt = " + e.getWheelRotation() + " " + amt);
// int max = vertical.getMaximum();
// System.out.println("UNIT SCROLL of " + amt + " at value " + vertical.getValue() + " and max " + max);
// System.out.println(" get wheel rotation is " + e.getWheelRotation());
// int ex = e.getModifiersEx();
// String mods = InputEvent.getModifiersExText(ex);
// if (ex != 0) {
// System.out.println(" 3 2 1 0");
// System.out.println(" 10987654321098765432109876543210");
// System.out.println(" " + PApplet.binary(e.getModifiersEx()));
//// if (mods.length() > 0) {
// System.out.println(" mods extext = " + mods + " " + mods.length() + " " + PApplet.hex(mods.charAt(0)));
// }
// System.out.println(" " + e);
// inertia scrolling on OS X will fire several shift-wheel events
// that are negative values.. this makes the scrolling area jump.
boolean
isHorizontal
=
Platform
.
isMacOS
() &&
e
.
isShiftDown
();
if
(
isHorizontal
) {
horizontal
.
setValue
(
horizontal
.
getValue
() +
scrollAmount
);
}
else
{
vertical
.
setValue
(
vertical
.
getValue
() +
scrollAmount
);
}
}
}
}
});
}
/**
* Override this to provide your own painter for this {@link JEditTextArea}.
* @param defaults
* @return a newly constructed {@link TextAreaPainter}.
*/
protected
TextAreaPainter
createPainter
(
final
TextAreaDefaults
defaults
) {
return
new
TextAreaPainter
(
this
,
defaults
);
}
/**
* Inline Input Method Support for Japanese.
*/
public
InputMethodRequests
getInputMethodRequests
() {
if
(
Preferences
.
getBoolean
(
"editor.input_method_support"
)) {
if
(
inputMethodSupport
==
null
) {
inputMethodSupport
=
new
InputMethodSupport
(
this
);
}
return
inputMethodSupport
;
}
return
null
;
}
/**
* Get current position of the vertical scroll bar. [fry]
* @deprecated Use {@link #getVerticalScrollPosition()}.
*/
public
int
getScrollPosition
() {
return
getVerticalScrollPosition
();
}
/**
* Set position of the vertical scroll bar. [fry]
* @deprecated Use {@link #setVerticalScrollPosition(int)}.
*/
public
void
setScrollPosition
(
int
what
) {
setVerticalScrollPosition
(
what
);
}
/**
* Get current position of the vertical scroll bar.
*/
public
int
getVerticalScrollPosition
() {
return
vertical
.
getValue
();
}
/**
* Set position of the vertical scroll bar.
*/
public
void
setVerticalScrollPosition
(
int
what
) {
vertical
.
setValue
(
what
);
}
/**
* Get current position of the horizontal scroll bar.
*/
public
int
getHorizontalScrollPosition
() {
return
horizontal
.
getValue
();
}
/**
* Set position of the horizontal scroll bar.
*/
public
void
setHorizontalScrollPosition
(
int
what
) {
horizontal
.
setValue
(
what
);
}
/**
* Returns the object responsible for painting this text area.
*/
public
final
TextAreaPainter
getPainter
() {
return
painter
;
}
public
final
Printable
getPrintable
() {
return
painter
.
getPrintable
();
}
/**
* Returns the input handler.
*/
public
final
InputHandler
getInputHandler
() {
return
inputHandler
;
}
/**
* Sets the input handler.
* @param inputHandler The new input handler
*/
public
void
setInputHandler
(
InputHandler
inputHandler
) {
this
.
inputHandler
=
inputHandler
;
}
/**
* Returns true if the caret is blinking, false otherwise.
*/
public
final
boolean
isCaretBlinkEnabled
() {
return
caretBlinks
;
}
/**
* Toggles caret blinking.
* @param caretBlinks True if the caret should blink, false otherwise
*/
public
void
setCaretBlinkEnabled
(
boolean
caretBlinks
) {
this
.
caretBlinks
=
caretBlinks
;
if
(!
caretBlinks
) {
blink
=
false
;
}
painter
.
invalidateSelectedLines
();
}
/**
* Returns true if the caret is visible, false otherwise.
*/
public
final
boolean
isCaretVisible
() {
return
(!
caretBlinks
||
blink
) &&
caretVisible
;
}
/**
* Sets if the caret should be visible.
* @param caretVisible True if the caret should be visible, false
* otherwise
*/
public
void
setCaretVisible
(
boolean
caretVisible
) {
this
.
caretVisible
=
caretVisible
;
blink
=
true
;
painter
.
invalidateSelectedLines
();
}
/**
* Blinks the caret.
*/
public
final
void
blinkCaret
() {
if
(
caretBlinks
) {
blink
= !
blink
;
painter
.
invalidateSelectedLines
();
}
else
{
blink
=
true
;
}
}
/**
* Returns the number of lines from the top and button of the
* text area that are always visible.
*/
public
final
int
getElectricScroll
() {
return
electricScroll
;
}
/**
* Sets the number of lines from the top and bottom of the text
* area that are always visible
* @param electricScroll The number of lines always visible from
* the top or bottom
*/
public
final
void
setElectricScroll
(
int
electricScroll
) {
this
.
electricScroll
=
electricScroll
;
}
/**
* Updates the state of the scroll bars. This should be called
* if the number of lines in the document changes, or when the
* size of the text area changes.
*/
public
void
updateScrollBars
() {
if
(
vertical
!=
null
&&
visibleLines
!=
0
) {
vertical
.
setValues
(
firstLine
,
visibleLines
,
0
,
getLineCount
());
vertical
.
setUnitIncrement
(
2
);
vertical
.
setBlockIncrement
(
visibleLines
);
}
//if (horizontal != null && width != 0) {
if
((
horizontal
!=
null
) && (
painter
.
getWidth
() !=
0
)) {
//int value = horizontal.getValue();
//System.out.println("updateScrollBars");
//int width = painter.getWidth();
int
lineCount
=
getLineCount
();
int
maxLineLength
=
0
;
for
(
int
i
=
0
;
i
<
lineCount
;
i
++) {
int
lineLength
=
getLineLength
(
i
);
if
(
lineLength
>
maxLineLength
) {
maxLineLength
=
lineLength
;
}
}
int
charWidth
=
painter
.
getFontMetrics
().
charWidth
(
'w'
);
int
width
=
maxLineLength
*
charWidth
;
int
painterWidth
=
painter
.
getScrollWidth
();
// Update to how horizontal scrolling is handled
// http://code.google.com/p/processing/issues/detail?id=280
// http://code.google.com/p/processing/issues/detail?id=316
//setValues(int newValue, int newExtent, int newMin, int newMax)
if
(
horizontalOffset
<
0
) {
horizontal
.
setValues
(-
horizontalOffset
,
painterWidth
, -
leftHandGutter
,
width
);
}
else
{
horizontal
.
setValues
(-
leftHandGutter
,
painterWidth
, -
leftHandGutter
,
width
);
}
//horizontal.setUnitIncrement(painter.getFontMetrics().charWidth('w'));
horizontal
.
setUnitIncrement
(
charWidth
);
horizontal
.
setBlockIncrement
(
width
/
2
);
}
}
/**
* Returns the line displayed at the text area's origin.
*/
public
final
int
getFirstLine
() {
return
firstLine
;
}
/**
* Sets the line displayed at the text area's origin without
* updating the scroll bars.
*/
public
void
setFirstLine
(
int
firstLine
) {
if
(
firstLine
<
0
||
firstLine
>
getLineCount
()) {
throw
new
IllegalArgumentException
(
"First line out of range: "
+
firstLine
+
" [0, "
+
getLineCount
() +
"]"
);
}
if
(
firstLine
==
this
.
firstLine
)
return
;
this
.
firstLine
=
firstLine
;
if
(
firstLine
!=
vertical
.
getValue
()) {
updateScrollBars
();
}
painter
.
repaint
();
}
/**
* Convenience for checking what's on-screen. [fry]
*/
public
final
int
getLastLine
() {
return
getFirstLine
() +
getVisibleLines
();
}
/**
* Returns the number of lines visible in this text area.
*/
public
final
int
getVisibleLines
() {
return
visibleLines
;
}
/**
* Recalculates the number of visible lines. This should not
* be called directly.
*/
public
final
void
recalculateVisibleLines
() {
if
(
painter
==
null
)
return
;
int
height
=
painter
.
getHeight
();
int
lineHeight
=
painter
.
getFontMetrics
().
getHeight
();
visibleLines
=
height
/
lineHeight
;
updateScrollBars
();
}
/**
* Returns the horizontal offset of drawn lines.
*/
public
final
int
getHorizontalOffset
() {
return
horizontalOffset
;
}
/**
* Sets the horizontal offset of drawn lines. This can be used to
* implement horizontal scrolling.
* @param horizontalOffset offset The new horizontal offset
*/
public
void
setHorizontalOffset
(
int
horizontalOffset
) {
if
(
horizontalOffset
==
this
.
horizontalOffset
) {
return
;
}
this
.
horizontalOffset
=
horizontalOffset
;
if
(
horizontalOffset
!=
horizontal
.
getValue
()) {
updateScrollBars
();
}
painter
.
repaint
();
}
/**
* A fast way of changing both the first line and horizontal
* offset.
* @param firstLine The new first line
* @param horizontalOffset The new horizontal offset
* @return True if any of the values were changed, false otherwise
*/
public
boolean
setOrigin
(
int
firstLine
,
int
horizontalOffset
) {
boolean
changed
=
false
;
if
(
horizontalOffset
!=
this
.
horizontalOffset
) {
this
.
horizontalOffset
=
horizontalOffset
;
changed
=
true
;
}
if
(
firstLine
!=
this
.
firstLine
) {
this
.
firstLine
=
firstLine
;
changed
=
true
;
}
if
(
changed
) {
updateScrollBars
();
painter
.
repaint
();
}
return
changed
;
}
/**
* Ensures that the caret is visible by scrolling the text area if
* necessary.
* @return True if scrolling was actually performed, false if the
* caret was already visible
*/
public
boolean
scrollToCaret
() {
int
line
=
getCaretLine
();
int
lineStart
=
getLineStartOffset
(
line
);
int
offset
=
Math
.
max
(
0
,
Math
.
min
(
getLineLength
(
line
) -
1
,
getCaretPosition
() -
lineStart
));
return
scrollTo
(
line
,
offset
);
}
/**
* Ensures that the specified line and offset is visible by scrolling
* the text area if necessary.
* @param line The line to scroll to
* @param offset The offset in the line to scroll to
* @return True if scrolling was actually performed, false if the
* line and offset was already visible
*/
public
boolean
scrollTo
(
int
line
,
int
offset
) {
// visibleLines == 0 before the component is realized
// we can't do any proper scrolling then, so we have
// this hack...
if
(
visibleLines
==
0
) {
setFirstLine
(
Math
.
max
(
0
,
line
-
electricScroll
));
return
true
;
}
int
newFirstLine
=
firstLine
;
int
newHorizontalOffset
=
horizontalOffset
;
if
(
line
<
firstLine
+
electricScroll
) {
newFirstLine
=
Math
.
max
(
0
,
line
-
electricScroll
);
}
else
if
(
line
+
electricScroll
>=
firstLine
+
visibleLines
) {
newFirstLine
= (
line
-
visibleLines
) +
electricScroll
+
1
;
if
(
newFirstLine
+
visibleLines
>=
getLineCount
())
newFirstLine
=
getLineCount
() -
visibleLines
;
if
(
newFirstLine
<
0
)
newFirstLine
=
0
;
}
int
x
=
_offsetToX
(
line
,
offset
);
int
width
=
painter
.
getFontMetrics
().
charWidth
(
'w'
);
if
(
x
<
0
) {
newHorizontalOffset
=
Math
.
max
(
0
,
horizontalOffset
-
x
+
width
+
5
);
}
else
if
(
x
+
width
>=
painter
.
getWidth
()) {
newHorizontalOffset
=
horizontalOffset
+
(
painter
.
getWidth
() -
x
) -
width
-
5
;
}
return
setOrigin
(
newFirstLine
,
newHorizontalOffset
);
}
/**
* Converts a line index to a y co-ordinate.
* @param line The line
*/
public
int
lineToY
(
int
line
) {
FontMetrics
fm
=
painter
.
getFontMetrics
();
return
(
line
-
firstLine
) *
fm
.
getHeight
()
- (
fm
.
getLeading
() +
fm
.
getMaxDescent
());
}
/**
* Converts a y co-ordinate to a line index.
* @param y The y co-ordinate
*/
public
int
yToLine
(
int
y
) {
FontMetrics
fm
=
painter
.
getFontMetrics
();
int
height
=
fm
.
getHeight
();
return
Math
.
max
(
0
,
Math
.
min
(
getLineCount
() -
1
,
y
/
height
+
firstLine
));
}
/**
* Converts an offset in a line into an x co-ordinate. This is a
* slow version that can be used any time.
* @param line The line
* @param offset The offset, from the start of the line
*/
public
final
int
offsetToX
(
int
line
,
int
offset
) {
// don't use cached tokens
painter
.
currentLineTokens
=
null
;
return
_offsetToX
(
line
,
offset
);
}
/**
* Converts an offset in a line into an x coordinate. This is a
* fast version that should only be used if no changes were made
* to the text since the last repaint.
* @param line The line
* @param offset The offset, from the start of the line
*/
public
int
_offsetToX
(
int
line
,
int
offset
) {
TokenMarker
tokenMarker
=
getTokenMarker
();
// Use painter's cached info for speed
FontMetrics
fm
=
painter
.
getFontMetrics
();
getLineText
(
line
,
lineSegment
);
int
segmentOffset
=
lineSegment
.
offset
;
int
x
=
horizontalOffset
;
// If syntax coloring is disabled, do simple translation
if
(
tokenMarker
==
null
) {
lineSegment
.
count
=
offset
;
return
x
+
Utilities
.
getTabbedTextWidth
(
lineSegment
,
fm
,
x
,
painter
,
0
);
}
else
{
// If syntax coloring is enabled, we have to do this
// because tokens can vary in width
Token
tokens
;
if
(
painter
.
currentLineIndex
==
line
&&
painter
.
currentLineTokens
!=
null
) {
tokens
=
painter
.
currentLineTokens
;
}
else
{
painter
.
currentLineIndex
=
line
;
tokens
=
painter
.
currentLineTokens
=
tokenMarker
.
markTokens
(
lineSegment
,
line
);
}
// Font defaultFont = painter.getFont();
SyntaxStyle
[]
styles
=
painter
.
getStyles
();
for
(;;) {
byte
id
=
tokens
.
id
;
if
(
id
==
Token
.
END
) {
return
x
;
}
if
(
id
==
Token
.
NULL
) {
fm
=
painter
.
getFontMetrics
();
}
else
{
//fm = styles[id].getFontMetrics(defaultFont, this);
fm
=
painter
.
getFontMetrics
(
styles
[
id
]);
}
int
length
=
tokens
.
length
;
if
(
offset
+
segmentOffset
<
lineSegment
.
offset
+
length
) {
lineSegment
.
count
=
offset
- (
lineSegment
.
offset
-
segmentOffset
);
return
x
+
Utilities
.
getTabbedTextWidth
(
lineSegment
,
fm
,
x
,
painter
,
0
);
}
else
{
lineSegment
.
count
=
length
;
x
+=
Utilities
.
getTabbedTextWidth
(
lineSegment
,
fm
,
x
,
painter
,
0
);
lineSegment
.
offset
+=
length
;
}
tokens
=
tokens
.
next
;
}
}
}
/**
* Converts an x co-ordinate to an offset within a line.
* @param line The line
* @param x The x co-ordinate
*/
public
int
xToOffset
(
int
line
,
int
x
) {
TokenMarker
tokenMarker
=
getTokenMarker
();
/* Use painter's cached info for speed */
FontMetrics
fm
=
painter
.
getFontMetrics
();
// System.out.println("metrics: " + fm);
getLineText
(
line
,
lineSegment
);
char
[]
segmentArray
=
lineSegment
.
array
;
int
segmentOffset
=
lineSegment
.
offset
;
int
segmentCount
=
lineSegment
.
count
;
int
width
=
horizontalOffset
;
if
(
tokenMarker
==
null
)
{
for
(
int
i
=
0
;
i
<
segmentCount
;
i
++)
{
char
c
=
segmentArray
[
i
+
segmentOffset
];
int
charWidth
;
if
(
c
==
'\t'
)
charWidth
= (
int
)
painter
.
nextTabStop
(
width
,
i
)
-
width
;
else
charWidth
=
fm
.
charWidth
(
c
);
if
(
painter
.
isBlockCaretEnabled
())
{
if
(
x
-
charWidth
<=
width
)
return
i
;
}
else
{
if
(
x
-
charWidth
/
2
<=
width
)
return
i
;
}
width
+=
charWidth
;
}
return
segmentCount
;
}
else
{
Token
tokens
;
if
(
painter
.
currentLineIndex
==
line
&&
painter
.
currentLineTokens
!=
null
) {
tokens
=
painter
.
currentLineTokens
;
}
else
{
painter
.
currentLineIndex
=
line
;
tokens
=
painter
.
currentLineTokens
=
tokenMarker
.
markTokens
(
lineSegment
,
line
);
}
int
offset
=
0
;
// Font defaultFont = painter.getFont();
SyntaxStyle
[]
styles
=
painter
.
getStyles
();
// System.out.println("painter is " + painter + ", doc is " + document);
for
(;;) {
byte
id
=
tokens
.
id
;
if
(
id
==
Token
.
END
)
return
offset
;
if
(
id
==
Token
.
NULL
) {
fm
=
painter
.
getFontMetrics
();
}
else
{
//fm = styles[id].getFontMetrics(defaultFont, this);
fm
=
painter
.
getFontMetrics
(
styles
[
id
]);
}
int
length
=
tokens
.
length
;
for
(
int
i
=
0
;
i
<
length
;
i
++) {
// System.out.println("segmentOffset = " + segmentOffset +
// ", offset = " + offset +
// ", i = " + i +
// ", length = " + length +
// ", array len = " + segmentArray.length);
char
c
=
segmentArray
[
segmentOffset
+
offset
+
i
];
int
charWidth
;
if
(
c
==
'\t'
) {
charWidth
= (
int
)
painter
.
nextTabStop
(
width
,
offset
+
i
) -
width
;
}
else
{
charWidth
=
fm
.
charWidth
(
c
);
}
if
(
painter
.
isBlockCaretEnabled
()) {
if
(
x
-
charWidth
<=
width
) {
return
offset
+
i
;
}
}
else
{
if
(
x
-
charWidth
/
2
<=
width
) {
return
offset
+
i
;
}
}
width
+=
charWidth
;
}
offset
+=
length
;
tokens
=
tokens
.
next
;
}
}
}
/**
* Converts a point to an offset, from the start of the text.
* @param x The x co-ordinate of the point
* @param y The y co-ordinate of the point
*/
public
int
xyToOffset
(
int
x
,
int
y
)
{
int
line
=
yToLine
(
y
);
int
start
=
getLineStartOffset
(
line
);
return
start
+
xToOffset
(
line
,
x
);
}
/**
* Returns the document this text area is editing.
*/
public
final
SyntaxDocument
getDocument
()
{
return
document
;
}
/**
* Sets the document this text area is editing.
* @param document The document
*/
public
void
setDocument
(
SyntaxDocument
document
) {
if
(
this
.
document
==
document
)
return
;
if
(
this
.
document
!=
null
)
this
.
document
.
removeDocumentListener
(
documentHandler
);
this
.
document
=
document
;
document
.
addDocumentListener
(
documentHandler
);
select
(
0
,
0
);
updateScrollBars
();
painter
.
repaint
();
}
/**
* Set document with a twist, includes the old caret
* and scroll positions, added for p5. [fry]
*/
public
void
setDocument
(
SyntaxDocument
document
,
int
start
,
int
stop
,
int
scroll
) {
if
(
this
.
document
==
document
)
return
;
if
(
this
.
document
!=
null
)
this
.
document
.
removeDocumentListener
(
documentHandler
);
this
.
document
=
document
;
document
.
addDocumentListener
(
documentHandler
);
select
(
start
,
stop
);
updateScrollBars
();
setVerticalScrollPosition
(
scroll
);
painter
.
repaint
();
}
/**
* Returns the document's token marker. Equivalent to calling
* <code>getDocument().getTokenMarker()</code>.
*/
public
final
TokenMarker
getTokenMarker
() {
return
document
.
getTokenMarker
();
}
/**
* Sets the document's token marker. Equivalent to caling
* <code>getDocument().setTokenMarker()</code>.
* @param tokenMarker The token marker
*/
public
final
void
setTokenMarker
(
TokenMarker
tokenMarker
) {
document
.
setTokenMarker
(
tokenMarker
);
}
/**
* Returns the length of the document. Equivalent to calling
* <code>getDocument().getLength()</code>.
*/
public
final
int
getDocumentLength
() {
return
document
.
getLength
();
}
/**
* Returns the number of lines in the document.
*/
public
final
int
getLineCount
() {
return
document
.
getDefaultRootElement
().
getElementCount
();
}
/**
* Returns the line containing the specified offset.
* @param offset The offset
*/
public
final
int
getLineOfOffset
(
int
offset
) {
return
document
.
getDefaultRootElement
().
getElementIndex
(
offset
);
}
/**
* Returns the start offset of the specified line.
* @param line The line
* @return The start offset of the specified line, or -1 if the line is
* invalid
*/
public
int
getLineStartOffset
(
int
line
) {
Element
lineElement
=
document
.
getDefaultRootElement
().
getElement
(
line
);
return
(
lineElement
==
null
) ? -
1
:
lineElement
.
getStartOffset
();
}
public
int
getLineStartNonWhiteSpaceOffset
(
int
line
) {
int
offset
=
getLineStartOffset
(
line
);
int
length
=
getLineLength
(
line
);
String
str
=
getText
(
offset
,
length
);
for
(
int
i
=
0
;
i
<
str
.
length
();
i
++) {
if
(!
Character
.
isWhitespace
(
str
.
charAt
(
i
))) {
return
offset
+
i
;
}
}
return
offset
+
length
;
}
/**
* Returns the end offset of the specified line.
* @param line The line
* @return The end offset of the specified line, or -1 if the line is
* invalid.
*/
public
int
getLineStopOffset
(
int
line
) {
Element
lineElement
=
document
.
getDefaultRootElement
().
getElement
(
line
);
return
(
lineElement
==
null
) ? -
1
:
lineElement
.
getEndOffset
();
}
public
int
getLineStopNonWhiteSpaceOffset
(
int
line
) {
int
offset
=
getLineStopOffset
(
line
);
int
length
=
getLineLength
(
line
);
String
str
=
getText
(
offset
-
length
-
1
,
length
);
for
(
int
i
=
0
;
i
<
length
;
i
++) {
if
(!
Character
.
isWhitespace
(
str
.
charAt
(
length
-
i
-
1
))) {
return
offset
-
i
;
}
}
return
offset
-
length
;
}
/**
* Returns the start offset of the line after this line, or the end of
* this line if there is no next line.
* @param line The line
* @return The end offset of the specified line, or -1 if the line is
* invalid.
*/
public
int
getLineSelectionStopOffset
(
int
line
) {
Element
lineElement
=
document
.
getDefaultRootElement
().
getElement
(
line
);
return
(
lineElement
==
null
) ? -
1
:
Math
.
min
(
lineElement
.
getEndOffset
(),
getDocumentLength
());
}
/**
* Returns the length of the specified line.
* @param line The line
*/
public
int
getLineLength
(
int
line
) {
Element
lineElement
=
document
.
getDefaultRootElement
().
getElement
(
line
);
return
(
lineElement
==
null
) ? -
1
:
lineElement
.
getEndOffset
() -
lineElement
.
getStartOffset
() -
1
;
}
/**
* Returns the entire text of this text area.
*/
public
String
getText
() {
try
{
return
document
.
getText
(
0
,
document
.
getLength
());
}
catch
(
BadLocationException
bl
) {
bl
.
printStackTrace
();
return
null
;
}
}
/**
* Sets the entire text of this text area.
*/
public
void
setText
(
String
text
) {
try
{
document
.
beginCompoundEdit
();
document
.
remove
(
0
,
document
.
getLength
());
document
.
insertString
(
0
,
text
,
null
);
}
catch
(
BadLocationException
bl
) {
bl
.
printStackTrace
();
}
finally
{
document
.
endCompoundEdit
();
}
}
/**
* Returns the specified substring of the document.
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL