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:
1--
|--2
|--3
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 :
<zk>
<separator height="10px" />
<tree id="tree" rows="7" model="@load(vmName.unidadeTreeModel)"
droppable="true" onDrop="@command('mudaUnidadePai', valor=event)" >
<treecols>
<treecol style="font-weight: bold;">
<label value="Nome"
style="font-weight: bold;" />
<label value="Editar" />
<label value="Excluir"/>
<label value="Numero" />
</treecol>
</treecols>
<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"
onClick="@command('confirmarExclusaoSelected',
selected=each)" />
<label value="@load(each.numero)"/>
</treecell>
</treerow>
</treeitem>
</template>
</tree>
</zk>
Here is code of my Abstract tree model :
public class UnidadeTreeModel extends AbstractTreeModel{
private static final long serialVersionUID = 3854683349236034878L;
private UnidadeServiceImpl unidadeService;
public UnidadeTreeModel(Unidade unidade) {
super(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:
@Controller @Scope("session") public class OrganizacaoViewModel extends AbstractViewModelEnderecoLista implements IViewModelEnderecoLista, Serializable {
private static final long serialVersionUID = 5809964914070454399L;
private static final Logger LOGGER = LoggerFactory.getLogger(OrganizacaoViewModel.class);
@Wire
Tree tree;
@Init
@Override
public void init(@ContextParam(ContextType.SESSION) Session session, @ContextParam(ContextType.VIEW) Component component) {
setService((UnidadeServiceImpl) SpringUtil.getBean("unidadeService"));
tipoUnidadeService = (TipoUnidadeServiceImpl) SpringUtil.getBean("tipoUnidadeService");
}
@AfterCompose(superclass = true)
@Override
public void atualizarView() {
montaArvore();
atualizarLista();
BindUtils.postNotifyChange(null, null, OrganizacaoViewModel.this, "*");
}
public void montaArvore() {
if (getService().buscarRaiz(true) == null) {
root = new Unidade();
root.setNumero("NBASE");
root.setDescricao("DBASE");
root.setRaiz(true);
try {
getService().gravar(root);
} catch (DuplicateEntryException e) {
LOGGER.info("Montar arvore", e);
}
}
if (unidadeTreeModel != null) {
paths = unidadeTreeModel.getOpenPaths();
} else {
unidadeTreeModel = new UnidadeTreeModel(getService().buscarRaiz(true));
}
unidadeTreeModel.addOpenPaths(paths);
tree.setModel(unidadeTreeModel);
paths = null;
}
@Command
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();
filho.setParent(getService().buscarRaiz(true));
try {
getService().gravar(filho);
} 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();
pai.getUnidades().add(filho);
filho.setParent(pai);
try {
getService().gravar(filho);
} catch (DuplicateEntryException e) {
LOGGER.info("Mudae unidade pai", e);
}
}
BindUtils.postNotifyChange(null, null, OrganizacaoViewModel.this, "*");
}
}
Instead of using true
value for droppable and draggable properties, try to use a string.
More help here.