-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathRTextAreaBase.java
More file actions
1274 lines (1093 loc) · 37.5 KB
/
Copy pathRTextAreaBase.java
File metadata and controls
1274 lines (1093 loc) · 37.5 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
/*
* 04/07/2005
*
* RTextAreaBase.java - The base class for an RTextArea.
*
* This library is distributed under a modified BSD license. See the included
* RSyntaxTextArea.License.txt file for details.
*/
package org.fife.ui.rtextarea;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.TextUI;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.StyleContext;
/**
* This is the base class for <code>RTextArea</code>; basically it's just an
* extension of <code>javax.swing.JTextArea</code> adding a bunch of properties.
* <p>
*
* This class is only supposed to be overridden by <code>RTextArea</code>.
*
* @author Robert Futrell
* @version 0.8
*/
public abstract class RTextAreaBase extends JTextArea {
public static final String BACKGROUND_IMAGE_PROPERTY = "background.image";
public static final String CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY = "RTA.currentLineHighlightColor";
public static final String CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY = "RTA.currentLineHighlightFade";
public static final String HIGHLIGHT_CURRENT_LINE_PROPERTY = "RTA.currentLineHighlight";
public static final String ROUNDED_SELECTION_PROPERTY = "RTA.roundedSelection";
private boolean tabsEmulatedWithSpaces; // If true, tabs will be expanded to spaces.
private boolean highlightCurrentLine; // If true, the current line is highlighted.
private boolean paintCurrentLineHighlightFirst; // If true (default is false), the current line highlight will be painted before the custom line highlights.
private Color currentLineColor; // The color used to highlight the current line.
private boolean marginLineEnabled; // If true, paint a "margin line."
private Color marginLineColor; // The color used to paint the margin line.
private int marginLineX; // The x-location of the margin line.
private int marginSizeInChars; // How many 'm' widths the margin line is over.
private boolean fadeCurrentLineHighlight; // "Fade effect" for current line highlight.
private boolean roundedSelectionEdges;
private int previousCaretY;
int currentCaretY; // Used to know when to rehighlight current line.
private BackgroundPainterStrategy backgroundPainter; // Paints the background.
private RTAMouseListener mouseListener;
private static final Color DEFAULT_CARET_COLOR = new ColorUIResource(255,51,51);
private static final Color DEFAULT_CURRENT_LINE_HIGHLIGHT_COLOR = new Color(255,255,170);
private static final Color DEFAULT_MARGIN_LINE_COLOR = new Color(255,224,224);
private static final int DEFAULT_TAB_SIZE = 4;
private static final int DEFAULT_MARGIN_LINE_POSITION = 80;
/**
* Constructor.
*/
public RTextAreaBase() {
init();
}
/**
* Constructor.
*
* @param doc The document for the editor.
*/
public RTextAreaBase(AbstractDocument doc) {
super(doc);
init();
}
/**
* Constructor.
*
* @param text The initial text to display.
*/
public RTextAreaBase(String text) {
// Don't call super(text) to avoid NPE due to our funky RTextAreaUI...
init();
setText(text);
}
/**
* Constructor.
*
* @param rows The number of rows to display.
* @param cols The number of columns to display.
* @throws IllegalArgumentException If either <code>rows</code> or
* <code>cols</code> is negative.
*/
public RTextAreaBase(int rows, int cols) {
super(rows, cols);
init();
}
/**
* Constructor.
*
* @param text The initial text to display.
* @param rows The number of rows to display.
* @param cols The number of columns to display.
* @throws IllegalArgumentException If either <code>rows</code> or
* <code>cols</code> is negative.
*/
public RTextAreaBase(String text, int rows, int cols) {
// Don't call this super() due to NPE from our funky RTextAreaUI...
//super(text, rows, cols);
super(rows, cols);
init();
setText(text);
}
/**
* Constructor.
*
* @param doc The document for the editor.
* @param text The initial text to display.
* @param rows The number of rows to display.
* @param cols The number of columns to display.
* @throws IllegalArgumentException If either <code>rows</code> or
* <code>cols</code> is negative.
*/
public RTextAreaBase(AbstractDocument doc, String text, int rows,
int cols) {
// Don't call super() with text due to NPE from our funky RTextAreaUI...
super(doc, null/*text*/, rows, cols);
init();
setText(text);
}
/**
* Adds listeners that listen for changes to the current line, so we can
* update our "current line highlight." This is needed only because of an
* apparent difference between the JRE 1.4.2 and 1.5.0 (needed on 1.4.2,
* not needed on 1.5.0).
*/
private void addCurrentLineHighlightListeners() {
boolean add = true;
MouseMotionListener[] mouseMotionListeners = getMouseMotionListeners();
for (int i=0; i<mouseMotionListeners.length; i++) {
if (mouseMotionListeners[i]==mouseListener) {
add = false;
break;
}
}
if (add==true) {
//System.err.println("Adding mouse motion listener!");
addMouseMotionListener(mouseListener);
}
MouseListener[] mouseListeners = getMouseListeners();
for (int i=0; i<mouseListeners.length; i++) {
if (mouseListeners[i]==mouseListener) {
add = false;
break;
}
}
if (add==true) {
//System.err.println("Adding mouse listener!");
addMouseListener(mouseListener);
}
}
@Override
public void addNotify() {
super.addNotify();
// If the caret is not on the first line, we must recalculate the line
// highlight y-offset after the text area is sized. This seems to be
// the best way to do it.
if (getCaretPosition() != 0) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
possiblyUpdateCurrentLineHighlightLocation();
}
});
}
}
/*
* TODO: Figure out why RTextArea doesn't work with RTL orientation!
*/
// public void applyComponentOrientation(ComponentOrientation orientation) {
// super.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
// }
/**
* Converts all instances of a number of spaces equal to a tab size
* into a tab in this text area.
*
* @see #convertTabsToSpaces
* @see #getTabsEmulated
* @see #setTabsEmulated
*/
public void convertSpacesToTabs() {
// FIXME: This is inefficient and will yield an OutOfMemoryError if
// done on a large document. We should scan 1 line at a time and
// replace; it'll be slower but safer.
int caretPosition = getCaretPosition();
int tabSize = getTabSize();
String tabInSpaces = "";
for (int i=0; i<tabSize; i++)
tabInSpaces += " ";
String text = getText();
setText(text.replaceAll(tabInSpaces, "\t"));
int newDocumentLength = getDocument().getLength();
// Place the caret back in its proper position.
if (caretPosition<newDocumentLength)
setCaretPosition(caretPosition);
else
setCaretPosition(newDocumentLength-1);
}
/**
* Converts all instances of a tab into a number of spaces equivalent
* to a tab in this text area.
*
* @see #convertSpacesToTabs
* @see #getTabsEmulated
* @see #setTabsEmulated
*/
public void convertTabsToSpaces() {
// FIXME: This is inefficient and will yield an OutOfMemoryError if
// done on a large document. We should scan 1 line at a time and
// replace; it'll be slower but safer.
int caretPosition = getCaretPosition();
int tabSize = getTabSize();
StringBuilder tabInSpaces = new StringBuilder();
for (int i=0; i<tabSize; i++)
tabInSpaces.append(' ');
String text = getText();
setText(text.replaceAll("\t", tabInSpaces.toString()));
// Put caret back at same place in document.
setCaretPosition(caretPosition);
}
/**
* Returns the caret event/mouse listener for <code>RTextArea</code>s.
*
* @return The caret event/mouse listener.
*/
protected abstract RTAMouseListener createMouseListener();
/**
* Returns the a real UI to install on this text component. Subclasses
* can override this method to return an extended version of
* <code>RTextAreaUI</code>.
*
* @return The UI.
*/
protected abstract RTextAreaUI createRTextAreaUI();
/**
* Forces the current line highlight to be repainted. This hack is
* necessary for those situations when the view (appearance) changes
* but the caret's location hasn't (and thus the current line highlight
* coordinates won't get changed). Examples of this are when
* word wrap is toggled and when syntax styles are changed in an
* <code>RSyntaxTextArea</code>.
*/
protected void forceCurrentLineHighlightRepaint() {
// Check isShowing() to prevent BadLocationException
// in constructor if linewrap is set to true.
if (isShowing()) {
// Changing previousCaretY makes us sure to get a repaint.
previousCaretY = -1;
// Trick it into checking for the need to repaint by firing
// a false caret event.
fireCaretUpdate(mouseListener);
}
}
/**
* Returns the <code>java.awt.Color</code> used as the background color for
* this text area. If a <code>java.awt.Image</code> image is currently
* being used instead, <code>null</code> is returned.
*
* @return The current background color, or <code>null</code> if an image
* is currently the background.
*/
@Override
public final Color getBackground() {
Object bg = getBackgroundObject();
return (bg instanceof Color) ? (Color)bg : null;
}
/**
* Returns the image currently used for the background.
* If the current background is currently a <code>java.awt.Color</code> and
* not a <code>java.awt.Image</code>, then <code>null</code> is returned.
*
* @return A <code>java.awt.Image</code> used for the background, or
* <code>null</code> if the background is not an image.
* @see #setBackgroundImage
*/
public final Image getBackgroundImage() {
Object bg = getBackgroundObject();
return (bg instanceof Image) ? (Image)bg : null;
}
/**
* Returns the <code>Object</code> representing the background for all
* documents in this tabbed pane; either a <code>java.awt.Color</code> or a
* <code>java.lang.Image</code> containing the image used as the background
* for this text area.
*
* @return The <code>Object</code> used for the background.
* @see #setBackgroundObject(Object newBackground)
*/
public final Object getBackgroundObject() {
if (backgroundPainter==null)
return null;
return (backgroundPainter instanceof ImageBackgroundPainterStrategy) ?
(Object)((ImageBackgroundPainterStrategy)backgroundPainter).
getMasterImage() :
(Object)((ColorBackgroundPainterStrategy)backgroundPainter).
getColor();
}
/**
* Gets the line number that the caret is on.
*
* @return The zero-based line number that the caret is on.
*/
public final int getCaretLineNumber() {
try {
return getLineOfOffset(getCaretPosition());
} catch (BadLocationException ble) {
return 0; // Never happens
}
}
/**
* Gets the position from the beginning of the current line that the caret
* is on.
*
* @return The zero-based position from the beginning of the current line
* that the caret is on.
*/
public final int getCaretOffsetFromLineStart() {
try {
int pos = getCaretPosition();
return pos - getLineStartOffset(getLineOfOffset(pos));
} catch (BadLocationException ble) {
return 0; // Never happens
}
}
/**
* Returns the y-offset of the caret.
*
* @return The y-offset of the caret.
*/
protected int getCurrentCaretY() {
return currentCaretY;
}
/**
* Returns the color being used to highlight the current line. Note that
* if highlighting the current line is turned off, you will not be seeing
* this highlight.
*
* @return The color being used to highlight the current line.
* @see #getHighlightCurrentLine()
* @see #setHighlightCurrentLine(boolean)
* @see #setCurrentLineHighlightColor
*/
public Color getCurrentLineHighlightColor() {
return currentLineColor;
}
/**
* Returns the default caret color.
*
* @return The default caret color.
*/
public static final Color getDefaultCaretColor() {
return DEFAULT_CARET_COLOR;
}
/**
* Returns the "default" color for highlighting the current line. Note
* that this color was chosen only because it looks nice (to me) against a
* white background.
*
* @return The default color for highlighting the current line.
*/
public static final Color getDefaultCurrentLineHighlightColor() {
return DEFAULT_CURRENT_LINE_HIGHLIGHT_COLOR;
}
/**
* Returns the default font for text areas.
*
* @return The default font.
*/
public static final Font getDefaultFont() {
// Use StyleContext to get a composite font for better Asian language
// support; see Sun bug S282887.
StyleContext sc = StyleContext.getDefaultStyleContext();
Font font = null;
if (isOSX()) {
// Snow Leopard (1.6) uses Menlo as default monospaced font,
// pre-Snow Leopard used Monaco.
font = sc.getFont("Menlo", Font.PLAIN, 12);
if (!"Menlo".equals(font.getFamily())) {
font = sc.getFont("Monaco", Font.PLAIN, 12);
if (!"Monaco".equals(font.getFamily())) { // Shouldn't happen
font = sc.getFont("Monospaced", Font.PLAIN, 13);
}
}
}
else {
// Consolas added in Vista, used by VS2010+.
font = sc.getFont("Consolas", Font.PLAIN, 13);
if (!"Consolas".equals(font.getFamily())) {
font = sc.getFont("Monospaced", Font.PLAIN, 13);
}
}
//System.out.println(font.getFamily() + ", " + font.getName());
return font;
}
/**
* Returns the default foreground color for text in this text area.
*
* @return The default foreground color.
*/
public static final Color getDefaultForeground() {
return Color.BLACK;
}
/**
* Returns the default color for the margin line.
*
* @return The default margin line color.
* @see #getMarginLineColor()
* @see #setMarginLineColor(Color)
*/
public static final Color getDefaultMarginLineColor() {
return DEFAULT_MARGIN_LINE_COLOR;
}
/**
* Returns the default margin line position.
*
* @return The default margin line position.
* @see #getMarginLinePosition
* @see #setMarginLinePosition
*/
public static final int getDefaultMarginLinePosition() {
return DEFAULT_MARGIN_LINE_POSITION;
}
/**
* Returns the default tab size, in spaces.
*
* @return The default tab size.
*/
public static final int getDefaultTabSize() {
return DEFAULT_TAB_SIZE;
}
/**
* Returns whether the current line highlight is faded.
*
* @return Whether the current line highlight is faded.
* @see #setFadeCurrentLineHighlight
*/
public boolean getFadeCurrentLineHighlight() {
return fadeCurrentLineHighlight;
}
/**
* Returns whether or not the current line is highlighted.
*
* @return Whether or the current line is highlighted.
* @see #setHighlightCurrentLine(boolean)
* @see #getCurrentLineHighlightColor
* @see #setCurrentLineHighlightColor
*/
public boolean getHighlightCurrentLine() {
return highlightCurrentLine;
}
/**
* Returns whether or not the current line highlight should be painted (overwritten) before the custom linr highlight.
*
* @return true if the custom line highlight should overwrite the current line highlight.
*/
public boolean getPaintCurrentLineHighlightFirst() {
return this.paintCurrentLineHighlightFirst;
}
/**
* Returns the offset of the last character of the line that the caret is
* on.
*
* @return The last offset of the line that the caret is currently on.
*/
public final int getLineEndOffsetOfCurrentLine() {
try {
return getLineEndOffset(getCaretLineNumber());
} catch (BadLocationException ble) {
return 0; // Never happens
}
}
/**
* Returns the height of a line of text in this text area.
*
* @return The height of a line of text.
*/
public int getLineHeight() {
return getRowHeight();
}
/**
* Returns the offset of the first character of the line that the caret is
* on.
*
* @return The first offset of the line that the caret is currently on.
*/
public final int getLineStartOffsetOfCurrentLine() {
try {
return getLineStartOffset(getCaretLineNumber());
} catch (BadLocationException ble) {
return 0; // Never happens
}
}
/**
* Returns the color used to paint the margin line.
*
* @return The margin line color.
* @see #setMarginLineColor(Color)
*/
public Color getMarginLineColor() {
return marginLineColor;
}
/**
* Returns the margin line position (in pixels) from the left-hand side of
* the text area.
*
* @return The margin line position.
* @see #getDefaultMarginLinePosition
* @see #setMarginLinePosition
*/
public int getMarginLinePixelLocation() {
return marginLineX;
}
/**
* Returns the margin line position (which is the number of 'm' widths in
* the current font the margin line is over).
*
* @return The margin line position.
* @see #getDefaultMarginLinePosition
* @see #setMarginLinePosition
*/
public int getMarginLinePosition() {
return marginSizeInChars;
}
/**
* Returns whether selection edges are rounded in this text area.
*
* @return Whether selection edges are rounded.
* @see #setRoundedSelectionEdges(boolean)
*/
public boolean getRoundedSelectionEdges() {
return roundedSelectionEdges;
}
/**
* Returns whether or not tabs are emulated with spaces (i.e., "soft"
* tabs).
*
* @return <code>true</code> if tabs are emulated with spaces;
* <code>false</code> if they aren't.
* @see #setTabsEmulated
*/
public boolean getTabsEmulated() {
return tabsEmulatedWithSpaces;
}
/**
* Initializes this text area.
*/
protected void init() {
// Sets the UI. Note that setUI() is overridden in RTextArea to only
// update the popup menu; this method must be called to set the real
// UI. This is done because the look and feel of an RTextArea is
// independent of the installed Java look and feels.
setRTextAreaUI(createRTextAreaUI());
// So we get notified when the component is resized.
enableEvents(AWTEvent.COMPONENT_EVENT_MASK|AWTEvent.KEY_EVENT_MASK);
// Defaults for various properties.
setHighlightCurrentLine(true);
this.setPaintCurrentLineHighlightFirst(false);
setCurrentLineHighlightColor(getDefaultCurrentLineHighlightColor());
setMarginLineEnabled(false);
setMarginLineColor(getDefaultMarginLineColor());
setMarginLinePosition(getDefaultMarginLinePosition());
setBackgroundObject(Color.WHITE);
setWrapStyleWord(true);// We only support wrapping at word boundaries.
setTabSize(5);
setForeground(Color.BLACK);
setTabsEmulated(false);
// Stuff needed by the caret listener below.
previousCaretY = currentCaretY = getInsets().top;
// Stuff to highlight the current line.
mouseListener = createMouseListener();
// Also acts as a focus listener so we can update our shared actions
// (cut, copy, etc. on the popup menu).
addFocusListener(mouseListener);
addCurrentLineHighlightListeners();
}
/**
* Returns whether or not the margin line is being painted.
*
* @return Whether or not the margin line is being painted.
* @see #setMarginLineEnabled
*/
public boolean isMarginLineEnabled() {
return marginLineEnabled;
}
/**
* Returns whether the OS we're running on is OS X.
*
* @return Whether the OS we're running on is OS X.
*/
public static boolean isOSX() {
// Recommended at:
// http://developer.apple.com/mac/library/technotes/tn2002/tn2110.html
String osName = System.getProperty("os.name").toLowerCase();
return osName.startsWith("mac os x");
}
/**
* Paints the text area.
*
* @param g The graphics context with which to paint.
*/
@Override
protected void paintComponent(Graphics g) {
//long startTime = System.currentTimeMillis();
backgroundPainter.paint(g, getVisibleRect());
// Paint the main part of the text area.
TextUI ui = getUI();
if (ui != null) {
// Not allowed to modify g, so make a copy.
Graphics scratchGraphics = g.create();
try {
ui.update(scratchGraphics, this);
} finally {
scratchGraphics.dispose();
}
}
//long endTime = System.currentTimeMillis();
//System.err.println(endTime-startTime);
}
/**
* Updates the current line highlight location.
*/
protected void possiblyUpdateCurrentLineHighlightLocation() {
int width = getWidth();
int lineHeight = getLineHeight();
int dot = getCaretPosition();
// If we're wrapping lines we need to check the actual y-coordinate
// of the caret, not just the line number, since a single logical
// line can span multiple physical lines.
if (getLineWrap()) {
try {
Rectangle temp = modelToView(dot);
if (temp!=null) {
currentCaretY = temp.y;
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // Should never happen.
}
}
// No line wrap - we can simply check the line number (quicker).
else {
// Document doc = getDocument();
// if (doc!=null) {
// Element map = doc.getDefaultRootElement();
// int caretLine = map.getElementIndex(dot);
// Rectangle alloc = ((RTextAreaUI)getUI()).
// getVisibleEditorRect();
// if (alloc!=null)
// currentCaretY = alloc.y + caretLine*lineHeight;
// }
// Modified for code folding requirements
try {
Rectangle temp = modelToView(dot);
if (temp!=null) {
currentCaretY = temp.y;
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // Should never happen.
}
}
// Repaint current line (to fill in entire highlight), and old line
// (to erase entire old highlight) if necessary. Always repaint
// current line in case selection is added or removed.
repaint(0,currentCaretY, width,lineHeight);
if (previousCaretY!=currentCaretY) {
repaint(0,previousCaretY, width,lineHeight);
}
previousCaretY = currentCaretY;
}
/**
* Overridden so we can tell when the text area is resized and update the
* current-line highlight, if necessary (i.e., if it is enabled and if
* lineWrap is enabled.
*
* @param e The component event about to be sent to all registered
* <code>ComponentListener</code>s.
*/
@Override
protected void processComponentEvent(ComponentEvent e) {
// In line wrap mode, resizing the text area means that the caret's
// "line" could change - not to a different logical line, but a
// different physical one. So, here we force a repaint of the current
// line's highlight if necessary.
if (e.getID()==ComponentEvent.COMPONENT_RESIZED &&
getLineWrap()==true && getHighlightCurrentLine()) {
previousCaretY = -1; // So we are sure to repaint.
fireCaretUpdate(mouseListener);
}
super.processComponentEvent(e);
}
/**
* Sets the background color of this text editor. Note that this is
* equivalent to calling <code>setBackgroundObject(bg)</code>.<p>
*
* NOTE: the opaque property is set to <code>true</code> when the
* background is set to a color (by this method). When an image is used
* for the background, opaque is set to false. This is because
* we perform better when setOpaque is true, but if we use an
* image for the background when opaque is true, we get on-screen
* garbage when the user scrolls via the arrow keys. Thus we
* need setOpaque to be false in that case.<p>
* You never have to change the opaque property yourself; it is always done
* for you.
*
* @param bg The color to use as the background color.
*/
@Override
public void setBackground(Color bg) {
Object oldBG = getBackgroundObject();
if (oldBG instanceof Color) { // Just change color of strategy.
((ColorBackgroundPainterStrategy)backgroundPainter).
setColor(bg);
}
else { // Was an image painter...
backgroundPainter = new ColorBackgroundPainterStrategy(bg);
}
setOpaque(bg.getAlpha()==0xff);
firePropertyChange("background", oldBG, bg);
repaint();
}
/**
* Sets this image as the background image. This method fires a
* property change event of type {@link #BACKGROUND_IMAGE_PROPERTY}.<p>
*
* NOTE: the opaque property is set to <code>true</code> when the
* background is set to a color. When an image is used for the
* background (by this method), opaque is set to false. This is because
* we perform better when setOpaque is true, but if we use an
* image for the background when opaque is true, we get on-screen
* garbage when the user scrolls via the arrow keys. Thus we
* need setOpaque to be false in that case.<p>
* You never have to change the opaque property yourself; it is always done
* for you.
*
* @param image The image to use as this text area's background.
* @see #getBackgroundImage
*/
public void setBackgroundImage(Image image) {
Object oldBG = getBackgroundObject();
if (oldBG instanceof Image) { // Just change image being displayed.
((ImageBackgroundPainterStrategy)backgroundPainter).
setImage(image);
}
else { // Was a color strategy...
ImageBackgroundPainterStrategy strategy =
new BufferedImageBackgroundPainterStrategy(this);
strategy.setImage(image);
backgroundPainter = strategy;
}
setOpaque(false);
firePropertyChange(BACKGROUND_IMAGE_PROPERTY, oldBG, image);
repaint();
}
/**
* Makes the background into this <code>Object</code>.
*
* @param newBackground The <code>java.awt.Color</code> or
* <code>java.awt.Image</code> object. If <code>newBackground</code>
* is not either of these, the background is set to plain white.
*/
public void setBackgroundObject(Object newBackground) {
if (newBackground instanceof Color) {
setBackground((Color)newBackground);
}
else if (newBackground instanceof Image) {
setBackgroundImage((Image)newBackground);
}
else {
setBackground(Color.WHITE);
}
}
/*
* TODO: Figure out why RTextArea doesn't work with RTL (e.g. Arabic)
* and fix it!
*/
// public void setComponentOrientation(ComponentOrientation o) {
// if (!o.isLeftToRight()) {
// o = ComponentOrientation.LEFT_TO_RIGHT;
// }
// super.setComponentOrientation(o);
// }
/**
* Sets the color to use to highlight the current line. Note that if
* highlighting the current line is turned off, you will not be able to
* see this highlight. This method fires a property change of type
* {@link #CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY}.
*
* @param color The color to use to highlight the current line.
* @throws NullPointerException if <code>color</code> is <code>null</code>.
* @see #getHighlightCurrentLine()
* @see #setHighlightCurrentLine(boolean)
* @see #getCurrentLineHighlightColor
*/
public void setCurrentLineHighlightColor(Color color) {
if (color==null)
throw new NullPointerException();
if (!color.equals(currentLineColor)) {
Color old = currentLineColor;
currentLineColor = color;
firePropertyChange(CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY,
old, color);
}
}
/**
* Sets whether the current line highlight should have a "fade" effect.
* This method fires a property change event of type
* <code>CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY</code>.
*
* @param fade Whether the fade effect should be enabled.
* @see #getFadeCurrentLineHighlight
*/
public void setFadeCurrentLineHighlight(boolean fade) {
if (fade!=fadeCurrentLineHighlight) {
fadeCurrentLineHighlight = fade;
if (getHighlightCurrentLine())
forceCurrentLineHighlightRepaint();
firePropertyChange(CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY,
!fade, fade);
}
}
/**
* Sets the font for this text area. This is overridden only so that we
* can update the size of the "current line highlight" and the location of
* the "margin line," if necessary.
*
* @param font The font to use for this text component.
*/
@Override
public void setFont(Font font) {
if (font!=null && font.getSize()<=0) {
throw new IllegalArgumentException("Font size must be > 0");
}
super.setFont(font);
if (font!=null) {
updateMarginLineX();
if (highlightCurrentLine)
possiblyUpdateCurrentLineHighlightLocation();
}
}
/**
* Sets whether or not the current line is highlighted. This method
* fires a property change of type {@link #HIGHLIGHT_CURRENT_LINE_PROPERTY}.