X

Geertjan's Blog

  • November 24, 2005

Expanding the NetBeans Component Palette without Creating a NetBeans Module

Geertjan Wielenga
Product Manager
In a few recent blog entries, I described how to really quickly create a NetBeans module containing code snippets, such as <p> tags (or even more extensive tag combinations). However, not everyone wants to do it that way—maybe all you want to do is add code snippets to the Component Palette, but not share them with anyone else (hmmm, sounds a bit selfish, doesn't it). For example, at the end of Simple Extension of NetBeans Component Palette, Kovica asks:

What do you think about adding snippets like this: you edit a file, select a text, right click and select Add to palette. Then a dialog opens asking you into what category to add selected text. I hope you can and will do this. Please.

So, I talked to Libor (the guy who implemented the Component Palette for JSP files and HTML files) and he tried to leave a comment in response to Kovica's question, but the comment thing in this blog didn't work for him. So he sent me an e-mail with instructions for adding code snippets. It's not a perfect solution by any means, and it will be improved in future releases. But here's how you can do it, despite the lack of 'official' support for doing this.

  1. Open a JSP page. The Component Palette should open automatically. If it doesn't, choose Ctrl-Shift-8.
  2. Right-click in the Component Palette and choose 'Create New Category'. Create a new category. For example, I called mine "Geertjan's category".
  3. Right-click an item from one of the other categories, choose Copy, and then paste the item in your new category.
  4. Now close the IDE.

Whenever you see a set of instructions that tells you to close the IDE, you know that you're about to do something unorthodox... So, here it is:

  1. Go to the NetBeans user directory.
  2. Go to this directory within the NetBeans user directory:

    config/JSPPalette/[new category]/[clip definition file]

    For example, here's the place where mine is found, within the category "Geertjan's category":

  3. Inside the XML file that you find in the directory above, do the following:

    • Completely delete the class tag.

    • In the place where the class tag was found, paste the following (if you want to add a code snippet that inserts a <br/> tag):
      <body>
        <![CDATA[
          <br/>
        ]]>
      </body>
  4. Save and close the file.
  5. Restart the IDE. You will now see your code snippet in the Component Palette when you open a JSP file. When you drag and drop it (or double-click it), the tag that you created manually outside the IDE will be inserted at the cursor in the Source Editor.

Unfortunately, the name, tooltip, and icons will remain unchanged, which means that they don't fit with the new tag content. On the other hand, using this round-about approach, you can very quickly add many new code snippets to the Component Palette. Just continue copying that XML file that was generated for you in the NetBeans user directory, and your Component Palette will quickly expand to include a variety of your favorite code snippets.

Join the discussion

Comments ( 5 )
  • Kovica Thursday, November 24, 2005
    Cool!
    Is it possible to have palette opened also for a java file? I'm asking this, because if I have a java file opened and then open also Palette there are no categories, no items in that palette. But if I open JSP file and then Palette I can see categories and item in the palette.
  • Geertjan Friday, November 25, 2005
    Great question. I'm looking into this at the moment. I'll blog about it soon...
  • guest Tuesday, February 21, 2006
    :D
  • Konrad Monday, March 27, 2006
    Just wondering what the current status of being able to add self-created code snipets to .java or .xml files? I just came across some very useful Ant code when creating, or rather testing, web services in NetBeans5.x:
    <!-- Overrides build-impl.xml target to start server and
    generate client artifacts before building test. -->
    <target name="-pre-compile-test">
    <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
    <classpath path="${javac.classpath}"/>
    </taskdef>
    <!-- Use "debug" or "run" here. -->
    <!--antcall target="run"/-->
    <antcall target="debug"/>
    <echo>running wsimport</echo>

    <wsimport
    debug="true"
    keep="true"
    destdir="test"
    package="my.sample.test.generated"
    wsdl="http://localhost:8080/SampleApp/hello?wsdl"/>
    </target>
    Of course, some values need to be updated, but this code snippet would be great to have in the NetBeans palette for .xml file types.
    Thanks! :)
  • Konrad Monday, March 27, 2006
    Actually, just came across this from the NB mailing list:
    http://freeweb.siol.net/kovica/
    scroll down to the part about "Code Snippets".
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.