Geertjan has written some excellent blog entries about adding stuff to the palette (especially this one was very helpful). I wanted to create something similar to allow users to add their own fields, variables and parameters to the palette of my jasperreport editor, but I wanted to invoke this from the popupmenus of the palette. I couldn’t find anything on how to do so via the layer.xml. But when I had a look at the Palette API tutorial , I figured out how to do it. It’s really very simple. The PaletteController’s constructor takes a PaletteActions Object as argument, which can be used to add custom actions:
8<————————————————————>8
public static PaletteController getPalette() {
//create the palette
if( null == thePalette ) {
try {
thePalette = PaletteFactory.createPalette( “JasperPalette”, new MyActions() );
} catch (IOException ex) {
ex.printStackTrace();
}
}
return thePalette;
}
private static class MyActions extends PaletteActions {
public Action[] getImportActions() {
return null;
}
public Action[] getCustomPaletteActions() {
return null;
}
public Action[] getCustomCategoryActions(Lookup lookup) {
return new Action []{
new AddFieldAction() // custom action
};
}
public Action[] getCustomItemActions(Lookup lookup) {
return null;
}
public Action getPreferredAction(Lookup lookup) {
return null;
}
} 8<————————————————————>8
My Paletteitems are described in simple properties files, so writing the files is extremely simple. I used Geertjan’s code as a template for this:
8<————————————————————>8
public void performAction() {
FieldJPanel panel = new FieldJPanel();
DialogDescriptor descriptor = new DialogDescriptor(panel,"New Field" );
FileObject fields =Repository.getDefault().getDefaultFileSystem().getRoot().
getFileObject("JasperPalette/Fields");
try {
FileObject relFile = fields.getFileObject("testfile", EXTENSION);
if (relFile==null) {
relFile = fields.createData("testfile", EXTENSION);
}
FileLock lock = relFile.lock();
InputStream in = AddFieldAction.class.getClassLoader().getResourceAsStream("de/genomatix/jasperreporteditor/ui/action/resources/textfield_template.rel");
Properties properties = new Properties();
properties.load(in);
OutputStream fout = relFile.getOutputStream(lock);
properties.store(fout,"");
fout.close();
lock.releaseLock();
} catch (IOException ex) {
ex.printStackTrace();
}
}
8<————————————————————>8
The Action shows up in the popupmenu, when you right-click a category name in the Palette.
This minimal example simply copies the file. In the original I show a dialog first to allow users to customize some properties.