001 package org.apache.myfaces.tobago.component; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.apache.commons.beanutils.PropertyUtils; 021 import org.apache.commons.logging.Log; 022 import org.apache.commons.logging.LogFactory; 023 import org.apache.myfaces.tobago.TobagoConstants; 024 025 import javax.faces.component.UIComponent; 026 import javax.faces.context.FacesContext; 027 import javax.faces.el.ValueBinding; 028 import javax.swing.tree.DefaultMutableTreeNode; 029 import javax.swing.tree.TreeNode; 030 import java.util.Map; 031 032 @Deprecated 033 public class UITreeOldNode extends javax.faces.component.UIInput { 034 035 private static final Log LOG = LogFactory.getLog(UITreeOldNode.class); 036 037 private static final String SUB_REFERENCE_KEY = "subReferenceKey"; 038 039 protected UITreeOldNode(UIComponent parent, int index) { 040 super(); 041 if (parent instanceof UITreeOldNode) { 042 String parentSubReference = ((UITreeOldNode) parent).getSubReference(); 043 if (parentSubReference == null) { 044 getAttributes().put(SUB_REFERENCE_KEY, "childAt[" + index + "]"); 045 } else { 046 getAttributes().put(SUB_REFERENCE_KEY, parentSubReference + ".childAt[" + index + "]"); 047 } 048 } 049 setRendererType(TobagoConstants.RENDERER_TYPE_TREE_OLD_NODE); 050 parent.getChildren().add(this); 051 initId(); 052 initName(); 053 initDisabled(); 054 initTip(); 055 } 056 057 public UITreeOldNode() { 058 } 059 060 // ///////////////////////////////////////////// code 061 062 public boolean getRendersChildren() { 063 return true; 064 } 065 066 public String getSubReference() { 067 return (String) getAttributes().get(SUB_REFERENCE_KEY); 068 } 069 070 public DefaultMutableTreeNode getTreeNode() { 071 return (DefaultMutableTreeNode) getValue(); 072 } 073 074 public Object getValue() { 075 TreeNode value = null; 076 UITreeOld root = findTreeRoot(); 077 String subReference = getSubReference(); 078 if (LOG.isDebugEnabled()) { 079 LOG.debug("root = '" + root + "'"); 080 LOG.debug("subReference = '" + subReference + "'"); 081 } 082 TreeNode rootNode = (TreeNode) root.getValue(); 083 084 if (LOG.isDebugEnabled()) { 085 LOG.debug("rootNode = '" + rootNode + "'"); 086 } 087 if (rootNode != null) { 088 try { 089 if (subReference == null) { 090 value = rootNode; 091 } else { 092 value = (TreeNode) PropertyUtils.getProperty(rootNode, subReference); 093 } 094 if (LOG.isDebugEnabled()) { 095 LOG.debug("treeNode = '" + value + "'"); 096 } 097 } catch (Throwable e) { 098 LOG.error("subReference = '" + subReference + "'", e); 099 } 100 } 101 return value; 102 } 103 104 protected void createTreeNodes() { 105 106 TreeNode node = (TreeNode) getValue(); 107 if (node != null) { 108 int childCount = node.getChildCount(); 109 for (int i = 0; i < childCount; i++) { 110 UITreeOldNode component = new UITreeOldNode(this, i); 111 component.createTreeNodes(); 112 } 113 } 114 } 115 116 private void initName() { 117 TreeNode treeNode = (TreeNode) getValue(); 118 if (treeNode != null) { 119 Object name = getReference(treeNode, TobagoConstants.ATTR_NAME_REFERENCE); 120 if (name == null) { 121 name = toString(); 122 } 123 getAttributes().put(TobagoConstants.ATTR_NAME, name.toString()); 124 } 125 } 126 127 private void initTip() { 128 TreeNode treeNode = (TreeNode) getValue(); 129 if (treeNode != null) { 130 Object tip = getReference(treeNode, TobagoConstants.ATTR_TIP_REFERENCE); 131 if (tip != null) { 132 getAttributes().put(TobagoConstants.ATTR_TIP, tip.toString()); 133 } 134 } 135 } 136 137 private void initDisabled() { 138 TreeNode treeNode = (TreeNode) getValue(); 139 if (treeNode != null) { 140 Object disabled = getReference(treeNode, 141 TobagoConstants.ATTR_DISABLED_REFERENCE); 142 if (!(disabled instanceof Boolean)) { 143 if (disabled instanceof String) { 144 disabled = Boolean.valueOf((String) disabled); 145 } else { 146 disabled = false; 147 } 148 } 149 getAttributes().put(TobagoConstants.ATTR_DISABLED, disabled); 150 } 151 } 152 153 private void initId() { 154 TreeNode treeNode = (TreeNode) getValue(); 155 if (treeNode != null) { 156 Object id = getReference(treeNode, TobagoConstants.ATTR_ID_REFERENCE); 157 if (!(id instanceof String)) { 158 id = "node" + Integer.toString(System.identityHashCode(treeNode)); 159 } 160 setId((String) id); 161 } 162 } 163 164 @SuppressWarnings("unchecked") 165 private Object getReference(TreeNode treeNode, String key) { 166 Object value = null; 167 UITreeOld root = findTreeRoot(); 168 String reference = (String) root.getAttributes().get(key); 169 if (reference != null) { 170 try { 171 FacesContext facesContext = FacesContext.getCurrentInstance(); 172 Map requestMap = facesContext.getExternalContext().getRequestMap(); 173 String ref = "#{tobagoTreeNode." + reference + "}"; 174 ValueBinding vb = facesContext.getApplication().createValueBinding(ref); 175 requestMap.put("tobagoTreeNode", treeNode); 176 value = vb.getValue(facesContext); 177 requestMap.remove("tobagoTreeNode"); 178 } catch (Exception e) { 179 LOG.warn( 180 "Can't find " + key + " over ref='" + reference 181 + "' treeNode='" + treeNode + "! " + treeNode.getClass().getName(), e); 182 } 183 } 184 return value; 185 } 186 187 public UITreeOld findTreeRoot() { 188 UIComponent ancestor = getParent(); 189 while (ancestor != null && ancestor instanceof UITreeOldNode) { 190 ancestor = ancestor.getParent(); 191 } 192 if (ancestor instanceof UITreeOld) { 193 return (UITreeOld) ancestor; 194 } 195 return null; 196 } 197 198 public void updateModel(FacesContext facesContext) { 199 // nothig to update for treeNode's 200 } 201 202 }