[ 
https://issues.apache.org/jira/browse/NETBEANS-5596?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

ASF GitHub Bot updated NETBEANS-5596:
-------------------------------------
    Labels: pull-request-available  (was: )

> No syntax coloring with the Java Editor Kit
> -------------------------------------------
>
>                 Key: NETBEANS-5596
>                 URL: https://issues.apache.org/jira/browse/NETBEANS-5596
>             Project: NetBeans
>          Issue Type: Bug
>          Components: java - Editor
>    Affects Versions: 12.3
>            Reporter: Nicolas Baumann
>            Assignee: Nicolas Baumann
>            Priority: Minor
>              Labels: pull-request-available
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> I think this is somewhere between a bug and an improvement request because I 
> have no doubt that syntax coloring works well inside the IDE but my issue 
> happens when I use the java editor as a dependency for a plain java 
> application rather than a netbeans platform application.
> I have to activate syntax coloring programatically with the line of code 
> below whereas it should be done automatically based on the mime type of the 
> editor which is text/x-java.
> {code:java}
> doc.putProperty(Language.class, JavaTokenId.language()){code}
> There are some cases where the editor is not accessible due to member 
> visibility, for example with the the DiffView from org-netbeans-modules-diff.
> Here is a mininal code sample to reproduce :
>  
> {code:java}
> final JFrame f = new JFrame("JAVA Syntax Coloring");
> final JEditorPane pane = new JEditorPane();
>  
> pane.setEditorKit(CloneableEditorSupport.getEditorKit(JavaKit.JAVA_MIME_TYPE));
>  //pane.getDocument().putProperty(Language.class, JavaTokenId.language()); // 
> activates syntax coloring
>  try {
>  SwingUtilities.invokeAndWait(() -> {
>  try {
>  pane.getDocument().insertString(0, "public class Hello {}", null);
>  } catch (final BadLocationException e) {
>  e.printStackTrace();
>  }
>  });
>  } catch (final Exception e) {
>  e.printStackTrace();
>  }
>  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>  f.getContentPane().add(new JScrollPane(pane));
>  f.setSize(400, 300);
>  f.setVisible(true);
> {code}
>  
> From the javadoc :
> [https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-lexer/org/netbeans/api/lexer/TokenHierarchy.html#get-D-]
> _Get or create mutable token hierarchy for the given swing document._
>  _The document may define a top language by doing 
> {{doc.putProperty("mimeType", mimeType)}} (a language defined for the given 
> mime type will be searched and used) or by doing 
> {{putProperty(Language.class, language)}}. Otherwise the returned hierarchy 
> will be inactive and 
> [{{TokenHierarchy.tokenSequence()}}|https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-lexer/org/netbeans/api/lexer/TokenHierarchy.html#tokenSequence--]
>  will return null._
> In my case setting the mimeType property on the document was not enough to 
> activate the token hierarchy for syntax coloring. Only setting the 
> Language.class property resulted in activating it. But it's not possible with 
> an editor that I did not create myself and which I cannot have access to.
>  
> Suggestion :
> In the JavaKit class there is method createDefaultDocument() which creates a 
> default document. Is it possible for you to set the Langage.class property 
> here ?
>  
> Here is a workaround that I use to activate syntax coloring in the diff view.
> In the file org/netbeans/modules/java/editor/resources/layer.xml :
> {code:java}
> <file name="EditorKit.instance">
>  <attr name="instanceClass" stringvalue="my.own.pckg.SyntaxColoringJavaKit"/>
>  </file>{code}
>  
>  
> {code:java}
> public class SyntaxColoringJavaKit extends 
> org.netbeans.modules.editor.java.JavaKit {
> private static final long serialVersionUID = 1L;
>  @Override
>  public Document createDefaultDocument() {
>    final Document document = super.createDefaultDocument();
>    document.putProperty(Language.class, JavaTokenId.language());
>    return document;
>  }
>  
> }
> {code}
>  
>  Below the maven dependencies that I use in this example :
> {code:java}
> <dependencies>
>     <dependency> 
>         <groupId>org.netbeans.modules</groupId>
>         <artifactId>org-netbeans-modules-editor-mimelookup-impl</artifactId> 
>         <version>RELEASE123</version>
>     </dependency> 
>     <dependency>
>         <groupId>org.netbeans.modules</groupId>
>         <artifactId>org-netbeans-modules-editor-plain</artifactId>
>         <version>RELEASE123</version> 
>     </dependency> 
>     <dependency>
>         <groupId>org.netbeans.modules</groupId>
>         <artifactId>org-netbeans-modules-java-editor</artifactId> 
>         <version>RELEASE123</version> </dependency> 
>     <dependency> 
>         <groupId>org.frgaal</groupId>
>         <artifactId>compiler</artifactId>
>         <version>14.0.0</version>
>     </dependency>
> </dependencies>{code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to