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      private Object getReference(TreeNode treeNode, String key) {
165        Object value = null;
166        String reference = null;
167        try {
168          FacesContext facesContext = FacesContext.getCurrentInstance();
169          UITreeOld root = findTreeRoot();
170          ValueBinding binding = root.getValueBinding(key);
171          if (binding == null) {
172            reference = (String) root.getAttributes().get(key);
173            if (reference == null) {
174              return null;
175            }
176            String ref = "#{tobagoTreeNode." + reference + "}";
177            binding = facesContext.getApplication().createValueBinding(ref);
178          } else {
179            reference = binding.getExpressionString();
180          }
181          Map requestMap = facesContext.getExternalContext().getRequestMap();
182          //noinspection unchecked
183          requestMap.put("tobagoTreeNode", treeNode);
184          value = binding.getValue(facesContext);
185          requestMap.remove("tobagoTreeNode");
186        } catch (Exception e) {
187          LOG.warn(
188              "Can't find " + key + " over ref='" + reference
189                  + "' treeNode='" + treeNode + "! " + treeNode.getClass().getName(), e);
190        }
191        return value;
192      }
193    
194      public UITreeOld findTreeRoot() {
195        UIComponent ancestor = getParent();
196        while (ancestor != null && ancestor instanceof UITreeOldNode) {
197          ancestor = ancestor.getParent();
198        }
199        if (ancestor instanceof UITreeOld) {
200          return (UITreeOld) ancestor;
201        }
202        return null;
203      }
204    
205      public void updateModel(FacesContext facesContext) {
206        // nothig to update for treeNode's
207      }
208    
209    }