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.beans.BeanDescriptor;
22  import java.beans.IntrospectionException;
23  import java.beans.PropertyDescriptor;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.logging.Level;
30  import java.util.logging.Logger;
31  
32  import javax.faces.component.UIComponent;
33  import javax.faces.view.facelets.FaceletContext;
34  import javax.faces.view.facelets.TagAttribute;
35  import javax.faces.view.facelets.TagConfig;
36  import javax.faces.view.facelets.TagException;
37  import javax.faces.view.facelets.TagHandler;
38  
39  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
40  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
41  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
42  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
43  
44  /**
45   * Insert or move the facet from the composite component body to the expected location.
46   * 
47   * @author Leonardo Uribe (latest modification by $Author: struberg $)
48   * @version $Revision: 1194861 $ $Date: 2011-10-29 05:02:34 -0500 (Sat, 29 Oct 2011) $
49   */
50  @JSFFaceletTag(name="composite:insertFacet")
51  public class InsertFacetHandler extends TagHandler
52  {
53      //public static String USES_INSERT_FACET = "org.apache.myfaces.USES_INSERT_FACET";
54      //public static String INSERT_FACET_TARGET_ID = "org.apache.myfaces.INSERT_FACET_TARGET_ID.";
55      //public static String INSERT_FACET_ORDERING = "org.apache.myfaces.INSERT_FACET_ORDERING.";
56      
57      public static final String INSERT_FACET_USED = "org.apache.myfaces.INSERT_FACET_USED";
58      
59      /**
60       * Key used to save on bean descriptor a map containing the metadata
61       * information related to this tag. It will be used later to check "required" property.
62       */
63      public static final String INSERT_FACET_KEYS = "org.apache.myfaces.INSERT_FACET_KEYS";
64      
65      private static final Logger log = Logger.getLogger(InsertFacetHandler.class.getName());
66      
67      /**
68       * The name that identify the current facet.
69       */
70      @JSFFaceletAttribute(name="name",
71              className="javax.el.ValueExpression",
72              deferredValueType="java.lang.String",
73              required=true)
74      protected final TagAttribute _name;
75      
76      /**
77       * Define if the facet to be inserted is required or not for every instance of
78       * this composite component.
79       */
80      @JSFFaceletAttribute(name="required",
81              className="javax.el.ValueExpression",
82              deferredValueType="boolean")
83      protected final TagAttribute _required;
84      
85      public InsertFacetHandler(TagConfig config)
86      {
87          super(config);
88          _name = getRequiredAttribute("name");
89          _required = getAttribute("required");
90      }
91      
92      public String getFacetName(FaceletContext ctx)
93      {
94          return _name.getValue(ctx);
95      }
96  
97      @SuppressWarnings("unchecked")
98      public void apply(FaceletContext ctx, UIComponent parent)
99              throws IOException
100     {
101         if (((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata())
102         {
103             String facetName = _name.getValue(ctx);
104             
105             UIComponent compositeBaseParent
106                     = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
107             
108             CompositeComponentBeanInfo beanInfo = 
109                 (CompositeComponentBeanInfo) compositeBaseParent.getAttributes()
110                 .get(UIComponent.BEANINFO_KEY);
111             
112             if (beanInfo == null)
113             {
114                 if (log.isLoggable(Level.SEVERE))
115                 {
116                     log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
117                 }
118                 return;
119             }
120             
121             BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); 
122 
123             List<String> facetList = (List<String>) beanDescriptor.getValue(INSERT_FACET_USED);
124             
125             if (facetList == null)
126             {
127                 //2. If not found create it and set
128                 facetList = new ArrayList<String>();
129                 beanDescriptor.setValue(
130                         INSERT_FACET_USED,
131                         facetList);
132             }
133             
134             facetList.add(facetName);
135 
136             Map<String, PropertyDescriptor> insertFacetPropertyDescriptorMap = (Map<String, PropertyDescriptor>)
137                 beanDescriptor.getValue(INSERT_FACET_KEYS);
138         
139             if (insertFacetPropertyDescriptorMap == null)
140             {
141                 insertFacetPropertyDescriptorMap = new HashMap<String, PropertyDescriptor>();
142                 beanDescriptor.setValue(INSERT_FACET_KEYS, insertFacetPropertyDescriptorMap);
143             }
144             
145             PropertyDescriptor facetDescriptor = _createFacetPropertyDescriptor(facetName, ctx, parent);
146             insertFacetPropertyDescriptorMap.put(facetName, facetDescriptor);
147         }
148         else
149         {
150             String facetName = _name.getValue(ctx);
151             
152             AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
153             
154             UIComponent parentCompositeComponent
155                     = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
156 
157             actx.includeCompositeComponentDefinition(parent, facetName);
158         }
159         
160     }
161     
162     private PropertyDescriptor _createFacetPropertyDescriptor(String facetName, FaceletContext ctx, UIComponent parent)
163     throws TagException, IOException
164     {
165         try
166         {
167             CompositeComponentPropertyDescriptor facetPropertyDescriptor = 
168                 new CompositeComponentPropertyDescriptor(facetName);
169             
170             if (_required != null)
171             {
172                 facetPropertyDescriptor.setValue("required", _required.getValueExpression(ctx, Boolean.class));
173             }
174             
175             return facetPropertyDescriptor;
176         }
177         catch (IntrospectionException e)
178         {
179             if (log.isLoggable(Level.SEVERE))
180             {
181                 log.log(Level.SEVERE, "Cannot create PropertyDescriptor for attribute ",e);
182             }
183             throw new TagException(tag,e);
184         }
185     }
186 
187 }