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.component.Facets;
24  import org.apache.myfaces.tobago.component.UILabel;
25  import org.apache.myfaces.tobago.component.UISeparator;
26  
27  import javax.el.ELException;
28  import javax.el.ValueExpression;
29  import javax.faces.application.Application;
30  import javax.faces.component.UIComponent;
31  import javax.faces.component.UIOutput;
32  import javax.faces.view.facelets.ComponentConfig;
33  import javax.faces.view.facelets.ComponentHandler;
34  import javax.faces.view.facelets.FaceletContext;
35  import javax.faces.view.facelets.MetaRuleset;
36  import javax.faces.view.facelets.TagAttribute;
37  import java.io.IOException;
38  
39  /**
40   * @deprecated since Tobago 3.0. The tx-library is deprecated, please use the tc-library.
41   */
42  @Deprecated
43  public class SeparatorExtensionHandler extends ComponentHandler {
44    private TagAttribute labelAttribute;
45  
46    public SeparatorExtensionHandler(final ComponentConfig config) {
47      super(config);
48      labelAttribute = getAttribute(Attributes.label.getName());
49    }
50  
51    @Override
52    public void applyNextHandler(final FaceletContext faceletContext, final UIComponent separator)
53        throws IOException, ELException {
54      if (ComponentHandler.isNew(separator)) {
55        final UIComponent component = separator.getFacets().remove(Facets.LABEL);
56        nextHandler.apply(faceletContext, component);
57        separator.getFacets().put(Facets.LABEL, component);
58      } else {
59        nextHandler.apply(faceletContext, separator.getFacet(Facets.LABEL));
60      }
61    }
62  
63    @Override
64    public void onComponentCreated(
65        final FaceletContext faceletContext, final UIComponent separator, final UIComponent parent) {
66      final Application application = faceletContext.getFacesContext().getApplication();
67      final UIOutput label = (UIOutput) application.createComponent(UILabel.COMPONENT_TYPE);
68      label.setId(separator.getId() + "_tx_label");
69      label.setRendererType("Label");
70      setAttributes(faceletContext, label);
71      separator.getFacets().put(Facets.LABEL, label);
72      if (labelAttribute != null) {
73        if (labelAttribute.isLiteral()) {
74          label.setValue(labelAttribute.getValue(faceletContext));
75        } else {
76          final ValueExpression expression = labelAttribute.getValueExpression(faceletContext, String.class);
77          label.setValueExpression(Attributes.value.getName(), expression);
78        }
79      }
80    }
81  
82    @Override
83    protected MetaRuleset createMetaRuleset(final Class aClass) {
84      final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
85      if (UISeparator.class.isAssignableFrom(aClass)) {
86        metaRuleset.ignore(Attributes.label.getName());
87        return metaRuleset;
88      } else {
89        final TagAttribute[] attrs = tag.getAttributes().getAll();
90        for (int i = 0; i < attrs.length; i++) {
91          final TagAttribute attr = attrs[i];
92          metaRuleset.ignore(attr.getLocalName());
93        }
94        return metaRuleset;
95      }
96    }
97  }