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.config.TobagoConfig;
29  import org.apache.myfaces.tobago.context.Markup;
30  
31  import javax.el.ValueExpression;
32  import javax.faces.application.Application;
33  import javax.faces.component.UIComponent;
34  import javax.faces.context.FacesContext;
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(final 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(
61        final FaceletContext faceletContext, final UIComponent panel, final UIComponent parent) {
62      super.onComponentPopulated(faceletContext, panel, parent);
63  
64      if (!TobagoConfig.getInstance(faceletContext.getFacesContext()).isClassicDateTimePicker()) {
65        return;
66      }
67  
68      if (panel.getChildCount() == 2) {
69        final Application application = faceletContext.getFacesContext().getApplication();
70        final UIForm form = (UIForm) application.createComponent(UIForm.COMPONENT_TYPE);
71        form.setRendererType(RendererTypes.FORM);
72        final String formId = formIdAttribute != null
73            ? formIdAttribute.getValue(faceletContext)
74            : panel.getId() + "_tx_form";
75        form.setId(formId);
76        panel.getChildren().add(form);
77  
78        final UIDatePicker picker = (UIDatePicker) application.createComponent(UIDatePicker.COMPONENT_TYPE);
79        picker.setRendererType(RendererTypes.DATE_PICKER);
80        picker.setFor("@auto");
81        final String pickerId = pickerIdAttribute != null
82            ? pickerIdAttribute.getValue(faceletContext)
83            : panel.getId() + "_tx_picker";
84        picker.setId(pickerId);
85        if (picker.getAttributes().get(OnComponentCreated.MARKER) == null) {
86          picker.getAttributes().put(OnComponentCreated.MARKER, Boolean.TRUE);
87          picker.onComponentCreated(faceletContext.getFacesContext(), panel);
88        }
89        if (markupAttribute != null) {
90          if (markupAttribute.isLiteral()) {
91            picker.setMarkup(Markup.valueOf(markupAttribute.getValue()));
92          } else {
93            final ValueExpression expression = markupAttribute.getValueExpression(faceletContext, Object.class);
94            picker.setValueExpression(Attributes.MARKUP, expression);
95          }
96        }
97        form.getChildren().add(picker);
98      }
99    }
100 
101   protected String getColumns(final String first) {
102 
103     if (TobagoConfig.getInstance(FacesContext.getCurrentInstance()).isClassicDateTimePicker()) {
104       return first + ";*;auto";
105     } else {
106       return super.getColumns(first);
107     }
108   }
109 }