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 javax.faces.component.behavior;
20  
21  import java.util.Collections;
22  import java.util.Set;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.render.ClientBehaviorRenderer;
27  
28  /**
29   * @since 2.0
30   */
31  public class ClientBehaviorBase extends BehaviorBase implements ClientBehavior
32  {
33  
34      private transient FacesContext _facesContext;
35      
36      /**
37       * 
38       */
39      public ClientBehaviorBase()
40      {
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      public void decode(FacesContext context, UIComponent component)
47      {
48          if (context == null)
49          {
50              throw new NullPointerException("context");
51          }
52          
53          if (component == null)
54          {
55              throw new NullPointerException("component");
56          }
57          
58          // If a BehaviorRenderer is available for the specified behavior renderer type, this method delegates 
59          // to the BehaviorRenderer's decode() method. Otherwise, no decoding is performed. 
60          ClientBehaviorRenderer renderer = getRenderer(context);
61          if (renderer != null)
62          {
63              renderer.decode(context, component, this);
64          }
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70       public Set<ClientBehaviorHint> getHints()
71      {
72          return Collections.<ClientBehaviorHint>emptySet();
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      public String getRendererType()
79      {
80          return null;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public String getScript(ClientBehaviorContext behaviorContext)
87      {
88          if (behaviorContext == null)
89          {
90              throw new NullPointerException("behaviorContext");
91          }
92          
93          ClientBehaviorRenderer renderer = getRenderer(behaviorContext.getFacesContext());
94          if (renderer != null)
95          {
96              // If a BehaviorRenderer is available for the specified behavior renderer type, this method delegates 
97              // to the BehaviorRenderer.getScript method.
98              try
99              {
100                 setCachedFacesContext(behaviorContext.getFacesContext());
101                 return renderer.getScript(behaviorContext, this);
102             }
103             finally
104             {
105                 setCachedFacesContext(null);
106             }
107         }
108         
109         // Otherwise, this method returns null.
110         return null;
111     }
112     
113     protected ClientBehaviorRenderer getRenderer(FacesContext context)
114     {
115         if (context == null)
116         {
117             throw new NullPointerException("context");
118         }
119         
120         String rendererType = getRendererType();
121         if (rendererType != null)
122         {
123             return context.getRenderKit().getClientBehaviorRenderer(rendererType);
124         }
125         
126         return null;
127     }
128     
129     FacesContext getFacesContext()
130     {
131         if (_facesContext == null)
132         {
133             return FacesContext.getCurrentInstance();
134         }
135         else
136         {
137             return _facesContext;
138         }
139     }
140     
141     boolean isCachedFacesContext()
142     {
143         return _facesContext != null;
144     }
145     
146     void setCachedFacesContext(FacesContext facesContext)
147     {
148         _facesContext = facesContext;
149     }
150 }