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 static org.apache.myfaces.tobago.TobagoConstants.ATTR_ALT;
021    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_CALENDAR_DATE_INPUT_ID;
022    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_COLUMNS;
023    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_LABEL;
024    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_POPUP_RESET;
025    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_ROWS;
026    import static org.apache.myfaces.tobago.TobagoConstants.FACET_LAYOUT;
027    import static org.apache.myfaces.tobago.TobagoConstants.FACET_PICKER_POPUP;
028    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_BOX;
029    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_BUTTON;
030    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_CALENDAR;
031    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_GRID_LAYOUT;
032    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_HIDDEN;
033    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_IMAGE;
034    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_PANEL;
035    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_POPUP;
036    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_TIME;
037    import org.apache.myfaces.tobago.context.ResourceManagerUtil;
038    import org.apache.myfaces.tobago.event.DatePickerController;
039    import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
040    
041    import javax.faces.component.UIComponent;
042    import javax.faces.component.UIGraphic;
043    import javax.faces.context.FacesContext;
044    import javax.faces.event.FacesEvent;
045    import java.util.Map;
046    
047    /*
048     * Created by IntelliJ IDEA.
049     * User: bommel
050     * Date: 30.05.2006
051     * Time: 19:22:40
052     */
053    public class UIDatePicker extends UICommand implements OnComponentCreated {
054    
055      public static final String COMPONENT_TYPE =
056          "org.apache.myfaces.tobago.DatePicker";
057    
058      private String forComponent;
059    
060      public Object saveState(FacesContext context) {
061        Object[] values = new Object[2];
062        values[0] = super.saveState(context);
063        values[1] = forComponent;
064        return values;
065      }
066    
067      public void restoreState(FacesContext context, Object savedState) {
068        Object[] values = (Object[]) savedState;
069        super.restoreState(context, values[0]);
070        forComponent = (String) values[1];
071      }
072    
073      private UIComponent getUIDateInput(UIComponent parent) {
074        for (Object object : parent.getChildren()) {
075          UIComponent child = (UIComponent) object;
076          if (child instanceof UIDateInput) {
077            return child;
078          }
079        }
080        return null;
081      }
082    
083      public String getFor() {
084        if ("@auto".equals(forComponent)) {
085          UIComponent component = getUIDateInput(getParent());
086          if (component == null && getParent() instanceof UIForm) {
087            component = getUIDateInput(getParent().getParent());
088          }
089          if (component != null) {
090            return component.getId();
091          }
092        }
093        return forComponent;
094      }
095    
096      public UIComponent getForComponent() {
097        if ("@auto".equals(forComponent)) {
098          UIComponent component = getUIDateInput(getParent());
099          if (component == null && getParent() instanceof UIForm) {
100            component = getUIDateInput(getParent().getParent());
101          }
102          return component;
103        } else {
104          return findComponent(forComponent);
105        }
106      }
107    
108      public void setFor(String forComponent) {
109        this.forComponent = forComponent;
110      }
111    
112      public void broadcast(FacesEvent facesEvent) {
113        FacesContext facesContext = FacesContext.getCurrentInstance();
114        UIPopup popup = (UIPopup) getFacets().get(FACET_PICKER_POPUP);
115        String clientId = getForComponent().getClientId(facesContext);
116        UIComponent box = popup.findComponent("box");
117        UIComponent calendar = box.findComponent("calendar");
118        calendar.getAttributes().put(ATTR_CALENDAR_DATE_INPUT_ID, clientId);
119        UIComponent time = box.findComponent("time");
120        if (time != null) {
121          time.getAttributes().put(ATTR_CALENDAR_DATE_INPUT_ID, clientId);
122        }
123        super.broadcast(facesEvent);
124      }
125    
126      public void onComponentCreated() {
127        preparePicker(this);
128      }
129    
130      public void preparePicker(UIDatePicker link) {
131        FacesContext facesContext = FacesContext.getCurrentInstance();
132    
133        if (forComponent == null) {
134          link.setFor("@auto");
135        }
136        link.setImmediate(true);
137    
138        UIHiddenInput hidden =
139            (UIHiddenInput) ComponentUtil.createComponent(facesContext,
140                UIHiddenInput.COMPONENT_TYPE, RENDERER_TYPE_HIDDEN);
141        link.getChildren().add(hidden);
142    
143        // create popup
144        final UIPopup popup =
145            (UIPopup) ComponentUtil.createComponent(facesContext, UIPopup.COMPONENT_TYPE,
146                RENDERER_TYPE_POPUP);
147        //popup.setId(link.getId() + "popup");
148        link.getFacets().put(FACET_PICKER_POPUP, popup);
149    
150        popup.setRendered(false);
151    
152        Map<String, Object> attributes = popup.getAttributes();
153        attributes.put(ATTR_POPUP_RESET, Boolean.TRUE);
154        //int popupHeight = ThemeConfig.getValue(facesContext, link, "CalendarPopupHeight");
155        //attributes.put(ATTR_HEIGHT, String.valueOf(popupHeight));
156        final UIComponent box = ComponentUtil.createComponent(
157            facesContext, UIPanel.COMPONENT_TYPE, RENDERER_TYPE_BOX);
158        popup.getChildren().add(box);
159        box.setId("box");
160        // TODO: set string resources in renderer
161        box.getAttributes().put(ATTR_LABEL, ResourceManagerUtil.getPropertyNotNull(
162            facesContext, "tobago", "datePickerTitle"));
163        UIComponent layout = ComponentUtil.createComponent(
164            facesContext, UIGridLayout.COMPONENT_TYPE, RENDERER_TYPE_GRID_LAYOUT);
165        box.getFacets().put(FACET_LAYOUT, layout);
166        layout.setId("layout");
167        layout.getAttributes().put(ATTR_ROWS, "*;fixed;fixed");
168    //    layout.getAttributes().put(TobagoConstants.ATTR_BORDER, "1");
169    
170        final UIComponent calendar = ComponentUtil.createComponent(
171            facesContext, javax.faces.component.UIOutput.COMPONENT_TYPE,
172            RENDERER_TYPE_CALENDAR);
173    
174        calendar.setId("calendar");
175        box.getChildren().add(calendar);
176    
177        // add time input
178        final UIComponent timePanel = ComponentUtil.createComponent(
179            facesContext, UIPanel.COMPONENT_TYPE, RENDERER_TYPE_PANEL);
180        timePanel.setId("timePanel");
181        box.getChildren().add(timePanel);
182        layout = ComponentUtil.createComponent(
183            facesContext, UIGridLayout.COMPONENT_TYPE, RENDERER_TYPE_GRID_LAYOUT);
184        timePanel.getFacets().put(FACET_LAYOUT, layout);
185        layout.setId("timePanelLayout");
186        layout.getAttributes().put(ATTR_COLUMNS, "1*;fixed;1*");
187        UIComponent cell = ComponentUtil.createComponent(
188            facesContext, UIPanel.COMPONENT_TYPE, RENDERER_TYPE_PANEL);
189        cell.setId("cell1");
190        timePanel.getChildren().add(cell);
191    
192        final UIComponent time = ComponentUtil.createComponent(
193            facesContext,
194            org.apache.myfaces.tobago.component.UIInput.COMPONENT_TYPE,
195            RENDERER_TYPE_TIME);
196        timePanel.getChildren().add(time);
197        time.setId("time");
198    
199        cell = ComponentUtil.createComponent(
200            facesContext, UIPanel.COMPONENT_TYPE, RENDERER_TYPE_PANEL);
201        cell.setId("cell2");
202        timePanel.getChildren().add(cell);
203    
204        
205    
206        UIComponent buttonPanel = ComponentUtil.createComponent(
207            facesContext, UIPanel.COMPONENT_TYPE, RENDERER_TYPE_PANEL);
208        buttonPanel.setId("buttonPanel");
209        layout = ComponentUtil.createComponent(
210            facesContext, UIGridLayout.COMPONENT_TYPE, RENDERER_TYPE_GRID_LAYOUT);
211        buttonPanel.getFacets().put(FACET_LAYOUT, layout);
212        layout.getAttributes().put(ATTR_COLUMNS, "*;*");
213    //    layout.getAttributes().put(TobagoConstants.ATTR_BORDER, "1");
214    
215        box.getChildren().add(buttonPanel);
216    
217        final UICommand okButton =
218            (UICommand) ComponentUtil.createComponent(facesContext,
219                org.apache.myfaces.tobago.component.UICommand.COMPONENT_TYPE,
220                RENDERER_TYPE_BUTTON);
221        buttonPanel.getChildren().add(okButton);
222        okButton.setId("ok" + DatePickerController.CLOSE_POPUP);
223        attributes = okButton.getAttributes();
224        attributes.put(ATTR_LABEL, ResourceManagerUtil.getPropertyNotNull(
225            facesContext, "tobago", "datePickerOk"));
226    
227        final UICommand cancelButton =
228            (UICommand) ComponentUtil.createComponent(facesContext,
229                org.apache.myfaces.tobago.component.UICommand.COMPONENT_TYPE,
230                RENDERER_TYPE_BUTTON);
231        buttonPanel.getChildren().add(cancelButton);
232        attributes = cancelButton.getAttributes();
233        attributes.put(ATTR_LABEL, ResourceManagerUtil.getPropertyNotNull(
234            facesContext, "tobago", "datePickerCancel"));
235        cancelButton.setId(DatePickerController.CLOSE_POPUP);
236    
237        // create image
238        UIGraphic image = (UIGraphic) ComponentUtil.createComponent(
239            facesContext, UIGraphic.COMPONENT_TYPE, RENDERER_TYPE_IMAGE);
240        image.setRendered(true);
241        image.setValue("image/date.gif");
242        image.getAttributes().put(ATTR_ALT, ""); //TODO: i18n
243        StyleClasses.ensureStyleClasses(image).addFullQualifiedClass("tobago-input-picker"); // XXX not a standard name
244        link.getChildren().add(image);
245      }
246    
247    }