001    /*
002     * Copyright 2002-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.form.binding.swing.date;
017    
018    import net.sf.nachocalendar.components.DateField;
019    import org.springframework.beans.BeanWrapper;
020    import org.springframework.beans.BeanWrapperImpl;
021    import org.springframework.binding.form.FormModel;
022    import org.springframework.richclient.form.binding.Binding;
023    import org.springframework.util.Assert;
024    
025    import javax.swing.*;
026    import java.awt.*;
027    import java.util.Map;
028    
029    /**
030     * Binds a <cod>Date</code> to a NachoCalendar <code>DateField</code>
031     * 
032     * @author Geoffrey De Smet
033     * @author Benoit Xhenseval (added dateFormat setting)
034     */
035    public class NachoCalendarDateFieldBinder extends AbstractDateFieldBinder {
036    
037            public static final String ALLOWS_INVALID_KEY = "allowsInvalid";
038            public static final String ANTI_ALIASED_KEY = "antiAliased";
039            public static final String FIRST_DAY_OF_WEEK_KEY = "firstDayOfWeek";
040            public static final String HEADER_RENDERER_KEY = "headerRenderer";
041            public static final String MODEL_KEY = "model";
042            public static final String PRINT_MOON_KEY = "printMoon";
043            public static final String RENDERER_KEY = "renderer";
044            public static final String SHOW_OK_CANCEL_KEY = "showOkCancel";
045            public static final String SHOW_TODAY_KEY = "showToday";
046            public static final String TODAY_CAPTION_KEY = "todayCaption";
047            public static final String WORKING_DAYS_KEY = "workingDays";
048            public static final String SHOW_WEEKNUMBERS_KEY = "showWeekNumbers";
049    
050            public NachoCalendarDateFieldBinder() {
051                    super(new String[] { SHOW_OK_CANCEL_KEY, ALLOWS_INVALID_KEY, PRINT_MOON_KEY, FIRST_DAY_OF_WEEK_KEY,
052                                    HEADER_RENDERER_KEY, MODEL_KEY, RENDERER_KEY, SHOW_TODAY_KEY, TODAY_CAPTION_KEY, ANTI_ALIASED_KEY,
053                                    WORKING_DAYS_KEY, DATE_FORMAT, SHOW_WEEKNUMBERS_KEY });
054            }
055    
056            protected Binding doBind(JComponent control, FormModel formModel, String formPropertyPath, Map context) {
057                    Assert.isTrue(control instanceof DateField, "Control must be an instance of DateField.");
058                    NachoCalendarDateFieldBinding binding = new NachoCalendarDateFieldBinding((DateField) control, formModel,
059                                    formPropertyPath);
060                    applyContext(binding, context);
061                    return binding;
062            }
063    
064            protected void applyContext(NachoCalendarDateFieldBinding binding, Map context) {
065                    super.applyContext(binding, context);
066            BeanWrapper wrapper = new BeanWrapperImpl(binding.getControl());
067                    Object dateFormat = context.get(DATE_FORMAT);
068                    // remove DATE_FORMAT temporarily since it is handled in the super class
069                    context.remove(DATE_FORMAT);
070                    wrapper.setPropertyValues(context);
071                    if (dateFormat != null) {
072                            // restore the original context
073                            context.put(DATE_FORMAT, dateFormat);
074            }
075            }
076    
077            protected JComponent createControl(Map context) {
078                    final int preferredHeight = getComponentFactory().createComboBox().getPreferredSize().height;
079    
080                    boolean showWeekNumbers = false;
081                    if (context.containsKey(SHOW_WEEKNUMBERS_KEY)) {
082                            showWeekNumbers = (Boolean) context.get(SHOW_WEEKNUMBERS_KEY);
083                            context.remove(SHOW_WEEKNUMBERS_KEY);
084                    }
085    
086    
087                    DateField dateField = new DateField(showWeekNumbers) {
088    
089                            private static final long serialVersionUID = 1L;
090    
091                            public Dimension getPreferredSize() {
092                                    Dimension size = super.getPreferredSize();
093                                    size.height = preferredHeight;
094    
095                                    return size;
096                            }
097                    };
098    
099                    return dateField;
100            }
101    }