1

I have a question about the handling of node properties in an outlineview.

I have three level of nodes, the rootNode, the node and each node may have sub-nodes. Apart from the rootNode, all nodes and subnodes shall have the same (Boolean => checkbox) property. In my outlineview I have two columns, the node(s) column, and a property column with checkboxes.

What I need now is the behaviour, that when I activate the checkbox of a node, all its sub-nodes checkboxes shall be activated as well, when I deactivate the checkbox of a node, all its sub-nodes checkboxes shall be de-activated as well. If I expand the tree to see the sub-nodes, each sub-node may be selected as well.

My current code looks like the following (some parts are found on the internet):

The main api

public class Category {

    private String name;
    private Boolean x;

    public Category() {
        this("empty");
    }

    public Category(String name) {
        this.name = name;
    }

    public Category(String name, Boolean x) {
        this.name = name;
        this.x = x;
    }

    public String getName() {
        return name;
    }

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

    public Boolean getx() {
        return x;
    }

    public void setx(Boolean x) {
        this.x = x;
    }
}

My ChildFactory for the nodes (category) looks like

public class CategoryChildrenFactory extends ChildFactory.Detachable<Category> {

    /* detachable has no real effect on the current code, used to control 
    the life cylce */
    @Override
    protected boolean createKeys(List<Category> toPopulate) {

        toPopulate.add(new Category("Cat1", false));
        toPopulate.add(new Category("Cat2", false));
        toPopulate.add(new Category("Cat3", false));

        return true;
    }

    @Override
    protected Node createNodeForKey(Category key) {
        AllNode cn = new AllNode(key);
        return cn;
    }
}

and for the sub-nodes

public class MovieChildrenFactory extends ChildFactory<String>{

    Category category;

    public MovieChildrenFactory(Category category) {
        this.category = category;
    }

    @Override
    protected boolean createKeys(List<String> toPopulate) {
        toPopulate.add("m_1");
        toPopulate.add("m_2");
        toPopulate.add("m_3");

        return true;
    }

    @Override
    protected Node createNodeForKey(String key) {
        return new AllNode(category, key);
    }
}

The nodes creation is put into a single class for both types (nodes, subnodes)

public class AllNode extends AbstractNode {

    Category category;
    String title;
    private Sheet.Set set;

    public AllNode(Category category) {
        this(category, null);
        this.category = category;
        set = Sheet.createPropertiesSet();
    }

    public AllNode(Category category, String title) {
        super(Children.LEAF, Lookups.singleton(category));
        if (title == null) {
            this.setChildren(Children.create(new MovieChildrenFactory(category), true));
        }
        this.title = title;
        set = Sheet.createPropertiesSet();
    }

    @Override
    public String getHtmlDisplayName() {
        String name = "";
        if (title == null) {
            name = category.getName();
        } else {
            name = title;
        }
        return name;
    }

    @Override
    protected Sheet createSheet() {
        Sheet sheet = Sheet.createDefault();
        Category obj = getLookup().lookup(Category.class);
        PlotMathProperties pmp = new PlotMathProperties(obj, title);
        set.put(pmp.getMyProperty());
        sheet.put(set);
        return sheet;
    }
}

The properties are handled by

public class PlotMathProperties {

    private MyProperty myProperty;
    private String categoryName;
    private String title;

    public PlotMathProperties(Category category, String title) {
        this.categoryName = category.getName();
        this.title= title;
        this.myProperty= new MyProperty ();
    }

    public XProperty getMyProperty () {
        return myProperty;
    }

    public class MyProperty extends PropertySupport.ReadWrite<Boolean> {

        private Boolean isMyProp = false;

        public MyProperty () {
            super("x", Boolean.class, "XPROP", "Is this a coloured or black and white movie");
        }

        @Override
        public Boolean getValue() throws IllegalAccessException, InvocationTargetException {
            return isMyProp ;
        }

        @Override
        public void setValue(Boolean val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            isMyProp = val;
            if (isMyProp) {
                System.out.println("active: " + categoryName + ", " + title);
            } else {
                System.out.println("de-active: " + categoryName + ", " + title);
            }
        }
    }
}

Together with a TopComponent the outlineview looks nice and works well.

Has anyone an idea how to setup the the behaviour for the check-boxes

regards

Puce
  • 37,247
  • 13
  • 80
  • 152
WeserLinux
  • 31
  • 2
  • It sounds like you should have a look at `CheckableNode`. Implement this interface on your node and you'll get the behavior your looking for. http://bits.netbeans.org/dev/javadoc/org-openide-explorer/org/openide/explorer/view/CheckableNode.html – Jonathan Spooner Mar 24 '12 at 01:30

0 Answers0