@@ -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 (defaults .font , defaults .bgcolor , defaults .fgcolor , (int ) painter .getPreferredSize ().getHeight ());
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,12 @@ 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
+ editorLineNumbers .updateLineNumbers (getFirstLine () + 1 , Math .min (getFirstLine () + getVisibleLines () + 1 , getLineCount ()));
325
+ editorLineNumbers .updateWidthForNumDigits (String .valueOf (getLineCount ()).length ());
318
326
}
319
327
320
328
/**
@@ -335,7 +343,7 @@ public void setFirstLine(int firstLine) {
335
343
if (firstLine != vertical .getValue ()) {
336
344
updateScrollBars ();
337
345
}
338
- painter . repaint ();
346
+ repaintEditor ();
339
347
}
340
348
341
349
/**
@@ -377,7 +385,7 @@ public void setHorizontalOffset(int horizontalOffset)
377
385
this .horizontalOffset = horizontalOffset ;
378
386
if (horizontalOffset != horizontal .getValue ())
379
387
updateScrollBars ();
380
- painter . repaint ();
388
+ repaintEditor ();
381
389
}
382
390
383
391
/**
@@ -407,12 +415,17 @@ public boolean setOrigin(int firstLine, int horizontalOffset)
407
415
if (changed )
408
416
{
409
417
updateScrollBars ();
410
- painter . repaint ();
418
+ repaintEditor ();
411
419
}
412
420
413
421
return changed ;
414
422
}
415
423
424
+ private void repaintEditor () {
425
+ painter .repaint ();
426
+ updateLineNumbers ();
427
+ }
428
+
416
429
/**
417
430
* Ensures that the caret is visible by scrolling the text area if
418
431
* necessary.
@@ -732,7 +745,7 @@ public void setDocument(SyntaxDocument document) {
732
745
733
746
select (0 , 0 );
734
747
updateScrollBars ();
735
- painter . repaint ();
748
+ repaintEditor ();
736
749
}
737
750
738
751
@@ -753,7 +766,7 @@ public void setDocument(SyntaxDocument document,
753
766
select (start , stop );
754
767
updateScrollBars ();
755
768
setScrollPosition (scroll );
756
- painter . repaint ();
769
+ repaintEditor ();
757
770
}
758
771
759
772
@@ -1747,6 +1760,7 @@ public void processKeyEvent(KeyEvent evt) {
1747
1760
}
1748
1761
1749
1762
// protected members
1763
+ protected static String LEFT = "left" ;
1750
1764
protected static String CENTER = "center" ;
1751
1765
protected static String RIGHT = "right" ;
1752
1766
protected static String BOTTOM = "bottom" ;
@@ -1755,6 +1769,7 @@ public void processKeyEvent(KeyEvent evt) {
1755
1769
protected static Timer caretTimer ;
1756
1770
1757
1771
protected TextAreaPainter painter ;
1772
+ protected TextAreaLineNumbers editorLineNumbers ;
1758
1773
1759
1774
//protected EditPopupMenu popup;
1760
1775
protected JPopupMenu popup ;
@@ -1881,7 +1896,9 @@ class ScrollLayout implements LayoutManager
1881
1896
1882
1897
public void addLayoutComponent (String name , Component comp )
1883
1898
{
1884
- if (name .equals (CENTER ))
1899
+ if (name .equals (LEFT ))
1900
+ left = comp ;
1901
+ else if (name .equals (CENTER ))
1885
1902
center = comp ;
1886
1903
else if (name .equals (RIGHT ))
1887
1904
right = comp ;
@@ -1893,6 +1910,8 @@ else if(name.equals(LEFT_OF_SCROLLBAR))
1893
1910
1894
1911
public void removeLayoutComponent (Component comp )
1895
1912
{
1913
+ if (left == comp )
1914
+ left = null ;
1896
1915
if (center == comp )
1897
1916
center = null ;
1898
1917
if (right == comp )
@@ -1913,6 +1932,8 @@ public Dimension preferredLayoutSize(Container parent)
1913
1932
Dimension centerPref = center .getPreferredSize ();
1914
1933
dim .width += centerPref .width ;
1915
1934
dim .height += centerPref .height ;
1935
+ Dimension leftPref = left .getPreferredSize ();
1936
+ dim .width += leftPref .width ;
1916
1937
Dimension rightPref = right .getPreferredSize ();
1917
1938
dim .width += rightPref .width ;
1918
1939
Dimension bottomPref = bottom .getPreferredSize ();
@@ -1931,6 +1952,8 @@ public Dimension minimumLayoutSize(Container parent)
1931
1952
Dimension centerPref = center .getMinimumSize ();
1932
1953
dim .width += centerPref .width ;
1933
1954
dim .height += centerPref .height ;
1955
+ Dimension leftPref = left .getMinimumSize ();
1956
+ dim .width += leftPref .width ;
1934
1957
Dimension rightPref = right .getMinimumSize ();
1935
1958
dim .width += rightPref .width ;
1936
1959
Dimension bottomPref = bottom .getMinimumSize ();
@@ -1950,11 +1973,19 @@ public void layoutContainer(Container parent)
1950
1973
int ibottom = insets .bottom ;
1951
1974
int iright = insets .right ;
1952
1975
1976
+ int leftWidth = left .getSize ().width ;
1953
1977
int rightWidth = right .getPreferredSize ().width ;
1954
1978
int bottomHeight = bottom .getPreferredSize ().height ;
1955
- int centerWidth = size .width - rightWidth - ileft - iright ;
1979
+ int centerWidth = size .width - leftWidth - rightWidth - ileft - iright ;
1956
1980
int centerHeight = size .height - bottomHeight - itop - ibottom ;
1957
1981
1982
+ left .setBounds (ileft ,
1983
+ itop ,
1984
+ leftWidth ,
1985
+ centerHeight );
1986
+
1987
+ ileft += leftWidth ;
1988
+
1958
1989
center .setBounds (ileft , // + LEFT_EXTRA,
1959
1990
itop ,
1960
1991
centerWidth , // - LEFT_EXTRA,
@@ -1984,6 +2015,7 @@ public void layoutContainer(Container parent)
1984
2015
}
1985
2016
1986
2017
// private members
2018
+ private Component left ;
1987
2019
private Component center ;
1988
2020
private Component right ;
1989
2021
private Component bottom ;
0 commit comments