📄 uitreedata.java
字号:
/* * 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 javax.faces.component.UIComponentBase;import javax.faces.component.NamingContainer;import javax.faces.component.UIComponent;import javax.faces.component.EditableValueHolder;import javax.faces.context.FacesContext;import javax.faces.event.AbortProcessingException;import javax.faces.event.FacesEvent;import javax.faces.event.PhaseId;import javax.faces.event.FacesListener;import javax.faces.el.ValueBinding;import javax.faces.application.FacesMessage;import java.io.Serializable;import java.io.IOException;import java.util.Iterator;import java.util.Map;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 * 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. * * @author Sean Schofield * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission) * @version $Revision: 1.5 $ $Date: 2005/02/26 00:26:00 $ */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; private TreeModel _model; private TreeNode _value; private String _var; private String _nodeId; private Map _saved = new HashMap(); /** * Constructor */ public UITreeData() { setRendererType(DEFAULT_RENDERER_TYPE); } // see superclass for documentation public String getFamily() { return COMPONENT_FAMILY; } // see superclass for documentation public Object saveState(FacesContext context) { Object values[] = new Object[3]; values[0] = super.saveState(context); values[1] = _value; values[2] = _var; return ((Object) (values)); } // see superclass for documentation public void restoreState(FacesContext context, Object state) { Object values[] = (Object[]) state; super.restoreState(context, values[0]); _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; String currNodeId = getNodeId(); setNodeId(childEvent.getNodeId()); FacesEvent nodeEvent = childEvent.getFacesEvent(); nodeEvent.getComponent().broadcast(nodeEvent); setNodeId(currNodeId); return; } 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; _saved = new HashMap(); processNodes(context, PROCESS_DECODES, null, 0); setNodeId(null); decode(context); } // see superclass for documentation public void processValidators(FacesContext context) { if (context == null) throw new NullPointerException("context"); if (!isRendered()) return; processNodes(context, PROCESS_VALIDATORS, null, 0); setNodeId(null); } // see superclass for documentation public void processUpdates(FacesContext context) { if (context == null) throw new NullPointerException("context"); if (!isRendered()) return; processNodes(context, PROCESS_UPDATES, null, 0); setNodeId(null); } // see superclass for documentation public String getClientId(FacesContext context) { String ownClientId = super.getClientId(context); if (_nodeId != null) { return ownClientId + NamingContainer.SEPARATOR_CHAR + _nodeId; } 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)) { throw new IllegalArgumentException("name " + name); } super.setValueBinding(name, binding); } // see superclass for documentation public void encodeBegin(FacesContext context) throws IOException { /** * The renderer will handle most of the encoding, but if there are any * error messages queued for the components (validation errors), we * do want to keep the saved state so that we can render the node with * the invalid value. */ if (!keepSaved(context)) { _saved = new HashMap(); } super.encodeBegin(context); } /** * Sets the value of the TreeData. * * @param value The new value */ public void setValue(TreeNode value) { _model = null; _value = value; } /** * Gets the value of the TreeData. * * @return The value */ public Object getValue() { if (_value != null) return _value; 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 * when iterating. * * @param var The new request-scope attribute name */ public void setVar(String var) { _var = var; } /** * 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; } 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 { 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 * 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) { return getDataModel().getPathInformation(nodeId); } /** * 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) { return getDataModel().isLastChild(nodeId); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -