001    package org.springframework.richclient.samples.showcase.binding;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    import javax.swing.JComponent;
007    import javax.swing.JLabel;
008    import javax.swing.JPanel;
009    
010    import org.springframework.binding.form.FormModel;
011    import org.springframework.richclient.command.ActionCommand;
012    import org.springframework.richclient.command.CommandGroup;
013    import org.springframework.richclient.form.FormModelHelper;
014    import org.springframework.richclient.form.builder.TableFormBuilder;
015    import org.springframework.richclient.samples.showcase.util.AbstractReporterForm;
016    import org.springframework.richclient.samples.showcase.util.AbstractReporterTitledApplicationDialog;
017    import org.springframework.richclient.samples.showcase.util.Reporter;
018    import org.springframework.rules.PropertyConstraintProvider;
019    import org.springframework.rules.constraint.property.PropertyConstraint;
020    import org.springframework.rules.factory.Constraints;
021    
022    import com.jgoodies.forms.factories.FormFactory;
023    import com.jgoodies.forms.layout.CellConstraints;
024    import com.jgoodies.forms.layout.ColumnSpec;
025    import com.jgoodies.forms.layout.FormLayout;
026    import com.jgoodies.forms.layout.RowSpec;
027    
028    public class ParentChildFormDialog extends AbstractReporterTitledApplicationDialog {
029    
030            private class ChildPOJO implements PropertyConstraintProvider {
031                    private String childName;
032    
033                    private String childDescription;
034    
035                    private Map<String, PropertyConstraint> propertyConstraints;
036    
037                    public ChildPOJO() {
038                            propertyConstraints = new HashMap<String, PropertyConstraint>(1);
039                            propertyConstraints.put("childName", Constraints.instance().required("childName"));
040                    }
041    
042                    public String getChildName() {
043                            return childName;
044                    }
045    
046                    public void setChildName(String childName) {
047                            this.childName = childName;
048                    }
049    
050                    public String getChildDescription() {
051                            return childDescription;
052                    }
053    
054                    public void setChildDescription(String childDescription) {
055                            this.childDescription = childDescription;
056                    }
057    
058                    @Override
059                    public String toString() {
060                            return "childName = " + childName + ", childDescription = " + childDescription;
061                    }
062    
063                    public PropertyConstraint getPropertyConstraint(String propertyName) {
064                            return propertyConstraints.get(propertyName);
065                    }
066            }
067    
068            private class ParentPOJO implements PropertyConstraintProvider {
069                    private String parentName;
070    
071                    private String parentDescription;
072    
073                    private Map<String, PropertyConstraint> propertyConstraints;
074    
075                    public ParentPOJO() {
076                            propertyConstraints = new HashMap<String, PropertyConstraint>(1);
077                            propertyConstraints.put("parentName", Constraints.instance().required("parentName"));
078                    }
079    
080                    public String getParentName() {
081                            return parentName;
082                    }
083    
084                    public void setParentName(String parentName) {
085                            this.parentName = parentName;
086                    }
087    
088                    public String getParentDescription() {
089                            return parentDescription;
090                    }
091    
092                    public void setParentDescription(String parentDescription) {
093                            this.parentDescription = parentDescription;
094                    }
095    
096                    @Override
097                    public String toString() {
098                            return "parentName = " + parentName + ", parentDescription = " + parentDescription;
099                    }
100    
101                    public PropertyConstraint getPropertyConstraint(String propertyName) {
102                            return propertyConstraints.get(propertyName);
103                    }
104            }
105    
106            private class ChildForm extends AbstractReporterForm {
107    
108                    public ChildForm() {
109                            super(FormModelHelper.createFormModel(new ChildPOJO(), "child"), "child");
110                    }
111    
112                    @Override
113                    protected JComponent createFormControl() {
114                            TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
115                            builder.add("childName");
116                            builder.row();
117                            builder.add("childDescription");
118                            return builder.getForm();
119                    }
120    
121            }
122    
123            private class ParentForm extends AbstractReporterForm {
124    
125                    private ChildForm childForm;
126    
127                    public ParentForm() {
128                            super(FormModelHelper.createFormModel(new ParentPOJO(), "parent"), "parent");
129                    }
130    
131                    @Override
132                    protected JComponent createFormControl() {
133                            TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
134                            builder.add("parentName");
135                            builder.row();
136                            builder.add("parentDescription");
137                            JPanel panel = new JPanel(new FormLayout(new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC,
138                                            FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC }, new RowSpec[] {
139                                            FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
140                                            FormFactory.UNRELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
141                                            FormFactory.DEFAULT_ROWSPEC, FormFactory.UNRELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
142                                            FormFactory.UNRELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC }));
143                            CellConstraints cc = new CellConstraints();
144                            panel.add(new JLabel(getMessage("parentForm.label")), cc.xy(1, 1));
145                            panel.add(builder.getForm(), cc.xy(3, 3));
146                            CommandGroup parentFormcommandGroup = CommandGroup.createCommandGroup(new ActionCommand[] {
147                                            getEnableFormModelCommand(), getReadOnlyFormModelCommand(), getValidatingFormModelCommand() });
148                            panel.add(parentFormcommandGroup.createButtonBar(), cc.xy(3, 5));
149                            panel.add(new JLabel(getMessage("childForm.label")), cc.xy(1, 7));
150                            childForm = new ChildForm();
151                            childForm.setMessageArea(getMessageArea());
152                            panel.add(childForm.getControl(), cc.xy(3, 9));
153                            CommandGroup childFormcommandGroup = CommandGroup.createCommandGroup(new ActionCommand[] {
154                                            childForm.getEnableFormModelCommand(), childForm.getReadOnlyFormModelCommand(),
155                                            childForm.getValidatingFormModelCommand() });
156                            panel.add(childFormcommandGroup.createButtonBar(), cc.xy(3, 11));
157                            addChildForm(childForm);
158                            newSingleLineResultsReporter(ParentChildFormDialog.this);
159                            return panel;
160                    }
161    
162                    @Override
163                    public StringBuilder getFieldsDetails(StringBuilder builder, FormModel formModel) {
164                            builder = super.getFieldsDetails(builder, formModel);
165                            return super.getFieldsDetails(builder, childForm.getFormModel());
166                    }
167    
168                    @Override
169                    public StringBuilder getFormObjectDetails(StringBuilder builder, FormModel formModel) {
170                            builder = super.getFormObjectDetails(builder, formModel);
171                            return super.getFormObjectDetails(builder, childForm.getFormModel());
172                    }
173    
174                    @Override
175                    public StringBuilder getFormModelDetails(StringBuilder builder, FormModel formModel) {
176                            builder = super.getFormModelDetails(builder, formModel);
177                            return super.getFormModelDetails(builder, childForm.getFormModel());
178                    }
179    
180                    @Override
181                    public void registerFormModelPropertyChangeListener() {
182                            childForm.registerFormModelPropertyChangeListener();
183                            super.registerFormModelPropertyChangeListener();
184                    }
185    
186                    @Override
187                    public void unregisterFormModelPropertyChangeListener() {
188                            childForm.unregisterFormModelPropertyChangeListener();
189                            super.unregisterFormModelPropertyChangeListener();
190                    }
191            }
192    
193            @Override
194            protected Reporter getReporter() {
195                    return new ParentForm();
196            }
197    
198    }