How to use Netbeans platform syntax highlighting with JEditorPane?

There are many textbooks on the Internet that provide very complex or broken examples. It seems that people are recommending others to use the syntax highlighting offered by netbeans, but I am completely puzzled by how to do this!

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

However, I still cannot use this example (since it is intended for people who do not want to use the Netbeans platform, but just part of it), and I'm still not sure if I can just use the syntax highlighting in simple "n" mode. For example, netbeans support a few highlights of the default language, can I just use markers in JEditorPane to parse Ruby / Python / Java? or do I need to write my own parser: - |?

I really appreciate a small simple example of how to enable syntax highlighting in a standalone application using the netbeans platform.

+4
source share
5 answers

Here is how I use it:

String mimeType = "text/x-java"; // NOI18N JEditorPane editorPane = new JEditorPane(); editorPane.setEditorKit(MimeLookup.getLookup(mimeType).lookup(EditorKit.class)); 
+1
source

Hello,

I found a similar lack of information, if you are trying to make a standalone platform application, in the end, this is how I did it in my application, yes, it can be a reinvention of the wheel .. but since I could not find the wheel in the first place, it can also create one.

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

A small package with the necessary files was built and pulled it into my platform application under one of the modules. You will need tools.jar, where all these bits of the Scanner are hidden, it lives under the / lib JDK installation folder - you have to wrap this.

Then I used an example in a test program to figure out how to set styles - I like full control over the marker color.

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); } } 

It's just a java scanner, of course, with this example you can play with grammar and tokens and come up with your own rules, I think there are tutorials on all of this.

Hope this helps a bit.

+1
source

Partial Answer:

Apparently, the following will include syntax highlighting for Java (and some code completion), however, it does not seem to work for other languages ​​(except java, XML), although this should be [1]. Also I cannot find a way to include line numbers (they are included, but they are not displayed)!

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

If someone decides to help with this, a more general example, including line numbers and other properties, would be good. Of course, this should not be really complicated?!?

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

0
source

The following should give you syntax highlighting for javascript. Find mimes for other types to use a 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); 
0
source

To get line numbers, you can use the following snippet:

 BaseTextUI eui = new BaseTextUI(); eui.installUI(editor); panel.add(eui.getEditorUI().getExtComponent()); 
-1
source

Source: https://habr.com/ru/post/1304053/


All Articles