@@ -87,6 +87,7 @@ public JEditTextArea(TextAreaDefaults defaults)
8787
8888 // Initialize some misc. stuff
8989 painter = new TextAreaPainter (this ,defaults );
90+ editorLineNumbers = new TextAreaLineNumbers (this ,defaults );
9091 documentHandler = new DocumentHandler ();
9192 eventListenerList = new EventListenerList ();
9293 caretEvent = new MutableCaretEvent ();
@@ -96,6 +97,7 @@ public JEditTextArea(TextAreaDefaults defaults)
9697
9798 // Initialize the GUI
9899 setLayout (new ScrollLayout ());
100+ add (LEFT , editorLineNumbers );
99101 add (CENTER , painter );
100102 add (RIGHT , vertical = new JScrollBar (JScrollBar .VERTICAL ));
101103 add (BOTTOM , horizontal = new JScrollBar (JScrollBar .HORIZONTAL ));
@@ -315,6 +317,14 @@ public void updateScrollBars() {
315317 horizontal .setUnitIncrement (charWidth );
316318 horizontal .setBlockIncrement (width / 2 );
317319 }
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+ }
318328 }
319329
320330 /**
@@ -335,7 +345,7 @@ public void setFirstLine(int firstLine) {
335345 if (firstLine != vertical .getValue ()) {
336346 updateScrollBars ();
337347 }
338- painter . repaint ();
348+ repaintEditor ();
339349 }
340350
341351 /**
@@ -377,7 +387,7 @@ public void setHorizontalOffset(int horizontalOffset)
377387 this .horizontalOffset = horizontalOffset ;
378388 if (horizontalOffset != horizontal .getValue ())
379389 updateScrollBars ();
380- painter . repaint ();
390+ repaintEditor ();
381391 }
382392
383393 /**
@@ -407,12 +417,17 @@ public boolean setOrigin(int firstLine, int horizontalOffset)
407417 if (changed )
408418 {
409419 updateScrollBars ();
410- painter . repaint ();
420+ repaintEditor ();
411421 }
412422
413423 return changed ;
414424 }
415425
426+ private void repaintEditor () {
427+ painter .repaint ();
428+ updateLineNumbers ();
429+ }
430+
416431 /**
417432 * Ensures that the caret is visible by scrolling the text area if
418433 * necessary.
@@ -732,7 +747,7 @@ public void setDocument(SyntaxDocument document) {
732747
733748 select (0 , 0 );
734749 updateScrollBars ();
735- painter . repaint ();
750+ repaintEditor ();
736751 }
737752
738753
@@ -753,7 +768,7 @@ public void setDocument(SyntaxDocument document,
753768 select (start , stop );
754769 updateScrollBars ();
755770 setScrollPosition (scroll );
756- painter . repaint ();
771+ repaintEditor ();
757772 }
758773
759774
@@ -790,7 +805,11 @@ public final int getDocumentLength()
790805 */
791806 public final int getLineCount ()
792807 {
793- return document .getDefaultRootElement ().getElementCount ();
808+ if (document != null ) {
809+ return document .getDefaultRootElement ().getElementCount ();
810+ } else {
811+ return 0 ;
812+ }
794813 }
795814
796815 /**
@@ -1747,6 +1766,7 @@ public void processKeyEvent(KeyEvent evt) {
17471766 }
17481767
17491768 // protected members
1769+ protected static String LEFT = "left" ;
17501770 protected static String CENTER = "center" ;
17511771 protected static String RIGHT = "right" ;
17521772 protected static String BOTTOM = "bottom" ;
@@ -1755,6 +1775,7 @@ public void processKeyEvent(KeyEvent evt) {
17551775 protected static Timer caretTimer ;
17561776
17571777 protected TextAreaPainter painter ;
1778+ protected TextAreaLineNumbers editorLineNumbers ;
17581779
17591780 //protected EditPopupMenu popup;
17601781 protected JPopupMenu popup ;
@@ -1881,7 +1902,9 @@ class ScrollLayout implements LayoutManager
18811902
18821903 public void addLayoutComponent (String name , Component comp )
18831904 {
1884- if (name .equals (CENTER ))
1905+ if (name .equals (LEFT ))
1906+ left = comp ;
1907+ else if (name .equals (CENTER ))
18851908 center = comp ;
18861909 else if (name .equals (RIGHT ))
18871910 right = comp ;
@@ -1893,6 +1916,8 @@ else if(name.equals(LEFT_OF_SCROLLBAR))
18931916
18941917 public void removeLayoutComponent (Component comp )
18951918 {
1919+ if (left == comp )
1920+ left = null ;
18961921 if (center == comp )
18971922 center = null ;
18981923 if (right == comp )
@@ -1913,6 +1938,8 @@ public Dimension preferredLayoutSize(Container parent)
19131938 Dimension centerPref = center .getPreferredSize ();
19141939 dim .width += centerPref .width ;
19151940 dim .height += centerPref .height ;
1941+ Dimension leftPref = left .getPreferredSize ();
1942+ dim .width += leftPref .width ;
19161943 Dimension rightPref = right .getPreferredSize ();
19171944 dim .width += rightPref .width ;
19181945 Dimension bottomPref = bottom .getPreferredSize ();
@@ -1931,6 +1958,8 @@ public Dimension minimumLayoutSize(Container parent)
19311958 Dimension centerPref = center .getMinimumSize ();
19321959 dim .width += centerPref .width ;
19331960 dim .height += centerPref .height ;
1961+ Dimension leftPref = left .getMinimumSize ();
1962+ dim .width += leftPref .width ;
19341963 Dimension rightPref = right .getMinimumSize ();
19351964 dim .width += rightPref .width ;
19361965 Dimension bottomPref = bottom .getMinimumSize ();
@@ -1950,11 +1979,19 @@ public void layoutContainer(Container parent)
19501979 int ibottom = insets .bottom ;
19511980 int iright = insets .right ;
19521981
1982+ int leftWidth = left .getSize ().width ;
19531983 int rightWidth = right .getPreferredSize ().width ;
19541984 int bottomHeight = bottom .getPreferredSize ().height ;
1955- int centerWidth = size .width - rightWidth - ileft - iright ;
1985+ int centerWidth = size .width - leftWidth - rightWidth - ileft - iright ;
19561986 int centerHeight = size .height - bottomHeight - itop - ibottom ;
19571987
1988+ left .setBounds (ileft ,
1989+ itop ,
1990+ leftWidth ,
1991+ centerHeight );
1992+
1993+ ileft += leftWidth ;
1994+
19581995 center .setBounds (ileft , // + LEFT_EXTRA,
19591996 itop ,
19601997 centerWidth , // - LEFT_EXTRA,
@@ -1984,6 +2021,7 @@ public void layoutContainer(Container parent)
19842021 }
19852022
19862023 // private members
2024+ private Component left ;
19872025 private Component center ;
19882026 private Component right ;
19892027 private Component bottom ;
@@ -2395,4 +2433,8 @@ public boolean addEdit(UndoableEdit edit)
23952433 caretTimer .setInitialDelay (500 );
23962434 caretTimer .start ();
23972435 }
2436+
2437+ public void setDisplayLineNumbers (boolean displayLineNumbers ) {
2438+ editorLineNumbers .setDisplayLineNumbers (displayLineNumbers );
2439+ }
23982440}
0 commit comments