Uploaded image for project: 'NetBeans'
  1. NetBeans
  2. NETBEANS-5596

No syntax coloring with the Java Editor Kit

    XMLWordPrintableJSON

Details

    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.

      doc.putProperty(Language.class, JavaTokenId.language())

      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 :

       

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

       

      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() 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 :

      <file name="EditorKit.instance">
       <attr name="instanceClass" stringvalue="my.own.pckg.SyntaxColoringJavaKit"/>
       </file>

       

       

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

       

       Below the maven dependencies that I use in this example :

      <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>

      Attachments

        Issue Links

          Activity

            People

              Nicos109 Nicolas Baumann
              Nicos109 Nicolas Baumann
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 3h 20m
                  3h 20m