Tuesday Nov 11, 2008

Setting a Node's Tooltip on the Fly

I expanded my API today. It now looks like this:
package org.netbeans.demo;

public interface User {

    public String getName();

    public String getDescription();
    
}

As a result, the other module, i.e., the one that provides the service, now needs to update its service provider class:

package org.netbeans.demo;

import org.netbeans.demo.User;

public class User1 implements User {

    private String name;
    private String description;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

So, now, in the editor component, I add the user name and user description to the InstanceContent when the Enter key is pressed in the editor, which updates the node hierarchy even though the node hierarchy is in a different module to where the editor component is found:

Finally, in order to handle both the name and the description, the Children.Keys subclass now works with a string array, instead of a string, to get the name and description which are used in the creation of the node hierarchy:

public class UserChildren extends Children.Keys<String[]> implements LookupListener {

    private Lookup.Result result = null;
    private ArrayList<String[]> userDetails = new ArrayList();

    public UserChildren() {
        result = Utilities.actionsGlobalContext().lookupResult(User.class);
        result.addLookupListener(this);
    }

    @Override
    public void resultChanged(LookupEvent lookupEvent) {
        Collection c = result.allInstances();
        for (User user : c) {
            userDetails.add(new String[]{user.getName(), user.getDescription()});
        }
        setKeys(userDetails);
    }

    @Override
    protected void addNotify() {
        resultChanged(null);
    }

    @Override
    protected Node[] createNodes(String[] userDetails) {
        return new Node[]{new PropNode(userDetails)};
    }

    private class PropNode extends AbstractNode {

        private PropNode(String[] userDetails) {
            super(Children.LEAF);
            setDisplayName(userDetails[0]);
            setShortDescription(userDetails[1]);
        }
        
    }

}

Notice that AbstractNode.setShortDescription sets the tooltip. I also tried working with AbstractNode.setIconBaseWithExtension, but that unfortunately assumes that the image is within the module, so it is not possible to have the user select an external image (i.e., on disk) for setting a node's icon on the fly.

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
« November 2008 »
SunMonTueWedThuFriSat
      
4
7
15
16
17
20
22
23
25
27
29
30
      
Today