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