You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by or...@apache.org on 2005/02/17 01:53:05 UTC
cvs commit: incubator-myfaces/src/components/org/apache/myfaces/custom/tree2 HtmlTree.java HtmlTreeRenderer.java TreeModel.java TreeNode.java TreeNodeBase.java TreeTag.java UITreeData.java
oros 2005/02/16 16:53:05
Modified: src/components/org/apache/myfaces/custom/tree2 HtmlTree.java
HtmlTreeRenderer.java TreeModel.java TreeNode.java
TreeNodeBase.java TreeTag.java UITreeData.java
Log:
reformat
Revision Changes Path
1.2 +124 -114 incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/HtmlTree.java
Index: HtmlTree.java
===================================================================
RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/HtmlTree.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HtmlTree.java 15 Feb 2005 22:23:18 -0000 1.1
+++ HtmlTree.java 17 Feb 2005 00:53:04 -0000 1.2
@@ -1,114 +1,124 @@
-/*
- * Copyright 2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.myfaces.custom.tree2;
-
-import javax.faces.context.FacesContext;
-
-import java.util.HashSet;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
- * @author Sean Schofield
- * @version $Revision$ $Date$
- */
-public class HtmlTree extends UITreeData
-{
- public static final String COMPONENT_TYPE = "org.apache.myfaces.Tree2";
- private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Tree2";
-
- private String _varNodeToggler;
- private HashSet _expandedNodes = new HashSet();
-
- /**
- * Constructor
- */
- public HtmlTree()
- {
- setRendererType(DEFAULT_RENDERER_TYPE);
- }
-
- // see superclass for documentation
- public Object saveState(FacesContext context)
- {
- Object values[] = new Object[3];
- values[0] = super.saveState(context);
- values[1] = _expandedNodes;
- values[2] = _varNodeToggler;
- return ((Object) (values));
- }
-
- // see superclass for documentation
- public void restoreState(FacesContext context, Object state)
- {
- Object values[] = (Object[])state;
- super.restoreState(context, values[0]);
- _expandedNodes = (HashSet)values[1];
- _varNodeToggler = (String)values[2];
- }
-
- // see superclass for documentation
- public void setNodeId(String nodeId)
- {
- super.setNodeId(nodeId);
-
- if (_varNodeToggler != null)
- {
- Map requestMap = getFacesContext().getExternalContext().getRequestMap();
- requestMap.put(_varNodeToggler, this);
- }
- }
-
- public void setVarNodeToggler(String varNodeToggler)
- {
- _varNodeToggler = varNodeToggler;
- }
-
- public String toggleExpanded()
- {
- String nodeId = getNodeId();
-
- if (_expandedNodes.contains(nodeId))
- {
- _expandedNodes.remove(nodeId);
- }
- else
- {
- _expandedNodes.add(nodeId);
- }
-
- return null;
- }
-
- /**
- * Indicates whether or not the current {@link TreeNode} is expanded.
- * @return boolean
- */
- public boolean isNodeExpanded()
- {
- return (_expandedNodes.contains(getNodeId()) && getNode().getChildCount() > 0);
- }
-
- protected void processChildNodes(FacesContext context, TreeNode parentNode, int processAction)
- {
- String parentNodeId = getNodeId();
-
- if (_expandedNodes.contains(parentNodeId))
- {
- super.processChildNodes(context, parentNode, processAction);
- }
- }
-}
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.tree2;
+
+import javax.faces.context.FacesContext;
+
+import java.util.HashSet;
+import java.util.Map;
+
+
+/**
+ * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
+ * @author Sean Schofield
+ * @version $Revision$ $Date$
+ */
+public class HtmlTree extends UITreeData
+{
+
+ public static final String COMPONENT_TYPE = "org.apache.myfaces.Tree2";
+ private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Tree2";
+
+ private String _varNodeToggler;
+ private HashSet _expandedNodes = new HashSet();
+
+
+ /**
+ * Constructor
+ */
+ public HtmlTree()
+ {
+ setRendererType(DEFAULT_RENDERER_TYPE);
+ }
+
+
+ // see superclass for documentation
+ public Object saveState(FacesContext context)
+ {
+ Object values[] = new Object[3];
+ values[0] = super.saveState(context);
+ values[1] = _expandedNodes;
+ values[2] = _varNodeToggler;
+ return ((Object) (values));
+ }
+
+
+ // see superclass for documentation
+ public void restoreState(FacesContext context, Object state)
+ {
+ Object values[] = (Object[]) state;
+ super.restoreState(context, values[0]);
+ _expandedNodes = (HashSet) values[1];
+ _varNodeToggler = (String) values[2];
+ }
+
+
+ // see superclass for documentation
+ public void setNodeId(String nodeId)
+ {
+ super.setNodeId(nodeId);
+
+ if (_varNodeToggler != null)
+ {
+ Map requestMap = getFacesContext().getExternalContext().getRequestMap();
+ requestMap.put(_varNodeToggler, this);
+ }
+ }
+
+
+ public void setVarNodeToggler(String varNodeToggler)
+ {
+ _varNodeToggler = varNodeToggler;
+ }
+
+
+ public String toggleExpanded()
+ {
+ String nodeId = getNodeId();
+
+ if (_expandedNodes.contains(nodeId))
+ {
+ _expandedNodes.remove(nodeId);
+ } else
+ {
+ _expandedNodes.add(nodeId);
+ }
+
+ return null;
+ }
+
+
+ /**
+ * Indicates whether or not the current {@link TreeNode} is expanded.
+ *
+ * @return boolean
+ */
+ public boolean isNodeExpanded()
+ {
+ return (_expandedNodes.contains(getNodeId()) && getNode().getChildCount() > 0);
+ }
+
+
+ protected void processChildNodes(FacesContext context, TreeNode parentNode, int processAction)
+ {
+ String parentNodeId = getNodeId();
+
+ if (_expandedNodes.contains(parentNodeId))
+ {
+ super.processChildNodes(context, parentNode, processAction);
+ }
+ }
+}
1.2 +269 -262 incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/HtmlTreeRenderer.java
Index: HtmlTreeRenderer.java
===================================================================
RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/HtmlTreeRenderer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HtmlTreeRenderer.java 15 Feb 2005 22:23:18 -0000 1.1
+++ HtmlTreeRenderer.java 17 Feb 2005 00:53:04 -0000 1.2
@@ -1,263 +1,270 @@
-/*
- * Copyright 2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.myfaces.custom.tree2;
-
-
-
-import org.apache.myfaces.component.html.util.AddResource;
-import org.apache.myfaces.renderkit.html.HTML;
-
-import javax.faces.component.NamingContainer;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.FacesContext;
-import javax.faces.context.ResponseWriter;
-import javax.faces.render.Renderer;
-
-import java.io.IOException;
-import java.util.List;
-
-/**
- * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
- * @author Sean Schofield
- * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
- * @version $Revision$ $Date$
- */
-public class HtmlTreeRenderer extends Renderer
-{
-
- private static final int NOTHING = 0;
- private static final int CHILDREN = 1;
- private static final int EXPANDED = 2;
- private static final int LINES = 4;
- private static final int LAST = 8;
-
- /** @todo - this is not thread safe */
- int nodeLevel;
-
- // see superclass for documentation
- public boolean getRendersChildren()
- {
- return true;
- }
-
- public void encodeBegin(FacesContext context, UIComponent uiComponent) throws IOException
- {
- // Rendering occurs in encodeEnd.
- }
-
- /**
- * Renders the whole tree. It generates a <code><span></code> element with an <code>id</code>
- * attribute if the component has been given an explicit ID. The model nodes are rendered
- * recursively by the private <code>encodeNodes</code> method.
- *
- * @param context FacesContext
- * @param component The component whose children are to be rendered
- * @throws IOException
- */
- public void encodeChildren(FacesContext context, UIComponent component) throws IOException
- {
- // reset the nodeLevel (should already be zero but it can't hurt)
- nodeLevel = 0;
-
- if (!component.isRendered()) return;
-
- if (((HtmlTree)component).getValue() == null) return;
-
- ResponseWriter out = context.getResponseWriter();
- String clientId = null;
- if (component.getId() != null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
- {
- clientId = component.getClientId(context);
- }
-
- boolean isOuterSpanUsed = false;
- if (clientId != null)
- {
- isOuterSpanUsed = true;
- out.startElement("span", component);
- out.writeAttribute("id", clientId, "id");
- }
-
- encodeNodes(context, out, (HtmlTree)component, null, 0);
-
- ((HtmlTree)component).setNodeId(null);
- if (isOuterSpanUsed)
- {
- out.endElement("span");
- }
- }
-
- private void encodeNodes(FacesContext context, ResponseWriter out, HtmlTree tree, String parentId, int childCount)
- throws IOException
- {
-
- /** @todo - showNav and showLines should be parameters */
- boolean showNav = true;
- boolean showLines = true;
-
- tree.setNodeId(parentId != null ? parentId + NamingContainer.SEPARATOR_CHAR + childCount : "0");
-
- TreeNode node = tree.getNode();
-
- UIComponent facet = tree.getFacet(node.getType());
- if (facet == null)
- {
- throw new IllegalArgumentException("Unable to locate facet with the name: " + node.getType());
- }
-
- /** @todo - format rendered html */
-
- // start node table
- out.startElement(HTML.TABLE_ELEM, tree);
- out.writeAttribute(HTML.CELLPADDING_ATTR, "0", null);
- out.writeAttribute(HTML.CELLSPACING_ATTR, "0", null);
- out.writeAttribute(HTML.BORDER_ATTR, "0", null);
- out.startElement(HTML.TR_ELEM, tree);
-
- // render node padding
- String[] pathInfo = tree.getPathInformation(tree.getNodeId());
- for (int i=0;i<pathInfo.length-1;i++)
- {
- String lineSrc = (!tree.isLastChild(pathInfo[i]) && showLines)
- ? AddResource.getResourceMappedPath(HtmlTreeRenderer.class, "images/line-trunk.gif", context)
- : AddResource.getResourceMappedPath(HtmlTreeRenderer.class, "images/spacer.gif", context);
- out.startElement(HTML.TD_ELEM, tree);
- out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
- out.writeAttribute(HTML.HEIGHT_ATTR, "100%", null);
- out.writeURIAttribute("background", lineSrc, null);
- out.startElement(HTML.IMG_ELEM, tree);
- out.writeURIAttribute(HTML.SRC_ATTR, lineSrc, null);
- out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
- out.writeAttribute(HTML.HEIGHT_ATTR, "18", null);
- out.writeAttribute(HTML.BORDER_ATTR, "0", null);
- out.endElement(HTML.IMG_ELEM);
- out.endElement(HTML.TD_ELEM);
- }
-
- // render navigation
- if (showNav)
- {
- String navSrc = null;
- int bitMask = NOTHING;
- bitMask += (node.getChildCount()>0) ? CHILDREN : NOTHING;
- bitMask += (tree.isNodeExpanded()) ? EXPANDED : NOTHING;
- bitMask += (tree.isLastChild(tree.getNodeId())) ? LAST : NOTHING;
- bitMask += (showLines) ? LINES : NOTHING;
-
- switch (bitMask)
- {
- case (NOTHING):
- case (LAST):
- navSrc = "images/spacer.gif";
- break;
- case (LINES):
- navSrc = "images/line-middle.gif";
- break;
- case (LINES + LAST):
- navSrc = "images/line-last.gif";
- break;
- case (CHILDREN):
- case (CHILDREN + LAST):
- navSrc = "images/nav-plus.gif";
- break;
- case (CHILDREN + LINES):
- navSrc = "images/nav-plus-line-middle.gif";
- break;
- case (CHILDREN + LINES + LAST):
- navSrc = "images/nav-plus-line-last.gif";
- break;
- case (CHILDREN + EXPANDED):
- case (CHILDREN + EXPANDED + LAST):
- navSrc = "images/nav-minus.gif";
- break;
- case (CHILDREN + EXPANDED + LINES):
- navSrc = "images/nav-minus-line-middle.gif";
- break;
- case (CHILDREN + EXPANDED + LINES + LAST):
- navSrc = "images/nav-minus-line-last.gif";
- break;
- default:
- throw new IllegalArgumentException("Invalid bit mask of " + bitMask);
- }
-
- // render nav cell
- out.startElement(HTML.TD_ELEM, tree);
- out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
- out.writeAttribute(HTML.HEIGHT_ATTR, "100%", null);
- out.writeAttribute("valign", "top", null);
- if ((bitMask & LINES)!=0 && (bitMask & LAST)==0)
- {
- out.writeURIAttribute("background",
- AddResource.getResourceMappedPath(HtmlTreeRenderer.class, "images/line-trunk.gif", context),
- null);
- }
- out.startElement(HTML.IMG_ELEM, tree);
- out.writeURIAttribute(HTML.SRC_ATTR, AddResource.getResourceMappedPath(HtmlTreeRenderer.class, navSrc, context) , null);
- out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
- out.writeAttribute(HTML.HEIGHT_ATTR, "18", null);
- out.writeAttribute(HTML.BORDER_ATTR, "0", null);
- out.endElement(HTML.IMG_ELEM);
- out.endElement(HTML.TD_ELEM);
- }
-
- // render node
- out.startElement(HTML.TD_ELEM, tree);
- encodeRecursive(context, facet);
- out.endElement(HTML.TD_ELEM);
-
- // end node table
- out.endElement(HTML.TR_ELEM);
- out.endElement(HTML.TABLE_ELEM);
-
- // only encode the children if this node is expanded
- if (tree.isNodeExpanded())
- {
- nodeLevel++;
- int kidId = 0;
- String currId = tree.getNodeId();
- List children = node.getChildren();
- for (int i = 0; i < children.size(); i++) {
- encodeNodes(context, out, tree, currId, kidId++);
- }
- nodeLevel--;
- }
- }
-
- private void encodeRecursive(FacesContext context, UIComponent component) throws IOException
- {
- /**@todo consider moving this common functionality to a base class or utility class */
- if (!component.isRendered()) return;
-
- component.encodeBegin(context);
-
- if (component.getRendersChildren())
- {
- component.encodeChildren(context);
- }
- else
- {
- List childList = component.getChildren();
- for (int i=0; i < childList.size(); i++)
- {
- UIComponent child = (UIComponent)childList.get(i);
- encodeRecursive(context, child);
- }
- }
-
- component.encodeEnd(context);
- }
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.tree2;
+
+
+import org.apache.myfaces.component.html.util.AddResource;
+import org.apache.myfaces.renderkit.html.HTML;
+
+import javax.faces.component.NamingContainer;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.Renderer;
+
+import java.io.IOException;
+import java.util.List;
+
+
+/**
+ * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
+ * @author Sean Schofield
+ * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
+ * @version $Revision$ $Date$
+ */
+public class HtmlTreeRenderer extends Renderer
+{
+
+ private static final int NOTHING = 0;
+ private static final int CHILDREN = 1;
+ private static final int EXPANDED = 2;
+ private static final int LINES = 4;
+ private static final int LAST = 8;
+
+ /**
+ * @todo - this is not thread safe
+ */
+ int nodeLevel;
+
+
+ // see superclass for documentation
+ public boolean getRendersChildren()
+ {
+ return true;
+ }
+
+
+ public void encodeBegin(FacesContext context, UIComponent uiComponent) throws IOException
+ {
+ // Rendering occurs in encodeEnd.
+ }
+
+
+ /**
+ * Renders the whole tree. It generates a <code><span></code> element with an <code>id</code>
+ * attribute if the component has been given an explicit ID. The model nodes are rendered
+ * recursively by the private <code>encodeNodes</code> method.
+ *
+ * @param context FacesContext
+ * @param component The component whose children are to be rendered
+ * @throws IOException
+ */
+ public void encodeChildren(FacesContext context, UIComponent component) throws IOException
+ {
+ // reset the nodeLevel (should already be zero but it can't hurt)
+ nodeLevel = 0;
+
+ if (!component.isRendered()) return;
+
+ if (((HtmlTree) component).getValue() == null) return;
+
+ ResponseWriter out = context.getResponseWriter();
+ String clientId = null;
+ if (component.getId() != null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
+ {
+ clientId = component.getClientId(context);
+ }
+
+ boolean isOuterSpanUsed = false;
+ if (clientId != null)
+ {
+ isOuterSpanUsed = true;
+ out.startElement("span", component);
+ out.writeAttribute("id", clientId, "id");
+ }
+
+ encodeNodes(context, out, (HtmlTree) component, null, 0);
+
+ ((HtmlTree) component).setNodeId(null);
+ if (isOuterSpanUsed)
+ {
+ out.endElement("span");
+ }
+ }
+
+
+ private void encodeNodes(FacesContext context, ResponseWriter out, HtmlTree tree, String parentId, int childCount)
+ throws IOException
+ {
+
+ /** @todo - showNav and showLines should be parameters */
+ boolean showNav = true;
+ boolean showLines = true;
+
+ tree.setNodeId(parentId != null ? parentId + NamingContainer.SEPARATOR_CHAR + childCount : "0");
+
+ TreeNode node = tree.getNode();
+
+ UIComponent facet = tree.getFacet(node.getType());
+ if (facet == null)
+ {
+ throw new IllegalArgumentException("Unable to locate facet with the name: " + node.getType());
+ }
+
+ /** @todo - format rendered html */
+
+ // start node table
+ out.startElement(HTML.TABLE_ELEM, tree);
+ out.writeAttribute(HTML.CELLPADDING_ATTR, "0", null);
+ out.writeAttribute(HTML.CELLSPACING_ATTR, "0", null);
+ out.writeAttribute(HTML.BORDER_ATTR, "0", null);
+ out.startElement(HTML.TR_ELEM, tree);
+
+ // render node padding
+ String[] pathInfo = tree.getPathInformation(tree.getNodeId());
+ for (int i = 0; i < pathInfo.length - 1; i++)
+ {
+ String lineSrc = (!tree.isLastChild(pathInfo[i]) && showLines)
+ ? AddResource.getResourceMappedPath(HtmlTreeRenderer.class, "images/line-trunk.gif", context)
+ : AddResource.getResourceMappedPath(HtmlTreeRenderer.class, "images/spacer.gif", context);
+ out.startElement(HTML.TD_ELEM, tree);
+ out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
+ out.writeAttribute(HTML.HEIGHT_ATTR, "100%", null);
+ out.writeURIAttribute("background", lineSrc, null);
+ out.startElement(HTML.IMG_ELEM, tree);
+ out.writeURIAttribute(HTML.SRC_ATTR, lineSrc, null);
+ out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
+ out.writeAttribute(HTML.HEIGHT_ATTR, "18", null);
+ out.writeAttribute(HTML.BORDER_ATTR, "0", null);
+ out.endElement(HTML.IMG_ELEM);
+ out.endElement(HTML.TD_ELEM);
+ }
+
+ // render navigation
+ if (showNav)
+ {
+ String navSrc = null;
+ int bitMask = NOTHING;
+ bitMask += (node.getChildCount() > 0) ? CHILDREN : NOTHING;
+ bitMask += (tree.isNodeExpanded()) ? EXPANDED : NOTHING;
+ bitMask += (tree.isLastChild(tree.getNodeId())) ? LAST : NOTHING;
+ bitMask += (showLines) ? LINES : NOTHING;
+
+ switch (bitMask)
+ {
+ case (NOTHING):
+ case (LAST):
+ navSrc = "images/spacer.gif";
+ break;
+ case (LINES):
+ navSrc = "images/line-middle.gif";
+ break;
+ case (LINES + LAST):
+ navSrc = "images/line-last.gif";
+ break;
+ case (CHILDREN):
+ case (CHILDREN + LAST):
+ navSrc = "images/nav-plus.gif";
+ break;
+ case (CHILDREN + LINES):
+ navSrc = "images/nav-plus-line-middle.gif";
+ break;
+ case (CHILDREN + LINES + LAST):
+ navSrc = "images/nav-plus-line-last.gif";
+ break;
+ case (CHILDREN + EXPANDED):
+ case (CHILDREN + EXPANDED + LAST):
+ navSrc = "images/nav-minus.gif";
+ break;
+ case (CHILDREN + EXPANDED + LINES):
+ navSrc = "images/nav-minus-line-middle.gif";
+ break;
+ case (CHILDREN + EXPANDED + LINES + LAST):
+ navSrc = "images/nav-minus-line-last.gif";
+ break;
+ default:
+ throw new IllegalArgumentException("Invalid bit mask of " + bitMask);
+ }
+
+ // render nav cell
+ out.startElement(HTML.TD_ELEM, tree);
+ out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
+ out.writeAttribute(HTML.HEIGHT_ATTR, "100%", null);
+ out.writeAttribute("valign", "top", null);
+ if ((bitMask & LINES) != 0 && (bitMask & LAST) == 0)
+ {
+ out.writeURIAttribute("background",
+ AddResource.getResourceMappedPath(HtmlTreeRenderer.class, "images/line-trunk.gif", context),
+ null);
+ }
+ out.startElement(HTML.IMG_ELEM, tree);
+ out.writeURIAttribute(HTML.SRC_ATTR, AddResource.getResourceMappedPath(HtmlTreeRenderer.class, navSrc, context), null);
+ out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
+ out.writeAttribute(HTML.HEIGHT_ATTR, "18", null);
+ out.writeAttribute(HTML.BORDER_ATTR, "0", null);
+ out.endElement(HTML.IMG_ELEM);
+ out.endElement(HTML.TD_ELEM);
+ }
+
+ // render node
+ out.startElement(HTML.TD_ELEM, tree);
+ encodeRecursive(context, facet);
+ out.endElement(HTML.TD_ELEM);
+
+ // end node table
+ out.endElement(HTML.TR_ELEM);
+ out.endElement(HTML.TABLE_ELEM);
+
+ // only encode the children if this node is expanded
+ if (tree.isNodeExpanded())
+ {
+ nodeLevel++;
+ int kidId = 0;
+ String currId = tree.getNodeId();
+ List children = node.getChildren();
+ for (int i = 0; i < children.size(); i++)
+ {
+ encodeNodes(context, out, tree, currId, kidId++);
+ }
+ nodeLevel--;
+ }
+ }
+
+
+ private void encodeRecursive(FacesContext context, UIComponent component) throws IOException
+ {
+ /**@todo consider moving this common functionality to a base class or utility class */
+ if (!component.isRendered()) return;
+
+ component.encodeBegin(context);
+
+ if (component.getRendersChildren())
+ {
+ component.encodeChildren(context);
+ } else
+ {
+ List childList = component.getChildren();
+ for (int i = 0; i < childList.size(); i++)
+ {
+ UIComponent child = (UIComponent) childList.get(i);
+ encodeRecursive(context, child);
+ }
+ }
+
+ component.encodeEnd(context);
+ }
}
\ No newline at end of file
1.2 +194 -185 incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeModel.java
Index: TreeModel.java
===================================================================
RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeModel.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TreeModel.java 15 Feb 2005 22:23:18 -0000 1.1
+++ TreeModel.java 17 Feb 2005 00:53:04 -0000 1.2
@@ -1,185 +1,194 @@
-/*
- * Copyright 2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.myfaces.custom.tree2;
-
-import javax.faces.component.NamingContainer;
-
-import java.util.StringTokenizer;
-import java.util.ArrayList;
-
-/**
- * Model class for the tree component. It provides random access to nodes in a tree
- * made up of instances of the {@link TreeNode} class.
- *
- * @author Sean Schofield
- * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
- * @version $Revision$ $Date$
- */
-public class TreeModel
-{
- private final static String SEPARATOR = String.valueOf(NamingContainer.SEPARATOR_CHAR);
-
- private TreeNode root;
- private TreeNode currentNode;
-
- /**
- * Constructor
- * @param root The root TreeNode
- */
- public TreeModel(TreeNode root)
- {
- this.root = root;
- }
-
- /**
- * Gets the current {@link TreeNode} or <code>null</code> if no node ID is selected.
- * @return The current node
- */
- public TreeNode getNode()
- {
- return currentNode;
- }
-
- /**
- * Sets the current {@link TreeNode} to the specified node ID, which is a colon-separated list
- * of node indexes. For instance, "0:0:1" means "the second child node of the first child node
- * under the root node."
- *
- * @param nodeId The id of the node to set
- */
- public void setNodeId(String nodeId)
- {
- if (nodeId == null)
- {
- currentNode = null;
- return;
- }
-
-// TreeNode node = root;
-//
-// StringBuffer sb = new StringBuffer();
-// StringTokenizer st = new StringTokenizer(nodeId, SEPARATOR);
-// sb.append(st.nextToken()).append(SEPARATOR);
-//
-// while (st.hasMoreTokens())
-// {
-// int nodeIndex = Integer.parseInt(st.nextToken());
-// sb.append(nodeIndex);
-//
-// try
-// {
-// node = (TreeNode)node.getChildren().get(nodeIndex);
-// }
-// catch (IndexOutOfBoundsException e)
-// {
-// String msg = "Node with id " + sb.toString() + ". Failed to parse " + nodeId;
-// throw new IllegalArgumentException(msg);
-// }
-// sb.append(SEPARATOR);
-// }
-//
-// currentNode = node;
-
- currentNode = getNodeById(nodeId);
- }
-
- /**
- * Gets an array of String containing the ID's of all of the {@link TreeNode}s in the path to
- * the specified node. The path information will be an array of <code>String</code> objects
- * representing node ID's. The array will starting with the ID of the root node and end with
- * the ID of the specified node.
- *
- * @param nodeId The id of the node for whom the path information is needed.
- * @return String[]
- */
- public String[] getPathInformation(String nodeId)
- {
- if (nodeId == null)
- {
- throw new IllegalArgumentException("Cannot determine parents for a null node.");
- }
-
- ArrayList pathList = new ArrayList();
- pathList.add(nodeId);
-
- while (nodeId.lastIndexOf(SEPARATOR) != -1)
- {
- nodeId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
- pathList.add(nodeId);
- }
-
- String[] pathInfo = new String[pathList.size()];
-
- for (int i=0; i < pathInfo.length; i++)
- {
- pathInfo[i] = (String)pathList.get(pathInfo.length - i - 1);
- }
-
- return pathInfo;
- }
-
- /**
- * Indicates whether or not the specified {@link TreeNode} is the last child in the <code>List</code>
- * of children. If the node id provided corresponds to the root node, this returns <code>true</code>.
- *
- * @param nodeId The ID of the node to check
- * @return boolean
- */
- public boolean isLastChild(String nodeId)
- {
- if (nodeId.lastIndexOf(SEPARATOR) == -1)
- {
- // root node considered to be the last child
- return true;
- }
-
- //first get the id of the parent
- String parentId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
- String childString = nodeId.substring(nodeId.lastIndexOf(SEPARATOR) + 1);
- int childId = Integer.parseInt(childString);
- TreeNode parentNode = getNodeById(parentId);
-
- return childId + 1== parentNode.getChildCount();
- }
-
- private TreeNode getNodeById(String nodeId)
- {
- TreeNode node = root;
-
- StringBuffer sb = new StringBuffer();
- StringTokenizer st = new StringTokenizer(nodeId, SEPARATOR);
- sb.append(st.nextToken()).append(SEPARATOR);
-
- while (st.hasMoreTokens())
- {
- int nodeIndex = Integer.parseInt(st.nextToken());
- sb.append(nodeIndex);
-
- try
- {
- node = (TreeNode)node.getChildren().get(nodeIndex);
- }
- catch (IndexOutOfBoundsException e)
- {
- String msg = "Node with id " + sb.toString() + ". Failed to parse " + nodeId;
- throw new IllegalArgumentException(msg);
- }
- sb.append(SEPARATOR);
- }
-
- return node;
- }
-}
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.tree2;
+
+import javax.faces.component.NamingContainer;
+
+import java.util.StringTokenizer;
+import java.util.ArrayList;
+
+
+/**
+ * Model class for the tree component. It provides random access to nodes in a tree
+ * made up of instances of the {@link TreeNode} class.
+ *
+ * @author Sean Schofield
+ * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
+ * @version $Revision$ $Date$
+ */
+public class TreeModel
+{
+
+ private final static String SEPARATOR = String.valueOf(NamingContainer.SEPARATOR_CHAR);
+
+ private TreeNode root;
+ private TreeNode currentNode;
+
+
+ /**
+ * Constructor
+ *
+ * @param root The root TreeNode
+ */
+ public TreeModel(TreeNode root)
+ {
+ this.root = root;
+ }
+
+
+ /**
+ * Gets the current {@link TreeNode} or <code>null</code> if no node ID is selected.
+ *
+ * @return The current node
+ */
+ public TreeNode getNode()
+ {
+ return currentNode;
+ }
+
+
+ /**
+ * Sets the current {@link TreeNode} to the specified node ID, which is a colon-separated list
+ * of node indexes. For instance, "0:0:1" means "the second child node of the first child node
+ * under the root node."
+ *
+ * @param nodeId The id of the node to set
+ */
+ public void setNodeId(String nodeId)
+ {
+ if (nodeId == null)
+ {
+ currentNode = null;
+ return;
+ }
+
+// TreeNode node = root;
+//
+// StringBuffer sb = new StringBuffer();
+// StringTokenizer st = new StringTokenizer(nodeId, SEPARATOR);
+// sb.append(st.nextToken()).append(SEPARATOR);
+//
+// while (st.hasMoreTokens())
+// {
+// int nodeIndex = Integer.parseInt(st.nextToken());
+// sb.append(nodeIndex);
+//
+// try
+// {
+// node = (TreeNode)node.getChildren().get(nodeIndex);
+// }
+// catch (IndexOutOfBoundsException e)
+// {
+// String msg = "Node with id " + sb.toString() + ". Failed to parse " + nodeId;
+// throw new IllegalArgumentException(msg);
+// }
+// sb.append(SEPARATOR);
+// }
+//
+// currentNode = node;
+
+ currentNode = getNodeById(nodeId);
+ }
+
+
+ /**
+ * Gets an array of String containing the ID's of all of the {@link TreeNode}s in the path to
+ * the specified node. The path information will be an array of <code>String</code> objects
+ * representing node ID's. The array will starting with the ID of the root node and end with
+ * the ID of the specified node.
+ *
+ * @param nodeId The id of the node for whom the path information is needed.
+ * @return String[]
+ */
+ public String[] getPathInformation(String nodeId)
+ {
+ if (nodeId == null)
+ {
+ throw new IllegalArgumentException("Cannot determine parents for a null node.");
+ }
+
+ ArrayList pathList = new ArrayList();
+ pathList.add(nodeId);
+
+ while (nodeId.lastIndexOf(SEPARATOR) != -1)
+ {
+ nodeId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
+ pathList.add(nodeId);
+ }
+
+ String[] pathInfo = new String[pathList.size()];
+
+ for (int i = 0; i < pathInfo.length; i++)
+ {
+ pathInfo[i] = (String) pathList.get(pathInfo.length - i - 1);
+ }
+
+ return pathInfo;
+ }
+
+
+ /**
+ * Indicates whether or not the specified {@link TreeNode} is the last child in the <code>List</code>
+ * of children. If the node id provided corresponds to the root node, this returns <code>true</code>.
+ *
+ * @param nodeId The ID of the node to check
+ * @return boolean
+ */
+ public boolean isLastChild(String nodeId)
+ {
+ if (nodeId.lastIndexOf(SEPARATOR) == -1)
+ {
+ // root node considered to be the last child
+ return true;
+ }
+
+ //first get the id of the parent
+ String parentId = nodeId.substring(0, nodeId.lastIndexOf(SEPARATOR));
+ String childString = nodeId.substring(nodeId.lastIndexOf(SEPARATOR) + 1);
+ int childId = Integer.parseInt(childString);
+ TreeNode parentNode = getNodeById(parentId);
+
+ return childId + 1 == parentNode.getChildCount();
+ }
+
+
+ private TreeNode getNodeById(String nodeId)
+ {
+ TreeNode node = root;
+
+ StringBuffer sb = new StringBuffer();
+ StringTokenizer st = new StringTokenizer(nodeId, SEPARATOR);
+ sb.append(st.nextToken()).append(SEPARATOR);
+
+ while (st.hasMoreTokens())
+ {
+ int nodeIndex = Integer.parseInt(st.nextToken());
+ sb.append(nodeIndex);
+
+ try
+ {
+ node = (TreeNode) node.getChildren().get(nodeIndex);
+ } catch (IndexOutOfBoundsException e)
+ {
+ String msg = "Node with id " + sb.toString() + ". Failed to parse " + nodeId;
+ throw new IllegalArgumentException(msg);
+ }
+ sb.append(SEPARATOR);
+ }
+
+ return node;
+ }
+}
1.2 +86 -70 incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeNode.java
Index: TreeNode.java
===================================================================
RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeNode.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TreeNode.java 15 Feb 2005 22:23:18 -0000 1.1
+++ TreeNode.java 17 Feb 2005 00:53:04 -0000 1.2
@@ -1,70 +1,86 @@
-/*
- * Copyright 2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.myfaces.custom.tree2;
-
-import java.util.List;
-import java.io.Serializable;
-
-/**
- * Defines the requirements for an object that can be used as a tree node for
- * use in a {@link UITreeData} component. (inspired by javax.swing.tree.TreeNode).
- *
- * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
- * @author Sean Schofield
- * @version $Revision$ $Date$
- */
-public interface TreeNode extends Serializable
-{
- public boolean isLeaf();
-
- public void setLeaf(boolean leaf);
-
- public List getChildren();
-
- /**
- * Gets the type of {@link TreeNode}.
- * @return The node type
- */
- public String getType();
-
- /**
- * Sets the type of {@link TreeNode}.
- * @param type The node type
- */
- public void setType(String type);
-
- public String getDescription();
-
- public void setDescription(String description);
-
- /**
- * Sets the identifier associated with the {@link TreeNode}.
- * @param id The identifier
- */
- public void setIdentifier(String identifier);
-
- /**
- * Gets the identifier asociated with the {@link TreeNode}.
- * @return the identifier
- */
- public String getIdentifier();
-
- /**
- * Gets the number of children this node has.
- * @return the number of children
- */
- public int getChildCount();
-}
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.tree2;
+
+import java.util.List;
+import java.io.Serializable;
+
+
+/**
+ * Defines the requirements for an object that can be used as a tree node for
+ * use in a {@link UITreeData} component. (inspired by javax.swing.tree.TreeNode).
+ *
+ * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
+ * @author Sean Schofield
+ * @version $Revision$ $Date$
+ */
+public interface TreeNode extends Serializable
+{
+
+ public boolean isLeaf();
+
+
+ public void setLeaf(boolean leaf);
+
+
+ public List getChildren();
+
+
+ /**
+ * Gets the type of {@link TreeNode}.
+ *
+ * @return The node type
+ */
+ public String getType();
+
+
+ /**
+ * Sets the type of {@link TreeNode}.
+ *
+ * @param type The node type
+ */
+ public void setType(String type);
+
+
+ public String getDescription();
+
+
+ public void setDescription(String description);
+
+
+ /**
+ * Sets the identifier associated with the {@link TreeNode}.
+ *
+ * @param id The identifier
+ */
+ public void setIdentifier(String identifier);
+
+
+ /**
+ * Gets the identifier asociated with the {@link TreeNode}.
+ *
+ * @return the identifier
+ */
+ public String getIdentifier();
+
+
+ /**
+ * Gets the number of children this node has.
+ *
+ * @return the number of children
+ */
+ public int getChildCount();
+}
1.2 +96 -80 incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeNodeBase.java
Index: TreeNodeBase.java
===================================================================
RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeNodeBase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TreeNodeBase.java 15 Feb 2005 22:23:18 -0000 1.1
+++ TreeNodeBase.java 17 Feb 2005 00:53:04 -0000 1.2
@@ -1,80 +1,96 @@
-package org.apache.myfaces.custom.tree2;
-
-import java.util.List;
-import java.util.ArrayList;
-
-public class TreeNodeBase implements TreeNode
-{
- private ArrayList children = new ArrayList();
- private String type;
- private String description;
- private boolean leaf;
- private String identifier;
- private boolean expanded;
-
- public TreeNodeBase()
- {}
-
- public TreeNodeBase(String type, String description, boolean leaf)
- {
- this(type, description, null, leaf);
- }
-
- public TreeNodeBase(String type, String description, String identifier, boolean leaf)
- {
- this.type = type;
- this.description = description;
- this.identifier = identifier;
- this.leaf = leaf;
- }
-
- public boolean isLeaf()
- {
- return leaf;
- }
-
- public void setLeaf(boolean leaf)
- {
- this.leaf = leaf;
- }
-
- public List getChildren()
- {
- return children;
- }
-
- public String getType()
- {
- return type;
- }
-
- public void setType(String type)
- {
- this.type = type;
- }
-
- public void setDescription(String description)
- {
- this.description = description;
- }
-
- public String getDescription()
- {
- return description;
- }
-
- public void setIdentifier(String identifier)
- {
- this.identifier = identifier;
- }
-
- public String getIdentifier()
- {
- return identifier;
- }
-
- public int getChildCount()
- {
- return getChildren().size();
- }
-}
+package org.apache.myfaces.custom.tree2;
+
+import java.util.List;
+import java.util.ArrayList;
+
+
+public class TreeNodeBase implements TreeNode
+{
+
+ private ArrayList children = new ArrayList();
+ private String type;
+ private String description;
+ private boolean leaf;
+ private String identifier;
+ private boolean expanded;
+
+
+ public TreeNodeBase()
+ {
+ }
+
+
+ public TreeNodeBase(String type, String description, boolean leaf)
+ {
+ this(type, description, null, leaf);
+ }
+
+
+ public TreeNodeBase(String type, String description, String identifier, boolean leaf)
+ {
+ this.type = type;
+ this.description = description;
+ this.identifier = identifier;
+ this.leaf = leaf;
+ }
+
+
+ public boolean isLeaf()
+ {
+ return leaf;
+ }
+
+
+ public void setLeaf(boolean leaf)
+ {
+ this.leaf = leaf;
+ }
+
+
+ public List getChildren()
+ {
+ return children;
+ }
+
+
+ public String getType()
+ {
+ return type;
+ }
+
+
+ public void setType(String type)
+ {
+ this.type = type;
+ }
+
+
+ public void setDescription(String description)
+ {
+ this.description = description;
+ }
+
+
+ public String getDescription()
+ {
+ return description;
+ }
+
+
+ public void setIdentifier(String identifier)
+ {
+ this.identifier = identifier;
+ }
+
+
+ public String getIdentifier()
+ {
+ return identifier;
+ }
+
+
+ public int getChildCount()
+ {
+ return getChildren().size();
+ }
+}
1.2 +93 -86 incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeTag.java
Index: TreeTag.java
===================================================================
RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/TreeTag.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TreeTag.java 15 Feb 2005 22:23:18 -0000 1.1
+++ TreeTag.java 17 Feb 2005 00:53:04 -0000 1.2
@@ -1,86 +1,93 @@
-/*
- * Copyright 2005 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.myfaces.custom.tree2;
-
-
-
-import org.apache.myfaces.taglib.UIComponentTagBase;
-
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-import javax.faces.el.ValueBinding;
-
-/**
- * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
- * @author Sean Schofield
- * @version $Revision$ $Date$
- */
-public class TreeTag extends UIComponentTagBase //UIComponentBodyTagBase
-{
- private String _value;
- private String _var;
- private String _varNodeToggler;
-
- public String getComponentType()
- {
- return "org.apache.myfaces.HtmlTree2";
- }
-
- public String getRendererType()
- {
- return "org.apache.myfaces.HtmlTree2";
- }
-
- public void setValue(String value)
- {
- _value = value;
- }
-
- /**
- * @param var The var to set.
- */
- public void setVar(String var)
- {
- _var = var;
- }
-
- public void setVarNodeToggler(String varNodeToggler)
- {
- _varNodeToggler = varNodeToggler;
- }
-
- protected void setProperties(UIComponent component)
- {
- super.setProperties(component);
-
- FacesContext context = getFacesContext();
- if (_value != null)
- {
- ValueBinding vb = context.getApplication().createValueBinding(_value);
- component.setValueBinding("value", vb);
- }
-
- if (_var != null)
- {
- ((HtmlTree)component).setVar(_var);
- }
-
- if (_varNodeToggler != null)
- {
- ((HtmlTree)component).setVarNodeToggler(_varNodeToggler);
- }
- }
-}
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.tree2;
+
+
+import org.apache.myfaces.taglib.UIComponentTagBase;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+
+/**
+ * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
+ * @author Sean Schofield
+ * @version $Revision$ $Date$
+ */
+public class TreeTag extends UIComponentTagBase //UIComponentBodyTagBase
+{
+
+ private String _value;
+ private String _var;
+ private String _varNodeToggler;
+
+
+ public String getComponentType()
+ {
+ return "org.apache.myfaces.HtmlTree2";
+ }
+
+
+ public String getRendererType()
+ {
+ return "org.apache.myfaces.HtmlTree2";
+ }
+
+
+ public void setValue(String value)
+ {
+ _value = value;
+ }
+
+
+ /**
+ * @param var The var to set.
+ */
+ public void setVar(String var)
+ {
+ _var = var;
+ }
+
+
+ public void setVarNodeToggler(String varNodeToggler)
+ {
+ _varNodeToggler = varNodeToggler;
+ }
+
+
+ protected void setProperties(UIComponent component)
+ {
+ super.setProperties(component);
+
+ FacesContext context = getFacesContext();
+ if (_value != null)
+ {
+ ValueBinding vb = context.getApplication().createValueBinding(_value);
+ component.setValueBinding("value", vb);
+ }
+
+ if (_var != null)
+ {
+ ((HtmlTree) component).setVar(_var);
+ }
+
+ if (_varNodeToggler != null)
+ {
+ ((HtmlTree) component).setVarNodeToggler(_varNodeToggler);
+ }
+ }
+}
1.3 +184 -135 incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/UITreeData.java
Index: UITreeData.java
===================================================================
RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/tree2/UITreeData.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- UITreeData.java 16 Feb 2005 00:50:37 -0000 1.2
+++ UITreeData.java 17 Feb 2005 00:53:04 -0000 1.3
@@ -34,23 +34,25 @@
import java.util.HashMap;
import java.util.List;
+
/**
- * TreeData is a {@link UIComponent} that supports binding data stored in a tree represented
- * by a {@link TreeNode} instance. During iterative processing over the tree nodes in the
- * data model, the object for the current node is exposed as a request attribute under the key
+ * TreeData is a {@link UIComponent} that supports binding data stored in a tree represented
+ * by a {@link TreeNode} instance. During iterative processing over the tree nodes in the
+ * data model, the object for the current node is exposed as a request attribute under the key
* specified by the <code>var</code> property. {@link javax.faces.render.Renderer}s of this component should use
- * the appropriate {@link Facet} to assist in rendering.
- *
+ * the appropriate {@link Facet} to assist in rendering.
+ *
* @author Sean Schofield
* @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
* @version $Revision$ $Date$
*/
public class UITreeData extends UIComponentBase implements NamingContainer
{
+
public static final String COMPONENT_TYPE = "org.apache.myfaces.Tree2";
public static final String COMPONENT_FAMILY = "org.apache.myfaces.HtmlTree2"; //"javax.faces.Data";
private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Tree2";
-
+
private static final int PROCESS_DECODES = 1;
private static final int PROCESS_VALIDATORS = 2;
private static final int PROCESS_UPDATES = 3;
@@ -60,7 +62,8 @@
private String _var;
private String _nodeId;
private Map _saved = new HashMap();
-
+
+
/**
* Constructor
*/
@@ -69,12 +72,14 @@
setRendererType(DEFAULT_RENDERER_TYPE);
}
+
// see superclass for documentation
public String getFamily()
{
return COMPONENT_FAMILY;
- }
-
+ }
+
+
// see superclass for documentation
public Object saveState(FacesContext context)
{
@@ -85,54 +90,58 @@
return ((Object) (values));
}
+
// see superclass for documentation
public void restoreState(FacesContext context, Object state)
{
- Object values[] = (Object[])state;
+ Object values[] = (Object[]) state;
super.restoreState(context, values[0]);
- _value = (TreeNode)values[1];
- _var = (String)values[2];
- }
-
+ _value = (TreeNode) values[1];
+ _var = (String) values[2];
+ }
+
+
public void queueEvent(FacesEvent event)
{
super.queueEvent(new FacesEventWrapper(event, getNodeId(), this));
}
+
public void broadcast(FacesEvent event) throws AbortProcessingException
{
if (event instanceof FacesEventWrapper)
{
- FacesEventWrapper childEvent = (FacesEventWrapper)event;
+ FacesEventWrapper childEvent = (FacesEventWrapper) event;
String currNodeId = getNodeId();
setNodeId(childEvent.getNodeId());
FacesEvent nodeEvent = childEvent.getFacesEvent();
nodeEvent.getComponent().broadcast(nodeEvent);
setNodeId(currNodeId);
return;
- }
- else
+ } else
{
super.broadcast(event);
return;
}
}
+
// see superclass for documentation
public void processDecodes(FacesContext context)
{
if (context == null) throw new NullPointerException("context");
if (!isRendered()) return;
- _model = null;
+ _model = null;
_saved = new HashMap();
-
+
processNodes(context, PROCESS_DECODES, null, 0);
-
+
setNodeId(null);
decode(context);
- }
-
+ }
+
+
// see superclass for documentation
public void processValidators(FacesContext context)
{
@@ -144,6 +153,7 @@
setNodeId(null);
}
+
// see superclass for documentation
public void processUpdates(FacesContext context)
{
@@ -153,8 +163,9 @@
processNodes(context, PROCESS_UPDATES, null, 0);
setNodeId(null);
- }
-
+ }
+
+
// see superclass for documentation
public String getClientId(FacesContext context)
{
@@ -162,27 +173,27 @@
if (_nodeId != null)
{
return ownClientId + NamingContainer.SEPARATOR_CHAR + _nodeId;
- }
- else
+ } else
{
return ownClientId;
}
}
-
+
+
// see superclass for documentation
public void setValueBinding(String name, ValueBinding binding)
{
if ("value".equals(name))
{
_model = null;
- }
- else if ("nodeVar".equals(name) || "nodeId".equals(name) || "treeVar".equals(name))
+ } else if ("nodeVar".equals(name) || "nodeId".equals(name) || "treeVar".equals(name))
{
throw new IllegalArgumentException("name " + name);
}
super.setValueBinding(name, binding);
- }
-
+ }
+
+
// see superclass for documentation
public void encodeBegin(FacesContext context) throws IOException
{
@@ -196,12 +207,14 @@
{
_saved = new HashMap();
}
-
+
super.encodeBegin(context);
}
-
+
+
/**
* Sets the value of the TreeData.
+ *
* @param value The new value
*/
public void setValue(TreeNode value)
@@ -210,8 +223,10 @@
_value = value;
}
+
/**
* Gets the value of the TreeData.
+ *
* @return The value
*/
public Object getValue()
@@ -220,11 +235,12 @@
ValueBinding vb = getValueBinding("value");
return vb != null ? vb.getValue(getFacesContext()) : null;
}
-
+
+
/**
- * Set the request-scope attribute under which the data object for the current node wil be exposed
+ * Set the request-scope attribute under which the data object for the current node wil be exposed
* when iterating.
- *
+ *
* @param var The new request-scope attribute name
*/
public void setVar(String var)
@@ -232,74 +248,79 @@
_var = var;
}
+
/**
- * Return the request-scope attribute under which the data object for the current node will be exposed
+ * Return the request-scope attribute under which the data object for the current node will be exposed
* when iterating. This property is not enabled for value binding expressions.
- *
+ *
* @return The iterrator attribute
*/
public String getVar()
{
return _var;
- }
-
+ }
+
+
/**
* Calls through to the {@link TreeModel} and returns the current {@link TreeNode} or <code>null</code>.
+ *
* @return The current node
*/
public TreeNode getNode()
{
TreeModel model = getDataModel();
-
+
if (model == null)
{
return null;
}
-
+
return model.getNode();
}
-
+
+
public String getNodeId()
{
- return _nodeId;
+ return _nodeId;
}
+
public void setNodeId(String nodeId)
{
saveDescendantState();
-
+
_nodeId = nodeId;
-
+
TreeModel model = getDataModel();
if (model == null)
{
return;
}
model.setNodeId(nodeId);
-
+
restoreDescendantState();
-
+
Map requestMap = getFacesContext().getExternalContext().getRequestMap();
-
+
if (_var != null)
{
if (nodeId == null)
{
requestMap.remove(_var);
- }
- else
+ } else
{
requestMap.put(_var, getNode());
}
}
}
-
+
+
/**
- * Gets an array of String containing the ID's of all of the {@link TreeNode}s in the path to
- * the specified node. The path information will be an array of <code>String</code> objects
- * representing node ID's. The array will starting with the ID of the root node and end with
+ * Gets an array of String containing the ID's of all of the {@link TreeNode}s in the path to
+ * the specified node. The path information will be an array of <code>String</code> objects
+ * representing node ID's. The array will starting with the ID of the root node and end with
* the ID of the specified node.
- *
+ *
* @param nodeId The id of the node for whom the path information is needed.
* @return String[]
*/
@@ -307,11 +328,12 @@
{
return getDataModel().getPathInformation(nodeId);
}
-
+
+
/**
- * Indicates whether or not the specified {@link TreeNode} is the last child in the <code>List</code>
+ * Indicates whether or not the specified {@link TreeNode} is the last child in the <code>List</code>
* of children. If the node id provided corresponds to the root node, this returns <code>true</code>.
- *
+ *
* @param nodeId The ID of the node to check
* @return boolean
*/
@@ -319,12 +341,13 @@
{
return getDataModel().isLastChild(nodeId);
}
-
+
+
/**
- * Returns a previously cached {@link TreeModel}, if any, or sets the cache variable to either the
- * current value (if its a {@link TreeModel}) or to a new instance of {@link TreeModel} (if it's a
+ * Returns a previously cached {@link TreeModel}, if any, or sets the cache variable to either the
+ * current value (if its a {@link TreeModel}) or to a new instance of {@link TreeModel} (if it's a
* {@link TreeNode}) with the provided value object as the root node.
- *
+ *
* @return TreeModel
*/
private TreeModel getDataModel()
@@ -333,23 +356,23 @@
{
return _model;
}
-
+
Object value = getValue();
if (value != null)
{
if (value instanceof TreeModel)
{
- _model = (TreeModel)value;
- }
- else if (value instanceof TreeNode)
+ _model = (TreeModel) value;
+ } else if (value instanceof TreeNode)
{
- _model = new TreeModel((TreeNode)value);
+ _model = new TreeModel((TreeNode) value);
}
}
-
+
return _model;
}
+
private void processNodes(FacesContext context, int processAction, String parentId, int childLevel)
{
UIComponent facet = null;
@@ -357,39 +380,40 @@
TreeNode node = getNode();
facet = getFacet(node.getType());
-
+
if (facet == null)
{
throw new IllegalArgumentException("Unable to locate facet with the name: " + node.getType());
}
-
+
switch (processAction)
{
case PROCESS_DECODES:
-
+
facet.processDecodes(context);
break;
-
+
case PROCESS_VALIDATORS:
-
+
facet.processValidators(context);
break;
-
+
case PROCESS_UPDATES:
-
+
facet.processUpdates(context);
break;
}
-
+
processChildNodes(context, node, processAction);
}
-
+
+
/**
- * Process the child nodes of the supplied parent @{link TreeNode}. This method is protected so that
+ * Process the child nodes of the supplied parent @{link TreeNode}. This method is protected so that
* it can be overriden by a subclass that may want to control how child nodes are processed.
- *
- * @param context FacesContext
- * @param parentNode The parent node whose children are to be processed
+ *
+ * @param context FacesContext
+ * @param parentNode The parent node whose children are to be processed
* @param processAction An <code>int</code> representing the type of action to process
*/
protected void processChildNodes(FacesContext context, TreeNode parentNode, int processAction)
@@ -399,16 +423,17 @@
List children = parentNode.getChildren();
- for (int i=0; i < children.size(); i++)
+ for (int i = 0; i < children.size(); i++)
{
processNodes(context, processAction, currId, kidId++);
- }
- }
-
- /**
- * To support using input components for the nodes (e.g., input fields, checkboxes, and selection
- * lists) while still only using one set of components for all nodes, the state held by the components
- * for the current node must be saved for a new node is selected.
+ }
+ }
+
+
+ /**
+ * To support using input components for the nodes (e.g., input fields, checkboxes, and selection
+ * lists) while still only using one set of components for all nodes, the state held by the components
+ * for the current node must be saved for a new node is selected.
*/
private void saveDescendantState()
{
@@ -416,24 +441,25 @@
Iterator i = getFacets().values().iterator();
while (i.hasNext())
{
- UIComponent facet = (UIComponent)i.next();
+ UIComponent facet = (UIComponent) i.next();
saveDescendantState(facet, context);
}
}
-
+
+
/**
* Overloaded helper method for the no argument version of this method.
- *
+ *
* @param component The component whose state needs to be saved
- * @param context FacesContext
+ * @param context FacesContext
*/
private void saveDescendantState(UIComponent component, FacesContext context)
{
if (component instanceof EditableValueHolder)
{
- EditableValueHolder input = (EditableValueHolder)component;
+ EditableValueHolder input = (EditableValueHolder) component;
String clientId = component.getClientId(context);
- SavedState state = (SavedState)_saved.get(clientId);
+ SavedState state = (SavedState) _saved.get(clientId);
if (state == null)
{
state = new SavedState();
@@ -444,14 +470,15 @@
state.setSubmittedValue(input.getSubmittedValue());
state.setLocalValueSet(input.isLocalValueSet());
}
-
+
List kids = component.getChildren();
- for (int i=0; i < kids.size(); i++)
+ for (int i = 0; i < kids.size(); i++)
{
- saveDescendantState((UIComponent)kids.get(i), context);
+ saveDescendantState((UIComponent) kids.get(i), context);
}
}
+
/**
* Used to configure a new node with the state stored previously.
*/
@@ -461,27 +488,28 @@
Iterator i = getFacets().values().iterator();
while (i.hasNext())
{
- UIComponent facet = (UIComponent)i.next();
+ UIComponent facet = (UIComponent) i.next();
restoreDescendantState(facet, context);
}
}
-
+
+
/**
* Overloaded helper method for the no argument version of this method.
*
* @param component The component whose state needs to be restored
- * @param context FacesContext
+ * @param context FacesContext
*/
private void restoreDescendantState(UIComponent component, FacesContext context)
{
String id = component.getId();
component.setId(id); // forces the cilent id to be reset
-
+
if (component instanceof EditableValueHolder)
{
- EditableValueHolder input = (EditableValueHolder)component;
+ EditableValueHolder input = (EditableValueHolder) component;
String clientId = component.getClientId(context);
- SavedState state = (SavedState)_saved.get(clientId);
+ SavedState state = (SavedState) _saved.get(clientId);
if (state == null)
{
state = new SavedState();
@@ -491,80 +519,93 @@
input.setSubmittedValue(state.getSubmittedValue());
input.setLocalValueSet(state.isLocalValueSet());
}
-
+
List kids = component.getChildren();
- for (int i=0; i < kids.size(); i++)
+ for (int i = 0; i < kids.size(); i++)
{
- restoreDescendantState((UIComponent)kids.get(i), context);
- }
+ restoreDescendantState((UIComponent) kids.get(i), context);
+ }
}
-
+
+
/**
* A regular bean with accessor methods for all state variables.
- *
+ *
* @author Sean Schofield
* @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
* @version $Revision$ $Date$
*/
private static class SavedState implements Serializable
{
+
private Object submittedValue;
private boolean valid = true;
private Object value;
private boolean localValueSet;
-
+
+
Object getSubmittedValue()
{
return submittedValue;
}
-
+
+
void setSubmittedValue(Object submittedValue)
{
this.submittedValue = submittedValue;
}
-
+
+
boolean isValid()
{
return valid;
}
-
+
+
void setValid(boolean valid)
{
this.valid = valid;
}
-
+
+
Object getValue()
{
return value;
}
-
+
+
void setValue(Object value)
{
this.value = value;
}
-
+
+
boolean isLocalValueSet()
{
return localValueSet;
}
-
+
+
void setLocalValueSet(boolean localValueSet)
{
this.localValueSet = localValueSet;
}
}
-
+
+
/**
- * Inner class used to wrap the original events produced by child components in the tree.
- * This will allow the tree to find the appropriate component later when its time to
- * broadcast the events to registered listeners. Code is based on a similar private
+ * Inner class used to wrap the original events produced by child components in the tree.
+ * This will allow the tree to find the appropriate component later when its time to
+ * broadcast the events to registered listeners. Code is based on a similar private
* class for UIData.
*/
private static class FacesEventWrapper extends FacesEvent
{
+
private FacesEvent _wrappedFacesEvent;
private String _nodeId;
+
public FacesEventWrapper(FacesEvent facesEvent, String nodeId, UIComponent component)
{
super(component);
@@ -572,72 +613,80 @@
_nodeId = nodeId;
}
+
public PhaseId getPhaseId()
{
return _wrappedFacesEvent.getPhaseId();
}
+
public void setPhaseId(PhaseId phaseId)
{
_wrappedFacesEvent.setPhaseId(phaseId);
}
+
public void queue()
{
_wrappedFacesEvent.queue();
}
+
public String toString()
{
return _wrappedFacesEvent.toString();
}
+
public boolean isAppropriateListener(FacesListener faceslistener)
{
// this event type is only intended for wrapping a real event
return false;
}
+
public void processListener(FacesListener faceslistener)
{
- throw new UnsupportedOperationException(
- "This event type is only intended for wrapping a real event"
- );
+ throw new UnsupportedOperationException("This event type is only intended for wrapping a real event");
}
+
public FacesEvent getFacesEvent()
{
return _wrappedFacesEvent;
}
+
public String getNodeId()
{
return _nodeId;
}
- }
-
+ }
+
+
/**
* Returns true if there is an error message queued for at least one of the nodes.
+ *
* @param context FacesContext
- * @return whether an error message is present
+ * @return whether an error message is present
*/
private boolean keepSaved(FacesContext context)
{
- Iterator clientIds =_saved.keySet().iterator();
+ Iterator clientIds = _saved.keySet().iterator();
while (clientIds.hasNext())
{
- String clientId = (String)clientIds.next();
+ String clientId = (String) clientIds.next();
Iterator messages = context.getMessages(clientId);
while (messages.hasNext())
{
- FacesMessage message = (FacesMessage)messages.next();
+ FacesMessage message = (FacesMessage) messages.next();
if (message.getSeverity().compareTo(FacesMessage.SEVERITY_ERROR) >= 0)
{
return true;
}
}
}
-
+
return false;
}
}