0

CREATING DYNAMIC TREE USING MVVM TREEMODEL

asked 2014-11-07 11:02:54 +0800

jeevaJR7 gravatar image jeevaJR7
1 2

updated 2014-11-07 11:20:32 +0800

I have created dynamic tree using mvvm and drag and drop using composer. but the problem is this tree is always closed.I want this tree to always open whenever i load this file from database .is any solution available for that? and another question is can i make this dynamic tree as organaizational treeview in this same program using zk??

tree.zul <zk>

viewModel="@init('com.hierarchy.vm.HierarchyVM') @id('vm')" width="100%" height="100%"
validationMessages="@id('vmsgs')"  >
    <hlayout style="background:silver">
        <div width="100%" height="800px" style="background:silver" align="center">
        <tree  id="tree1" height="100%" width="100%" model="@load(vm.treeModel)" droppable="true" >
                <template name="model" var="emp">
                    <treeitem open="true" >
                        <treerow  droppable="true" draggable="true" apply="com.hierarchy.vm.HierarchyVM"  >
                                <treecell  >
                                    <button width="200px" height="40px" style="color:blue" label="@load(c:cat5(emp.data.name,'             ','[',emp.data.title,']'))"></button> 
                                </treecell>
                        </treerow>
                    </treeitem>
                </template>
                 <zscript><![CDATA[

                    ]]></zscript>
        </tree>
        </div>
    </hlayout>

</zk>

view model.java

private EmployeeData employeeData;

private ListModelList<EmployeeData> employeeList;

public EmployeeData getEmployeeData() {
    return employeeData;
}



public void setEmployeeData(EmployeeData employeeData) {
    this.employeeData = employeeData;
}



public ListModelList<EmployeeData> getEmployeeList() {
    return employeeList;
}



public void setEmployeeList(ListModelList<EmployeeData> employeeList) {
    this.employeeList = employeeList;
}






@Init
public void init(){
     employeeData = new EmployeeData();
     employeeList = new ListModelList<EmployeeData>();

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyDb","root","root");

        String query = "Select * from EmployeeData";
        PreparedStatement preparedStatement=connection.prepareStatement(query);

        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            employeeList.add(new EmployeeData(resultSet.getString("title"),resultSet.getString("name")));
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}



public DefaultTreeModel<EmployeeData> getTreeModel() {
    DefaultTreeModel<EmployeeData> result = genTreeModel();
    return result;
}

public DefaultTreeModel<EmployeeData> genTreeModel() {
    DefaultTreeNode<EmployeeData> root = new DefaultTreeNode<EmployeeData>(null, mockNode(0));
    DefaultTreeModel<EmployeeData> result = new DefaultTreeModel<EmployeeData>(root);
    return result;
}

public ArrayList<DefaultTreeNode<EmployeeData>> mockNode(int level){

    ArrayList<DefaultTreeNode<EmployeeData>> result = new ArrayList<DefaultTreeNode<EmployeeData>>();
        for(int i=0; i<employeeList.size(); i++){

                if(i==level){
                    DefaultTreeNode<EmployeeData> root = new DefaultTreeNode<EmployeeData>
                                                            (employeeList.get(i), mockNode(level+1));
                    if(root!=null){
                        result.add(root);
                    }
                }
        }
    return result;

}


    @SuppressWarnings({ "unchecked" })
    public void doAfterCompose(Component win) throws Exception {

            win.addEventListener("onDrop", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    // TODO Auto-generated method stub
                    DropEvent dropevent = ((DropEvent) event);
                    List list = dropevent.getTarget().getParent().getChildren();
                    for (Object element : list) {
                        if (element instanceof Treechildren) {
                            ((Treechildren)element).appendChild((dropevent.getDragged().getParent()));


                        }

                    }

                }

            });
    }

}

delete flag offensive retag edit

Comments

most important here is not to mix model based Tree creation, and then update the components directly, if a tree model is used, then changes to the tree should be done in the treemodel. Of course DefaultTreeModel is Read only and needs to be extended for that

cor3000 ( 2014-11-10 09:20:12 +0800 )edit

this demo http://www.zkoss.org/zkdemo/tree/dynamic_tree shows how a TreeModel can be implemented to be modifiable, then addOpenObject works naturally

cor3000 ( 2014-11-10 09:21:27 +0800 )edit

6 Answers

Sort by ยป oldest newest most voted
0

answered 2014-11-08 14:31:26 +0800

Darksu gravatar image Darksu
1991 1 4

Hello jeevaJR7,

Regarding the first question please refer to the following url:

http://forum.zkoss.org/question/75893/zk6-tree-model-how-to-have-all-nodes-open-at-start/

And use the following code:

private void doExpandAll(Component component, boolean aufklappen) {
    if (component instanceof Treeitem) {
        Treeitem treeitem = (Treeitem) component;
        this.treeModel.addOpenPath(this.treeModel.getPath((DefaultTreeNode) treeitem.getValue()));
    }
    Collection<?> com = component.getChildren();
    if (com != null) {
        for (Iterator<?> iterator = com.iterator(); iterator.hasNext();) {
            doExpandAll((Component) iterator.next(), aufklappen);
        }
    }
}

Hope it helps.

Best Regards,

Darksu

link publish delete flag offensive edit
0

answered 2014-11-10 04:11:11 +0800

jeevaJR7 gravatar image jeevaJR7
1 2

what is that this.treemodel?? in my code i have gettreemodel function only..i have not declared it anywhere in this file..and i don't need add anything in my zul file?? is this code enough to generate?

link publish delete flag offensive edit
0

answered 2014-11-10 04:22:13 +0800

jeevaJR7 gravatar image jeevaJR7
1 2

Hi Darksu,

I think I don't need to add this code in my View model. I have find the solution for that

treeitem open="@load(true)" />

It will work :-D

link publish delete flag offensive edit
0

answered 2014-11-10 09:02:48 +0800

gediminas gravatar image gediminas
39 3

I think a better way to open some treeitems initialy is this:

  1. Add required open items in TreeModel for example by invoking addOpenObject in TreeModel constructor
  2. Remove the open="true" attribute from treeitem in ZUL file
link publish delete flag offensive edit

Comments

see my comment above, you cannot mix model based tree rendering and modifying the component, in that case the model is not aware of the changes and addOpenObject will not work, unless the model is aware of the "Object" and it's treenode

cor3000 ( 2014-11-10 09:26:37 +0800 )edit

Yeah, what I wanted to say is that I think TreeModel based approach is usually better (both in clarity and being more in line with MVVM principles).

gediminas ( 2014-11-10 09:34:24 +0800 )edit

I don't mean one or the other approach is better, but when using a TreeModel then don't change the Treeitem components directly or it will be out of sync with the model. If no TreeModel is used then adding removing components and setting open=true is ok. In MVVM the model based approach fits best.

cor3000 ( 2014-11-10 09:58:24 +0800 )edit
0

answered 2014-11-10 09:19:47 +0800

wariatmosznowy gravatar image wariatmosznowy
1

Yup that's totally true, it works really well

link publish delete flag offensive edit
0

answered 2014-11-10 09:56:27 +0800

jeevaJR7 gravatar image jeevaJR7
1 2

Another question is can i make this dynamic tree as organaizational treeview in this same program using zk??

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: 2014-11-07 11:02:54 +0800

Seen: 77 times

Last updated: Nov 10 '14

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