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