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    
017    package org.springframework.richclient.samples.showcase.binding;
018    
019    import java.util.Date;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import javax.swing.JComponent;
024    
025    import org.springframework.binding.form.ConfigurableFormModel;
026    import org.springframework.binding.form.FieldMetadata;
027    import org.springframework.binding.form.support.ReadOnlyFieldMetadata;
028    import org.springframework.binding.value.ValueModel;
029    import org.springframework.binding.value.support.MessageFormatValueModel;
030    import org.springframework.richclient.dialog.TitledApplicationDialog;
031    import org.springframework.richclient.form.AbstractForm;
032    import org.springframework.richclient.form.FormModelHelper;
033    import org.springframework.richclient.form.binding.Binding;
034    import org.springframework.richclient.form.binding.swing.SwingBindingFactory;
035    import org.springframework.richclient.form.binding.swing.date.NachoCalendarDateFieldBinder;
036    import org.springframework.richclient.form.builder.TableFormBuilder;
037    
038    public class CalendarBindingDialog extends TitledApplicationDialog {
039    
040            private class Values {
041                    private String name;
042    
043                    private String surname;
044    
045                    private Date birthday;
046    
047                    public String getName() {
048                            return name;
049            }
050    
051                    public void setName(String name) {
052                            this.name = name;
053                    }
054    
055                    public String getSurname() {
056                            return surname;
057                    }
058    
059                    public void setSurname(String surname) {
060                            this.surname = surname;
061                    }
062    
063                    public Date getBirthday() {
064                            return birthday;
065                    }
066    
067                    public void setBirthday(Date birthday) {
068                            this.birthday = birthday;
069                    }
070    
071            }
072    
073            private class MessageValueModelForm extends AbstractForm {
074    
075                    public MessageValueModelForm() {
076                            super(FormModelHelper.createFormModel(new Values()));
077                    }
078    
079                    @Override
080                    protected JComponent createFormControl() {
081                            TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
082                            builder.add("name");
083                            builder.row();
084                            builder.add("surname");
085                            builder.row();
086    
087                            Map<String, Object> context = new HashMap<String, Object>();
088                            context.put(NachoCalendarDateFieldBinder.SHOW_OK_CANCEL_KEY, Boolean.TRUE);
089                            context.put(NachoCalendarDateFieldBinder.SHOW_WEEKNUMBERS_KEY, Boolean.TRUE);
090    //                      context.put(NachoCalendarDateFieldBinder.DATE_FORMAT, "MM'/'yyyy");
091                            context.put(NachoCalendarDateFieldBinder.WORKING_DAYS_KEY, new boolean[] {true, true, true, false, false, false, false});
092                            final SwingBindingFactory bf = (SwingBindingFactory) getBindingFactory();
093                            Binding b = bf.createBinding("birthday", context);
094                            builder.add(b);
095    //                      builder.add("birthday");
096                            builder.row();
097                            ConfigurableFormModel formModel = getFormModel();
098                            ValueModel derivedValueModel = new MessageFormatValueModel("{0} {1} was born on {2}", new ValueModel[] {
099                                            getValueModel("name"), getValueModel("surname"), getValueModel("birthday") });
100                            FieldMetadata fieldMetaData = new ReadOnlyFieldMetadata(getFormModel(), String.class);
101                            formModel.add("derivedBirthday", derivedValueModel, fieldMetaData);
102                            builder.add("derivedBirthday");
103                            return builder.getForm();
104                    }
105    
106            }
107    
108            @Override
109            protected JComponent createTitledDialogContentPane() {
110                    return (new MessageValueModelForm()).getControl();
111            }
112    
113            @Override
114            protected boolean onFinish() {
115                    return true;
116            }
117    
118    }