0

Problem with changing MVVM tree model

asked 2013-05-08 21:06:53 +0800

davout gravatar image davout
1429 3 18

I'm using a Tree with a MVVM form to display a two level hierarchy of data, like:

- Category 1
  |--- Sub Category 1-1
- Category 2
  |--- Sub Category 2-1
  |--- Sub Category 2-2
 - Category 3
  |--- Sub Category 3-1

I've developed by own custom model and node classes to support this tree. The tree supports drag'n drop of items and the form has buttons for moving 'category' items up and down in the order.

However, when I try to move a category item that is not at the top to the top position I'm getting an error.

Here's the method that handles the 'move to top' button event handler...

@Command("moveCategoryTop")
@NotifyChange({"categories","selectedCategory"})
public void moveCategoryTop() {
    // find the root node from the tree model
    EisTreeModelNode aRoot = getCategories().getRoot();
    // find the currently selected node
    EisTreeModelNode aSelectedNode = getSelectedCategory();
    // remove the selected node from the root child list
    aRoot.removeChildNode(aSelectedNode);
    // add the selected node back in as the first child of the root
    aRoot.addChildNode(0, aSelectedNode);
    // setup all top level nodes so that are open
    openAllNodes();
    // change selection to make it the new top category
    getCategories().clearSelection();
    int[] aSelectionPath = {0};
    getCategories().addSelectionPath(aSelectionPath);
}

After completing the method the app throws an invalid index error inside my model - asking for a child node at an index position outside of what the parent node has.

It's as though after the drag'n'drop action has been completed the tree is trying to repaint itself using the model state BEFORE the drop action code shown above was executed.

Any ideas?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2013-05-09 06:12:48 +0800

davout gravatar image davout
1429 3 18

Worth adding that the Tree has 'checkmark="true"'in play and as I'm dragging the active/selected item I'm wondering if the tree isn't trying to repaint itself before it reacts to a change in the model.

link publish delete flag offensive edit
0

answered 2013-05-20 06:14:48 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

Regarding to move ndoe that is not at the top to the top, the code below works fine with ZK 6.5.1.1

zul

<zk>
    <window apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('test.TestVM')">
        <tree model="@bind(vm.model)"
            width="600px" height="200px"
            onSelect="@command('keepSelectedNode')">
            <treecols>
                <treecol label="data" />
            </treecols>
            <template name="model" var="node">
                <treeitem>
                    <treerow>
                        <treecell label="@bind(node.data)" />
                    </treerow>
                </treeitem>
            </template>
        </tree>
        <button label="move to top" onClick="@command('moveToTop')" />
    </window>
</zk>

TestVM.java

package test;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zul.DefaultTreeModel;
import org.zkoss.zul.DefaultTreeNode;
import org.zkoss.zul.Tree;
import org.zkoss.zul.TreeModel;

/**
 * tested with ZK 6.5.1.1
 * 
 * @author benbai
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TestVM {
    DefaultTreeModel _model;
    DefaultTreeNode _selectedNode;


    public TreeModel<DefaultTreeNode<String>> getModel () {
        DefaultTreeNode root = new DefaultTreeNode(null,
                new DefaultTreeNode[] {
                    new DefaultTreeNode("aaa"),
                    new DefaultTreeNode("bbb", new DefaultTreeNode[] {new DefaultTreeNode("ddd")}),
                    new DefaultTreeNode("ccc")
        });
        if (_model == null) {
            _model = new DefaultTreeModel<String>(root);
        }
        return _model;
    }

    @Command
    @NotifyChange("selectedNodePath")
    public void keepSelectedNode (@ContextParam(ContextType.TRIGGER_EVENT) SelectEvent event) {
        _selectedNode = ((Tree)event.getTarget()).getSelectedItem().getValue();
    }
    @Command
    public void moveToTop () {
        if (_selectedNode != null) {
            DefaultTreeNode root = (DefaultTreeNode)_selectedNode.getModel().getRoot();
            _selectedNode.getParent().remove(_selectedNode);
            root.insert(_selectedNode, 0);
        }
    }
}

Regarding drag'n'drop issue, could you provide your scenario more details?

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-05-08 21:06:53 +0800

Seen: 76 times

Last updated: May 20 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More