Skip to content

Commit acd511f

Browse files
committed
Improves the display of line numbers
1 parent 2f8110a commit acd511f

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

services/src/main/groovy/org/jd/gui/view/component/CustomLineNumbersPage.groovy

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import javax.swing.JComponent
2121
import javax.swing.text.EditorKit
2222
import javax.swing.text.JTextComponent
2323
import java.awt.Color
24+
import java.awt.Dimension
25+
import java.awt.Font
2426
import java.awt.FontMetrics
2527
import java.awt.Graphics
2628
import java.awt.Graphics2D
@@ -45,6 +47,7 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
4547
* Map[textarea line number] = original line number
4648
*/
4749
protected int[] lineNumberMap = null
50+
protected int maxLineNumber = 0
4851

4952
void setMaxLineNumber(int maxLineNumber) {
5053
if (maxLineNumber > 0) {
@@ -55,6 +58,10 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
5558
System.arraycopy(lineNumberMap, 0, tmp, 0, lineNumberMap.length)
5659
lineNumberMap = tmp
5760
}
61+
62+
if (this.maxLineNumber < maxLineNumber) {
63+
this.maxLineNumber = maxLineNumber
64+
}
5865
}
5966
}
6067

@@ -71,15 +78,7 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
7178
}
7279
}
7380

74-
int getMaximumSourceLineNumber() {
75-
int max = 0
76-
for (int ln : lineNumberMap) {
77-
if (max < ln) {
78-
max = ln
79-
}
80-
}
81-
return max
82-
}
81+
int getMaximumSourceLineNumber() { maxLineNumber }
8382

8483
@CompileStatic
8584
int getTextAreaLineNumber(int originalLineNumber) {
@@ -100,13 +99,14 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
10099
return textAreaLineNumber
101100
}
102101

103-
protected RSyntaxTextArea newRSyntaxTextArea() { new SourceSyntaxTextArea() }
102+
@Override protected RSyntaxTextArea newRSyntaxTextArea() { new SourceSyntaxTextArea() }
104103

105104
@CompileStatic
106105
class SourceSyntaxTextArea extends RSyntaxTextArea {
107106
/**
108107
* @see HyperlinkPage.HyperlinkSyntaxTextArea#getUnderlineForToken(org.fife.ui.rsyntaxtextarea.Token)
109108
*/
109+
@Override
110110
boolean getUnderlineForToken(Token t) {
111111
def entry = hyperlinks.floorEntry(t.offset)
112112
if (entry) {
@@ -118,7 +118,7 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
118118
return super.getUnderlineForToken(t)
119119
}
120120

121-
protected RTextAreaUI createRTextAreaUI() { new SourceSyntaxTextAreaUI(this) }
121+
@Override protected RTextAreaUI createRTextAreaUI() { new SourceSyntaxTextAreaUI(this) }
122122
}
123123

124124
/**
@@ -127,13 +127,13 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
127127
@CompileStatic
128128
class SourceSyntaxTextAreaUI extends RSyntaxTextAreaUI {
129129
SourceSyntaxTextAreaUI(JComponent rSyntaxTextArea) { super(rSyntaxTextArea) }
130-
EditorKit getEditorKit(JTextComponent tc) { new SourceSyntaxTextAreaEditorKit() }
131-
Rectangle getVisibleEditorRect() { super.getVisibleEditorRect() }
130+
@Override EditorKit getEditorKit(JTextComponent tc) { new SourceSyntaxTextAreaEditorKit() }
131+
@Override Rectangle getVisibleEditorRect() { super.getVisibleEditorRect() }
132132
}
133133

134134
@CompileStatic
135135
class SourceSyntaxTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
136-
LineNumberList createLineNumberList(RTextArea textArea) { new SourceLineNumberList(textArea) }
136+
@Override LineNumberList createLineNumberList(RTextArea textArea) { new SourceLineNumberList(textArea) }
137137
}
138138

139139
/**
@@ -145,6 +145,7 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
145145
protected Map<?,?> aaHints
146146
protected Rectangle visibleRect
147147
protected Insets textAreaInsets
148+
protected Dimension preferredSize
148149

149150
SourceLineNumberList(RTextArea textArea) {
150151
super(textArea, null)
@@ -297,5 +298,30 @@ abstract class CustomLineNumbersPage extends HyperlinkPage {
297298
}
298299

299300
int getRhsBorderWidth() { ((RSyntaxTextArea)rTextArea).isCodeFoldingEnabled() ? 0 : 4 }
301+
302+
@Override
303+
Dimension getPreferredSize() {
304+
if (preferredSize == null) {
305+
int lineCount = getMaximumSourceLineNumber()
306+
307+
if (lineCount > 0) {
308+
Font font = getFont()
309+
FontMetrics fontMetrics = getFontMetrics(font)
310+
int count = 1
311+
312+
while (lineCount >= 10) {
313+
lineCount = lineCount / 10 as int
314+
count++
315+
}
316+
317+
int preferredWidth = fontMetrics.charWidth('9' as char) * count + 10
318+
preferredSize = new Dimension(preferredWidth, 0)
319+
} else {
320+
preferredSize = new Dimension(0, 0)
321+
}
322+
}
323+
324+
return preferredSize
325+
}
300326
}
301327
}

0 commit comments

Comments
 (0)