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