1

我将StandaloneTextArea (STA) 组件用于具有语法突出显示的编辑器。我知道如何通过 xml 文件定义函数和关键字,但我不知道如何自定义 STA 的字体和颜色。

这是我到目前为止所尝试的:

Mode mode = new Mode("asm");
mode.setProperty("file", assemblymodes[COMPILER_ACME]);
ModeProvider.instance.addMode(mode);
standaloneTextArea.getBuffer().setMode(mode);

我尝试了一些设置颜色的方法,但没有成功paintersetStyle

TextAreaPainter painter = standaloneTextArea.getPainter();
painter.setStyles(SyntaxUtilities.loadStyles(Font.MONOSPACED, 14));

我知道有颜色属性view.style.comment1,但是如何分配这些属性以便将自定义配色方案应用于语法突出显示?

4

1 回答 1

1

好的,我找到了解决方案。我首先创建了 StandaloneTextArea,通过TextArea.createTextArea()它在设置属性时似乎不起作用,所以我必须从 StandaloneTextArea 派生我自己的类:

public class RL64TextArea extends StandaloneTextArea {
    final static Properties props;
    static IPropertyManager propertyManager;

    static {
        props = new Properties();
        props.putAll(loadProperties("jedit_keys.props"));
        props.putAll(loadProperties("jedit.props"));
        propertyManager = new IPropertyManager() {
            @Override
            public String getProperty(String name) {
                return props.getProperty(name);
            }
        };
    }

    public void setProperty(String name, String val) {
        props.setProperty(name, val);
    }

    private static Properties loadProperties(String fileName) {
        Properties loadedProps = new Properties();
        InputStream in = StandaloneTextArea.class.getResourceAsStream(fileName);
        try {
            loadedProps.load(in);
        }
        catch (IOException e) {
            ConstantsR64.r64logger.log(Level.WARNING,e.getLocalizedMessage());
        }
        finally {
            IOUtilities.closeQuietly(in);
        }
        return loadedProps;
    }

    public RL64TextArea() {
        super(propertyManager);

        // set syntax rules
        Mode mode = new Mode("asm");
        mode.setProperty("file", "syntax-rules.xml");
        ModeProvider.instance.addMode(mode);
        // add mode to buffer
        getBuffer().setMode(mode);

        // set colors
        setProperty("view.style.function", "color:"+ColorSchemes.getColor(scheme, ColorSchemes.COLOR_SCRIPTKEYWORD));
        setProperty("view.style.keyword1", "color:"+ColorSchemes.getColor(scheme, ColorSchemes.COLOR_KEYWORD));
        // ... more colors

        // load styles
        getPainter().setStyles(SyntaxUtilities.loadStyles(Font.MONOSPACED, 12));
    }
}
于 2014-05-22T06:38:19.843 回答