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  import org.apache.myfaces.tobago.internal.util.Deprecation;
24  
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.MetaRuleset;
31  import javax.faces.view.facelets.Metadata;
32  import javax.faces.view.facelets.TagAttribute;
33  import java.io.IOException;
34  
35  
36  /**
37   * Base class of the Facelets handlers for the <tx:menuXXX /> extension tags.
38   */
39  /**
40   * @deprecated since Tobago 3.0. The tx-library is deprecated, please use the tc-library.
41   */
42  @Deprecated
43  public abstract class TobagoMenuExtensionHandler extends ComponentHandler {
44  
45    private Class subComponentLastType = Object.class;
46    private Metadata subComponentMapper;
47    private TagAttribute fieldIdAttribute;
48  
49    public TobagoMenuExtensionHandler(final ComponentConfig config) {
50      super(config);
51      fieldIdAttribute = getAttribute("fieldId");
52    }
53  
54    protected abstract String getSubComponentType();
55  
56    protected abstract String getSubRendererType();
57  
58    protected abstract String getFacetName();
59  
60    @Override
61    public void applyNextHandler(final FaceletContext faceletContext, final UIComponent menuCommand) throws IOException {
62      if (ComponentHandler.isNew(menuCommand)) {
63        Deprecation.LOG.warn("The tx library is deprecated, please use the tc library. "
64            + "See 'Migration to 3.0' on the web site.");
65        final UIComponent component = menuCommand.getFacets().remove(getFacetName());
66        nextHandler.apply(faceletContext, component);
67        menuCommand.getFacets().put(getFacetName(), component);
68      } else {
69        nextHandler.apply(faceletContext, menuCommand.getFacet(getFacetName()));
70      }
71    }
72  
73    @Override
74    public void onComponentCreated(
75        final FaceletContext faceletContext, final UIComponent menuCommand, final UIComponent parent) {
76  
77      final Application application = faceletContext.getFacesContext().getApplication();
78      final UIComponent component = application.createComponent(getSubComponentType());
79      final String uid = fieldIdAttribute != null
80          ? fieldIdAttribute.getValue(faceletContext)
81          : menuCommand.getId() + "_tx_" + getFacetName();
82      component.setId(uid);
83      component.setRendererType(getSubRendererType());
84      setSubComponentAttributes(faceletContext, component);
85      menuCommand.getFacets().put(getFacetName(), component);
86    }
87  
88    private void setSubComponentAttributes(final FaceletContext ctx, final Object instance) {
89      if (instance != null) {
90        final Class type = instance.getClass();
91        if (subComponentMapper == null || !subComponentLastType.equals(type)) {
92          subComponentLastType = type;
93          subComponentMapper = createSubComponentMetaRuleset(type).finish();
94        }
95        subComponentMapper.applyMetadata(ctx, instance);
96      }
97    }
98  
99    protected MetaRuleset createSubComponentMetaRuleset(final Class aClass) {
100     final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
101     final TagAttribute [] attrs = tag.getAttributes().getAll();
102     for (int i = 0; i < attrs.length; i++) {
103       final TagAttribute attr = attrs[i];
104       if (!(attr.getLocalName().equals(Attributes.converter.getName())
105           || attr.getLocalName().equals(Attributes.value.getName()))) {
106         metaRuleset.ignore(attr.getLocalName());
107       }
108     }
109     return metaRuleset;
110   }
111 
112   @Override
113   protected MetaRuleset createMetaRuleset(final Class aClass) {
114     final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
115     metaRuleset.ignore(Attributes.converter.getName());
116     metaRuleset.ignore(Attributes.value.getName());
117     return metaRuleset;
118   }
119 }