On NetBeans.org there’s a nice tutorials that shows how to create a palette for your Editor. If you follow the steps in that tutorial, it will give you a palette that is active when your file is opened. That’s nice, but in my custom FileType Editor I want a palette that should be only activated, when the Visual Editor is activated.
You can achieve that by creating a PaletteController factory class like in the example, but without the annotation for registering it. Here’s my class:
public class Box2DPaletteController {
private static Box2DPaletteController palette = null;
public static PaletteController createPalette() {
try {
if (null == palette) {
return PaletteFactory.createPalette(
//Folder:
"Box2DPalette",
//Palette Actions:
new PaletteActions() {
@Override public Action[] getImportActions() {return null;}
@Override public Action[] getCustomPaletteActions() {return null;}
@Override public Action[] getCustomCategoryActions(Lookup lkp) {return null;}
@Override public Action[] getCustomItemActions(Lookup lkp) {return null;}
@Override public Action getPreferredAction(Lookup lkp) {return null;}
},
//Palette Filter:
null,
//Drag and Drop Handler:
new DragAndDropHandler(true) {
@Override public void customize(ExTransferable et, Lookup lkp) {
}
});
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
}
To activate the Palette for my Visual Editor, which is created by a MultiViewElement, I just need to put it into that MultiViewElements Lookup, so I modify the constructor to initialize a palette:
private PaletteController paletteController;
public Box2DVisualElement(Lookup lkp) {
obj = lkp.lookup(Box2DDataObject.class);
assert obj != null;
name = obj.getPrimaryFile().getName();
paletteController = Box2DPaletteController.createPalette();
and my generated getLookup method from:
@Override
public Lookup getLookup() {
return obj.getLookup();
to:
@Override
public Lookup getLookup() {
return new ProxyLookup(obj.getLookup(), Lookups.fixed(paletteController));
}
That’s it, now the palette will only show, when the visual editor is selected:
The complete project is available on GitHub. Enjoy!