X

Geertjan's Blog

  • October 14, 2006

An Extendable Code Snippet Palette for Java Files

Geertjan Wielenga
Product Manager
One thing I've missed in NetBeans is a palette with code snippets for Java files. The Source Editor's Design view for Swing containers, such as a JFrame or a TopComponent, comes with its own palette with drag-and-drop Swing components. However, when you open a normal Java file, there's no palette. I'd like to be able to select a snippet of Java code, right-click in the Source Editor, and then say "Add to Palette", exactly like the module I created previously for adding HTML snippets to the HTML palette. In that case, however, a palette already existed, so all I had to do was work out how to let the user generate the files necessary for extending the palette. In the case of Java files, there is no palette. The first thing I needed to do was create a palette for a file type that already exists (i.e., not one over which I have control). So I created a new data loader for files with the ".java" file extension and then implemented the DataEditorSupport class, which connects a data object to an editor. (I had to make sure, in the manifest file, that my data loader loaded before the one for the 'normal' Java file, so that mine got preferential treatment.) Basically, I created a whole new editor. Not hard, since all the code is provided in the Component Palette Module Tutorial (the tutorial includes a ZIP file that you can download, providing all the code you need).

The new editor includes a palette. And, in exactly the same way as described for HTML files, palette items can be added to this palette. The module has the same shortcomings as the one for HTML palette items: you can't specify a label or an icon for the palette item, but to overcome that, you're given a list of item names and icons that you can select. Even though this is not very nice, it works well enough for when you're working with bits of code that you want to reuse elsewhere or store for safekeeping somewhere where it won't get lost (the palette is persisted across restarts and only if you remove the user directory will you lose your palette items).

So, first you make a selection in a Java file. Then you right-click in the Source Editor (or you could map the action to a keyboard shortcut) and choose "Add to Palette":

Next, you get a dialog box where you can view and modify the selected code. Here's also where you chose a label and an icon. (The labels provided are things like "class_1", "class_2", "method_1", "field_1", and there is a very long list of them.) This is what the dalog box looks like:

Note that the edit area is basically a Java editor. So, apart from the syntax highlighting, you can right-click it and get all the Source Editor's functionality for Java files, such as "Reformat Code". Maybe it would be nice if the module could work out what is under the cursor and then assign an appropriate name. But, there's no guarantee that the user will only want to create snippets of neat little methods and fields. Maybe big chunks, encapsulating a variety of methods, fields, and so on, would be selected by the user, so that's why I decided it was best to just let the user select a label, rather than getting the module to do that for you.

Then you click OK. And now you have a new item in your palette:

When you drop that item in a Java file, the IDE will paste the code snippet that you selected at the start of this procedure. If someone is interested in this module, please let me know. I have it available for NetBeans IDE 5.0, but making it ready for 5.5 should not be much work.

Join the discussion

Comments ( 5 )
  • Chuck Davis Saturday, October 14, 2006
    Geertgan:
    This is a very handy feature. Just one request, allow us to create a name of our choosing for the additions! This would be exceptionally handy for doing live demos -- you can have sections of code ready to drop in before their very eyes!
    Further, get the NB team to include this as a standard feature of the new NB 6.0 editor!
  • Geertjan Saturday, October 14, 2006
    Hi Chuck, if I could do it, I would. There's a problem with how I've implemented this, basically all I do is generate an XML file of a very specific type (doc type "editor_palette_item"), which specifies the name and icon in the Bundle.properties file... so that's where the problem is, because the user can't add to the module's Bundle, only (and that's my workaround) select from entries that I put in there. A shortcoming in the API, I plan to create an issue for this. I know of another way, but that's more work, hope to do it soon. Thanks for the encouragement. You're right, this should be a standard feature in NetBeans.
  • Geertjan Saturday, October 14, 2006
    By the way, if you want to drop code in before someone's eyes in demonstrations, you can use code templates. But I think a pallete would be cooler. There's something about drag and drop that is pretty cool, and also more fun than code templates.
  • kovica Sunday, October 15, 2006
    Well, you had this for quite some time with my module, Code Snippets (kovica.googlepages.com). It's true it did not work via Palette.
  • Geertjan Sunday, October 15, 2006
    Hi Kovica. Yes, you're right. I'e actually used your module quite a bit and its great. I highly recommend it.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.