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(final 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(final FaceletContext faceletContext, final UIComponent menuCommand) throws IOException {
56      if (ComponentHandler.isNew(menuCommand)) {
57        final UIComponent component = 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(
66        final FaceletContext faceletContext, final UIComponent menuCommand, final UIComponent parent) {
67  
68      final Application application = faceletContext.getFacesContext().getApplication();
69      final UIComponent component = application.createComponent(getSubComponentType());
70      final String uid = fieldIdAttribute != null
71          ? fieldIdAttribute.getValue(faceletContext)
72          : menuCommand.getId() + "_tx_" + getFacetName();
73      component.setId(uid);
74      component.setRendererType(getSubRendererType());
75      setSubComponentAttributes(faceletContext, component);
76      menuCommand.getFacets().put(getFacetName(), component);
77    }
78  
79    private void setSubComponentAttributes(final FaceletContext ctx, final Object instance) {
80      if (instance != null) {
81        final Class type = instance.getClass();
82        if (subComponentMapper == null || !subComponentLastType.equals(type)) {
83          subComponentLastType = type;
84          subComponentMapper = createSubComponentMetaRuleset(type).finish();
85        }
86        subComponentMapper.applyMetadata(ctx, instance);
87      }
88    }
89  
90    protected MetaRuleset createSubComponentMetaRuleset(final Class aClass) {
91      final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
92      final TagAttribute [] attrs = tag.getAttributes().getAll();
93      for (int i = 0; i < attrs.length; i++) {
94        final TagAttribute attr = attrs[i];
95        if (!(attr.getLocalName().equals(Attributes.CONVERTER)
96            || attr.getLocalName().equals(Attributes.VALUE))) {
97          metaRuleset.ignore(attr.getLocalName());
98        }
99      }
100     return metaRuleset;
101   }
102 
103   protected MetaRuleset createMetaRuleset(final Class aClass) {
104     final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
105     metaRuleset.ignore(Attributes.CONVERTER);
106     metaRuleset.ignore(Attributes.VALUE);
107     return metaRuleset;
108   }
109 }