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