1 16 package org.apache.myfaces.custom.tree2; 17 18 import javax.faces.context.FacesContext; 19 import javax.faces.component.UICommand; 20 import javax.faces.component.html.HtmlCommandLink; 21 import javax.faces.event.ActionEvent; 22 import javax.faces.el.MethodBinding; 23 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.io.IOException ; 27 28 29 36 public class HtmlTree extends UITreeData 37 { 38 public static final String COMPONENT_TYPE = "org.apache.myfaces.Tree2"; 39 private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Tree2"; 40 private static final String NODE_STATE_KEY = "org.apache.myfaces.tree.NODE_STATE_KEY"; 41 private UICommand _expandControl; 42 private String _varNodeToggler; 43 private HashSet _expandedNodes = new HashSet (); 44 private String _selectedNodeId; 45 46 49 public HtmlTree() 50 { 51 setRendererType(DEFAULT_RENDERER_TYPE); 52 _expandControl = new HtmlCommandLink(); 53 } 54 55 public Object saveState(FacesContext context) 57 { 58 Object values[] = new Object [4]; 59 values[0] = super.saveState(context); 60 values[1] = _expandedNodes; 61 values[2] = _varNodeToggler; 62 values[3] = _selectedNodeId; 63 64 return ((Object ) (values)); 65 } 66 67 public void restoreState(FacesContext context, Object state) 69 { 70 Object values[] = (Object [])state; 71 super.restoreState(context, values[0]); 72 _expandedNodes = (HashSet )values[1]; 73 setVarNodeToggler((String )values[2]); 74 _selectedNodeId = (String )values[3]; 75 } 76 77 public void setNodeId(String nodeId) 79 { 80 super.setNodeId(nodeId); 81 82 if (_varNodeToggler != null) 83 { 84 Map requestMap = getFacesContext().getExternalContext().getRequestMap(); 85 requestMap.put(_varNodeToggler, this); 86 } 87 } 88 89 public void processDecodes(FacesContext context) 90 { 91 super.processDecodes(context); 92 93 Map sessionMap = context.getExternalContext().getSessionMap(); 95 sessionMap.put(NODE_STATE_KEY + ":" + getId(), _expandedNodes); 96 } 97 98 public void encodeBegin(FacesContext context) 99 throws IOException 100 { 101 108 Map sessionMap = context.getExternalContext().getSessionMap(); 110 HashSet nodeState = (HashSet )sessionMap.get(NODE_STATE_KEY + ":" + getId()); 111 112 if (nodeState != null) 113 { 114 _expandedNodes = nodeState; 115 } 116 117 super.encodeBegin(context); 118 } 119 120 127 public UICommand getExpandControl() 128 { 129 return _expandControl; 130 } 131 132 public void setVarNodeToggler(String varNodeToggler) 133 { 134 _varNodeToggler = varNodeToggler; 135 136 String bindingString = "#{" + varNodeToggler + ".toggleExpanded}"; 138 MethodBinding actionBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(bindingString, null); 139 _expandControl.setAction(actionBinding); 140 } 141 142 public String toggleExpanded() 143 { 144 String nodeId = getNodeId(); 145 146 if (_expandedNodes.contains(nodeId)) 147 { 148 _expandedNodes.remove(nodeId); 149 } 150 else 151 { 152 _expandedNodes.add(nodeId); 153 } 154 155 return null; 156 } 157 158 162 public boolean isNodeExpanded() 163 { 164 return (_expandedNodes.contains(getNodeId()) && getNode().getChildCount() > 0); 165 } 166 167 protected void processChildNodes(FacesContext context, TreeNode parentNode, int processAction) 168 { 169 super.processChildNodes(context, parentNode, processAction); 170 } 171 172 178 public void setNodeSelected(ActionEvent event) 179 { 180 _selectedNodeId = getNodeId(); 181 } 182 183 187 public boolean isNodeSelected() 188 { 189 return getNodeId().equals(_selectedNodeId); 190 } 191 } 192 | Popular Tags |