NetBeans Modules for Dummies (Part 1)

Let's pretend we haven't heard of Maven. Let's not use yesterday's blog entry to install Mevenide. Let's leave all that for another day. We'll appreciate Maven and Mevenide much more once we see how much more work we need to do without them. For now, let's just use our faithful friend NetBeans IDE together with Ant to create a simple NetBeans Module (NBM). You'll see it's actually pretty easy. At the end of the story, we'll use the Update Center to install the NBM in the NetBeans IDE. The NBM won't do much -- it will just add a menu, submenu, and menu item to the IDE's menubar. When you click the menu item you'll get a JOptionPane with a message. Still, despite its simplicity, we'll have a basic understanding of the process and have laid the basis for more advanced experiments.

  1. Let the IDE create the NBM structure. Click Ctrl-Shift-N. In the New Project wizard, select the "General" category and then the "Java Application" project. Name the project as follows:

    org.netbeans.modules.test

    Before clicking Finish, specify that the following main class should be created:

    org.netbeans.modules.test.TestAction
  2. Code the NBM. Click Finish and use the Source Editor to change the class so that it looks as follows:

    public class TestAction extends CallableSystemAction {
        
        public HelpCtx getHelpCtx() {
            return HelpCtx.DEFAULT_HELP;
        }
        
        public String getName() {
            return "Start Test";
        }
        
        public void performAction() {
            String msg = "Test succeeded...";
            JOptionPane.showMessageDialog(null,msg);
        }
    }

    Click Alt-Shift-F in the Source Editor to generate import statements. You'll notice that some packages are still needed. Right-click the project's Libraries node and add the following JARs:

    • netbeans-installation-directory/platform5/core/openide.jar
    • netbeans-installation-directory/platform5/core/openide-loader.jar

    Now click Alt-Shift-F again. The following two import statements should be automatically added to your file:

    import org.openide.util.HelpCtx;
    import org.openide.util.actions.CallableSystemAction;

  3. Hook the code to a menu. Add a directory called resources containing a file called mf-layer.xml in src/org/netbeans/modules:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE filesystem
     PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN"
            "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
    <filesystem>
    
     <folder name="Menu">
       <folder name="Test">
         <folder name="Subtest">
            <file name="org-netbeans-modules-test-TestAction.instance"/>
         </folder> 
       </folder>    
     </folder>
    
    </filesystem>
    

    You can play around with the Menu section above. For example, do you want to add menus and submenus to invoke applications from the IDE? Simply create folder sections as shown above and hook them up to the Java classes that contain the code (also shown above). If you want to add a menu item to an existing menu, specify the name of the existing menu at the highest level and the menu items will arrange themselves below it. For example, if you put "View" instead of "Test" above, the "Subtest" menu item will be added below the "View" menu in the IDE. There's much more you can do with this file, but this is enough for now.

    The structure of your project in the Files window should now be as follows:

  4. Create the JAR. In the Projects window, right-click the project and choose Build Project. (Alternatively, use the File menu to set the project as the main project and then you can click F6 to build the project, even in the Files window.)
  5. Create the JAR's XML file. Add the following to the build.xml file:

    <target name="create-xml-file-for-module" 
            depends="compile,jar" description="Create JAR">
     
      <!-- Create directory for JAR. -->
      <mkdir dir="NetBeans/${ant.project.name}"/>
      
      <!-- Create directory for JAR's XML file. -->
      <mkdir dir="NetBeans/${ant.project.name}/config/Modules"/>
      
      <!-- Copy the JAR (therefore, you must build the JAR first!). -->
      <copy file="${dist.jar}"
            todir="NetBeans/${ant.project.name}/modules"/>
      
      <!-- Create the XML for the JAR. -->
      <taskdef name="createmodulexml" 
                  classname="org.netbeans.nbbuild.CreateModuleXML" 
                  classpath="${run.classpath}" />
      <createmodulexml xmldir="NetBeans/${ant.project.name}/config/Modules">
        <enabled dir="NetBeans/${ant.project.name}">
          <include name="modules/${ant.project.name}.jar"/>
        </enabled>
      </createmodulexml>
    
    </target>

    Now run the target. You'll get this error:

    build.xml:79: taskdef class org.netbeans.nbbuild.CreateModuleXML cannot be found

    Right-click the project's Libraries node and add the nbantext.jar. When you do so, the IDE adds it to the javac.classpath property in the project.properties file, so that you can use it transparently in the next step. (If you don't have it, you can get it from the Update Center.)

    Now run the target again. You'll get this error:

    build.xml:80: Missing manifest tag "OpenIDE-Module".

    Or you might get an error telling you that the project is not a module.

    Add this to the manifest.mf file:

    Manifest-Version: 1.0
    OpenIDE-Module: org.netbeans.modules.test/1
    OpenIDE-Module-IDE-Dependencies: IDE/1 > 4.0 
    OpenIDE-Module-Layer: org/netbeans/modules/resources/mf-layer.xml
    OpenIDE-Module-Specification-Version: 1.0

    Now run the target again and everything should be well.

  6. Create the NBM. Add the following to the build.xml file:

    <target name="create-nbm" depends="create-xml-file-for-module" description="Create NBM.">
    
    <taskdef name="makenbm" classpath="${run.classpath}" 
           classname="org.netbeans.nbbuild.MakeNBM"/>
    
    <taskdef name="genlist" classpath="${run.classpath}" 
           classname="org.netbeans.nbbuild.MakeListOfNBM"/>
    
    <genlist outputfiledir="NetBeans/${ant.project.name}" module="modules/${ant.project.name}.jar">
    <fileset dir="NetBeans/${ant.project.name}">
    	<include name="modules/${ant.project.name}.jar"/>
    	<include name="config/Modules/${ant.project.name}.xml"/>
    </fileset>
    </genlist>
    
    <makenbm file="${ant.project.name}.nbm" 
        needsrestart="false" 
          productdir="netbeans/${ant.project.name}" 
    	 module="modules/${ant.project.name}.jar" 
           homepage="http://www.netbeans.org" 
       distribution="http://www.netbeans.org"/>
    
    </target>

    Now run the target. The genlist section above gives me a java.lang.NullPointerException, but does the required job anyway -- it creates update_tracking/org-netbeans-modules-test.xml. However, it prevents the NBM from being generated. So, remove the genlist section and run the target again. You'll get this error:

    build.xml:91: Can't read nb.system.dir property

    Add this to the project.properties file:

    nb.system.dir=config

    (Update: In part 4 of this "NetBeans Modules for Dummies" pseudo-series the problems relating to nb.system.dir and the java.lang.NullPointerException are discussed and solved.)

    Now run the target again. Look in your Files window and you will see the NBM file that Ant has created for you:

  7. Install the NBM. In the IDE, choose Tools > Update Center. Select the radiobutton at the bottom of the dialog box so that you can browse to your NBM file. After you've installed it, there'll be a new menu in the IDE, next to the Help menu. It will be called Test. Its submenu is called Subtest and when you click on Start Test you'll get a message that says "Test succeeded..."

    If you want to play around with the above structure, go back to step 3, modify the layer XML file, build the project, run the two targets again, remove the module from the IDE (go to Tools > Options > IDE Configuration > System, expand the Modules node and delete the module), and install the new version. You can also use the Options window to re-order the menu in the menubar, and any menu items within the menu. To do this, go to IDE Configuration > Look and Feel, and then right-click the Menu Bar node or any of its subnodes.

With Mevenide, the creation part of step 3 and all of steps 5 - 6 are obsolete. Mevenide structures the NBM project, creates all necessary files, and uses Maven to carry out compilation and generation tasks. As a result, your only contribution to the process is the coding of the Java classes (step 2) and the adding of elements to the layer XML file (step 3).

Comments:

Does it means that the abuliding 'Module Wizard' will base on Mevanide?
If not. What is the difference between this and the home-grown one?
A duplicate work?

We are welcoming your detail informations, as this detail the process of NBMs.
BTW. why didn't supply a preview function to assist me to comment.(maybe my shame!)

-pprun

Posted by pprun on May 07, 2005 at 04:17 PM PDT #

Hi pprun, I'm not 100% sure about the relationship between Mevenide and 4.2 development plans in the NBM area. However, my understanding is that if you want to use Maven in/from the IDE, you should use Mevenide. Thanks for your comments and sorry for there not being a Preview function -- I didn't make this blog, I only write in it... :-)

Posted by Geertjan on May 09, 2005 at 06:48 AM PDT #

I guess I am to dumb I got no imports found on the following types : callablesystemaction and helpctx I am using netbeans 4.1

Posted by Timothy on June 05, 2005 at 04:17 AM PDT #

that was pressing Alt-Shift-F on #2

Posted by Timothy on June 05, 2005 at 04:22 AM PDT #

Hi Timothy, Are you sure you added openide.jar and open-ideloader.jar, as explained in step 2 above? To check, expand the Libraries node and see if you can see them there. That should resolve your problem. If not, please leave a note again! Geertjan

Posted by Geertjan on June 07, 2005 at 05:50 PM PDT #

hi can u tell me how to garbage collect a topcomponent on close

Posted by abhisek on February 20, 2008 at 03:08 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today