package Templates.API_Support.Nodes_API;

import java.util.*;

import org.openide.nodes.*;

/**
 * List of children of a containing node.
 * Each child node is represented by one key from some data model.
 * Remember to document what your permitted keys are!
 * Edit this template to work with the classes and logic of your data model.
 * @author __USER__
 */
public class __Sample_container__Children extends Children.Keys implements MyDataListener {

    private final MyDataModel model;

    public __Sample_container__Children(MyDataModel model) {
        this.model = model;
    }

    protected void addNotify() {
        super.addNotify();
        // set the children to use:
        updateKeys();
        // and listen to changes in the model too:
        model.addModelListener(this);
    }

    private void updateKeys() {
        // get your keys somehow from the data model:
        MyDataElement[] keys = model.getChildren();
        // you can also use Collection rather than an array
        setKeys(keys);
    }

    protected void removeNotify() {
        model.removeModelListener(this);
        setKeys(Collections.EMPTY_SET);
        super.removeNotify();
    }

    protected Node[] createNodes(Object key) {
        // interpret your key here...usually one node generated, but could be zero or more
        return new Node[] {new MyNode((MyDataElement)key)};
    }

    public void modelChanged(MyModelEvent ev) {
        // your data model changed, so update the children to match:
        updateKeys();
    }

}
