Adding an Item to a File's Pop-Up Menu in NetBeans IDE 4.1

In Playing with File Types in NetBeans IDE 4.1, I learnt how to create a module that identifies a file by its extension. In that particular blog entry, the module I ended up with was able to distinguish between different JSP Objects. By default, the IDE lumps tag files and JSP files together as "JSP Objects". My module made a distinction between them and proved it by showing a different icon and menu selection for tag files. However, the menu selection was provided by the org.openide.actions package. In other words, the menu items were predefined. This is what the relevant part of my TagExtensionsDataNode class looked like:

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;
}
And the following are some of the import statements that were included at the top of the file:

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.

Comments:

sx

Posted by guest on July 18, 2006 at 10:37 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today