Olá Pessoal,
Eu tenho uma árvore implementada com drag n’ drop nela, só que tem um porém quando eu arrasto um item de um grupo para o outro funciona, mas quando eu vou colocar mais 1 dentro do outro grupo eles trocam ao invés de ficar os dois. Vou mandar os dois códigos para vcs analisarem.
.java
package br.com.sistema.controleHoras;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.ajax4jsf.context.AjaxContext;
import org.richfaces.component.UIDragSupport;
import org.richfaces.component.UITree;
import org.richfaces.component.UITreeNode;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.DropEvent;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
import org.richfaces.model.TreeRowKey;
public class SimpleTreeBean {
private TreeNode rootNode = null;
private List<TreeNode<String>> selectedNodeChildren = new ArrayList<TreeNode<String>>();
private String nodeTitle;
private static final String DATA_PATH = "/properties/data.properties";
private void addNodes(String path, TreeNode node, Properties properties) {
boolean end = false;
int counter = 1;
while (!end) {
String key = path != null ? path + '.' + counter : String
.valueOf(counter);
String value = properties.getProperty(key);
if (value != null) {
TreeNodeImpl nodeImpl = new TreeNodeImpl();
nodeImpl.setData(value);
node.addChild(new Integer(counter), nodeImpl);
addNodes(key, nodeImpl, properties);
counter++;
} else {
end = true;
}
}
}
private void loadTree() {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH);
try {
Properties properties = new Properties();
properties.load(dataStream);
rootNode = new TreeNodeImpl();
addNodes(null, rootNode, properties);
} catch (IOException e) {
throw new FacesException(e.getMessage(), e);
} finally {
if (dataStream != null) {
try {
dataStream.close();
} catch (IOException e) {
externalContext.log(e.getMessage(), e);
}
}
}
}
public void processSelection(NodeSelectedEvent event) {
HtmlTree tree = (HtmlTree) event.getComponent();
nodeTitle = (String) tree.getRowData();
selectedNodeChildren.clear();
TreeNode<String> currentNode = tree.getModelTreeNode(tree.getRowKey());
TreeNodeImpl<String> demoCurrentNodeImpl = (TreeNodeImpl<String>) (currentNode instanceof TreeNodeImpl ? currentNode
: null);
if (currentNode.isLeaf()
&& (demoCurrentNodeImpl != null || demoCurrentNodeImpl == null)) {
selectedNodeChildren.add(currentNode);
} else {
Iterator<Map.Entry<Object, TreeNode<String>>> it = currentNode
.getChildren();
while (it != null && it.hasNext()) {
Map.Entry<Object, TreeNode<String>> entry = it.next();
selectedNodeChildren.add(entry.getValue());
}
}
}
private Object getNewId(TreeNode parentNode) {
Map<Object, TreeNode> childs = new HashMap<Object, TreeNode>();
Iterator<Map.Entry<Object, TreeNode>> iter = parentNode.getChildren();
while (iter != null && iter.hasNext()) {
Map.Entry<Object, TreeNode> entry = iter.next();
childs.put(entry.getKey(), entry.getValue());
}
Integer index = 1;
while (childs.containsKey(index)) {
index++;
}
return index;
}
public void dropListener(DropEvent dropEvent) {
UITreeNode destNode = (dropEvent.getSource() instanceof UITreeNode) ? (UITreeNode) dropEvent
.getSource()
: null;
UITree destTree = destNode != null ? destNode.getUITree() : null;
TreeRowKey dropNodeKey = (dropEvent.getDropValue() instanceof TreeRowKey) ? (TreeRowKey) dropEvent
.getDropValue()
: null;
TreeNode droppedInNode = dropNodeKey != null ? destTree
.getTreeNode(dropNodeKey) : null;
UITreeNode srcNode = (dropEvent.getDraggableSource() instanceof UITreeNode) ? (UITreeNode) dropEvent
.getDraggableSource()
: null;
UITree srcTree = srcNode != null ? srcNode.getUITree() : null;
TreeRowKey dragNodeKey = (dropEvent.getDragValue() instanceof TreeRowKey) ? (TreeRowKey) dropEvent
.getDragValue()
: null;
TreeNode draggedNode = dragNodeKey != null ? srcTree
.getTreeNode(dragNodeKey) : null;
if (dropEvent.getDraggableSource() instanceof UIDragSupport
&& srcTree == null && draggedNode == null
&& dropEvent.getDragValue() instanceof TreeNode) {
srcTree = destTree;
draggedNode = (TreeNode) dropEvent.getDragValue();
dragNodeKey = srcTree.getTreeNodeRowKey(draggedNode) instanceof TreeRowKey ? (TreeRowKey) srcTree
.getTreeNodeRowKey(draggedNode)
: null;
}
if (droppedInNode != null
&& (droppedInNode.equals(draggedNode)
|| droppedInNode.getParent().getParent() != null || draggedNode
.getParent().getParent() == null)) {
return;
}
FacesContext context = FacesContext.getCurrentInstance();
if (dropNodeKey != null) {
destTree.addRequestKey(dropNodeKey);
Object state = null;
if (dragNodeKey != null) {
TreeNode parentNode = draggedNode.getParent();
state = srcTree.removeNode(dragNodeKey);
Object rowKey = srcTree.getTreeNodeRowKey(parentNode);
srcTree.addRequestKey(rowKey);
if (dropEvent.getDraggableSource() instanceof UIDragSupport) {
selectedNodeChildren.remove(draggedNode);
if (droppedInNode.equals(parentNode)) {
selectedNodeChildren.add(draggedNode);
}
}
} else if (dropEvent.getDragValue() != null) {
draggedNode = new TreeNodeImpl<String>();
draggedNode.setData(dropEvent.getDragValue().toString());
}
Object id = getNewId(destTree.getTreeNode(dropNodeKey));
destTree.addNode(dropNodeKey, draggedNode, id, state);
}
AjaxContext ac = AjaxContext.getCurrentInstance();
try {
ac.addComponentToAjaxRender(destTree);
} catch (Exception e) {
System.err.print(e.getMessage());
}
}
public List<TreeNode<String>> getSelectedNodeChildren() {
return selectedNodeChildren;
}
public void setSelectedNodeChildren(List<TreeNode<String>> selectedNodeChildren) {
this.selectedNodeChildren = selectedNodeChildren;
}
public TreeNode getTreeNode() {
if (rootNode == null) {
loadTree();
}
return rootNode;
}
public String getNodeTitle() {
return nodeTitle;
}
public void setNodeTitle(String nodeTitle) {
this.nodeTitle = nodeTitle;
}
}
e o meu .jsf
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<head></head>
<body>
<rich:dragIndicator id="indicator1">
<f:facet name="single">
<h:graphicImage styleClass="indicatorPicture"
value="/imagens/icon_certo.JPG" />
</f:facet>
</rich:dragIndicator>
<rich:dragIndicator id="indicator2" />
<h:form>
<h:panelGrid columns="2" width="100%" columnClasses="col1,col2">
<rich:tree ajaxKeys="#{null}" style="width:300px"
nodeSelectListener="#{simpleTreeBean.processSelection}"
reRender="selectedNode" ajaxSubmitSelection="true"
switchType="client" dragIndicator="indicator2"
value="#{simpleTreeBean.treeNode}" var="item" id="tree"
treeNodeVar="treeNode" dropListener="#{simpleTreeBean.dropListener}"
nodeFace="#{treeNode.parent.parent == null ? 'node' : 'leaf'}">
<rich:treeNode type="node" acceptedTypes="pic" >
<h:outputText value="#{item}"/>
</rich:treeNode>
<rich:treeNode type="leaf" dragType="pic">
<rich:dndParam name="label" type="drag">#{item}</rich:dndParam>
<h:outputText value="#{item}"/>
</rich:treeNode>
</rich:tree>
</h:panelGrid>
</h:form>
</body>
</html>
desde já eu agradeço quem puder me ajudar com o problema, Obrigado!
att,
lymoreira.