Hi Sean,
well I couldn't resist it. I have created a patch that includes most of my ideas.
It was a fast hack, there may be new bugs with it.
Most of the changes are in UITree.saveState, UITree.restoreState and UITree.getDataModel ... now it is again possible to only supply a root TreeNode.
I put the state functions into TreeState. Unfortunatly the new expandAll functions do not work, because they are coupled closely with the model.
To make them work again we would need a reference to the model in that functions and a function like String getNodeId(TreeNode n) in TreeModel.
Tell me what you think...
Regards,
Mathias
Index: C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeModel.java
===================================================================
--- C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeModel.java
(revision 231022)
+++ C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeModel.java
(working copy)
@@ -17,6 +17,7 @@
import java.io.Serializable;
+
/**
* Model class for the tree component. It provides random access to nodes in
a tree
* made up of instances of the [EMAIL PROTECTED] TreeNode} class.
@@ -58,17 +59,10 @@
*/
public boolean isLastChild(String nodeId);
- /**
- * Indicates whether or not the specified [EMAIL PROTECTED] TreeNode} is
expanded.
- *
- * @param nodeId The id of the node in question.
- * @return If the node is expanded.
- */
- public boolean isNodeExpanded(String nodeId);
- /**
- * Toggle the expanded state of the specified [EMAIL PROTECTED] TreeNode}.
- * @param nodeId The id of the node whose expanded state should be toggled.
- */
- public void toggleExpanded(String nodeId);
+ public void setTreeState(TreeState state);
+ public TreeState getTreeState();
+
+
+
}
Index: C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeState.java
===================================================================
--- C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeState.java
(revision 0)
+++ C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeState.java
(revision 0)
@@ -0,0 +1,26 @@
+package org.apache.myfaces.custom.tree2;
+
+import java.io.Serializable;
+
+public interface TreeState extends Serializable {
+
+ /**
+ * Indicates whether or not the specified [EMAIL PROTECTED] TreeNode} is
expanded.
+ *
+ * @param nodeId The id of the node in question.
+ * @return If the node is expanded.
+ */
+ public boolean isNodeExpanded(String nodeId);
+
+ /**
+ * Toggle the expanded state of the specified [EMAIL PROTECTED] TreeNode}.
+ * @param nodeId The id of the node whose expanded state should be toggled.
+ */
+ public void toggleExpanded(String nodeId);
+
+
+ public boolean isTransient();
+
+ public void setTransient(boolean b);
+
+}
Index: C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/UITreeData.java
===================================================================
--- C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/UITreeData.java
(revision 231022)
+++ C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/UITreeData.java
(working copy)
@@ -63,6 +63,8 @@
private String _var;
private String _nodeId;
private Map _saved = new HashMap();
+
+ private TreeState _restoredState = null;
/**
* Constructor
@@ -82,11 +84,16 @@
// see superclass for documentation
public Object saveState(FacesContext context)
{
- Object values[] = new Object[3];
+ Object values[] = new Object[4];
values[0] = super.saveState(context);
//values[1] = _value;
values[1] = _model;
values[2] = _var;
+
+ TreeState t = getDataModel().getTreeState();
+ if ( t == null)
+ t = new TreeStateBase(); // the model supplier has forgotten to
return a valid state manager, but we need one
+ values[3] = (t.isTransient()) ? null : t; // save the state with the
component, unless it should explicitly not saved eg. session-scoped model and
state
return ((Object) (values));
}
@@ -100,6 +107,8 @@
//_value = values[1];
_model = (TreeModel)values[1];
_var = (String)values[2];
+
+ _restoredState = (TreeState) values[3];
}
public void queueEvent(FacesEvent event)
@@ -361,7 +370,7 @@
*
* @return TreeModel
*/
- private TreeModel getDataModel()
+ public TreeModel getDataModel()
{
if (_model != null)
{
@@ -375,13 +384,18 @@
{
_model = (TreeModel) value;
}
- else
+ else if (value instanceof TreeNode)
{
- throw new IllegalArgumentException("Value must now implement
TreeModel interface. " +
- "If you were using an instance of TreeNode use
TreeModelBase now instead.");
+ _model = new TreeModelBase((TreeNode) value);
+ } else
+ {
+ throw new IllegalArgumentException("Value must be a TreeModel
or TreeNode");
}
}
+ if (_restoredState != null)
+ _model.setTreeState(_restoredState); // set the restored state
(if there is one) on the model
+
return _model;
}
@@ -692,7 +706,7 @@
*/
public void toggleExpanded()
{
- getDataModel().toggleExpanded(getNodeId());
+ getDataModel().getTreeState().toggleExpanded(getNodeId());
}
/**
@@ -701,7 +715,7 @@
*/
public boolean isNodeExpanded()
{
- return getDataModel().isNodeExpanded(getNodeId());
+ return getDataModel().getTreeState().isNodeExpanded(getNodeId());
}
}
Index: C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeModelBase.java
===================================================================
--- C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeModelBase.java
(revision 231022)
+++ C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeModelBase.java
(working copy)
@@ -22,9 +22,6 @@
import javax.faces.component.NamingContainer;
import java.util.StringTokenizer;
import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-
/**
* Model class for the tree component. It provides random access to nodes in
a tree
* made up of instances of the [EMAIL PROTECTED] TreeNode} class.
@@ -35,12 +32,17 @@
*/
public class TreeModelBase implements TreeModel
{
- private static final Log log = LogFactory.getLog(TreeModelBase.class);
+
+ private static final long serialVersionUID = 3969414475396945742L;
+ private static final Log log = LogFactory.getLog(TreeModelBase.class);
private final static String SEPARATOR =
String.valueOf(NamingContainer.SEPARATOR_CHAR);
- private HashSet _expandedNodes = new HashSet();
+
private TreeNode root;
private TreeNode currentNode;
+
+ private TreeState treeState = new TreeStateBase();
+
/**
* Constructor
@@ -50,8 +52,22 @@
{
this.root = root;
}
+
+
- /**
+ public TreeState getTreeState() {
+ return treeState;
+ }
+
+
+
+ public void setTreeState(TreeState treeState) {
+ this.treeState = treeState;
+ }
+
+
+
+ /**
* Gets the current [EMAIL PROTECTED] TreeNode} or <code>null</code> if no
node ID is selected.
* @return The current node
*/
@@ -157,64 +173,5 @@
return node;
}
-
- // see interface
- public boolean isNodeExpanded(String nodeId)
- {
- return (_expandedNodes.contains(nodeId) && !getNode().isLeaf());
- }
-
- // see interface
- public void toggleExpanded(String nodeId)
- {
- if (_expandedNodes.contains(nodeId))
- {
- _expandedNodes.remove(nodeId);
- }
- else
- {
- _expandedNodes.add(nodeId);
- }
- }
-
- /**
- * If set to true, all nodes will be expanded by default. NOTE: A value
of false is ignored.
- * @param expandAll boolean
- */
- public void setExpandAll(boolean expandAll)
- {
- if (expandAll)
- {
- TreeNode originalNode = currentNode;
-
- //List rootChildren = root.getChildren();
- int kidId = 0;
-
- //for (int i = 0; i < rootChildren.size(); i++)
- //{
- expandEverything(null, kidId++);
- //}
-
- currentNode = originalNode;
- }
- }
-
- /**
- * Private helper method that recursviely expands all of the nodes.
- * @param parentId The id of the parent node (if applicable)
- * @param childCount The child number of the node to expand (will be
incremented as you recurse.)
- */
- private void expandEverything(String parentId, int childCount)
- {
- String nodeId = (parentId != null) ? parentId + SEPARATOR + childCount
: "0";
- setNodeId(nodeId);
-
- _expandedNodes.add(nodeId);
-
- List children = getNode().getChildren();
- for (int kount=0; kount < children.size(); kount++)
- {
- expandEverything(nodeId, kount);
- }
- }
+
}
Index: C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeStateBase.java
===================================================================
--- C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeStateBase.java
(revision 0)
+++ C:/Documents and Settings/mwerlit/My Documents/workspace/myfaces
current/tomahawk/src/java/org/apache/myfaces/custom/tree2/TreeStateBase.java
(revision 0)
@@ -0,0 +1,85 @@
+package org.apache.myfaces.custom.tree2;
+
+import java.util.HashSet;
+
+public class TreeStateBase implements TreeState {
+
+ private static final long serialVersionUID = -6767283932185878071L;
+ private HashSet _expandedNodes = new HashSet();
+ private boolean transien = false;
+
+// see interface
+ public boolean isNodeExpanded(String nodeId)
+ {
+ return (_expandedNodes.contains(nodeId) /*&& !getNode().isLeaf()*/);
+ }
+
+ // see interface
+ public void toggleExpanded(String nodeId)
+ {
+ if (_expandedNodes.contains(nodeId))
+ {
+ _expandedNodes.remove(nodeId);
+ }
+ else
+ {
+ _expandedNodes.add(nodeId);
+ }
+ }
+
+ public boolean isTransient() {
+ return transien;
+ }
+
+ public void setTransient(boolean transien) {
+ this.transien = transien;
+ }
+
+
+
+ /**
+ * If set to true, all nodes will be expanded by default. NOTE: A value
of false is ignored.
+ * @param expandAll boolean
+ */
+ /*
+ public void setExpandAll(boolean expandAll)
+ {
+ if (expandAll)
+ {
+ TreeNode originalNode = currentNode;
+
+ //List rootChildren = root.getChildren();
+ int kidId = 0;
+
+ //for (int i = 0; i < rootChildren.size(); i++)
+ //{
+ expandEverything(null, kidId++);
+ //}
+
+ currentNode = originalNode;
+ }
+ }
+ */
+
+ /**
+ * Private helper method that recursviely expands all of the nodes.
+ * @param parentId The id of the parent node (if applicable)
+ * @param childCount The child number of the node to expand (will be
incremented as you recurse.)
+ */
+ /*
+ private void expandEverything(String parentId, int childCount)
+ {
+ String nodeId = (parentId != null) ? parentId + SEPARATOR + childCount
: "0";
+ setNodeId(nodeId);
+
+ _expandedNodes.add(nodeId);
+
+ List children = getNode().getChildren();
+ for (int kount=0; kount < children.size(); kount++)
+ {
+ expandEverything(nodeId, kount);
+ }
+ }
+ */
+
+}

