NetBeans Modules for Dummies (Part 6)

In this final installment of the unofficial "NetBeans Modules for Dummies" guide, we're going to apply the principles of yesterday's installment to the Anagrams sample that is distributed with the NetBeans IDE. We'll start with a standard J2SE application and finish with an NBM. The NBM will give us a new menu called "Games" with a submenu called "Anagrams". Clicking on "Anagrams" will pop up the Anagrams sample. Of course, we could choose to install the NBM either in the NetBeans IDE or in the NetBeans Platform. (Maybe we could even make a whole bunch of games, then turn them into NBMs and install them in the NetBeans Platform and end up with our very own Games Center.)

  1. Get the J2SE sample. Choose File > New Project > Samples > General > Anagram Game. Click Next and Finish.
  2. Modulerize the sample. In Anagrams.java, two methods need to be changed: exitMenuItemActionPerformed and exitForm. Instead of closing the system when you close the module (which is the behavior of an application), you just want to dispose of the module when you close it. Hence, replace the content of these two methods with the following:

    setVisible(false);
    dispose();
  3. Create the module's supporting files.

    • The Action class. Right-click the Source Packages node and choose New Java Class. Name your class ShowAnagramsAction and type com.toy.anagrams in the Package field. Click Finish. Replace the code with the following:

      public class ShowAnagramsAction extends CallableSystemAction {
          
          public HelpCtx getHelpCtx() {
              return HelpCtx.DEFAULT_HELP;
          }
          
          public String getName() {
              return "Anagrams";
          }
          
          public void performAction() {
              Anagrams anagrams = new Anagrams();
              anagrams.setBounds(400, 200, 400, 220);
              anagrams.show();
          }
      }

      Click Alt-Shift-F in the Source Editor. You'll get error messages. Right-click the Libraries node, choose Add Library, and create a library that contains three jars: openide.jar, openide-loaders.jar, and nbantext.jar. (We don't need the latter in this step, but it's good to have it available already.) Once you've added these libraries, which you should be able to find in your distribution or in the Update Center (in the case of nbantext.jar), and click Alt-Shift-F again, the error messages disappear.

    • The Layer XML file. Right-click the Source Packages node and create an XML Document called layer. Put it in a package called com.toy.anagrams.resources. Click Finish. Replace the code with the following:

      <?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="Games">
              <file name="com.toy.anagrams.ShowAnagramsAction.instance"/>
         </folder>    
       </folder>
      
      </filesystem>
      

    • The Manifest file. Right-click the Project node and choose New > File/Folder > Other > Empty File. Call the file manifest.mf and click Finish. Replace the code with the following:

      Manifest-Version: 1.0
      OpenIDE-Module: anagrams/1
      OpenIDE-Module-IDE-Dependencies: IDE/1 > 4.0 
      OpenIDE-Module-Layer: com/toy/anagrams/resources/layer.xml
      OpenIDE-Module-Specification-Version: 1.0

      Now add the following line to the nbproject/project.properties file:

      manifest.file=manifest.mf
  4. Build the module.

    • Get the Ant scripts. Add the following two scripts to your build.xml file:

      <target name="create-xml-file-for-module" description="Create XML File.">
        
        <!-- Create directory for JAR. -->
        <mkdir dir="netbeans/${modules.dir}"/>
        
        <!-- Create directory for JAR's XML file. -->
        <mkdir dir="netbeans/${modulexml.dir}"/>
        
        <!-- Copy the JAR (therefore, you must build the JAR first!). -->
        <copy file="${dist.jar}"
              todir="netbeans/${modules.dir}"/>
      
        <!-- Create the XML for the JAR (therefore, must have an appropriate manifest.mf first!). -->
        <taskdef name="createmodulexml" 
             classname="org.netbeans.nbbuild.CreateModuleXML" 
             classpath="${javac.classpath}" />
        <createmodulexml xmldir="netbeans/${modulexml.dir}">
          <enabled dir="netbeans/${cluster.dir}">
            <include name="modules/${module.name}.jar"/>
          </enabled>
        </createmodulexml>
      
      </target>

      <target name="create-nbm" description="Create NBM File.">
      
      <taskdef name="genlist" classpath="${javac.classpath}" 
                              classname="org.netbeans.nbbuild.MakeListOfNBM"/>
      
      <genlist outputfiledir="netbeans/${cluster.dir}" module="modules/${module.name}.jar">
      <fileset dir="netbeans/${cluster.dir}">
      	<include name="modules/${module.name}.jar"/>
      	<include name="config/Modules/${module.name}.xml"/>
      </fileset>
      </genlist>
      
      <taskdef name="makenbm" classpath="${javac.classpath}" classname="org.netbeans.nbbuild.MakeNBM"/>
      
      <makenbm file="${module.name}.nbm" 
       needsrestart="false" 
         productdir="netbeans/${cluster.dir}" 
             module="modules/${module.name}.jar" 
           homepage="http://www.netbeans.org" 
       distribution="http://www.netbeans.org"/>
      
      </target> 
    • Define target properties. In part 4 of this series, the strict requirements of some versions of the nbantext.jar were described. So, some of the properties below -- not all -- are only there to prevent build errors occurring (when nb.system.dir isn't set, for example). Define them in the nbproject/project.properties file:

      cluster.dir=Anagrams
      modules.dir=${cluster.dir}/modules
      modulexml.dir=${cluster.dir}/config/Modules
      module.name=Anagrams
      nb.system.dir=config
    • Add the Ant scripts to the build process. When you include the little target below, you don't have to run the above two targets yourself. This is because the IDE has a number of targets as placeholders, which you can fill in with whatever you like. This target, for example, is automatically run after a project is built (which means that the JAR is available, which is what you need when creating an NBM). Here it is. Just add it to the build.xml file:

      <target name="-post-jar" depends="create-xml-file-for-module,create-nbm" />

    • Build the project. Right-click the project and choose Build Project. The messages in the Output window should end with something like the following:

      Enabled modules: [anagrams/1]
      create-nbm:
      Generating information for Auto Update...
      Building jar: C:\\IDEModules\\AnagramGame2\\anagrams.nbm
      jar:
      BUILD SUCCESSFUL (total time: 0 seconds)

  5. Install the module. Choose Tools > Update Center (either in the IDE or in the Platform), click the radiobutton at the bottom of the dialog box, and browse to your NBM. After some more clicking, your NBM will be installed and in the menubar you will see the following:

    You can re-order the menu and the menu item as discussed in previous blog entries. When you click the menu item, the Anagrams sample appears. (And you are able to close it, because of what you did in step 2 above.) Congratulations.

So now, since you know the basics of NetBeans Modules, you can no longer be called a dummy. Therefore, this series of short tips and articles aimed at NBM beginners has now come to an end. I will blog more about the things I discover in the NBM area, because it's all really interesting and relatively straightforward. Of course, the modules discussed here have been quite simple -- I'd like to find out more complex things such as, for example, how to create a node in the Runtime window, how to write a module to start a server, how to write a template and install it via a module, and the many other magical things the Layer XML file can do for me. But that's also one of the really cool things about NetBeans -- there's always something more to learn...

Comments:

Thought I would pass this along since others may run into this. It appears to be an issue on Linux and perhaps on Unix too. Likely not an issue for Windows platforms. The module.name in the project.properties file should be spelled with a lower 'a' instead of an uppercase 'A'. That is, the 'module.name' should listed as "module.name=anagrams". If you see a message like "Can't get manifest attributes for module jar file..." when trying to build the plug-in/module. This is likely the problem.

Posted by Charlie Hunt on May 11, 2005 at 07:53 AM PDT #

Thanks for passing this on! Great catch.

Posted by Geertjan on May 11, 2005 at 01:01 PM PDT #

I am trying to place a property file in the class path of my project in NetBeans5.0. I am not able to do it please advice.

Posted by Sudeep on May 25, 2006 at 03:11 AM PDT #

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