001 package org.apache.myfaces.tobago.component; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.apache.myfaces.tobago.internal.component.AbstractUIForm; 021 import org.apache.myfaces.tobago.renderkit.RendererBase; 022 023 import javax.faces.component.UIComponent; 024 import javax.faces.context.FacesContext; 025 import javax.faces.event.FacesEvent; 026 import javax.faces.render.Renderer; 027 import java.util.List; 028 029 /* 030 * Date: 30.05.2006 031 * Time: 19:22:40 032 */ 033 public class UIDatePicker extends UILink implements OnComponentCreated { 034 035 public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.DatePicker"; 036 037 private String forComponent; 038 039 public Object saveState(FacesContext context) { 040 Object[] values = new Object[2]; 041 values[0] = super.saveState(context); 042 values[1] = forComponent; 043 return values; 044 } 045 046 public void restoreState(FacesContext context, Object savedState) { 047 Object[] values = (Object[]) savedState; 048 super.restoreState(context, values[0]); 049 forComponent = (String) values[1]; 050 } 051 052 private UIComponent getUIDateInput(UIComponent parent) { 053 for (UIComponent child : (List<UIComponent>) parent.getChildren()) { 054 if (child instanceof UIDate) { 055 return child; 056 } 057 } 058 return null; 059 } 060 061 public String getFor() { 062 return forComponent; 063 } 064 065 // XXX May be better use ComponentUtils.evaluateAutoFor() 066 public UIComponent getForComponent() { 067 if ("@auto".equals(forComponent)) { 068 UIComponent component = getUIDateInput(getParent()); 069 if (component == null && getParent() instanceof AbstractUIForm) { 070 component = getUIDateInput(getParent().getParent()); 071 } 072 return component; 073 } else { 074 return findComponent(forComponent); 075 } 076 } 077 078 public void setFor(String forComponent) { 079 this.forComponent = forComponent; 080 } 081 082 public void broadcast(FacesEvent facesEvent) { 083 FacesContext facesContext = FacesContext.getCurrentInstance(); 084 UIComponent popup = (UIComponent) getFacets().get(Facets.PICKER_POPUP); 085 String clientId = getForComponent().getClientId(facesContext); 086 UIComponent box = popup.findComponent("box"); 087 UIComponent calendar = box.findComponent("calendar"); 088 calendar.getAttributes().put(Attributes.DATE_INPUT_ID, clientId); 089 UIComponent time = box.findComponent("time"); 090 if (time != null) { 091 time.getAttributes().put(Attributes.DATE_INPUT_ID, clientId); 092 } 093 super.broadcast(facesEvent); 094 } 095 096 public void onComponentCreated(FacesContext context, UIComponent parent) { 097 Renderer renderer = getRenderer(getFacesContext()); 098 if (renderer instanceof RendererBase) { 099 ((RendererBase) renderer).onComponentCreated(context, this, parent); 100 } 101 } 102 }