@@ -87,6 +87,7 @@ public JEditTextArea(TextAreaDefaults defaults)
87
87
88
88
// Initialize some misc. stuff
89
89
painter = new TextAreaPainter (this ,defaults );
90
+ editorLineNumbers = new TextAreaLineNumbers (this ,defaults );
90
91
documentHandler = new DocumentHandler ();
91
92
eventListenerList = new EventListenerList ();
92
93
caretEvent = new MutableCaretEvent ();
@@ -96,6 +97,7 @@ public JEditTextArea(TextAreaDefaults defaults)
96
97
97
98
// Initialize the GUI
98
99
setLayout (new ScrollLayout ());
100
+ add (LEFT , editorLineNumbers );
99
101
add (CENTER , painter );
100
102
add (RIGHT , vertical = new JScrollBar (JScrollBar .VERTICAL ));
101
103
add (BOTTOM , horizontal = new JScrollBar (JScrollBar .HORIZONTAL ));
@@ -315,6 +317,14 @@ public void updateScrollBars() {
315
317
horizontal .setUnitIncrement (charWidth );
316
318
horizontal .setBlockIncrement (width / 2 );
317
319
}
320
+ updateLineNumbers ();
321
+ }
322
+
323
+ private void updateLineNumbers () {
324
+ if (editorLineNumbers != null ) {
325
+ editorLineNumbers .updateLineNumbers (getFirstLine () + 1 , Math .min (getFirstLine () + getVisibleLines () + 1 , getLineCount ()));
326
+ editorLineNumbers .updateWidthForNumDigits (String .valueOf (getLineCount ()).length ());
327
+ }
318
328
}
319
329
320
330
/**
@@ -335,7 +345,7 @@ public void setFirstLine(int firstLine) {
335
345
if (firstLine != vertical .getValue ()) {
336
346
updateScrollBars ();
337
347
}
338
- painter . repaint ();
348
+ repaintEditor ();
339
349
}
340
350
341
351
/**
@@ -377,7 +387,7 @@ public void setHorizontalOffset(int horizontalOffset)
377
387
this .horizontalOffset = horizontalOffset ;
378
388
if (horizontalOffset != horizontal .getValue ())
379
389
updateScrollBars ();
380
- painter . repaint ();
390
+ repaintEditor ();
381
391
}
382
392
383
393
/**
@@ -407,12 +417,17 @@ public boolean setOrigin(int firstLine, int horizontalOffset)
407
417
if (changed )
408
418
{
409
419
updateScrollBars ();
410
- painter . repaint ();
420
+ repaintEditor ();
411
421
}
412
422
413
423
return changed ;
414
424
}
415
425
426
+ private void repaintEditor () {
427
+ painter .repaint ();
428
+ updateLineNumbers ();
429
+ }
430
+
416
431
/**
417
432
* Ensures that the caret is visible by scrolling the text area if
418
433
* necessary.
@@ -732,7 +747,7 @@ public void setDocument(SyntaxDocument document) {
732
747
733
748
select (0 , 0 );
734
749
updateScrollBars ();
735
- painter . repaint ();
750
+ repaintEditor ();
736
751
}
737
752
738
753
@@ -753,7 +768,7 @@ public void setDocument(SyntaxDocument document,
753
768
select (start , stop );
754
769
updateScrollBars ();
755
770
setScrollPosition (scroll );
756
- painter . repaint ();
771
+ repaintEditor ();
757
772
}
758
773
759
774
@@ -790,7 +805,11 @@ public final int getDocumentLength()
790
805
*/
791
806
public final int getLineCount ()
792
807
{
793
- return document .getDefaultRootElement ().getElementCount ();
808
+ if (document != null ) {
809
+ return document .getDefaultRootElement ().getElementCount ();
810
+ } else {
811
+ return 0 ;
812
+ }
794
813
}
795
814
796
815
/**
@@ -1751,6 +1770,7 @@ public void processKeyEvent(KeyEvent evt) {
1751
1770
}
1752
1771
1753
1772
// protected members
1773
+ protected static String LEFT = "left" ;
1754
1774
protected static String CENTER = "center" ;
1755
1775
protected static String RIGHT = "right" ;
1756
1776
protected static String BOTTOM = "bottom" ;
@@ -1759,6 +1779,7 @@ public void processKeyEvent(KeyEvent evt) {
1759
1779
protected static Timer caretTimer ;
1760
1780
1761
1781
protected TextAreaPainter painter ;
1782
+ protected TextAreaLineNumbers editorLineNumbers ;
1762
1783
1763
1784
//protected EditPopupMenu popup;
1764
1785
protected JPopupMenu popup ;
@@ -1885,7 +1906,9 @@ class ScrollLayout implements LayoutManager
1885
1906
1886
1907
public void addLayoutComponent (String name , Component comp )
1887
1908
{
1888
- if (name .equals (CENTER ))
1909
+ if (name .equals (LEFT ))
1910
+ left = comp ;
1911
+ else if (name .equals (CENTER ))
1889
1912
center = comp ;
1890
1913
else if (name .equals (RIGHT ))
1891
1914
right = comp ;
@@ -1897,6 +1920,8 @@ else if(name.equals(LEFT_OF_SCROLLBAR))
1897
1920
1898
1921
public void removeLayoutComponent (Component comp )
1899
1922
{
1923
+ if (left == comp )
1924
+ left = null ;
1900
1925
if (center == comp )
1901
1926
center = null ;
1902
1927
if (right == comp )
@@ -1917,6 +1942,8 @@ public Dimension preferredLayoutSize(Container parent)
1917
1942
Dimension centerPref = center .getPreferredSize ();
1918
1943
dim .width += centerPref .width ;
1919
1944
dim .height += centerPref .height ;
1945
+ Dimension leftPref = left .getPreferredSize ();
1946
+ dim .width += leftPref .width ;
1920
1947
Dimension rightPref = right .getPreferredSize ();
1921
1948
dim .width += rightPref .width ;
1922
1949
Dimension bottomPref = bottom .getPreferredSize ();
@@ -1935,6 +1962,8 @@ public Dimension minimumLayoutSize(Container parent)
1935
1962
Dimension centerPref = center .getMinimumSize ();
1936
1963
dim .width += centerPref .width ;
1937
1964
dim .height += centerPref .height ;
1965
+ Dimension leftPref = left .getMinimumSize ();
1966
+ dim .width += leftPref .width ;
1938
1967
Dimension rightPref = right .getMinimumSize ();
1939
1968
dim .width += rightPref .width ;
1940
1969
Dimension bottomPref = bottom .getMinimumSize ();
@@ -1954,11 +1983,19 @@ public void layoutContainer(Container parent)
1954
1983
int ibottom = insets .bottom ;
1955
1984
int iright = insets .right ;
1956
1985
1986
+ int leftWidth = left .getSize ().width ;
1957
1987
int rightWidth = right .getPreferredSize ().width ;
1958
1988
int bottomHeight = bottom .getPreferredSize ().height ;
1959
- int centerWidth = size .width - rightWidth - ileft - iright ;
1989
+ int centerWidth = size .width - leftWidth - rightWidth - ileft - iright ;
1960
1990
int centerHeight = size .height - bottomHeight - itop - ibottom ;
1961
1991
1992
+ left .setBounds (ileft ,
1993
+ itop ,
1994
+ leftWidth ,
1995
+ centerHeight );
1996
+
1997
+ ileft += leftWidth ;
1998
+
1962
1999
center .setBounds (ileft , // + LEFT_EXTRA,
1963
2000
itop ,
1964
2001
centerWidth , // - LEFT_EXTRA,
@@ -1988,6 +2025,7 @@ public void layoutContainer(Container parent)
1988
2025
}
1989
2026
1990
2027
// private members
2028
+ private Component left ;
1991
2029
private Component center ;
1992
2030
private Component right ;
1993
2031
private Component bottom ;
@@ -2404,4 +2442,8 @@ public boolean addEdit(UndoableEdit edit)
2404
2442
caretTimer .setInitialDelay (500 );
2405
2443
caretTimer .start ();
2406
2444
}
2445
+
2446
+ public void setDisplayLineNumbers (boolean displayLineNumbers ) {
2447
+ editorLineNumbers .setDisplayLineNumbers (displayLineNumbers );
2448
+ }
2407
2449
}
0 commit comments