beaTunes News

Thursday, December 18, 2008

Adding a custom menu item

beaTunes LogoAs I mentioned before, beaTunes 2 will be a lot more friendly towards developers who want to write plugins. As a matter of fact, many of the standard components you can see in the UI are plugins already. If you're curious, just unzip the beaTunes.jar (on OS X open the app bundle and navigate to Contents/Resources/Java - that's where all the jars are, on Windows, just go to the apps lib directory) and checkout the file plugin.xml in the META-INF directory. That file is read by beaTunes and the classes in it are instantiated and registered with the plugin manager. For you that basically means, you have to include a plugin.xml in your own plugin jar file.

But this post wasn't supposed to be about plugin.xml, it was supposed to be about how to get a new menu item into beaTunes. And that is actually pretty easy.

Obviously, a menu item is backed by an action, in the case of beaTunes it should be a com.tagtraum.beatunes.action.BaseAction. So, just subclass BaseAction. Because getId() and actionPerformed() are abstract, you will have to implement them. getId() is supposed to return a String id. Make something up. actionPerformed() does whatever you want your action to do.

public String getId() {
return "com.mycomp.action.somethingfancy";
}

public void actionPerformed(final ActionEvent e) {
// do something grand!
}

Now, obviously you want your action to have a name. To give it one, implement a constructor like this:

public MyAction(final BeaTunes beaTunes) {
super(beaTunes);
putValue(Action.NAME, "My Menu Item");
// add other action properties as you see fit
}

At this point you have an action with a name, an id and some action method, but beaTunes doesn't know yet where to put the thing. To tell it where to show it, you have to override the following method:

public ActionLocation[] getActionLocations() {
// install the action in just one location - in this case the tool menu
return new ActionLocation[] {
new AbsoluteActionLocation(BeaTunesUIRegion.TOOL_MENU)
};
}

The sample code above installs the action in the tool menu. Now, unfortunately that does not give you any control over where exactly in the tool menu the action will be placed. To get more control, you can use a RelativeActionLocation. Like this:

new RelativeActionLocation(BeaTunesUIRegion.EDIT_MENU,
RelativeActionLocation.RelativePosition.BEFORE, "tree.show.hide")

This location installs an action in the edit menu, right before the action with the id tree.show.hide.

Now compile your action, put it in a jar and add a META-INF/plugin.xml-file that looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<plugins>
<plugin class="com.yourcomp.MyAction"/>
</plugins>

Then place the jar into the beaTunes plugin directory and start beaTunes.

Done!

Note: This code will only work with beaTunes 2. It has been updated for EA16.

Labels: ,

0 Comments:

Post a Comment

<< Home