Adding comment and uncomment actions to a custom file type in NetBeans

Recently I have been asked to add comment and uncomment actions to the editor toolbar of a file type used by my company RCP Application based on NetBeans. This is how I did.

In my company application we had alredy defined a file type, but here I will create a new one with text/x-myfile as mime type.

So:

  • from the File menu select New Project… .
  • From Categories select NetBeans Modules.
  • From Projects select Module .
  • Press Next > .

1

Now I filled the information for my module, I did some Smurf programming using myfile prefix almost everywhere.

As in the picture you must insert

  • A Project Name: the name name of your project, and also the name of the folder that wil contain it.
  • Project Location: the directory where your project will be saved.
  • Since the module is just and example I chose to make it a Standalone Module using the current Development IDE as Platform. Usually I add it to my suite of modules.
  • Press Next > again.

2

  • The Code Name Base is an unique identifier for your module, it follows the same format of a Java package.
  • The Module Display Name is how the module will be presented to the user .
  • You can ignore the other settings.

3Now the module is ready  but empty, we need to add a new file type.

  • Expand the module node and the Source Package node and right click on the module main package, that is the module Code Name Base.
  • Select New and Other… .
  • In Categories select Module Development .
  • In File Types select File Type .
  • Select Next > .

4

  • Pick a custom MIME Type, I chose text/x-myfile ; there is a  x- prefix is because it is a custom one. You can also choose to create a custom xml file, in that case the format will by text/myfile-xml, but is it not our case.
  • by Filename Extension means the file will be recognized by its extensions, if it is an .xml file we can recognize the file by its root element.
  • Press Next > .

5

  • Choose a custom prefix for your classes and an icon that will represent the file.
  • We do not need Use Multiview so uncheck it.
  • Press Finish.

Now that we have our custom file, we can test it by right clicking on the module and select run. A new instance of NetBeans will be run with the new module installed. We can create a new file with the myfile extension and open it, the file will be recognized and opened with the defined icon.

5d

Now we will add the actions to comment and uncomment lines. First of all, we need to create a custom EditorKit.

We will create a new Java file in the module main package called MyFileEditorKit.java

This is its content:

package org.scantamburlo.myfile;

import javax.swing.Action;
import org.netbeans.modules.editor.NbEditorKit;

/**
 *
 * @author Enrico Scantamburlo
 */
public class MyFileEditorKit extends NbEditorKit {

 public MyFileEditorKit() {
 }

 @Override
 public Action getActionByName(String name) {
 if (commentAction.equals(name)) {
     // this will add at the beginning of the selected lines a !
     return new CommentAction("!");
 }
 if (uncommentAction.equals(name)) {
     // this will remove from the beginning of the selected lines a !
     return new UncommentAction("!");
 }
    return super.getActionByName(name);
 }

 @Override
 public String getContentType() {
     return "text/x-myfile";
 }

}

If we want to compile the file we must add two dependencies to the module:

  • Right click on the module
  • Select Properties
  • Select Libraries from Categories
  • Press Add…  and  add the a dependencies to the Editor module
  • Press Add…  and  add the a dependencies to the Editor Library module
  • Press OK

8

Then we must add a layer.xml to your module. This file is used to declare that NetBeans will use the previously declared EditorKit, there is a wizard for that.

  • Right click on the project node.
  • Select New and Other…
  • In Categories select Module Development .
  • In File Types select XML Layer .
  • Press Next > and Finish .

A new file will be created inside the project and opened.

Change its content to

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
<filesystem>
 <folder name="Editors">
 <folder name="text">
 <folder name="x-myfile">
 <file name="org.scantamburlo.myfile.MyFileEditorKit.instance" />
 <folder name="Toolbars">
 <folder name="Default">
 <file name="comment">
 <attr name="position" intvalue="21000"/>
 </file>
 <file name="uncomment">
 <attr name="position" intvalue="22000"/>
 </file>
 </folder>
 </folder>
 </folder>
 </folder>
 </folder>
</filesystem>

and run the module again.

10a

You will see two newly added actions on the editor toolbar. If you click comment a ! will appear at the beginning of the line, if you press uncomment the ! at the beginning of the line will be removed.

Leave a Comment

Your email address will not be published. Required fields are marked *