开发者

How to use Netbeans platform syntax highlight with JEditorPane?

T开发者_开发问答here are many tutorials online giving very complex or non-working examples on this. It seems that people recommend others to use the syntax highlighters offered by netbeans but I am totally puzzled on how to do so!

I have checked many many sites on this and the best I can find is : http://www.antonioshome.net/kitchen/netbeans/nbms-standalone.php

However I am still not able to use this example (as it is aimed to people who don't want to use the Netbeans platform but just a portion of it) and I am still not sure if I can just use syntax highlighting in a simple plug 'n play way. For example netbeans supports several language highlights by default, can I just use the highlighters in a JEditorPane to parse Ruby/Python/Java for example ? or do I need to write my own parser :-| ?

I will really appreciate a small simple example on how to plug syntax highlight in a standalone application using the netbeans platform.


This is how I use it:

String mimeType = "text/x-java"; // NOI18N
JEditorPane editorPane = new JEditorPane();

editorPane.setEditorKit(MimeLookup.getLookup(mimeType).lookup(EditorKit.class));


Hallo,

I found similar lack of info if you're trying to do a standalone platform app, in the end here is how I did it in my own application, yes it might be reinventing the wheel.. but since I couldnt find the wheel in the first place, might as well create one..

I took the information on how to create a java editor kit here: http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html

Built a small package with the necessary files, and pulled it into my platform application under one of the modules. You will need tools.jar where all those Scanner bits are hiding, it lives under the JDK install /lib folder - you'll have to wrap that.

Then used the example in the test program to figure out how to set the styles, - I like the complete control you have over token colouration.

Shamelessly copied from the included JavaKitTest..

    JavaContext styles = kit.getStylePreferences();
    Style s;

    //Make Comment lurid green
    s = styles.getStyleForScanValue(Token.COMMENT.getScanValue());
    StyleConstants.setForeground(s, new Color(102, 153, 153));

    //Make String err.. wotever color that is..
    s = styles.getStyleForScanValue(Token.STRINGVAL.getScanValue());
    StyleConstants.setForeground(s, new Color(102, 153, 102));

    //Make NEW nice n red
    s = styles.getStyleForScanValue(Token.NEW.getScanValue());
    StyleConstants.setForeground(s, new Color(102, 10, 10));


    //Do some other scan codes for keywords
    Color keyword = new Color(102, 102, 255);
    for (int code = 70; code <= 130; code++) {
        s = styles.getStyleForScanValue(code);
        if (s != null) {
            StyleConstants.setForeground(s, keyword);
        }
    }

This is just a java scanner, of course with this example you can get to play around with the grammar and tokens and come up with your own rules, I think there are tutorials on all that stuff..

Hope this helps a bit.


Partial Answer :

Apparently the following will enable syntax highlighting for Java (and some code completion) however it does not seem to work for other languages (except java, XML) even though it should [1]. Also I cannot find any way of enabling line numbers (they are enabled but they don't show up)!

yourEditor.setContentType("text/x-java");
yourEditor.putClientProperty("HighlightsLayerIncludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.SyntaxHighlighting$");

If someone decides to help with this, a more unified example including line numbers and other properties will be good. Surely it shouldn't be really complex ?!?

[1] http://netbeans.sourcearchive.com/lines/6.5-0ubuntu2/CodeTemplatesPanel_8java-source.html


The following should give you syntax highlighting for javascript. Find mimes for other types to use different syntax.

File tmpFile = File.createTempFile("tmp_sejsrunner", ".js");
tmpFile = FileUtil.normalizeFile(tmpFile);
FileObject fob = FileUtil.createData(tmpFile);

DataObject dob = DataObject.find(fob);

EditorKit kit = CloneableEditorSupport.getEditorKit("text/javascript");
this.scriptEditorPane.setEditorKit(kit);
this.scriptEditorPane.getDocument().putProperty(Document.StreamDescriptionProperty, dob);


To get line numbers you can use the following snippet:

BaseTextUI eui = new BaseTextUI();
eui.installUI(editor);
panel.add(eui.getEditorUI().getExtComponent());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜