X

Geertjan's Blog

  • November 25, 2005

Making the Component Palette Work for Other File Types

Geertjan Wielenga
Product Manager
Today I made the Component Palette work for Manifest files. Below you see the result—I have two code snippets in the Component Palette. I can drag-and-drop them (or double-click them) and then a key is inserted in the Manifest file. Here's what it looks like for me now (the tooltip shows what code will be inserted in the Manifest file):

The key to everything is this method in ManifestEditorSupport.java (especially note the bits in bold):

void associatePalette(ManifestEditorSupport s) {
DataObject dataObject = s.getDataObject();
if (dataObject instanceof ManifestDataObject) {
try {
PaletteController pc = ManifestPaletteFactory.getPalette();
Lookup pcl = Lookups.singleton(pc);
Lookup anl = getActivatedNodes()[0].getLookup();
Lookup actionMap = Lookups.singleton(getActionMap());
ProxyLookup l = new ProxyLookup(new Lookup[] { anl, actionMap, pcl });
associateLookup(l);
} catch (IOException ioe) {
//TODO exception handling
ioe.printStackTrace();
}
}
}

So, broadly, what you need to do is the following (this is partly from an e-mail from Libor, the guy who associated the Component Palette with JSP files and HTML files for NetBeans IDE 5.0):

  1. Implement the palette files, required by the NetBeans Palette API.
  2. Register the palette in the layer.xml file, as shown in previous blog entries.
  3. Make sure that the palette initialization is done correctly in the XXXEditor.associatePalette() method, as shown in the code above. The essential part is obtaining PaletteController from XXXPaletteFactory and associating it with the file type's lookup.

When you're working on this, the following are the main Java files (highlighted below) you'll be working with:

Unfortunately, I strongly doubt that it's possible to associate the Component Palette with existing file types. In my case, it was possible, because I had created support for Manifest files myself. I basically had to create the palette files and then modify the ManifestEditorSupport.java class to make them work together. But if, for example, you wanted to associate the Component Palette with Java files, how would you go about modifying the related EditorSupport.java class? I doubt that that is possible. (Correct me if I'm wrong, Libor.) This means that for existing file types (i.e., ones over which you have no control), you'll have to wait for the next release (or an addition to the Update Center). An alternative might be to check out the NetBeans sources, modify the appropriate file, and then rebuild the IDE...

By the way, you might have noticed that there's something odd about the Manifest file shown in the first illustration above—there's no syntax highlighting! Somehow, by associating the Component Palette with Manifest files, I disassociated syntax highlighting... Weird! Once I've sorted out this problem, and cleaned up my code a bit, I will write a tutorial that covers everything there is to know about the Component Palette, especially how to create NetBeans modules containing code snippets. However, there'll be nothing surprising in that tutorial if you've read the last few blog entries in this blog...

Be the first to comment

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