我正在尝试突出当前活动线的子弹背景,但如果我只设置子弹的背景颜色,则只突出显示数字部分。我希望子弹所占据的所有空间都能突出显示。
我希望9号左边的所有空间都能突出显示,也可能会向右突出显示。
我用来获得我目前所拥有的代码是
@Override
public void lineGetStyle(LineStyleEvent event) {
// Set the line number
int activeLine = styledText.getLineAtOffset(styledText.getCaretOffset());
int currentLine = styledText.getLineAtOffset(event.lineOffset);
event.bulletIndex = currentLine;
int width = 36;
if (styledText.getLineCount() > 999)
width = (int) ((Math.floor(Math.log10(styledText.getLineCount()))+1) * 12);
// Set the style, 12 pixles wide for each digit
StyleRange style = new StyleRange();
style.metrics = new GlyphMetrics(0, 0, width);
if (activeLine == currentLine) {
style.background = highlightedLine;
}
style.foreground = mainBackground;
// Create and set the bullet
event.bullet = new Bullet(ST.BULLET_NUMBER, style);
event.styles = matchKeywords(event);
}
这可能吗?
答案 0 :(得分:3)
您可以使用自定义绘画执行此操作。首先,将项目符号类型更改为ST.BULLET_CUSTOM
。然后添加PaintObjectListener
:
styledText.addPaintObjectListener(new PaintObjectListener() {
public void paintObject(PaintObjectEvent event) {
drawBullet(event.bullet, event.gc, event.x, event.y, event.bulletIndex, event.ascent, event.descent);
}
});
StyledTextRenderer中有drawBullet
的标准实施。我已将此更改为接受自定义项目符号作为编号项目符号,并在后台绘制一个额外的矩形:
void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) {
StyleRange style = bullet.style;
GlyphMetrics metrics = style.metrics;
Color color = style.foreground;
if (color != null) gc.setForeground(color);
Font font = style.font;
if (font != null) gc.setFont(font);
String string = "";
int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_CUSTOM|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER);
switch (type) {
case ST.BULLET_DOT: string = "\u2022"; break;
case ST.BULLET_CUSTOM: string = String.valueOf(index + 1); break;
case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break;
case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break;
case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break;
}
if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text;
gc.setBackground(style.background);
gc.fillRectangle(paintX, paintY, metrics.width, styledText.getLineHeight());
Display display = styledText.getDisplay();
TextLayout layout = new TextLayout(display);
layout.setText(string);
layout.setAscent(lineAscent);
layout.setDescent(lineDescent);
style = (StyleRange)style.clone();
style.metrics = null;
if (style.font == null) style.font = styledText.getFont();
layout.setStyle(style, 0, string.length());
int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - 8);
layout.draw(gc, x, paintY);
layout.dispose();
}