public Action[] getActions(boolean context) {
Action[] result = new Action[] {
SystemAction.get(EditAction.class),
SystemAction.get(CutAction.class),
SystemAction.get(CopyAction.class),
SystemAction.get(RenameAction.class),
SystemAction.get(DeleteAction.class),
};
return result;
}
import org.openide.actions.CopyAction;
import org.openide.actions.CutAction;
import org.openide.actions.DeleteAction;
import org.openide.actions.EditAction;
import org.openide.actions.RenameAction;
So, clearly, these were all predefined menu items. What if I wanted to add my own menu item? For example, I'd like to -- at some stage -- be able to add a menu item that adds XDoclet tags to a tag handler. I'd also like to be able to open an HTML file in DreamWeaver. Well, to those ends, I've worked out what is probably pretty much the simplest implementation of a menu item (it is found within the class that extends DataNode):
private final class TestAction extends AbstractAction {
private final DataObject obj;
public TestAction(DataObject obj) {
this.obj = obj;
putValue(Action.NAME, "Send a Greeting to the World");
}
public void actionPerformed(ActionEvent ae) {
String msg = "Hello World!";
JOptionPane.showMessageDialog(null,msg);
}
}
From the above, you can see that the menu item will be named "Send a Greeting to the World" and that, when you click it, the highly original greeting "Hello World!" will appear in a JOptionPane. To include an instance of TestAction in my menu selection, I would add it to the existing menu items as follows (note that the null, represents a separator in the menu):
public Action[] getActions(boolean context) {
Action[] result = new Action[] {
SystemAction.get(EditAction.class),
SystemAction.get(CutAction.class),
SystemAction.get(CopyAction.class),
SystemAction.get(RenameAction.class),
SystemAction.get(DeleteAction.class),
null,
new TestAction(getDataObject()),
};
return result;
}
I've changed the DataLoader that I created in Playing with File Types in NetBeans IDE 4.1 so that HTML files are recognized (instead of tag files). Having made the changes above (i.e., added an implementation of a menu item and then added an instance of it to the getActions() method), I rebuilt the module and ended up with the following when right-clicking an HTML file in the IDE:
Note that, to make it all work, you need to override the HTML object type's org.netbeans.modules.html.HtmlDataObject representation class, in the same way that you overrode the JSP object's representation class (i.e., in the manifest.mf file) in Playing with File Types in NetBeans IDE 4.1. This means that if you -- like me -- don't provide an icon in the class that extends DataNode, you'll end up -- just like me -- with an HTML file that doesn't have an icon.
For more in depth information on this subject, see Tim Boudreau's Module Building: Module Coupling - Implementing SetMainFileAction.