Geertjan's Blog

  • October 5, 2006

Extending a Component Palette. Help Needed! (Part 1)

Geertjan Wielenga
Product Manager
I think the IDE's HTML Component Palette (or any other) could be a lot more useful if one were able to right click inside the editor and then choose "Add to Palette". In fact, something exactly like this:

When I click that, an XML file with editor_palette_item as its root element is created in the user directory's HTMLPalette/HTML folder:

protected void performAction(Node[] activatedNodes) {
//Here we're getting the currently selected text:
JTextComponent editor = Registry.getMostActiveComponent();
selection = editor.getSelectedText();
//Here we're calling the dialog for setting the label and content:
Custom a = new Custom(this);
//Here we're not creating anything,
//just initializing variables:
htmlFolder = Repository.getDefault().getDefaultFileSystem().getRoot().
String paletteCategory = "HTML";
htmlCategoryFolder = Repository.getDefault().getDefaultFileSystem().getRoot().
getFileObject("HTMLPalette/" + paletteCategory);
//Here we're creating the file with the specified label and content:
try {
htmlFile = htmlCategoryFolder.getFileObject(getLabel(), EXTENSION);
if (htmlFile==null) {
htmlFile = htmlCategoryFolder.createData(getLabel(), EXTENSION);
lock = htmlFile.lock();
OutputStream fout = htmlFile.getOutputStream(lock);
OutputStream bout= new BufferedOutputStream(fout);
OutputStreamWriter out = new OutputStreamWriter(bout, "UTF-8");
out.write("<?xml version=\\"1.0\\" ");
out.write("<!DOCTYPE editor_palette_item PUBLIC ");
out.write("\\"-//NetBeans//Editor Palette Item 1.0//EN\\" ");
out.write("<editor_palette_item version=\\"1.0\\">\\r\\n");
out.write(" ]]>\\r\\n");
out.write("<description localizing-bundle=\\"org.netbeans." +
out.write("tooltip-key=\\"My_Hint\\" />\\r\\n");
out.flush(); // Don't forget to flush!
} catch (IOException ex) {

This is the dialog box that shows up:

You can tweak the selected tags here. You can also set a label to be displayed in the Component Palette. However, that's exactly where I need help! The label and the tooltip are set in the editor_palette_item file via a Bundle.properties file! It would be great if I could create such a file on the fly, in the same directory as where I create the editor_palette_item file, and then refer to it inside the file. Same with the icons. They're set by means of something like org/mymodule/mypackage/myicon16.png. So, since the label, tooltip, and icons are set within the module, I can't let the user set these! Bummer. If anyone knows how to get around this, please let me know! That's the only thing that's missing in this module. If you look at the code above, I get around this issue by hardcoding the label and tooltip's keys into the file, which means that all the added palette items have the same name... Again, if someone has any tips or advice on this point, drop me a line!

For the rest, it works. You can add items to the HTML Palette's HTML category (would be easy to extend it to the HTML Forms category, or to let the user add their own categories). The reason why the editor_palette_item file must be created is that this is the only type of file that is acceptable for dragging and dropping in the Source Editor. When I do that, I now get the tags that I selected in the editor. Pretty cool, I think, despite the current shortcoming.

In other news. Come to the NetBeans User Group Munich event on Tuesday, 17 October! Roumen will be there, as well as Tim Cramer and several users from the community will be doing presentations too. Looks like I'll be there also.

Join the discussion

Comments ( 1 )
  • Craig Marshall Friday, February 29, 2008

    I have been using this example in th RCP book to populate the palette with columns of a database table (which works well) but when the palette gets reset the 'dynamic' palette items are lost. I have implemented a custom reset action to rebuild them, but was wondering if it was possible to make the file objects read only so they dont get removed in a normal reset. I couldnt find documentation on making the file and folder objects read only...... any advise you can offer?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.