@@ -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
/**
@@ -1747,6 +1766,7 @@ public void processKeyEvent(KeyEvent evt) {
1747
1766
}
1748
1767
1749
1768
// protected members
1769
+ protected static String LEFT = "left" ;
1750
1770
protected static String CENTER = "center" ;
1751
1771
protected static String RIGHT = "right" ;
1752
1772
protected static String BOTTOM = "bottom" ;
@@ -1755,6 +1775,7 @@ public void processKeyEvent(KeyEvent evt) {
1755
1775
protected static Timer caretTimer ;
1756
1776
1757
1777
protected TextAreaPainter painter ;
1778
+ protected TextAreaLineNumbers editorLineNumbers ;
1758
1779
1759
1780
//protected EditPopupMenu popup;
1760
1781
protected JPopupMenu popup ;
@@ -1881,7 +1902,9 @@ class ScrollLayout implements LayoutManager
1881
1902
1882
1903
public void addLayoutComponent (String name , Component comp )
1883
1904
{
1884
- if (name .equals (CENTER ))
1905
+ if (name .equals (LEFT ))
1906
+ left = comp ;
1907
+ else if (name .equals (CENTER ))
1885
1908
center = comp ;
1886
1909
else if (name .equals (RIGHT ))
1887
1910
right = comp ;
@@ -1893,6 +1916,8 @@ else if(name.equals(LEFT_OF_SCROLLBAR))
1893
1916
1894
1917
public void removeLayoutComponent (Component comp )
1895
1918
{
1919
+ if (left == comp )
1920
+ left = null ;
1896
1921
if (center == comp )
1897
1922
center = null ;
1898
1923
if (right == comp )
@@ -1913,6 +1938,8 @@ public Dimension preferredLayoutSize(Container parent)
1913
1938
Dimension centerPref = center .getPreferredSize ();
1914
1939
dim .width += centerPref .width ;
1915
1940
dim .height += centerPref .height ;
1941
+ Dimension leftPref = left .getPreferredSize ();
1942
+ dim .width += leftPref .width ;
1916
1943
Dimension rightPref = right .getPreferredSize ();
1917
1944
dim .width += rightPref .width ;
1918
1945
Dimension bottomPref = bottom .getPreferredSize ();
@@ -1931,6 +1958,8 @@ public Dimension minimumLayoutSize(Container parent)
1931
1958
Dimension centerPref = center .getMinimumSize ();
1932
1959
dim .width += centerPref .width ;
1933
1960
dim .height += centerPref .height ;
1961
+ Dimension leftPref = left .getMinimumSize ();
1962
+ dim .width += leftPref .width ;
1934
1963
Dimension rightPref = right .getMinimumSize ();
1935
1964
dim .width += rightPref .width ;
1936
1965
Dimension bottomPref = bottom .getMinimumSize ();
@@ -1950,11 +1979,19 @@ public void layoutContainer(Container parent)
1950
1979
int ibottom = insets .bottom ;
1951
1980
int iright = insets .right ;
1952
1981
1982
+ int leftWidth = left .getSize ().width ;
1953
1983
int rightWidth = right .getPreferredSize ().width ;
1954
1984
int bottomHeight = bottom .getPreferredSize ().height ;
1955
- int centerWidth = size .width - rightWidth - ileft - iright ;
1985
+ int centerWidth = size .width - leftWidth - rightWidth - ileft - iright ;
1956
1986
int centerHeight = size .height - bottomHeight - itop - ibottom ;
1957
1987
1988
+ left .setBounds (ileft ,
1989
+ itop ,
1990
+ leftWidth ,
1991
+ centerHeight );
1992
+
1993
+ ileft += leftWidth ;
1994
+
1958
1995
center .setBounds (ileft , // + LEFT_EXTRA,
1959
1996
itop ,
1960
1997
centerWidth , // - LEFT_EXTRA,
@@ -1984,6 +2021,7 @@ public void layoutContainer(Container parent)
1984
2021
}
1985
2022
1986
2023
// private members
2024
+ private Component left ;
1987
2025
private Component center ;
1988
2026
private Component right ;
1989
2027
private Component bottom ;
@@ -2395,4 +2433,8 @@ public boolean addEdit(UndoableEdit edit)
2395
2433
caretTimer .setInitialDelay (500 );
2396
2434
caretTimer .start ();
2397
2435
}
2436
+
2437
+ public void setDisplayLineNumbers (boolean displayLineNumbers ) {
2438
+ editorLineNumbers .setDisplayLineNumbers (displayLineNumbers );
2439
+ }
2398
2440
}
0 commit comments