You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by "Nicolas Baumann (Jira)" <ji...@apache.org> on 2021/04/18 10:00:03 UTC

[jira] [Updated] (NETBEANS-5596) No syntax coloring with the Java Editor Kit

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

Nicolas Baumann updated NETBEANS-5596:
--------------------------------------
    Description: 
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> <dependencies> <!-- Compile time dependencies --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>org.netbeans.api</groupId> <artifactId>org-netbeans-modules-diff</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.netbeans.modules</groupId> <artifactId>org-netbeans-modules-editor-mimelookup-impl</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.netbeans.modules</groupId> <artifactId>org-netbeans-modules-editor-plain</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.netbeans.modules</groupId> <artifactId>org-netbeans-modules-java-editor</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.frgaal</groupId> <artifactId>compiler</artifactId> <version>14.0.0</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <netbeans.version>RELEASE123</netbeans.version> <project.scm.id>github.com</project.scm.id> </properties>{code}
 

 

 

 

 

  was:
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}
 

 

 

 

 

 

 


> 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
>            Priority: Major
>
> 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> <dependencies> <!-- Compile time dependencies --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>org.netbeans.api</groupId> <artifactId>org-netbeans-modules-diff</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.netbeans.modules</groupId> <artifactId>org-netbeans-modules-editor-mimelookup-impl</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.netbeans.modules</groupId> <artifactId>org-netbeans-modules-editor-plain</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.netbeans.modules</groupId> <artifactId>org-netbeans-modules-java-editor</artifactId> <version>${netbeans.version}</version> </dependency> <dependency> <groupId>org.frgaal</groupId> <artifactId>compiler</artifactId> <version>14.0.0</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <netbeans.version>RELEASE123</netbeans.version> <project.scm.id>github.com</project.scm.id> </properties>{code}
>  
>  
>  
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

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

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