001    /*
002     * Copyright 2002-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of 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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.form.binding.swing.date;
017    
018    import java.beans.PropertyChangeEvent;
019    import java.beans.PropertyChangeListener;
020    import java.text.SimpleDateFormat;
021    import java.util.Date;
022    
023    import javax.swing.JComponent;
024    
025    import net.sf.nachocalendar.components.DateField;
026    
027    import org.springframework.binding.form.FormModel;
028    
029    /**
030     * Binds a <cod>Date</code> to a NachoCalendar <code>DateField</code>
031     * 
032     * @author Geoffrey De Smet
033     */
034    public class NachoCalendarDateFieldBinding extends AbstractDateFieldBinding {
035    
036            private final DateField dateField;
037    
038            private Boolean showOkCancel;
039    
040            public NachoCalendarDateFieldBinding(DateField dateField, FormModel formModel, String formPropertyPath) {
041                    super(formModel, formPropertyPath);
042                    this.dateField = dateField;
043            }
044    
045            public Boolean getShowOkCancel() {
046                    return showOkCancel;
047            }
048    
049            public void setShowOkCancel(Boolean showOkCancel) {
050                    this.showOkCancel = showOkCancel;
051            }
052    
053            protected JComponent doBindControl() {
054                    dateField.setValue((Date) getValue());
055                    if (showOkCancel != null) {
056                            dateField.setShowOkCancel(showOkCancel.booleanValue());
057                    }
058                    if (getDateFormat() != null) {
059                            dateField.setDateFormat(new SimpleDateFormat(getDateFormat()));
060                    }
061                    dateField.addPropertyChangeListener("value", new PropertyChangeListener() {
062                            public void propertyChange(PropertyChangeEvent evt) {
063                                    controlValueChanged(dateField.getValue());
064                            }
065                    });
066                    
067                    return dateField;
068            }
069    
070            protected void valueModelChanged(Object newValue) {
071                    dateField.setValue((Date) newValue);
072            }
073    
074            protected void readOnlyChanged() {
075                    dateField.setEnabled(isEnabled() && !isReadOnly());
076            }
077    
078            protected void enabledChanged() {
079                    dateField.setEnabled(isEnabled() && !isReadOnly());
080            }
081    
082    }