@@ -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 /**
@@ -1751,6 +1770,7 @@ public void processKeyEvent(KeyEvent evt) {
17511770 }
17521771
17531772 // protected members
1773+ protected static String LEFT = "left" ;
17541774 protected static String CENTER = "center" ;
17551775 protected static String RIGHT = "right" ;
17561776 protected static String BOTTOM = "bottom" ;
@@ -1759,6 +1779,7 @@ public void processKeyEvent(KeyEvent evt) {
17591779 protected static Timer caretTimer ;
17601780
17611781 protected TextAreaPainter painter ;
1782+ protected TextAreaLineNumbers editorLineNumbers ;
17621783
17631784 //protected EditPopupMenu popup;
17641785 protected JPopupMenu popup ;
@@ -1885,7 +1906,9 @@ class ScrollLayout implements LayoutManager
18851906
18861907 public void addLayoutComponent (String name , Component comp )
18871908 {
1888- if (name .equals (CENTER ))
1909+ if (name .equals (LEFT ))
1910+ left = comp ;
1911+ else if (name .equals (CENTER ))
18891912 center = comp ;
18901913 else if (name .equals (RIGHT ))
18911914 right = comp ;
@@ -1897,6 +1920,8 @@ else if(name.equals(LEFT_OF_SCROLLBAR))
18971920
18981921 public void removeLayoutComponent (Component comp )
18991922 {
1923+ if (left == comp )
1924+ left = null ;
19001925 if (center == comp )
19011926 center = null ;
19021927 if (right == comp )
@@ -1917,6 +1942,8 @@ public Dimension preferredLayoutSize(Container parent)
19171942 Dimension centerPref = center .getPreferredSize ();
19181943 dim .width += centerPref .width ;
19191944 dim .height += centerPref .height ;
1945+ Dimension leftPref = left .getPreferredSize ();
1946+ dim .width += leftPref .width ;
19201947 Dimension rightPref = right .getPreferredSize ();
19211948 dim .width += rightPref .width ;
19221949 Dimension bottomPref = bottom .getPreferredSize ();
@@ -1935,6 +1962,8 @@ public Dimension minimumLayoutSize(Container parent)
19351962 Dimension centerPref = center .getMinimumSize ();
19361963 dim .width += centerPref .width ;
19371964 dim .height += centerPref .height ;
1965+ Dimension leftPref = left .getMinimumSize ();
1966+ dim .width += leftPref .width ;
19381967 Dimension rightPref = right .getMinimumSize ();
19391968 dim .width += rightPref .width ;
19401969 Dimension bottomPref = bottom .getMinimumSize ();
@@ -1954,11 +1983,19 @@ public void layoutContainer(Container parent)
19541983 int ibottom = insets .bottom ;
19551984 int iright = insets .right ;
19561985
1986+ int leftWidth = left .getSize ().width ;
19571987 int rightWidth = right .getPreferredSize ().width ;
19581988 int bottomHeight = bottom .getPreferredSize ().height ;
1959- int centerWidth = size .width - rightWidth - ileft - iright ;
1989+ int centerWidth = size .width - leftWidth - rightWidth - ileft - iright ;
19601990 int centerHeight = size .height - bottomHeight - itop - ibottom ;
19611991
1992+ left .setBounds (ileft ,
1993+ itop ,
1994+ leftWidth ,
1995+ centerHeight );
1996+
1997+ ileft += leftWidth ;
1998+
19621999 center .setBounds (ileft , // + LEFT_EXTRA,
19632000 itop ,
19642001 centerWidth , // - LEFT_EXTRA,
@@ -1988,6 +2025,7 @@ public void layoutContainer(Container parent)
19882025 }
19892026
19902027 // private members
2028+ private Component left ;
19912029 private Component center ;
19922030 private Component right ;
19932031 private Component bottom ;
@@ -2404,4 +2442,8 @@ public boolean addEdit(UndoableEdit edit)
24042442 caretTimer .setInitialDelay (500 );
24052443 caretTimer .start ();
24062444 }
2445+
2446+ public void setDisplayLineNumbers (boolean displayLineNumbers ) {
2447+ editorLineNumbers .setDisplayLineNumbers (displayLineNumbers );
2448+ }
24072449}
0 commit comments