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.

Comments:

I guess you could override getIcon() to have it return an ImageIcon which is loaded from any location you wish (e.g. using java.awt.Toolkit's getImage method).

Posted by Tom Wheeler on November 11, 2008 at 10:47 PM PST #

Geertjan, I love your blog BUT it's so hard to find anything in your past posts. Apparently blogs.sun.com doesn't have tagging? It's a real tragedy you have an avalanche of awesome material here but there's no way to access it but the search feature.... :(

Posted by Bob on November 11, 2008 at 11:53 PM PST #

@Tom, thanks! I'll investigate that.
@Bob, thanks and you can use the Search on the left of this blog or, even better, I've been gradually linking to relevant topics from the NetBeans Developer FAQ:
http://wiki.netbeans.org/NetBeansDeveloperFAQ

Posted by Geertjan on November 11, 2008 at 11:56 PM PST #

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