1

我对大纲视图中节点属性的处理有疑问。

我有三个级别的节点,rootNode,节点和每个节点可能有子节点。除 rootNode 外,所有节点和子节点都应具有相同的 (Boolean => checkbox) 属性。在我的大纲视图中,我有两列,节点列和带有复选框的属性列。

我现在需要的是行为,当我激活节点的复选框时,它的所有子节点复选框也将被激活,当我停用节点的复选框时,它的所有子节点复选框都将被停用也是。如果我展开树以查看子节点,则每个子节点也可能被选中。

我当前的代码如下所示(某些部分在互联网上找到):

主要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;
    }
}

我的节点(类别)的 ChildFactory 看起来像

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;
    }
}

对于子节点

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);
    }
}

两种类型(节点、子节点)的节点创建都放在一个类中

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;
    }
}

属性由

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);
            }
        }
    }
}

与 TopComponent 一起,outlineview 看起来不错并且运行良好。

有谁知道如何设置复选框的行为

问候

4

0 回答 0