View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag.composite;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import javax.el.ValueExpression;
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.view.AttachedObjectTarget;
30  
31  import org.apache.myfaces.shared.util.StringUtils;
32  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
33  
34  /**
35   * 
36   * @author Leonardo Uribe (latest modification by $Author$)
37   * @version $Revision$ $Date$
38   */
39  public class AttachedObjectTargetImpl implements AttachedObjectTarget, Serializable
40  {    
41      /**
42       * 
43       */
44      private static final long serialVersionUID = -7214478234269252354L;
45      
46      protected ValueExpression _name;
47      
48      protected ValueExpression _targets;
49  
50      public AttachedObjectTargetImpl()
51      {
52      }
53  
54      public String getName()
55      {
56          if (_name != null)
57          {
58              return (String) _name.getValue(FacesContext.getCurrentInstance().getELContext());
59          }        
60          return null;
61      }
62  
63      public List<UIComponent> getTargets(UIComponent topLevelComponent)
64      {
65          FacesContext facesContext = FacesContext.getCurrentInstance();
66          String [] targetsArray = getTargets(facesContext);
67          
68          if (targetsArray.length > 0)
69          {
70              List<UIComponent> targetsList = new ArrayList<UIComponent>(targetsArray.length);
71              final char separatorChar = facesContext.getNamingContainerSeparatorChar();
72              UIComponent facetBase = topLevelComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME);
73              for (String target : targetsArray)
74              {
75                  //UIComponent innerComponent = topLevelComponent.findComponent(
76                  //        topLevelComponent.getId() + UINamingContainer.getSeparatorChar(facesContext) + target);
77                  int separator = target.indexOf(separatorChar);
78                  UIComponent innerComponent = null;
79                  if (separator == -1)
80                  {
81                      innerComponent = ComponentSupport.findComponentChildOrFacetFrom(
82                              facetBase, target, null);
83                  }
84                  else
85                  {
86                      innerComponent = ComponentSupport.findComponentChildOrFacetFrom(
87                              facetBase, target.substring(0,separator), target);
88                  }
89                  
90                  if (innerComponent != null)
91                  {
92                      targetsList.add(innerComponent);
93                  }
94              }
95              return targetsList;
96          }
97          else
98          {
99              // composite:actionSource/valueHolder/editableValueHolder
100             // "name" description says if targets is not set, name is 
101             // the component id of the target component where
102             // it should be mapped to
103             String name = getName();
104             if (name != null)
105             {
106                 //UIComponent innerComponent = topLevelComponent.findComponent(
107                 //        topLevelComponent.getId() + UINamingContainer.getSeparatorChar(facesContext) + getName());
108                 UIComponent innerComponent = ComponentSupport.findComponentChildOrFacetFrom(
109                         topLevelComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME),
110                         name, null);
111                 if (innerComponent != null)
112                 {
113                     List<UIComponent> targetsList = new ArrayList<UIComponent>(1);
114                     targetsList.add(innerComponent);
115                     return targetsList;
116                 }
117             }
118             return Collections.emptyList();
119         }
120     }
121     
122     public String [] getTargets(FacesContext context)
123     {
124         if (_targets != null)
125         {
126             return StringUtils.splitShortString((String) _targets.getValue(context.getELContext()), ' ');
127         }
128         return org.apache.myfaces.shared.util.ArrayUtils.EMPTY_STRING_ARRAY;
129     }
130     
131     public void setName(ValueExpression ve)
132     {
133         _name = ve;
134     }
135     
136     public void setTargets(ValueExpression ve)
137     {
138         _targets = ve;
139     }
140 }