From 787f73dadebe147ab31333d0e0fab57afdf19c88 Mon Sep 17 00:00:00 2001 From: Cayci Date: Mon, 28 Oct 2013 00:19:30 -0400 Subject: [PATCH 1/3] Display line numbers for the file being edited --- .../processing/app/syntax/JEditTextArea.java | 46 ++++++++-- .../app/syntax/TextAreaLineNumbers.java | 85 +++++++++++++++++++ 2 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 app/src/processing/app/syntax/TextAreaLineNumbers.java diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index 924eeb7a9..1a8b5c204 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -87,6 +87,7 @@ public class JEditTextArea extends JComponent // Initialize some misc. stuff painter = new TextAreaPainter(this,defaults); + editorLineNumbers = new TextAreaLineNumbers(defaults.font, defaults.bgcolor, defaults.fgcolor, (int) painter.getPreferredSize().getHeight()); documentHandler = new DocumentHandler(); eventListenerList = new EventListenerList(); caretEvent = new MutableCaretEvent(); @@ -96,6 +97,7 @@ public class JEditTextArea extends JComponent // Initialize the GUI setLayout(new ScrollLayout()); + add(LEFT, editorLineNumbers); add(CENTER, painter); add(RIGHT, vertical = new JScrollBar(JScrollBar.VERTICAL)); add(BOTTOM, horizontal = new JScrollBar(JScrollBar.HORIZONTAL)); @@ -315,6 +317,12 @@ public class JEditTextArea extends JComponent horizontal.setUnitIncrement(charWidth); horizontal.setBlockIncrement(width / 2); } + updateLineNumbers(); + } + + private void updateLineNumbers() { + editorLineNumbers.updateLineNumbers(getFirstLine() + 1, Math.min(getFirstLine() + getVisibleLines() + 1, getLineCount())); + editorLineNumbers.updateWidthForNumDigits(String.valueOf(getLineCount()).length()); } /** @@ -335,7 +343,7 @@ public class JEditTextArea extends JComponent if (firstLine != vertical.getValue()) { updateScrollBars(); } - painter.repaint(); + repaintEditor(); } /** @@ -377,7 +385,7 @@ public class JEditTextArea extends JComponent this.horizontalOffset = horizontalOffset; if(horizontalOffset != horizontal.getValue()) updateScrollBars(); - painter.repaint(); + repaintEditor(); } /** @@ -407,12 +415,17 @@ public class JEditTextArea extends JComponent if(changed) { updateScrollBars(); - painter.repaint(); + repaintEditor(); } return changed; } + private void repaintEditor() { + painter.repaint(); + updateLineNumbers(); + } + /** * Ensures that the caret is visible by scrolling the text area if * necessary. @@ -732,7 +745,7 @@ public class JEditTextArea extends JComponent select(0, 0); updateScrollBars(); - painter.repaint(); + repaintEditor(); } @@ -753,7 +766,7 @@ public class JEditTextArea extends JComponent select(start, stop); updateScrollBars(); setScrollPosition(scroll); - painter.repaint(); + repaintEditor(); } @@ -1747,6 +1760,7 @@ public class JEditTextArea extends JComponent } // protected members + protected static String LEFT = "left"; protected static String CENTER = "center"; protected static String RIGHT = "right"; protected static String BOTTOM = "bottom"; @@ -1755,6 +1769,7 @@ public class JEditTextArea extends JComponent protected static Timer caretTimer; protected TextAreaPainter painter; + protected TextAreaLineNumbers editorLineNumbers; //protected EditPopupMenu popup; protected JPopupMenu popup; @@ -1881,7 +1896,9 @@ public class JEditTextArea extends JComponent public void addLayoutComponent(String name, Component comp) { - if(name.equals(CENTER)) + if(name.equals(LEFT)) + left = comp; + else if(name.equals(CENTER)) center = comp; else if(name.equals(RIGHT)) right = comp; @@ -1893,6 +1910,8 @@ public class JEditTextArea extends JComponent public void removeLayoutComponent(Component comp) { + if(left == comp) + left = null; if(center == comp) center = null; if(right == comp) @@ -1913,6 +1932,8 @@ public class JEditTextArea extends JComponent Dimension centerPref = center.getPreferredSize(); dim.width += centerPref.width; dim.height += centerPref.height; + Dimension leftPref = left.getPreferredSize(); + dim.width += leftPref.width; Dimension rightPref = right.getPreferredSize(); dim.width += rightPref.width; Dimension bottomPref = bottom.getPreferredSize(); @@ -1931,6 +1952,8 @@ public class JEditTextArea extends JComponent Dimension centerPref = center.getMinimumSize(); dim.width += centerPref.width; dim.height += centerPref.height; + Dimension leftPref = left.getMinimumSize(); + dim.width += leftPref.width; Dimension rightPref = right.getMinimumSize(); dim.width += rightPref.width; Dimension bottomPref = bottom.getMinimumSize(); @@ -1950,11 +1973,19 @@ public class JEditTextArea extends JComponent int ibottom = insets.bottom; int iright = insets.right; + int leftWidth = left.getSize().width; int rightWidth = right.getPreferredSize().width; int bottomHeight = bottom.getPreferredSize().height; - int centerWidth = size.width - rightWidth - ileft - iright; + int centerWidth = size.width - leftWidth - rightWidth - ileft - iright; int centerHeight = size.height - bottomHeight - itop - ibottom; + left.setBounds(ileft, + itop, + leftWidth, + centerHeight); + + ileft += leftWidth; + center.setBounds(ileft, // + LEFT_EXTRA, itop, centerWidth, // - LEFT_EXTRA, @@ -1984,6 +2015,7 @@ public class JEditTextArea extends JComponent } // private members + private Component left; private Component center; private Component right; private Component bottom; diff --git a/app/src/processing/app/syntax/TextAreaLineNumbers.java b/app/src/processing/app/syntax/TextAreaLineNumbers.java new file mode 100644 index 000000000..acc08d9dd --- /dev/null +++ b/app/src/processing/app/syntax/TextAreaLineNumbers.java @@ -0,0 +1,85 @@ +/* + * TextAreaLineNumbers.java - Show line numbers for the open file in the editor + * Copyright (C) 2013 Cayci Gorlitsky + * + * You may use and modify this package for any purpose. Redistribution is + * permitted, in both source and binary form, provided that this notice + * remains intact in all source distributions of this package. + */ + +package processing.app.syntax; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Rectangle; + +import javax.swing.JTextPane; +import javax.swing.border.MatteBorder; +import javax.swing.text.SimpleAttributeSet; +import javax.swing.text.StyleConstants; + +public class TextAreaLineNumbers extends JTextPane { + + private final int LEFT_INDENT = 6; + private final int RIGHT_INDENT = 6; + private final int RIGHT_BORDER_WIDTH = 1; + private final int PADDING_WIDTH = LEFT_INDENT + RIGHT_INDENT + RIGHT_BORDER_WIDTH; + + private final int MIN_WIDTH; + private final int DIGIT_WIDTH; + private final int MIN_NUM_DIGITS = 2; + + private int currStartNum = 0; + private int currEndNum = 0; + private int currNumDigits = MIN_NUM_DIGITS; + + public TextAreaLineNumbers(Font font, Color bgcolor, Color fgcolor, int preferredHeight) { + setFont(font); + setBackground(bgcolor); + setForeground(fgcolor); + setOpaque(true); + setEditable(false); + setEnabled(false); + setBorder(new MatteBorder(0, 0, 0, 1, new Color(240, 240, 240))); + + SimpleAttributeSet attribs = new SimpleAttributeSet(); + StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_RIGHT); + StyleConstants.setLeftIndent(attribs , 6); + StyleConstants.setRightIndent(attribs , 6); + setParagraphAttributes(attribs,true); + + DIGIT_WIDTH = getFontMetrics(getFont()).stringWidth("0"); + MIN_WIDTH = DIGIT_WIDTH * MIN_NUM_DIGITS + PADDING_WIDTH; + + setPreferredSize(new Dimension(MIN_WIDTH, preferredHeight)); + } + + public void updateLineNumbers(int startNum, int endNum) { + if (currStartNum == startNum && currEndNum == endNum) { + return; + } + currStartNum = startNum; + currEndNum = endNum; + + StringBuilder sb = new StringBuilder(); + for (int i = startNum; i < endNum; i++) { + sb.append(i).append("\n"); + } + sb.append(endNum); + setText(sb.toString()); + + invalidate(); + } + + public void updateWidthForNumDigits(int numDigits) { + if (currNumDigits == numDigits) { + return; + } + currNumDigits = numDigits; + + setBounds(new Rectangle(Math.max(MIN_WIDTH, DIGIT_WIDTH * numDigits + PADDING_WIDTH), getHeight())); + invalidate(); + } + +} From a2111fdcf666392d549916c3ff4b8d7607c66251 Mon Sep 17 00:00:00 2001 From: Cayci Date: Mon, 28 Oct 2013 21:01:35 -0400 Subject: [PATCH 2/3] add preference to enable/disable line numbers add preference to enable/disable line numbers and make font change work --- app/src/processing/app/Editor.java | 3 ++ app/src/processing/app/Preferences.java | 12 +++++ .../processing/app/syntax/JEditTextArea.java | 10 ++++- .../app/syntax/TextAreaLineNumbers.java | 44 ++++++++++++++----- build/shared/lib/preferences.txt | 3 ++ 5 files changed, 59 insertions(+), 13 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 774d20f74..1224cc760 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -430,6 +430,8 @@ public class Editor extends JFrame implements RunnerListener { textarea.setEditable(!external); saveMenuItem.setEnabled(!external); saveAsMenuItem.setEnabled(!external); + + textarea.setDisplayLineNumbers(Preferences.getBoolean("editor.linenumbers")); TextAreaPainter painter = textarea.getPainter(); if (external) { @@ -450,6 +452,7 @@ public class Editor extends JFrame implements RunnerListener { // apply changes to the font size for the editor //TextAreaPainter painter = textarea.getPainter(); painter.setFont(Preferences.getFont("editor.font")); + textarea.setLineNumbersFont(Preferences.getFont("editor.font")); //Font font = painter.getFont(); //textarea.getPainter().setFont(new Font("Courier", Font.PLAIN, 36)); diff --git a/app/src/processing/app/Preferences.java b/app/src/processing/app/Preferences.java index 00a5989ed..e084ddde6 100644 --- a/app/src/processing/app/Preferences.java +++ b/app/src/processing/app/Preferences.java @@ -177,6 +177,7 @@ public class Preferences { JCheckBox exportSeparateBox; JCheckBox verboseCompilationBox; JCheckBox verboseUploadBox; + JCheckBox displayLineNumbersBox; JCheckBox verifyUploadBox; JCheckBox externalEditorBox; JCheckBox memoryOverrideBox; @@ -382,6 +383,15 @@ public class Preferences { box.setBounds(left, top, d.width, d.height); top += d.height + GUI_BETWEEN; + // [ ] Display line numbers + + displayLineNumbersBox = new JCheckBox(_("Display line numbers")); + pain.add(displayLineNumbersBox); + d = displayLineNumbersBox.getPreferredSize(); + displayLineNumbersBox.setBounds(left, top, d.width + 10, d.height); + right = Math.max(right, left + d.width); + top += d.height + GUI_BETWEEN; + // [ ] Verify code after upload verifyUploadBox = new JCheckBox(_("Verify code after upload")); @@ -571,6 +581,7 @@ public class Preferences { // put each of the settings into the table setBoolean("build.verbose", verboseCompilationBox.isSelected()); setBoolean("upload.verbose", verboseUploadBox.isSelected()); + setBoolean("editor.linenumbers", displayLineNumbersBox.isSelected()); setBoolean("upload.verify", verifyUploadBox.isSelected()); // setBoolean("sketchbook.closing_last_window_quits", @@ -634,6 +645,7 @@ public class Preferences { // set all settings entry boxes to their actual status verboseCompilationBox.setSelected(getBoolean("build.verbose")); verboseUploadBox.setSelected(getBoolean("upload.verbose")); + displayLineNumbersBox.setSelected(getBoolean("editor.linenumbers")); verifyUploadBox.setSelected(getBoolean("upload.verify")); //closingLastQuitsBox. diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index 1a8b5c204..36fd20c7b 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -87,7 +87,7 @@ public class JEditTextArea extends JComponent // Initialize some misc. stuff painter = new TextAreaPainter(this,defaults); - editorLineNumbers = new TextAreaLineNumbers(defaults.font, defaults.bgcolor, defaults.fgcolor, (int) painter.getPreferredSize().getHeight()); + editorLineNumbers = new TextAreaLineNumbers(defaults, (int) painter.getPreferredSize().getHeight()); documentHandler = new DocumentHandler(); eventListenerList = new EventListenerList(); caretEvent = new MutableCaretEvent(); @@ -2427,4 +2427,12 @@ public class JEditTextArea extends JComponent caretTimer.setInitialDelay(500); caretTimer.start(); } + + public void setDisplayLineNumbers(boolean displayLineNumbers) { + editorLineNumbers.setDisplayLineNumbers(displayLineNumbers); + } + + public void setLineNumbersFont(Font font) { + editorLineNumbers.setTextFont(font); + } } diff --git a/app/src/processing/app/syntax/TextAreaLineNumbers.java b/app/src/processing/app/syntax/TextAreaLineNumbers.java index acc08d9dd..e374062dc 100644 --- a/app/src/processing/app/syntax/TextAreaLineNumbers.java +++ b/app/src/processing/app/syntax/TextAreaLineNumbers.java @@ -16,9 +16,12 @@ import java.awt.Rectangle; import javax.swing.JTextPane; import javax.swing.border.MatteBorder; +import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; +import processing.app.Preferences; + public class TextAreaLineNumbers extends JTextPane { private final int LEFT_INDENT = 6; @@ -34,26 +37,30 @@ public class TextAreaLineNumbers extends JTextPane { private int currEndNum = 0; private int currNumDigits = MIN_NUM_DIGITS; - public TextAreaLineNumbers(Font font, Color bgcolor, Color fgcolor, int preferredHeight) { - setFont(font); - setBackground(bgcolor); - setForeground(fgcolor); + public TextAreaLineNumbers(TextAreaDefaults defaults, int preferredHeight) { + setBackground(defaults.bgcolor); + setForeground(defaults.fgcolor); setOpaque(true); setEditable(false); setEnabled(false); setBorder(new MatteBorder(0, 0, 0, 1, new Color(240, 240, 240))); - - SimpleAttributeSet attribs = new SimpleAttributeSet(); - StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_RIGHT); - StyleConstants.setLeftIndent(attribs , 6); - StyleConstants.setRightIndent(attribs , 6); - setParagraphAttributes(attribs,true); + setTextFont(Preferences.getFont("editor.font")); DIGIT_WIDTH = getFontMetrics(getFont()).stringWidth("0"); MIN_WIDTH = DIGIT_WIDTH * MIN_NUM_DIGITS + PADDING_WIDTH; setPreferredSize(new Dimension(MIN_WIDTH, preferredHeight)); } + + public void setTextFont(Font font) { + setFont(font); + SimpleAttributeSet attribs = new SimpleAttributeSet(); + StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_RIGHT); + StyleConstants.setLeftIndent(attribs , 6); + StyleConstants.setRightIndent(attribs , 6); + StyleConstants.setFontSize(attribs, getFont().getSize()); + setParagraphAttributes(attribs,true); + } public void updateLineNumbers(int startNum, int endNum) { if (currStartNum == startNum && currEndNum == endNum) { @@ -68,7 +75,7 @@ public class TextAreaLineNumbers extends JTextPane { } sb.append(endNum); setText(sb.toString()); - + invalidate(); } @@ -78,8 +85,21 @@ public class TextAreaLineNumbers extends JTextPane { } currNumDigits = numDigits; - setBounds(new Rectangle(Math.max(MIN_WIDTH, DIGIT_WIDTH * numDigits + PADDING_WIDTH), getHeight())); + updateBounds(); + invalidate(); + } + + public void setDisplayLineNumbers(boolean displayLineNumbers) { + setVisible(displayLineNumbers); + if (displayLineNumbers) { + updateBounds(); + } else { + setBounds(new Rectangle(0, getHeight())); + } invalidate(); } + private void updateBounds() { + setBounds(new Rectangle(Math.max(MIN_WIDTH, DIGIT_WIDTH * currNumDigits + PADDING_WIDTH), getHeight())); + } } diff --git a/build/shared/lib/preferences.txt b/build/shared/lib/preferences.txt index 67246f860..f9bac321b 100755 --- a/build/shared/lib/preferences.txt +++ b/build/shared/lib/preferences.txt @@ -104,6 +104,9 @@ editor.caret.blink=true # area that's not in use by the text (replaced with tildes) editor.invalid=false +# show line numbers in editor +editor.linenumbers = false + # enable ctrl-ins, shift-ins, shift-delete for cut/copy/paste # on windows and linux, but disable on the mac editor.keys.alternative_cut_copy_paste = true From 66faf5d6c600127066b178e959c7a00f3ac6c31e Mon Sep 17 00:00:00 2001 From: Cayci Date: Sat, 2 Nov 2013 21:49:09 -0400 Subject: [PATCH 3/3] Render line numbers using TextAreaPainter Fix the line number font rendering problem on the Mac. Now, the TextAreaLineNumbers class extends the TextAreaPainter and just overloads the paintLine method to make it render the line number instead of rendering the whole line. Other than that, the superclass' drawing code is used. So: the same drawing code that renders the source code text in the editor is now also being used to render the line numbers. --- app/src/processing/app/Editor.java | 1 - .../processing/app/syntax/JEditTextArea.java | 18 +++-- .../app/syntax/TextAreaLineNumbers.java | 81 ++++++++++--------- 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 1224cc760..61051f93c 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -452,7 +452,6 @@ public class Editor extends JFrame implements RunnerListener { // apply changes to the font size for the editor //TextAreaPainter painter = textarea.getPainter(); painter.setFont(Preferences.getFont("editor.font")); - textarea.setLineNumbersFont(Preferences.getFont("editor.font")); //Font font = painter.getFont(); //textarea.getPainter().setFont(new Font("Courier", Font.PLAIN, 36)); diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index 36fd20c7b..07628e85e 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -87,7 +87,7 @@ public class JEditTextArea extends JComponent // Initialize some misc. stuff painter = new TextAreaPainter(this,defaults); - editorLineNumbers = new TextAreaLineNumbers(defaults, (int) painter.getPreferredSize().getHeight()); + editorLineNumbers = new TextAreaLineNumbers(this,defaults); documentHandler = new DocumentHandler(); eventListenerList = new EventListenerList(); caretEvent = new MutableCaretEvent(); @@ -321,8 +321,10 @@ public class JEditTextArea extends JComponent } private void updateLineNumbers() { - editorLineNumbers.updateLineNumbers(getFirstLine() + 1, Math.min(getFirstLine() + getVisibleLines() + 1, getLineCount())); - editorLineNumbers.updateWidthForNumDigits(String.valueOf(getLineCount()).length()); + if (editorLineNumbers != null) { + editorLineNumbers.updateLineNumbers(getFirstLine() + 1, Math.min(getFirstLine() + getVisibleLines() + 1, getLineCount())); + editorLineNumbers.updateWidthForNumDigits(String.valueOf(getLineCount()).length()); + } } /** @@ -803,7 +805,11 @@ public class JEditTextArea extends JComponent */ public final int getLineCount() { - return document.getDefaultRootElement().getElementCount(); + if (document != null) { + return document.getDefaultRootElement().getElementCount(); + } else { + return 0; + } } /** @@ -2431,8 +2437,4 @@ public class JEditTextArea extends JComponent public void setDisplayLineNumbers(boolean displayLineNumbers) { editorLineNumbers.setDisplayLineNumbers(displayLineNumbers); } - - public void setLineNumbersFont(Font font) { - editorLineNumbers.setTextFont(font); - } } diff --git a/app/src/processing/app/syntax/TextAreaLineNumbers.java b/app/src/processing/app/syntax/TextAreaLineNumbers.java index e374062dc..39f7438f2 100644 --- a/app/src/processing/app/syntax/TextAreaLineNumbers.java +++ b/app/src/processing/app/syntax/TextAreaLineNumbers.java @@ -10,19 +10,12 @@ package processing.app.syntax; import java.awt.Color; -import java.awt.Dimension; -import java.awt.Font; +import java.awt.Graphics; import java.awt.Rectangle; -import javax.swing.JTextPane; import javax.swing.border.MatteBorder; -import javax.swing.text.AttributeSet; -import javax.swing.text.SimpleAttributeSet; -import javax.swing.text.StyleConstants; -import processing.app.Preferences; - -public class TextAreaLineNumbers extends JTextPane { +public class TextAreaLineNumbers extends TextAreaPainter { private final int LEFT_INDENT = 6; private final int RIGHT_INDENT = 6; @@ -37,29 +30,14 @@ public class TextAreaLineNumbers extends JTextPane { private int currEndNum = 0; private int currNumDigits = MIN_NUM_DIGITS; - public TextAreaLineNumbers(TextAreaDefaults defaults, int preferredHeight) { - setBackground(defaults.bgcolor); - setForeground(defaults.fgcolor); - setOpaque(true); - setEditable(false); - setEnabled(false); - setBorder(new MatteBorder(0, 0, 0, 1, new Color(240, 240, 240))); - setTextFont(Preferences.getFont("editor.font")); - + + + public TextAreaLineNumbers(JEditTextArea textArea, TextAreaDefaults defaults) { + super(textArea, defaults); DIGIT_WIDTH = getFontMetrics(getFont()).stringWidth("0"); MIN_WIDTH = DIGIT_WIDTH * MIN_NUM_DIGITS + PADDING_WIDTH; - - setPreferredSize(new Dimension(MIN_WIDTH, preferredHeight)); - } - - public void setTextFont(Font font) { - setFont(font); - SimpleAttributeSet attribs = new SimpleAttributeSet(); - StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_RIGHT); - StyleConstants.setLeftIndent(attribs , 6); - StyleConstants.setRightIndent(attribs , 6); - StyleConstants.setFontSize(attribs, getFont().getSize()); - setParagraphAttributes(attribs,true); + setEnabled(false); + setBorder(new MatteBorder(0, 0, 0, RIGHT_BORDER_WIDTH, new Color(240, 240, 240))); } public void updateLineNumbers(int startNum, int endNum) { @@ -69,14 +47,30 @@ public class TextAreaLineNumbers extends JTextPane { currStartNum = startNum; currEndNum = endNum; - StringBuilder sb = new StringBuilder(); - for (int i = startNum; i < endNum; i++) { - sb.append(i).append("\n"); - } - sb.append(endNum); - setText(sb.toString()); - invalidate(); + repaint(); + } + + @Override + public void paint(Graphics gfx) { + super.paint(gfx); + getBorder().paintBorder(this, gfx, 0, 0, getSize().width, getSize().height); + } + + @Override + protected void paintLine(Graphics gfx, TokenMarker tokenMarker, + int line, int x) + { + currentLineIndex = line; + gfx.setFont(getFont()); + gfx.setColor(Color.GRAY); + int y = textArea.lineToY(line); + int startX = getBounds().x + getBounds().width; + if (line >= 0 && line < textArea.getLineCount()) { + String lineNumberString = String.valueOf(line+1); + int lineStartX = startX - RIGHT_BORDER_WIDTH - RIGHT_INDENT - fm.stringWidth(lineNumberString); + gfx.drawString(lineNumberString,lineStartX,y + fm.getHeight()); + } } public void updateWidthForNumDigits(int numDigits) { @@ -85,8 +79,11 @@ public class TextAreaLineNumbers extends JTextPane { } currNumDigits = numDigits; - updateBounds(); - invalidate(); + if (isVisible()) { + updateBounds(); + invalidate(); + repaint(); + } } public void setDisplayLineNumbers(boolean displayLineNumbers) { @@ -97,9 +94,13 @@ public class TextAreaLineNumbers extends JTextPane { setBounds(new Rectangle(0, getHeight())); } invalidate(); + repaint(); } private void updateBounds() { - setBounds(new Rectangle(Math.max(MIN_WIDTH, DIGIT_WIDTH * currNumDigits + PADDING_WIDTH), getHeight())); + if (isVisible()) { + setBounds(new Rectangle(Math.max(MIN_WIDTH, DIGIT_WIDTH * currNumDigits + PADDING_WIDTH), getHeight())); + textArea.validate(); + } } }