0

How to handle click events on a tree based on AbstractTreeModel?

asked 2012-08-02 20:12:57 +0800

starwarsfan gravatar image starwarsfan
36

updated 2012-08-02 20:13:31 +0800

Hi there

It seems to me that I'm completely blind at the moment and hope someone here could kick me into the right direction.

I've implemented a dynamic navigation tree using AbstractTreeModel based on the description here. But now I'm struggling on how to handle the events if one of the entries on the tree is clicked/selected!? How/where do I add the event listeners? All the examples just show how the tree could be created but I can't find any example which demonstrates the event handling on such a tree.

It would be much appreciated if anyone would give me an example/link/whatever to understand how this could be done.

Thx a lot in advance,
Yves

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2012-08-03 01:17:15 +0800

SimonPai gravatar image SimonPai
1696 1

Yves,

The TreeModel serves as the data provider, while the UI events are jobs to handle by controller, so they are meant to be separated and independent for a good MVC design. To handle events based on Treeitem, you can do it in the Tree's renderer.

Ragards,
Simon

link publish delete flag offensive edit

answered 2012-08-03 18:50:23 +0800

starwarsfan gravatar image starwarsfan
36

Hi Simon

Thx for your response. I understand, that makes sense. So I know how to go ahead. :-)

Kind regards,
Yves

link publish delete flag offensive edit

answered 2012-08-03 21:44:06 +0800

starwarsfan gravatar image starwarsfan
36

Hi once again

I did not get it done. :-( After spending some hours I have no idea how to setup a proper TreeitemRenderer for my usecase and now my brain feels completely blocked.

Here's some code:

Tree part of zul file:

        <west title="Navigation" apply="foo.NavigationTreeForwardComposer">
            <tree id="navigationTree" />
        </west>

public class NavigationTreeForwardComposer extends GenericForwardComposer {

    private Tree navigationTree;
    private NavigationTreeModel navigationTreeModel;

    @SuppressWarnings("unchecked")
    @Override
    public void doAfterCompose(final Component comp) throws Exception {
        super.doAfterCompose(comp);

//        navigationTree.setItemRenderer(new TreeitemRenderer<NavigationTreeNodeData>() {
//            @Override
//            public void render(final Treeitem item, final NavigationTreeNodeData data, final int index) throws Exception {
//                if (data == null) {
//                    return;
//                }
//
//                item.addEventListener(Events.ON_SELECT, new TreeEventListener() {
//                    @Override
//                    public void onEvent(final Event event) throws Exception {
//                        logger.debug("onOpen");
//                    }
//                });
//            }
//
//        });

        navigationTree.addEventListener(Events.ON_SELECT, new TreeEventListener()); // This event seems to be useless as I can't determine which node was clicked!?
        navigationTreeModel = new NavigationTreeModel(); // This is my model based on AbstractTreeModel which delivers the content of the tree dynamically.
        navigationTree.setModel(navigationTreeModel);
    }
}

The class NavigationTreeNodeData is the data container for the content of the tree. This is working very nice, the whole tree is shown and usable but if I activate the commented code in the snippet above, there is no tree any longer!? So what am I doing wrong? What's my mistake?

Please let me know if you need further details.

Kind regards,
Yves

link publish delete flag offensive edit

answered 2012-08-03 22:06:59 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

That's because you are adding a custom renderer, but your renderer doesn't render anything!
If setItemRenderer is never called, ZK uses a default one that just calls toString on the data and renders that.

However, since you are replacing the default renderer, you must add some actual rendering code in the class or you will end up with nothing.
There's a good example in the ZK Developer's Reference.

link publish delete flag offensive edit

answered 2012-08-04 21:17:45 +0800

starwarsfan gravatar image starwarsfan
36

Hi gekkio

You made my day! Thx a lot for pointing into the right direction, especially to the proper example on the Dev-Ref. I've read a lot of docs and HowTo's the last days, probably that was just too much... ;-)

Now it works as expected. Great!

Kind regards,
Yves

link publish delete flag offensive edit

answered 2012-08-07 09:47:05 +0800

ankitbarot gravatar image ankitbarot
6

how do you generate data in navigationTreeModel = new NavigationTreeModel(); ??

link publish delete flag offensive edit

answered 2012-08-07 19:36:55 +0800

starwarsfan gravatar image starwarsfan
36

Hi

I implemented isLeaf(...), getChild(...), getChildCount(...) and getIndexOfChild(...) as written on the docu for AbstractTreeModel.

Kind regards,
Yves

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2012-08-02 20:12:57 +0800

Seen: 181 times

Last updated: Aug 07 '12

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