-
Notifications
You must be signed in to change notification settings - Fork 250
/
FoldIndicator.java
executable file
·991 lines (829 loc) · 26.1 KB
/
FoldIndicator.java
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
/*
* 10/08/2011
*
* FoldIndicator.java - Gutter component allowing the user to expand and
* collapse folds.
*
* This library is distributed under a modified BSD license. See the included
* LICENSE file for details.
*/
package org.fife.ui.rtextarea;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.MouseInputAdapter;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.View;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.focusabletip.TipUtil;
import org.fife.ui.rsyntaxtextarea.folding.Fold;
import org.fife.ui.rsyntaxtextarea.folding.FoldManager;
/**
* Component in the gutter that displays +/- icons to expand and collapse
* fold regions in the editor.
*
* @author Robert Futrell
* @version 1.0
*/
public class FoldIndicator extends AbstractGutterComponent {
/**
* Used in {@link #paintComponent(Graphics)} to prevent reallocation on
* each paint.
*/
private Insets textAreaInsets;
/**
* Used in {@link #paintComponent(Graphics)} to prevent reallocation on
* each paint.
*/
private Rectangle visibleRect;
/**
* The fold to show the outline line for.
*/
private Fold foldWithOutlineShowing;
/**
* The color used for the foreground of armed folds.
*/
private Color armedForeground;
/**
* The color to use for fold icon backgrounds, if the default icons
* are used.
*/
private Color foldIconBackground;
/**
* The color to use for armed fold icon backgrounds, if the default icons
* are used. This may be {@code null}.
*/
private Color foldIconArmedBackground;
/**
* The icon used for collapsed folds.
*/
private FoldIndicatorIcon collapsedFoldIcon;
/**
* The icon used for expanded folds.
*/
private FoldIndicatorIcon expandedFoldIcon;
/**
* Used while painting; global flag to denote whether the mouse is over
* a fold indicator.
*/
private boolean mouseOverFoldIcon;
/**
* Used while painting; global flag to denote whether the
* currently-being-painted fold should be rendered as armed.
*/
private boolean paintFoldArmed;
/**
* Whether tool tips are displayed showing the contents of collapsed
* fold regions.
*/
private boolean showFoldRegionTips;
/**
* Whether the range of lines covered by an expanded, armed fold icon
* should be visually shown.
*/
private boolean showArmedFoldRange;
/**
* Optional additional left margin.
*/
private int additionalLeftMargin;
/**
* The strategy to use when rendering expanded folds.
*/
private ExpandedFoldRenderStrategy expandedFoldRenderStrategy;
/**
* The color used to paint fold outlines.
*/
public static final Color DEFAULT_FOREGROUND = Color.GRAY;
/**
* The default color used to paint the "inside" of fold icons.
*/
public static final Color DEFAULT_FOLD_BACKGROUND = Color.WHITE;
/**
* The alpha used for "collapsed" fold icons.
*/
private float collapsedFoldIconAlpha;
/**
* Used to update the collapsed fold icons' alpha value on a timer.
*/
private AlphaRunnable alphaRunnable;
/**
* The timer used to update collapsed fold icons' alpha.
*/
private Timer timer;
/**
* Listens for events in this component.
*/
private Listener listener;
private static final int COLLAPSED_FOLD_ALPHA_DELAY_MILLIS = 16;
private static final float ALPHA_DELTA = 0.1f;
public FoldIndicator(RTextArea textArea) {
super(textArea);
}
/**
* Overridden to use the editor's background if it's detected that the
* user isn't using white as the editor bg, but the system's tool tip
* background is yellow-ish.
*
* @return The tool tip.
*/
@Override
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(TipUtil.getToolTipBackground(textArea));
tip.setBorder(TipUtil.getToolTipBorder(textArea));
return tip;
}
/**
* Returns the amount of additional size to give the left margin of this
* component. This can be used to add blank space between this component
* and the line number indicator in the gutter.
*
* @return The additional left margin.
* @see #setAdditionalLeftMargin(int)
*/
public int getAdditionalLeftMargin() {
return additionalLeftMargin;
}
private Fold findOpenFoldClosestTo(Point p) {
Fold fold = null;
mouseOverFoldIcon = false;
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
if (rsta.isCodeFoldingEnabled()) { // Should always be true
int offs = rsta.viewToModel(p); // TODO: Optimize me
if (offs>-1) {
try {
int line = rsta.getLineOfOffset(offs);
FoldManager fm = rsta.getFoldManager();
fold = fm.getFoldForLine(line);
if (fold != null) {
// The mouse is directly over the fold indicator
mouseOverFoldIcon = true;
}
else {
fold = fm.getDeepestOpenFoldContaining(offs);
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
}
}
return fold;
}
/**
* Returns the foreground color used for armed folds.
*
* @return The foreground color used for armed folds.
* @see #setArmedForeground(Color)
*/
public Color getArmedForeground() {
return armedForeground;
}
/**
* Returns the strategy to use for rendering expanded folds.
*
* @return The strategy to use for rendering expanded folds.
* @see #setExpandedFoldRenderStrategy(ExpandedFoldRenderStrategy)
*/
public ExpandedFoldRenderStrategy getExpandedFoldRenderStrategy() {
return expandedFoldRenderStrategy;
}
/**
* Returns the color to use for the "background" of armed fold icons. This
* is ignored if custom icons are used.
*
* @return The background color. If this is {@code null}, there is no
* special color for armed fold icons.
* @see #setFoldIconArmedBackground(Color)
* @see #getFoldIconBackground()
*/
public Color getFoldIconArmedBackground() {
return foldIconArmedBackground;
}
/**
* Returns the color to use for the "background" of fold icons. This
* is ignored if custom icons are used.
*
* @return The background color.
* @see #setFoldIconBackground(Color)
* @see #getFoldIconArmedBackground()
*/
public Color getFoldIconBackground() {
return foldIconBackground;
}
/**
* Returns whether to paint expanded folds.
*
* @return Whether to paint expanded folds.
*/
private boolean getPaintExpandedFolds() {
return expandedFoldRenderStrategy == ExpandedFoldRenderStrategy.ALWAYS || collapsedFoldIconAlpha > 0;
}
@Override
public Dimension getPreferredSize() {
int iconWidth = Math.max(expandedFoldIcon.getIconWidth(), collapsedFoldIcon.getIconWidth());
int h = textArea!=null ? textArea.getHeight() : 100; // Arbitrary
return new Dimension(iconWidth + 4 + additionalLeftMargin, h);
}
/**
* Returns whether a line should be drawn to show the range of lines contained
* in an expanded fold when it is armed (hovered over).
*
* @return Whether to show an armed fold's range.
* @see #setShowArmedFoldRange(boolean)
*/
public boolean getShowArmedFoldRange() {
return showArmedFoldRange;
}
/**
* Returns whether tool tips are displayed showing the contents of
* collapsed fold regions when the mouse hovers over a +/- icon.
*
* @return Whether these tool tips are displayed.
* @see #setShowCollapsedRegionToolTips(boolean)
*/
public boolean getShowCollapsedRegionToolTips() {
return showFoldRegionTips;
}
/**
* Positions tool tips to be aligned in the text component, so that the
* displayed content is shown (almost) exactly where it would be in the
* editor.
*
* @param e The mouse location.
*/
@Override
public Point getToolTipLocation(MouseEvent e) {
// ToolTipManager requires both location and text to be null to hide
// a currently-visible tool tip window. If text is null but location
// has some value, it will show a tool tip with empty content, the size
// of its border (!).
String text = getToolTipText(e);
if (text==null) {
return null;
}
// Try to overlap the tip's text directly over the code
Point p = e.getPoint();
p.y = (p.y/textArea.getLineHeight()) * textArea.getLineHeight();
p.x = getWidth() + textArea.getMargin().left;
Gutter gutter = getGutter();
int gutterMargin = gutter.getInsets().right;
p.x += gutterMargin;
JToolTip tempTip = createToolTip();
p.x -= tempTip.getInsets().left;
p.y += 16;
return p;
}
/**
* Overridden to show the content of a collapsed fold on mouse-overs.
*
* @param e The mouse location.
*/
@Override
public String getToolTipText(MouseEvent e) {
String text = null;
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
if (rsta.isCodeFoldingEnabled()) {
FoldManager fm = rsta.getFoldManager();
int pos = rsta.viewToModel(new Point(0, e.getY()));
if (pos>=0) { // Not -1
int line = 0;
try {
line = rsta.getLineOfOffset(pos);
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
return null;
}
Fold fold = fm.getFoldForLine(line);
if (fold!=null && fold.isCollapsed()) {
int endLine = fold.getEndLine();
if (fold.getLineCount()>25) { // Not too big
endLine = fold.getStartLine() + 25;
}
StringBuilder sb = new StringBuilder("<html><nobr>");
while (line<=endLine && line<rsta.getLineCount()) { // Sanity
Token t = rsta.getTokenListForLine(line);
while (t!=null && t.isPaintable()) {
t.appendHTMLRepresentation(sb, rsta, true, true);
t = t.getNextToken();
}
sb.append("<br>");
line++;
}
text = sb.toString();
}
}
}
return text;
}
void gutterArmedUpdate(boolean armed) {
if (expandedFoldRenderStrategy == ExpandedFoldRenderStrategy.ON_HOVER) {
alphaRunnable.delta = armed ? ALPHA_DELTA : -ALPHA_DELTA;
timer.restart();
}
else {
collapsedFoldIconAlpha = 1;
timer.stop();
repaint();
}
}
@Override
void handleDocumentEvent(DocumentEvent e) {
int newLineCount = textArea.getLineCount();
if (newLineCount!=currentLineCount) {
currentLineCount = newLineCount;
repaint();
}
}
@Override
protected void init() {
super.init();
setForeground(DEFAULT_FOREGROUND);
setFoldIconBackground(DEFAULT_FOLD_BACKGROUND);
setStyle(FoldIndicatorStyle.MODERN);
listener = new Listener(this);
visibleRect = new Rectangle();
setShowCollapsedRegionToolTips(true);
alphaRunnable = new AlphaRunnable();
timer = new Timer(COLLAPSED_FOLD_ALPHA_DELAY_MILLIS, alphaRunnable);
timer.setRepeats(true);
}
@Override
void lineHeightsChanged() {
// TODO Auto-generated method stub
}
@Override
protected void paintComponent(Graphics g) {
if (textArea==null) {
return;
}
visibleRect = g.getClipBounds(visibleRect);
if (visibleRect==null) { // ???
visibleRect = getVisibleRect();
}
//System.out.println("FoldIndicator repainting: " + visibleRect);
if (visibleRect==null) {
return;
}
Color bg = getBackground();
if (getGutter()!=null) { // Should always be true
bg = getGutter().getBackground();
}
g.setColor(bg);
g.fillRect(0,visibleRect.y, getWidth(),visibleRect.height);
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
if (!rsta.isCodeFoldingEnabled()) {
return; // We should be hidden in this case, but still...
}
if (textArea.getLineWrap()) {
paintComponentWrapped(g);
return;
}
// Get where to start painting (top of the row).
// We need to be "scrolled up" up just enough for the missing part of
// the first line.
textAreaInsets = textArea.getInsets(textAreaInsets);
if (visibleRect.y<textAreaInsets.top) {
visibleRect.height -= (textAreaInsets.top - visibleRect.y);
visibleRect.y = textAreaInsets.top;
}
int cellHeight = textArea.getLineHeight();
int topLine = (visibleRect.y-textAreaInsets.top)/cellHeight;
int y = topLine*cellHeight +
(cellHeight-collapsedFoldIcon.getIconHeight())/2;
y += textAreaInsets.top;
// Get the first and last lines to paint.
FoldManager fm = rsta.getFoldManager();
topLine += fm.getHiddenLineCountAbove(topLine, true);
int width = getWidth();
int line = topLine;
boolean paintingOutlineLine = getShowArmedFoldRange() &&
foldWithOutlineShowing!=null &&
foldWithOutlineShowing.containsLine(line);
while (y<visibleRect.y+visibleRect.height) {
if (paintingOutlineLine) {
g.setColor(getForeground());
int w2 = width / 2;
if (line==foldWithOutlineShowing.getEndLine()) {
int y2 = y+cellHeight/2;
g.drawLine(w2,y, w2,y2);
g.drawLine(w2,y2, width-2,y2);
paintingOutlineLine = false;
}
else {
g.drawLine(w2,y, w2,y+cellHeight);
}
}
Fold fold = fm.getFoldForLine(line);
if (fold!=null) {
if (fold==foldWithOutlineShowing) {
if (!fold.isCollapsed()) {
paintingOutlineLine = getShowArmedFoldRange();
if (paintingOutlineLine) {
g.setColor(getForeground());
int w2 = width / 2;
g.drawLine(w2, y + cellHeight / 2, w2, y + cellHeight);
}
}
if (mouseOverFoldIcon) {
paintFoldArmed = true;
}
}
if (fold.isCollapsed()) {
int x = (width - collapsedFoldIcon.getIconWidth()) / 2;
paintIcon(collapsedFoldIcon, g, x, y, true);
// Skip to next line to paint, taking extra care for lines with
// block ends and begins together, e.g. "} else {"
do {
int hiddenLineCount = fold.getLineCount();
if (hiddenLineCount==0) {
// Fold parser identified a zero-line fold region.
// This is really a bug, but we'll be graceful here
// and avoid an infinite loop.
break;
}
line += hiddenLineCount;
fold = fm.getFoldForLine(line);
} while (fold!=null && fold.isCollapsed());
}
else if (getPaintExpandedFolds()) {
int x = (width - expandedFoldIcon.getIconWidth()) / 2;
paintIcon(expandedFoldIcon, g, x, y, false);
}
paintFoldArmed = false;
}
line++;
y += cellHeight;
}
}
/**
* Paints folding icons when line wrapping is enabled.
*
* @param g The graphics context.
*/
private void paintComponentWrapped(Graphics g) {
// The variables we use are as follows:
// - visibleRect is the "visible" area of the text area; e.g.
// [0,100, 300,100+(lineCount*cellHeight)-1].
// actualTop.y is the topmost-pixel in the first logical line we
// paint. Note that we may well not paint this part of the logical
// line, as it may be broken into many physical lines, with the first
// few physical lines scrolled past. Note also that this is NOT the
// visible rect of this line number list; this line number list has
// visible rect == [0,0, insets.left-1,visibleRect.height-1].
// - offset (<=0) is the y-coordinate at which we begin painting when
// we begin painting with the first logical line. This can be
// negative, signifying that we've scrolled past the actual topmost
// part of this line.
// The algorithm is as follows:
// - Get the starting y-coordinate at which to paint. This may be
// above the first visible y-coordinate as we're in line-wrapping
// mode, but we always paint entire logical lines.
// - Paint that line's indicator, if appropriate. Increment y to be
// just below the line we just painted (i.e., the beginning of the
// next logical line's view area).
// - Get the ending visual position for that line. We can now loop
// back, paint this line, and continue until our y-coordinate is
// past the last visible y-value.
// We avoid using modelToView/viewToModel where possible, as these
// methods trigger a parsing of the line into syntax tokens, which is
// costly. It's cheaper to just grab the child views' bounds.
// Some variables we'll be using.
int width = getWidth();
RTextAreaUI ui = (RTextAreaUI)textArea.getUI();
View v = ui.getRootView(textArea).getView(0);
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
int topPosition = textArea.viewToModel(
new Point(visibleRect.x,visibleRect.y));
int topLine = root.getElementIndex(topPosition);
int cellHeight = textArea.getLineHeight();
FoldManager fm = ((RSyntaxTextArea)textArea).getFoldManager();
// Compute the y at which to begin painting text, taking into account
// that 1 logical line => at least 1 physical line, so it may be that
// y<0. The computed y-value is the y-value of the top of the first
// (possibly) partially-visible view.
Rectangle visibleEditorRect = ui.getVisibleEditorRect();
Rectangle r = LineNumberList.getChildViewBounds(v, topLine,
visibleEditorRect);
int y = r.y;
y += (cellHeight-collapsedFoldIcon.getIconHeight())/2;
int visibleBottom = visibleRect.y + visibleRect.height;
int line = topLine;
boolean paintingOutlineLine = getShowArmedFoldRange() &&
foldWithOutlineShowing!=null &&
foldWithOutlineShowing.containsLine(line);
int lineCount = root.getElementCount();
while (y<visibleBottom && line<lineCount) {
int curLineH = LineNumberList.getChildViewBounds(v, line,
visibleEditorRect).height;
if (paintingOutlineLine) {
g.setColor(getForeground());
int w2 = width / 2;
if (line==foldWithOutlineShowing.getEndLine()) {
int y2 = y + curLineH - cellHeight/2;
g.drawLine(w2,y, w2,y2);
g.drawLine(w2,y2, width-2,y2);
paintingOutlineLine = false;
}
else {
g.drawLine(w2,y, w2,y+curLineH);
}
}
Fold fold = fm.getFoldForLine(line);
if (fold!=null) {
if (fold==foldWithOutlineShowing) {
if (!fold.isCollapsed()) {
paintingOutlineLine = getShowArmedFoldRange();
if (paintingOutlineLine) {
g.setColor(getForeground());
int w2 = width / 2;
g.drawLine(w2, y + cellHeight / 2, w2, y + curLineH);
}
}
if (mouseOverFoldIcon) {
paintFoldArmed = true;
}
}
if (fold.isCollapsed()) {
int x = (width - collapsedFoldIcon.getIconWidth()) / 2;
paintIcon(collapsedFoldIcon, g, x, y, true);
y += LineNumberList.getChildViewBounds(v, line,
visibleEditorRect).height;
line += fold.getLineCount() + 1;
}
else {
if (getPaintExpandedFolds()) {
int x = (width - expandedFoldIcon.getIconWidth()) / 2;
paintIcon(expandedFoldIcon, g, x, y, false);
}
y += curLineH;
line++;
}
paintFoldArmed = false;
}
else {
y += curLineH;
line++;
}
}
}
private void paintIcon(FoldIndicatorIcon icon, Graphics g, int x, int y, boolean collapsed) {
Graphics2D g2d = (Graphics2D)g;
Composite orig = g2d.getComposite();
if (!collapsed) {
if (collapsedFoldIconAlpha == 0) {
return;
}
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, collapsedFoldIconAlpha);
g2d.setComposite(ac);
}
try {
icon.setArmed(paintFoldArmed);
icon.paintIcon(this, g, x, y);
} finally {
if (!collapsed) {
g2d.setComposite(orig);
}
}
}
private int rowAtPoint(Point p) {
int line = 0;
try {
int offs = textArea.viewToModel(p);
if (offs>-1) {
line = textArea.getLineOfOffset(offs);
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
return line;
}
/**
* Adds to the amount of additional size to give the left margin of this
* component. This can be used to add blank space between this component
* and the line number indicator in the gutter.
*
* @param leftMargin The additional left margin. This should be
* {@code >= 0}.
* @see #getAdditionalLeftMargin()
*/
public void setAdditionalLeftMargin(int leftMargin) {
if (leftMargin < 0) {
throw new IllegalArgumentException("leftMargin must be >= 0");
}
this.additionalLeftMargin = leftMargin;
revalidate();
}
/**
* Sets the foreground color used for armed folds.
*
* @param fg The new armed fold foreground.
* @see #getArmedForeground()
*/
public void setArmedForeground(Color fg) {
if (fg==null) {
fg = FoldIndicator.DEFAULT_FOREGROUND;
}
armedForeground = fg;
}
private void setCollapsedFoldIconAlpha(float collapsedFoldIconAlpha) {
collapsedFoldIconAlpha = Math.max(0, Math.min(collapsedFoldIconAlpha, 1));
if (collapsedFoldIconAlpha != this.collapsedFoldIconAlpha) {
this.collapsedFoldIconAlpha = collapsedFoldIconAlpha;
repaint();
}
}
/**
* Sets the strategy to use for rendering expanded folds.
*
* @param strategy The strategy to use. This cannot be {@code null}.
* @see #getExpandedFoldRenderStrategy()
*/
public void setExpandedFoldRenderStrategy(ExpandedFoldRenderStrategy strategy) {
if (strategy == null) {
throw new NullPointerException("strategy cannot be null");
}
expandedFoldRenderStrategy = strategy;
collapsedFoldIconAlpha = strategy == ExpandedFoldRenderStrategy.ALWAYS ? 1 : 0;
}
/**
* Sets the color to use for the "background" of armed fold icons. This
* will be ignored if custom icons are used.
*
* @param bg The new background color. If {@code null} is passed in,
* there will be no special color for armed fold icons.
* @see #getFoldIconArmedBackground()
* @see #setFoldIconBackground(Color)
*/
public void setFoldIconArmedBackground(Color bg) {
foldIconArmedBackground = bg;
}
/**
* Sets the color to use for the "background" of fold icons. This will
* be ignored if custom icons are used.
*
* @param bg The new background color. This should not be {@code null}.
* @see #getFoldIconBackground()
* @see #setFoldIconArmedBackground(Color)
*/
public void setFoldIconBackground(Color bg) {
foldIconBackground = bg;
repaint();
}
/**
* Sets the icons to use to represent collapsed and expanded folds.
* This method can be used for further customization after setting this
* component's general appearance via {@link #setStyle(FoldIndicatorStyle)}.
*
* @param collapsedIcon The collapsed fold icon. This cannot be
* <code>null</code>.
* @param expandedIcon The expanded fold icon. This cannot be
* <code>null</code>.
* @see #setStyle(FoldIndicatorStyle)
*/
public void setFoldIcons(FoldIndicatorIcon collapsedIcon, FoldIndicatorIcon expandedIcon) {
this.collapsedFoldIcon = collapsedIcon;
this.expandedFoldIcon = expandedIcon;
revalidate(); // Icons may be different sizes.
repaint();
}
/**
* Toggles whether a line should be drawn to show the range of lines contained
* in an expanded fold when it is armed (hovered over).
*
* @param show Whether to show an armed fold's range.
* @see #getShowArmedFoldRange()
*/
public void setShowArmedFoldRange(boolean show) {
if (show != showArmedFoldRange) {
showArmedFoldRange = show;
repaint();
}
}
/**
* Toggles whether tool tips should be displayed showing the contents of
* collapsed fold regions when the mouse hovers over a +/- icon.
*
* @param show Whether to show these tool tips.
* @see #getShowCollapsedRegionToolTips()
*/
public void setShowCollapsedRegionToolTips(boolean show) {
if (show!=showFoldRegionTips) {
if (show) {
ToolTipManager.sharedInstance().registerComponent(this);
}
else {
ToolTipManager.sharedInstance().unregisterComponent(this);
}
showFoldRegionTips = show;
}
}
/**
* Toggles the presentation of this component. This method sets the icons used
* for fold regions to default values, amongst other configuration. To further
* customize these icons, see {@link #setFoldIcons(FoldIndicatorIcon, FoldIndicatorIcon)}.
*
* @param style The new presentation style.
* @see #setFoldIcons(FoldIndicatorIcon, FoldIndicatorIcon)
*/
void setStyle(FoldIndicatorStyle style) {
switch (style) {
case CLASSIC:
setFoldIcons(new PlusMinusFoldIcon(true), new PlusMinusFoldIcon(false));
setShowArmedFoldRange(true);
setExpandedFoldRenderStrategy(ExpandedFoldRenderStrategy.ALWAYS);
break;
case MODERN:
setFoldIcons(new ChevronFoldIcon(true), new ChevronFoldIcon(false));
setShowArmedFoldRange(false);
setExpandedFoldRenderStrategy(ExpandedFoldRenderStrategy.ON_HOVER);
break;
}
}
/**
* Overridden so we can track when code folding is enabled/disabled.
*/
@Override
public void setTextArea(RTextArea textArea) {
if (this.textArea!=null) {
this.textArea.removePropertyChangeListener(
RSyntaxTextArea.CODE_FOLDING_PROPERTY, listener);
}
super.setTextArea(textArea);
if (this.textArea!=null) {
this.textArea.addPropertyChangeListener(
RSyntaxTextArea.CODE_FOLDING_PROPERTY, listener);
}
}
/**
* Updates the alpha used for this component's "collapsed" fold icons, if
* necessary.
*/
private class AlphaRunnable implements ActionListener {
private float delta;
@Override
public void actionPerformed(ActionEvent e) {
setCollapsedFoldIconAlpha(collapsedFoldIconAlpha + delta);
if (collapsedFoldIconAlpha == 0 || collapsedFoldIconAlpha == 1) {
timer.stop();
}
}
}
/**
* Listens for events in this component.
*/
private class Listener extends MouseInputAdapter
implements PropertyChangeListener {
Listener(FoldIndicator fgc) {
fgc.addMouseListener(this);
fgc.addMouseMotionListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
int line = rowAtPoint(p);
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
FoldManager fm = rsta.getFoldManager();
Fold fold = fm.getFoldForLine(line);
if (fold!=null) {
fold.toggleCollapsedState();
getGutter().repaint();
textArea.repaint();
}
}
@Override
public void mouseExited(MouseEvent e) {
if (foldWithOutlineShowing!=null) {
foldWithOutlineShowing = null;
mouseOverFoldIcon = false;
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
boolean oldMouseOverFoldIcon = mouseOverFoldIcon;
Fold newSelectedFold = findOpenFoldClosestTo(e.getPoint());
if (newSelectedFold!=foldWithOutlineShowing &&
newSelectedFold!=null && !newSelectedFold.isOnSingleLine()) {
foldWithOutlineShowing = newSelectedFold;
repaint();
}
else if (mouseOverFoldIcon != oldMouseOverFoldIcon) {
repaint();
}
}
@Override
public void propertyChange(PropertyChangeEvent e) {
// Whether folding is enabled in the editor has changed.
repaint();
}
}
}