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