Hi.
What is the correct way to reset a servet side Tree2 component? I am
running into the dreaded
"Encountered a node [0] + with an illogical state". It is
understandable becase I have reset the
bean containing the tree but what keys (somewhere in app scope?)
should I clear so that the
application thinks it has never seen the tree before?
I have exactly the same problem! the app remember the old tree so if i
remove some node that was expanded i get that error! i searched
throughout the internet and i found many people with this problem but
never a solution. I thought that if i could collapse the tree (before i
delete the nodes) i would not get the error but I am not able to
collapse the tree working in clientSideToggle mode. So i solved using
serverSide Toggle mode (clientSideToggle="false") and collapsing the
node I want to delete before deleting it.
Anyway it would be very useful to know a way to reset all the
information about any previous version of the tree.
TreeModel from the Tree2 is quite weird. Try the attached tree model
(compatible with the Tree2 tree model).
Bye.
/lexi
package de.disy.preludio2.faces.treemodel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.myfaces.custom.tree2.TreeNode;
public class ExtendedTreeNode implements TreeNode {
private static final long serialVersionUID = 1L;
private final ExtendedTreeNode parent;
private final List children = new ArrayList();
private String type;
private String description;
private String identifier;
private boolean expanded;
public ExtendedTreeNode(ExtendedTreeNode parent, String type, String
description) {
this.parent = parent;
this.type = type;
this.description = description;
}
public ExtendedTreeNode getParent() {
return parent;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isLeaf() {
return children.size() == 0;
}
public void setLeaf(boolean leaf) {
}
public int getChildCount() {
return children.size();
}
public List getChildren() {
return children;
}
public int getIndex() {
if (getParent() == null) {
return 0;
}
else {
return getParent().getIndex(this);
}
}
public int getIndex(ExtendedTreeNode node) {
return children.indexOf(node);
}
public boolean isExpanded() {
return !isLeaf() && expanded;
}
public void expand() {
if (isLeaf())
expanded = false;
else
expanded = true;
}
public void collapse() {
expanded = false;
}
public void toggle() {
if (isLeaf() || isExpanded())
collapse();
else
expand();
}
public void clear() {
clearChildren();
}
public void clearChildren() {
for (Iterator iterator = children.iterator(); iterator.hasNext();) {
final ExtendedTreeNode node = (ExtendedTreeNode) iterator.next();
node.clear();
}
children.clear();
}
}package de.disy.preludio2.faces.treemodel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.myfaces.custom.tree2.TreeModel;
import org.apache.myfaces.custom.tree2.TreeNode;
import org.apache.myfaces.custom.tree2.TreeState;
public class ExtendedTreeModel implements TreeModel {
private static final long serialVersionUID = 1L;
private final Map nodeMap = new HashMap();
private final ExtendedTreeNode root;
private ExtendedTreeNode currentNode;
private final TreeState treeState = new HijackTreeState();
public ExtendedTreeModel(ExtendedTreeNode root) {
this.root = root;
refresh();
}
public void refresh() {
nodeMap.clear();
processNode("0", null, root);
if (!nodeMap.values().contains(currentNode)) {
currentNode = root;
}
}
private void processNode(String id, ExtendedTreeNode parentNode,
ExtendedTreeNode treeNode) {
treeNode.setIdentifier(id);
nodeMap.put(id, treeNode);
int index = 0;
for (Iterator iterator = treeNode.getChildren().iterator();
iterator.hasNext();) {
final String childId = id + SEPARATOR + index++;
final ExtendedTreeNode childTreeNode = (ExtendedTreeNode) iterator.next();
processNode(childId, treeNode, childTreeNode);
}
}
public TreeState getTreeState() {
return treeState;
}
public void setTreeState(TreeState treeState) {
}
public TreeNode getNode() {
return currentNode;
}
public ExtendedTreeNode getHijackTreeNode() {
return currentNode;
}
public void setNodeId(String nodeId) {
if (nodeId == null) {
currentNode = null;
}
else {
currentNode = getNodeById(nodeId);
}
}
public String[] getPathInformation(String nodeId) {
if (nodeId == null) {
throw new IllegalArgumentException("Cannot determine parents for a null
node.");
}
final ArrayList pathList = new ArrayList();
for (ExtendedTreeNode node = getNodeById(nodeId); node != null; node =
node.getParent()) {
pathList.add(0, node.getIdentifier());
}
return (String[]) pathList.toArray(new String[pathList.size()]);
}
public boolean isLastChild(String nodeId) {
final ExtendedTreeNode treeNode = getNodeById(nodeId);
final ExtendedTreeNode parentNode = treeNode.getParent();
if (parentNode == null) {
return true;
}
else {
final List children = parentNode.getChildren();
final int childCount = children.size();
return children.get(childCount - 1) == treeNode;
}
}
public ExtendedTreeNode getNodeById(String nodeId) {
return (ExtendedTreeNode) nodeMap.get(nodeId);
}
private class HijackTreeState implements TreeState {
boolean _transient = false;
public boolean isNodeExpanded(String nodeId) {
final ExtendedTreeNode node = getNodeById(nodeId);
if (node == null) {
return false;
}
else {
return node.isExpanded();
}
}
public void toggleExpanded(String nodeId) {
final ExtendedTreeNode node = getNodeById(nodeId);
if (node == null) {
// do nothing
}
else {
node.toggle();
}
}
public boolean isTransient() {
return _transient;
}
public void setTransient(boolean trans) {
_transient = trans;
}
public void expandPath(String[] nodePath) {
for (int i = 0; i < nodePath.length; i++) {
final String nodeId = nodePath[i];
final ExtendedTreeNode node = getNodeById(nodeId);
if (node != null) {
node.expand();
}
}
}
public void collapsePath(String[] nodePath) {
for (int i = 0; i < nodePath.length; i++) {
final String nodeId = nodePath[i];
final ExtendedTreeNode node = getNodeById(nodeId);
if (node != null) {
node.collapse();
}
}
}
}
}