Drag and drop ZK TreeModel issue

asked 2014-04-14 15:09:37 +0800

DouglasAM gravatar image DouglasAM
1 1

updated 2014-04-16 20:14:14 +0800


I'm having problems with drag and drop, I wouldn't like to allow one node to be moved within one own subnode, for example:




the node 2 can't be moved to node 3, if this happen node 3 has a new subnode but has no parent. The same can't happen with the node 1 that can't be moved to node 2.

Is there a way to prevent this without to implement a method in my viewmodel?

or this is a bug in the compnent.

I'm using AbstractTreeModel, because my tree can become very big and I want it to be loaded on demand, the most examples on the web are about DefaultTreeModel and I can't adapt this examples to my solution.

I'm using ZK 7.0.0 and any help I will apreciate.

bellow is the code used to implement:

.zul file :

    <separator height="10px" />
    <tree id="tree" rows="7" model="@load(vmName.unidadeTreeModel)"
        droppable="true" onDrop="@command('mudaUnidadePai', valor=event)" > 
            <treecol style="font-weight: bold;">
            <label value="Nome"
                style="font-weight: bold;" />
                <label value="Editar" />
                <label value="Excluir"/>
                <label value="Numero" />
        <template name="model">
        <treeitem >
               <treerow draggable="true" droppable="true"
               onDrop="@command('mudaUnidadePai', valor=event)">
                 <treecell label="@load(each.descricao)">
                        <a  label="Editar"                              
                            onClick="@command('editarSelected', selected=each)" />
                        <a label="Excluir"                               
                            selected=each)" />
                        <label value="@load(each.numero)"/>

Here is code of my Abstract tree model :

public class UnidadeTreeModel extends AbstractTreeModel<Unidade>{

   private static final long serialVersionUID = 3854683349236034878L;

   private UnidadeServiceImpl unidadeService;

   public UnidadeTreeModel(Unidade unidade) {
    unidadeService = (UnidadeServiceImpl) SpringUtil.getBean("unidadeService");

   public boolean isLeaf(Unidade node) {
    Unidade newNode = unidadeService.atualizar(node);
    return newNode.getUnidades() == null || newNode.getUnidades().size() == 0;

   public Unidade getChild(Unidade parent, int index) {
    Unidade newParent = unidadeService.atualizar(parent);
    if ((newParent).getUnidades() != null && ((Unidade)
         newParent).getUnidades().size() > index) {
        return (newParent).getUnidades().get(index);
    } else {
        return null;

  public int getChildCount(Unidade parent) {
    Unidade newParent = unidadeService.atualizar(parent);
    return ((newParent).getUnidades() != null) ?        
   ((Unidade)newParent).getUnidades().size() : 0;

  public int getIndexOfChild(Unidade parent, Unidade child) {
    Unidade newParent = unidadeService.atualizar(parent);
    for (int i = 0; i < newParent.getUnidades().size(); i++) {
        if (child.equals(newParent.getUnidades().get(i))) {
            return i;
    return -1;


And Finally part of the code from my viewModel:

public class OrganizacaoViewModel extends AbstractViewModelEnderecoLista<Unidade, 
UnidadeServiceImpl> implements IViewModelEnderecoLista<Unidade>, Serializable {

private static final long serialVersionUID = 5809964914070454399L;

 private static final Logger LOGGER=  

Tree tree;

public void init(@ContextParam(ContextType.SESSION) Session session,   
@ContextParam(ContextType.VIEW) Component component) {

    setService((UnidadeServiceImpl) SpringUtil.getBean("unidadeService"));
    tipoUnidadeService = (TipoUnidadeServiceImpl)     

@AfterCompose(superclass = true)
public void atualizarView() {
    BindUtils.postNotifyChange(null, null, OrganizacaoViewModel.this, "*");

   public void montaArvore() {

    if (getService().buscarRaiz(true) == null) {

        root = new Unidade();
        try {
        } catch (DuplicateEntryException e) {
            LOGGER.info("Montar arvore", e);
    if (unidadeTreeModel != null) {
        paths = unidadeTreeModel.getOpenPaths();
    } else {
        unidadeTreeModel = new UnidadeTreeModel(getService().buscarRaiz(true));
    paths = null;


public void mudaUnidadePai(@BindingParam("valor") DropEvent evento) {

    if (evento.getTarget().getId().equals("tree")) {
        // caso unidade seja arrastado para arvore, unidade volta para a raiz
        Treerow row = (Treerow) evento.getDragged();
        Treeitem item = (Treeitem) row.getParent();
        Unidade filho = item.getValue();
        try {
        } catch (DuplicateEntryException e) {
            LOGGER.info("Mudar unidade pai", e);
    } else {
        // extrai novo filho
        Treerow row = (Treerow) evento.getDragged();
        Treeitem item = (Treeitem) row.getParent();
        Unidade filho = item.getValue();

        Treerow row2 = (Treerow) evento.getTarget();
        Treeitem item2 = (Treeitem) row2.getParent();
        Unidade pai = item2.getValue();
            try {
            } catch (DuplicateEntryException e) {
                LOGGER.info("Mudae unidade pai", e);
       BindUtils.postNotifyChange(null, null, OrganizacaoViewModel.this, "*");

delete flag offensive retag edit


On my first post the code was so fuzzy, I changed and now is much better.

DouglasAM ( 2014-04-16 13:04:32 +0800 )edit
Be the first one to answer this question!
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

1 follower



Asked: 2014-04-14 15:09:37 +0800

Seen: 8 times

Last updated: Apr 16 '14

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