X

Geertjan's Blog

  • October 13, 2006

Extend the Palette!

Geertjan Wielenga
Product Manager
"Extending" a palette (i.e., one that you make yourself, not one provided by someone else) encapsulates four areas—the menu items in the palette's contextual menu, a category's contextual menu, and an item's contextual menu. But, also, the Palette Manager itself (under the Tools menu or by right-clicking in the palette, a category, or item) can be extended, by adding additional buttons to it. Each palette has its own manager, which requires zero programming on your part, because it is just part and parcel of palette support in the IDE.

The NetBeans API class involved here is PaletteActions. When you associate a palette with a TopComponent (or editor), you always do it like this:

controller = PaletteFactory.createPalette( "root", new MyPaletteActions(), null, null );

The two "nulls" above are placeholders for the palette filter (discussed recently) and the palette drag and drop handler (to be discussed soon). The "MyPaletteActions()" is a required argument. It creates a new instance of an implementation of aforementioned PaletteActions class. Initially, just to meet minimum requirements, you could create a skeleton implementation like this:

private static class MyPaletteActions extends PaletteActions {
public Action[] getImportActions() {
return null;
}
public Action[] getCustomPaletteActions() {
return null;
}
public Action[] getCustomCategoryActions(Lookup lookup) {
return null;
}
public Action[] getCustomItemActions(Lookup lookup) {
return null;
}
public Action getPreferredAction(Lookup lookup) {
return null;
}
}

However, when you get more adventurous, you would need to do nothing more than run through the New Action wizard and then create a new instance of the action, using one of the placeholders above. For example:

public Action[] getImportActions() {
return new Action[] { new SomeAction() };
}

In this particular case, you have now added a new button to the Palette Manager! That's all you need to do. For example, here's my new button:

In the case of categories and items, you can pass the selected category or item as a Node (thank you, Nodes API) to the action, so that the action can do something with the selected category or item. For example:

public Action[] getCustomItemActions(Lookup lookup) {
Node itemNode = (Node)lookup.lookup( Node.class );
if( null != itemNode ) {
return new Action[] { new CustomizeItemAction( itemNode ) };
}
return null;
}

What are the other methods good for? Well, here's an overview:

Just think, yesterday I didn't even know that there are three different contextual menus in the palette. Today, I'm explaining how to extend all of them. (I also had no idea that one can add buttons to the Palette Manager.) Pretty cool.

In other news. Component based web development in NetBeans! What?! How?! Read all about it in this new tutorial: Component Based Web Development in NetBeans IDE.

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.