Java:StyledText显示行号

给Swt的StyledText组件增加行号显示,参考这里

text.addLineStyleListener(new LineStyleListener() {
            public void lineGetStyle(LineStyleEvent e) {
                // Set the line number
                e.bulletIndex = text.getLineAtOffset(e.lineOffset);

                // Set the style, 12 pixles wide for each digit
                StyleRange style = new StyleRange();
                style.metrics = new GlyphMetrics(0, 0, Integer.toString(text.getLineCount() + 1).length() * 12);

                // Create and set the bullet
                e.bullet = new Bullet(ST.BULLET_NUMBER, style);
            }
        });
 text.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent event) {
                int maxLine = text.getLineCount();
                int lineCountWidth = Math.max(String.valueOf(maxLine).length(), 3);

                StyleRange style = new StyleRange();
                style.background = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
                style.metrics = new GlyphMetrics(0, 0, lineCountWidth * 8 + 5);
                Bullet bullet = new Bullet(ST.BULLET_NUMBER, style);
                text.setLineBullet(0, text.getLineCount(), null);
                text.setLineBullet(0, text.getLineCount(), bullet);
            }
        });