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.custom.aliasbean;
20  
21  import java.io.IOException;
22  
23  import javax.el.ELException;
24  import javax.faces.FacesException;
25  import javax.faces.application.Application;
26  import javax.faces.component.UIComponent;
27  import javax.faces.webapp.UIComponentTag;
28  
29  import com.sun.facelets.FaceletContext;
30  import com.sun.facelets.tag.TagAttribute;
31  import com.sun.facelets.tag.jsf.ComponentConfig;
32  import com.sun.facelets.tag.jsf.ComponentHandler;
33  
34  /**
35   * Tag handler used in facelets
36   * 
37   * @since 1.1.7
38   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
39   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
40   *
41   */
42  public class AliasBeanTagHandler extends ComponentHandler
43  {
44      private TagAttribute valueAttr;
45      private TagAttribute aliasAttr;
46  
47      public AliasBeanTagHandler(ComponentConfig tagConfig)
48      {
49          super(tagConfig);
50  
51          valueAttr = getRequiredAttribute("value");
52          aliasAttr = getRequiredAttribute("alias");
53      }
54  
55      protected void setAttributes(FaceletContext ctx, Object instance)
56      {
57          super.setAttributes(ctx, instance);
58          
59          Application app = ctx.getFacesContext().getApplication();
60          
61          AliasBean aliasBean = (AliasBean) instance;
62  
63          String value = valueAttr.getValue();
64          if (UIComponentTag.isValueReference(value))
65          {
66              aliasBean.setValueBinding("value", app.createValueBinding(valueAttr
67                      .getValue()));
68          }
69          else
70          {
71              aliasBean.setValue(value);
72          }
73  
74          String alias = aliasAttr.getValue();
75          if (UIComponentTag.isValueReference(alias))
76          {
77              aliasBean.setValueBinding("alias", app.createValueBinding(aliasAttr
78                      .getValue()));
79          }
80          else
81          {
82              aliasBean.setAlias(alias);
83          }
84      }
85      
86      protected void applyNextHandler(FaceletContext ctx, UIComponent component)
87              throws IOException, FacesException, ELException
88      {
89          AliasBean aliasBean = (AliasBean) component;
90          aliasBean.makeAlias(ctx.getFacesContext());
91          super.applyNextHandler(ctx, component);
92          aliasBean.removeAlias(ctx.getFacesContext());
93      }
94      
95      /**
96       * We have to add the children to the parent, for ensure proper
97       * behavior between aliasBean and aliasBeansScope.
98       * 
99       */
100     protected void onComponentCreated(FaceletContext faceletcontext,
101             UIComponent component, UIComponent parent)    
102     {
103         parent.getChildren().add(component);
104     }
105 
106 }