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.FacesException;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.behavior.ClientBehaviorHolder;
30  import javax.faces.context.FacesContext;
31  
32  import org.apache.myfaces.shared.util.StringUtils;
33  
34  /**
35   * @author Leonardo Uribe (latest modification by $Author$)
36   * @version $Revision$ $Date$
37   */
38  public class ClientBehaviorAttachedObjectTargetImpl 
39      implements ClientBehaviorAttachedObjectTarget, Serializable
40  {
41  
42      /**
43       * 
44       */
45      private static final long serialVersionUID = -4645087262404844925L;
46  
47      protected ValueExpression _name;
48      
49      protected ValueExpression _targets;
50      
51      protected ValueExpression _event;
52      
53      protected boolean _default;
54  
55      public ClientBehaviorAttachedObjectTargetImpl()
56      {
57      }
58  
59      public String getName()
60      {
61          if (_name != null)
62          {
63              return (String) _name.getValue(FacesContext.getCurrentInstance().getELContext());
64          }        
65          return null;
66      }
67  
68      public List<UIComponent> getTargets(UIComponent topLevelComponent)
69      {
70          FacesContext facesContext = FacesContext.getCurrentInstance();
71          String [] targetsArray = getTargets(facesContext);
72          
73          if (targetsArray.length > 0)
74          {
75              List<UIComponent> targetsList = new ArrayList<UIComponent>(targetsArray.length);
76              for (String target : targetsArray)
77              {
78                  UIComponent innerComponent = topLevelComponent.findComponent(target);
79                  
80                  if (innerComponent != null)
81                  {
82                      if (innerComponent instanceof ClientBehaviorHolder ||
83                          UIComponent.isCompositeComponent(innerComponent))
84                      {
85                          targetsList.add(
86                                  new ClientBehaviorRedirectEventComponentWrapper(innerComponent, getName(), getEvent()));
87                      }
88                      else
89                      {
90                          throw new FacesException("Component with clientId " + innerComponent.getClientId(facesContext)
91                                                   + "should be instance of ClientBehaviorHolder");
92                      }
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(getName());
107                 if (innerComponent != null)
108                 {
109                     if (innerComponent instanceof ClientBehaviorHolder ||
110                         UIComponent.isCompositeComponent(innerComponent))
111                     {
112                         List<UIComponent> targetsList = new ArrayList<UIComponent>(1);
113                         targetsList.add(
114                                 new ClientBehaviorRedirectEventComponentWrapper(innerComponent, getName(), getEvent()));
115                         return targetsList;
116                     }
117                     else
118                     {
119                         throw new FacesException("Component with clientId "+ innerComponent.getClientId(facesContext)
120                                                  + "should be instance of ClientBehaviorHolder");
121                     }
122                 }
123             }
124             return Collections.emptyList();
125         }
126     }
127     
128     public String [] getTargets(FacesContext context)
129     {
130         if (_targets != null)
131         {
132             return StringUtils.splitShortString((String) _targets.getValue(context.getELContext()), ' ');
133         }
134         return org.apache.myfaces.shared.util.ArrayUtils.EMPTY_STRING_ARRAY;
135     }
136     
137     public void setName(ValueExpression name)
138     {
139         _name = name;
140     }
141     
142     public void setTargets(ValueExpression ve)
143     {
144         _targets = ve;
145     }
146     
147     public String getEvent()
148     {
149         if (_event != null)
150         {
151             return (String) _event.getValue(FacesContext.getCurrentInstance().getELContext());
152         }        
153         return null;
154     }
155     
156     public void setEvent(ValueExpression event)
157     {
158         this._event = event;
159     }
160 
161     public boolean isDefaultEvent()
162     {
163         return _default;
164     }
165 
166     public void setDefault(boolean default1)
167     {
168         _default = default1;
169     }
170 }