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  
20  package org.apache.myfaces.tobago.facelets.extension;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  
24  import javax.faces.application.Application;
25  import javax.faces.component.UIComponent;
26  import javax.faces.view.facelets.ComponentConfig;
27  import javax.faces.view.facelets.ComponentHandler;
28  import javax.faces.view.facelets.FaceletContext;
29  import javax.faces.view.facelets.MetaRuleset;
30  import javax.faces.view.facelets.Metadata;
31  import javax.faces.view.facelets.TagAttribute;
32  import java.io.IOException;
33  
34  
35  /**
36   * Base class of the Facelets handlers for the <tx:menuXXX /> extension tags.
37   */
38  public abstract class TobagoMenuExtensionHandler extends ComponentHandler {
39  
40    private Class subComponentLastType = Object.class;
41    private Metadata subComponentMapper;
42    private TagAttribute fieldIdAttribute;
43  
44    public TobagoMenuExtensionHandler(ComponentConfig config) {
45      super(config);
46      fieldIdAttribute = getAttribute("fieldId");
47    }
48  
49    protected abstract String getSubComponentType();
50  
51    protected abstract String getSubRendererType();
52  
53    protected abstract String getFacetName();
54  
55    public void applyNextHandler(FaceletContext faceletContext, UIComponent menuCommand) throws IOException {
56      if (ComponentHandler.isNew(menuCommand)) {
57        UIComponent component = (UIComponent) menuCommand.getFacets().remove(getFacetName());
58        nextHandler.apply(faceletContext, component);
59        menuCommand.getFacets().put(getFacetName(), component);
60      } else {
61        nextHandler.apply(faceletContext, menuCommand.getFacet(getFacetName()));
62      }
63    }
64  
65    public void onComponentCreated(FaceletContext faceletContext, UIComponent menuCommand, UIComponent parent) {
66  
67      final Application application = faceletContext.getFacesContext().getApplication();
68      final UIComponent component = application.createComponent(getSubComponentType());
69      final String uid = fieldIdAttribute != null
70          ? fieldIdAttribute.getValue(faceletContext)
71          : "_tx_" + faceletContext.generateUniqueId("sub");
72      component.setId(uid);
73      component.setRendererType(getSubRendererType());
74      setSubComponentAttributes(faceletContext, component);
75      menuCommand.getFacets().put(getFacetName(), component);
76    }
77  
78    private void setSubComponentAttributes(FaceletContext ctx, Object instance) {
79      if (instance != null) {
80        Class type = instance.getClass();
81        if (subComponentMapper == null || !subComponentLastType.equals(type)) {
82          subComponentLastType = type;
83          subComponentMapper = createSubComponentMetaRuleset(type).finish();
84        }
85        subComponentMapper.applyMetadata(ctx, instance);
86      }
87    }
88  
89    protected MetaRuleset createSubComponentMetaRuleset(Class aClass) {
90      MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
91      TagAttribute [] attrs = tag.getAttributes().getAll();
92      for (int i = 0; i < attrs.length; i++) {
93        TagAttribute attr = attrs[i];
94        if (!(attr.getLocalName().equals(Attributes.CONVERTER)
95            || attr.getLocalName().equals(Attributes.VALUE))) {
96          metaRuleset.ignore(attr.getLocalName());
97        }
98      }
99      return metaRuleset;
100   }
101 
102   protected MetaRuleset createMetaRuleset(Class aClass) {
103     MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
104     metaRuleset.ignore(Attributes.CONVERTER);
105     metaRuleset.ignore(Attributes.VALUE);
106     return metaRuleset;
107   }
108 }