X

Geertjan's Blog

  • March 9, 2006

Looking Up NetBeans Lookups

Geertjan Wielenga
Product Manager
So, I have this problem where the popup menu in my custom window component's (i.e., a topcomponent) JEditorPane doesn't show an activated menu item for the action I registered in the layer.xml file (below, you see Edit Music String greyed out, while it isn't greyed out when I open a file that uses the same MIME type in the NetBeans editor, as shown yesterday):

So, following Sandip's advice (see comments at the end of yesterday's blog), I went here. I followed the instructions under "Using InstanceProvider for declaration of compound folder objects and inheritance". So, this is what I did:

  1. I created META-INF/services and put a file there with this name:

    org.netbeans.spi.editor.mimelookup.Class2LayerFolder

  2. Inside that file is this content:

    org.netbeans.modules.jfuguetest.PopupInitializer

  3. I created that file. It has this content:

    public class PopupInitializer implements Class2LayerFolder{
    public PopupInitializer() {
    }
    public Class getClazz(){
    return PopupActions.class;
    }
    public String getLayerFolderName(){
    return "Popup"; //NOI18N
    }
    public InstanceProvider getInstanceProvider() {
    return new PopupActions();
    }
    }

  4. Then I created this file:

    public class PopupActions implements InstanceProvider{
    List ordered;
    public PopupActions(){
    }
    public PopupActions(List ordered){
    this.ordered = ordered;
    }
    public List getPopupActions(){
    List retList = new ArrayList();
    for (int i = 0; i<ordered.size(); i++){
    DataObject dob = (DataObject) ordered.get(i);
    InstanceCookie ic = (InstanceCookie)dob.getCookie(InstanceCookie.class);
    if (ic!=null){
    try{
    if (String.class.isAssignableFrom(ic.instanceClass()) ||
    Action.class.isAssignableFrom(ic.instanceClass()) ||
    SystemAction.class.isAssignableFrom(ic.instanceClass()) ||
    JSeparator.class.isAssignableFrom(ic.instanceClass())){
    Object instance = ic.instanceCreate();
    retList.add(instance);
    }
    }catch(IOException ioe){
    ioe.printStackTrace();
    }catch(ClassNotFoundException cnfe){
    cnfe.printStackTrace();
    }
    } else{
    retList.add(dob.getName());
    }
    }
    return retList;
    }
    public Object createInstance(List ordered) {
    return new PopupActions(ordered);
    }
    }

Then I was confused where to put the lookup code. Eventually, guided by Sandip, I looked around in the topcomponent. I know I must have hit on something, because when I override the topcomponent's getLookup() method, I get absolutely no activated menu items at all (as opposed to just having my own menu item greyed out):

So what all of this tells me is that I'm in the right method. But, what to put there? It should be something like this:

public Lookup getLookup() {
MimeLookup lookup = MimeLookup.getMimeLookup("audio/midi");
PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class);
List popupActions = actions.getPopupActions();
...
...
...
return lookup;
}

And that's as far as I got. Boy, lookups aren't easy. Especially not when one feels that one is only sniffing at an understanding, without really grasping what's going on (despite a lot of reading).

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.