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  public class SeparatorExtensionHandler extends ComponentHandler {
40    private TagAttribute labelAttribute;
41  
42    public SeparatorExtensionHandler(final ComponentConfig config) {
43      super(config);
44      labelAttribute = getAttribute(Attributes.LABEL);
45    }
46  
47    public void applyNextHandler(final FaceletContext faceletContext, final UIComponent separator)
48        throws IOException, ELException {
49      if (ComponentHandler.isNew(separator)) {
50        final UIComponent component = (UIComponent) separator.getFacets().remove(Facets.LABEL);
51        nextHandler.apply(faceletContext, component);
52        separator.getFacets().put(Facets.LABEL, component);
53      } else {
54        nextHandler.apply(faceletContext, separator.getFacet(Facets.LABEL));
55      }
56    }
57  
58    public void onComponentCreated(
59        final FaceletContext faceletContext, final UIComponent separator, final UIComponent parent) {
60      final Application application = faceletContext.getFacesContext().getApplication();
61      final UIOutput label = (UIOutput) application.createComponent(UILabel.COMPONENT_TYPE);
62      label.setId(separator.getId() + "_tx_label");
63      label.setRendererType("Label");
64      setAttributes(faceletContext, label);
65      separator.getFacets().put(Facets.LABEL, label);
66      if (labelAttribute != null) {
67        if (labelAttribute.isLiteral()) {
68          label.setValue(labelAttribute.getValue(faceletContext));
69        } else {
70          final ValueExpression expression = labelAttribute.getValueExpression(faceletContext, String.class);
71          label.setValueExpression(Attributes.VALUE, expression);
72        }
73      }
74    }
75  
76    protected MetaRuleset createMetaRuleset(final Class aClass) {
77      final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
78      if (UISeparator.class.isAssignableFrom(aClass)) {
79        metaRuleset.ignore(Attributes.LABEL);
80        return metaRuleset;
81      } else {
82        final TagAttribute[] attrs = tag.getAttributes().getAll();
83        for (int i = 0; i < attrs.length; i++) {
84          final TagAttribute attr = attrs[i];
85          metaRuleset.ignore(attr.getLocalName());
86        }
87        return metaRuleset;
88      }
89    }
90  }