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    
028    /*
029     * Date: 30.05.2006
030     * Time: 19:22:40
031     */
032    public class UIDatePicker extends UILink implements OnComponentCreated {
033    
034      public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.DatePicker";
035    
036      private String forComponent;
037    
038      public Object saveState(FacesContext context) {
039        Object[] values = new Object[2];
040        values[0] = super.saveState(context);
041        values[1] = forComponent;
042        return values;
043      }
044    
045      public void restoreState(FacesContext context, Object savedState) {
046        Object[] values = (Object[]) savedState;
047        super.restoreState(context, values[0]);
048        forComponent = (String) values[1];
049      }
050    
051      private UIComponent getUIDateInput(UIComponent parent) {
052        for (Object object : parent.getChildren()) {
053          UIComponent child = (UIComponent) object;
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    }