Java 支持 IDocument 的 StyledText 控件

Java StyledText control with IDocument support

我打算编写一个 NASTRAN 文本编辑器(纯文本编辑器,eclipse 纯 E4 RCP 应用程序)。 NASTRAN 是一个工程结构分析应用程序。 简化来说,NASTRAN 使用每字段 8 个字符宽度的文本卡,每卡(行)最多 10 个字段。 查看到目前为止完成的工作的图

此编辑器的主要特点是显示带有彩色列背景的纯文本(固定间距字体),因此可以轻松区分每行中的不同字段。

我使用了 StyledText 控件,它提供了更改背景的方法:

styledText.setBackgroundImage(backgroundImage);

如何将 IDocument 接口与 StyledText 一起使用,以便它可以为我提供支持: 文本操作 职位 分区 线路信息 等...

其他文本控件(TextViewer、SourceViewer)提供setDocument(IDocument)方法来加载和操作文本数据

--org.eclipse.jface.text.TextViewer

  |
--org.eclipse.jface.text.source.SourceViewer

但是StyledText 扩展了 SWT Canvas 并且没有提供设置输入文档的方法

   --org.eclipse.swt.custom.StyledText

替代方法可能是如何更改 SourceViewer 控件中的背景,以便我可以拥有不同颜色的列。

提前致谢

TextViewer 和 SourceViewer 是 StyledText 的包装器,并提供处理 IDocument 接口的代码,因此您应该使用其中之一。

您可以调用查看器的 getTextWidget() 方法来访问他们正在使用的 StyledText 控件。

感谢greg-449的解答,问题已解决。 我对 class 包装另一个 class 的概念没有清楚的理解。所以我首先尝试创建一个 StyledText 对象。 现在清楚了

我在下面附上了我是如何进行的:创建一个 SourceViewer 控件,然后获取包装的 StyledText。 所以我可以为控件设置背景图像

public class NastranEditor {
    public StyledText st = null;
    public SourceViewer sv = null;
    private Image backgroundImage;//The image to appear at the backgroud
    //....
    @PostConstruct
    public void postConstruct(Composite parent){
        IVerticalRuler  ruler = new VerticalRuler(20);
        sv = new SourceViewer(parent, ruler, SWT.MULTI | SWT.V_SCROLL);
        st = sv.getTextWidget();
        st.setBackgroundImage(backgroundImage);
        //....
    }
    //....
}