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.OnComponentCreated;
24  import org.apache.myfaces.tobago.component.RendererTypes;
25  import org.apache.myfaces.tobago.component.UIDate;
26  import org.apache.myfaces.tobago.component.UIDatePicker;
27  import org.apache.myfaces.tobago.component.UIForm;
28  import org.apache.myfaces.tobago.context.Markup;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  
31  import javax.el.ValueExpression;
32  import javax.faces.application.Application;
33  import javax.faces.component.UIComponent;
34  import javax.faces.component.UIViewRoot;
35  import javax.faces.view.facelets.ComponentConfig;
36  import javax.faces.view.facelets.FaceletContext;
37  import javax.faces.view.facelets.TagAttribute;
38  
39  public class DateExtensionHandler extends TobagoLabelExtensionHandler {
40  
41    private TagAttribute pickerIdAttribute;
42    private TagAttribute formIdAttribute;
43    private TagAttribute markupAttribute;
44  
45    public DateExtensionHandler(ComponentConfig config) {
46      super(config);
47      pickerIdAttribute = getAttribute("pickerId");
48      formIdAttribute = getAttribute("formId");
49      markupAttribute = getAttribute(Attributes.MARKUP);
50    }
51  
52    protected String getSubComponentType() {
53      return UIDate.COMPONENT_TYPE;
54    }
55  
56    protected String getSubRendererType() {
57      return RendererTypes.DATE;
58    }
59  
60    public void onComponentPopulated(FaceletContext faceletContext, UIComponent panel, UIComponent parent) {
61      super.onComponentPopulated(faceletContext, panel, parent);
62      if (panel.getChildCount() == 2) {
63        Application application = faceletContext.getFacesContext().getApplication();
64        UIViewRoot root = ComponentUtils.findViewRoot(faceletContext, parent);
65  
66        UIForm form = (UIForm) application.createComponent(UIForm.COMPONENT_TYPE);
67        form.setRendererType(RendererTypes.FORM);
68        final String formId = formIdAttribute != null
69            ? formIdAttribute.getValue(faceletContext)
70            : "_tx_" + faceletContext.generateUniqueId("form");
71        form.setId(formId);
72        panel.getChildren().add(form);
73  
74        UIDatePicker picker = (UIDatePicker) application.createComponent(UIDatePicker.COMPONENT_TYPE);
75        picker.setRendererType(RendererTypes.DATE_PICKER);
76        picker.setFor("@auto");
77        final String pickerId = pickerIdAttribute != null
78            ? pickerIdAttribute.getValue(faceletContext)
79            : "_tx_" + faceletContext.generateUniqueId("picker");
80        picker.setId(pickerId);
81        if (picker.getAttributes().get(OnComponentCreated.MARKER) == null) {
82          picker.getAttributes().put(OnComponentCreated.MARKER, Boolean.TRUE);
83          picker.onComponentCreated(faceletContext.getFacesContext(), panel);
84        }
85        if (markupAttribute != null) {
86          if (markupAttribute.isLiteral()) {
87            picker.setMarkup(Markup.valueOf(markupAttribute.getValue()));
88          } else {
89            ValueExpression expression = markupAttribute.getValueExpression(faceletContext, Object.class);
90            picker.setValueExpression(Attributes.MARKUP, expression);
91          }
92        }
93        form.getChildren().add(picker);
94      }
95    }
96  
97    protected String getColumns(String first) {
98      return first + ";*;auto";
99    }
100 }