1

I'm using the StandaloneTextArea (STA) component for an editor with syntax highlighting. I know how to define functions and keywords via xml-file, but I don't know how to customize font and color of the STA.

Here is what I have tried so far:

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

I have tried something with painter and setStyle to set colors, with no success:

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

I know there are color properties like view.style.comment1, but how to assign these in order to apply custom color schemes to the syntax highlighting?

Daniel
  • 7,252
  • 6
  • 26
  • 38

1 Answers1

1

Ok, I found a solution. I first created the StandaloneTextArea via TextArea.createTextArea() which does not seem to work when setting properties, so I have to derive my own class from 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));
    }
}
Daniel
  • 7,252
  • 6
  • 26
  • 38