Thursday Oct 16, 2008

How Users Can Let Children Move Up & Move Down (Part 2)

In How Users Can Let Children Move Up & Move Down (Part 1), the children class extended Index.ArrayChildren. That was extremely convenient, because (in order for the 'Move Up' and 'Move Down' menu items to be enabled), an implementation of the Index class MUST be included in the root node's Lookup. Since an implementation of Index.ArrayChildren meets this requirement, things look good so long as you're using that class. However, what if you're extending Children.Keys instead? That's the vexing question posed in this blog entry.

In the case where you're not extending Index.ArrayChildren, you have a problem. In this case, you need to create a new class that extends Index.Support, with content like this:

import javax.swing.JOptionPane;
import org.openide.nodes.Index;
import org.openide.nodes.Node;

public class IndexSupportImpl extends Index.Support {

    private PropChildren prop;

    public IndexSupportImpl(PropChildren prop) {
        this.prop = prop;
    }

    @Override
    public Node[] getNodes() {
        return prop.getNodes();
    }

    @Override
    public int getNodesCount() {
        return prop.getNodesCount();
    }

    @Override
    public void reorder(final int[] perm) {
        JOptionPane.showMessageDialog(null, "reorder here!");
    }
    
}

The "only" problem is that I haven't been able to figure out how to implement the 'reorder' method above. I've seen all the related sources but haven't been able to figure it out. Anyway, now you need to add the above implementation of Index to the root node's Lookup:

PropChildren prop = new PropChildren();
explorerManager.setRootContext(new AbstractNode(prop, Lookups.singleton(new IndexSupportImpl(prop))));

And, finally, your children object needs to implement Index and then delegate to the above Index.Support class. At least, then the menu items will be enabled, even though the 'reorder' will need to be figured out per implementation.

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
« October 2008 »
SunMonTueWedThuFriSat
   
2
7
8
11
12
20
24
25
27
31
 
       
Today