001 /*
002 * Copyright 2002-2006 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.samples.simple.ui;
017
018 import com.jgoodies.forms.layout.FormLayout;
019 import org.springframework.richclient.form.AbstractFocussableForm;
020 import org.springframework.richclient.form.FormModelHelper;
021 import org.springframework.richclient.form.binding.swing.NumberBinder;
022 import org.springframework.richclient.form.builder.FormLayoutFormBuilder;
023 import org.springframework.richclient.form.builder.TableFormBuilder;
024 import org.springframework.richclient.samples.simple.domain.Contact;
025 import org.springframework.richclient.samples.simple.ui.binding.TodoItemListBinding;
026
027 import javax.swing.*;
028 import java.util.HashMap;
029
030 /**
031 * Form to handle the properties of a Contact object. It uses a {@link TableFormBuilder} to construct the layout of the
032 * form. Contact object properties are easily bound to UI controls using the form builder's
033 * {@link TableFormBuilder#add(String)} method. The platform takes care of determining which kind of control to create
034 * based on the type of the property in question.
035 * @author Larry Streepy
036 */
037 public class ContactForm extends AbstractFocussableForm
038 {
039 public ContactForm(Contact contact) {
040 super(FormModelHelper.createFormModel(contact, "contactForm"));
041 }
042
043 protected JComponent createFormControl()
044 {
045 FormLayout layout = new FormLayout("right:pref, 4dlu, fill:pref:grow, 6dlu, right:pref, 4dlu, fill:pref:grow", "default");
046 FormLayoutFormBuilder formBuilder = new FormLayoutFormBuilder(getBindingFactory(), layout);
047
048 formBuilder.setLabelAttributes("r, c");
049 formBuilder.addHorizontalSeparator("General", 7);
050 formBuilder.nextRow();
051 formBuilder.addPropertyAndLabel("lastName");
052 setFocusControl(formBuilder.addPropertyAndLabel("firstName", 5)[1]);
053 formBuilder.nextRow();
054 formBuilder.addPropertyAndLabel("dateOfBirth");
055 formBuilder.nextRow();
056 formBuilder.addPropertyAndLabel("homePhone");
057 formBuilder.addPropertyAndLabel("workPhone", 5);
058 formBuilder.nextRow();
059 formBuilder.addPropertyAndLabel("emailAddress");
060 formBuilder.nextRow();
061 formBuilder.addPropertyAndLabel("contactType");
062 formBuilder.nextRow();
063 NumberBinder binder = new NumberBinder();
064 binder.setLeftDecoration("€");
065 formBuilder.addLabel("monthlyIncome");
066 formBuilder.addBinding(binder.bind(getFormModel(), "monthlyIncome", new HashMap()), 3);
067 formBuilder.nextRow();
068 formBuilder.addHorizontalSeparator("Address", 7);
069 formBuilder.nextRow();
070 formBuilder.addPropertyAndLabel("address.address1");
071 formBuilder.nextRow();
072 formBuilder.addPropertyAndLabel("address.address2");
073 formBuilder.nextRow();
074 formBuilder.addPropertyAndLabel("address.address3");
075 formBuilder.nextRow();
076 formBuilder.addPropertyAndLabel("address.city");
077 formBuilder.nextRow();
078 // formBuilder.add(getBindingFactory().createBoundComboBox( "address.state", MasterLists.STATE_CODE), "colSpan=1 align=left" );
079 formBuilder.addPropertyAndLabel("address.state");
080 formBuilder.nextRow();
081 JComponent zipField = formBuilder.addPropertyAndLabel("address.zip")[1];
082 ((JTextField) zipField).setColumns(8);
083 formBuilder.nextRow();
084 formBuilder.addHorizontalSeparator("Memo", 7);
085 formBuilder.nextRow("fill:default:grow");
086 formBuilder.addTextArea("memo", 1, formBuilder.getRow(), 7, 1);
087 formBuilder.nextRow();
088 formBuilder.addHorizontalSeparator("Todo items", 7);
089 formBuilder.nextRow("fill:default:grow");
090 TodoItemListBinding todoItemListBinding = new TodoItemListBinding(getFormModel(), "todoItems");
091 formBuilder.addBinding(todoItemListBinding, 1, formBuilder.getRow(), 7, 1);
092
093 /*
094 TableFormBuilder formBuilder = new TableFormBuilder(getBindingFactory());
095 formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");
096
097 formBuilder.addSeparator("General");
098 formBuilder.row();
099 firstNameField = formBuilder.add("firstName")[1];
100 formBuilder.add("lastName");
101 formBuilder.row();
102 formBuilder.add("dateOfBirth", "colSpan=1");
103 formBuilder.row();
104 formBuilder.add("homePhone");
105 formBuilder.add("workPhone");
106 formBuilder.row();
107 formBuilder.add("emailAddress");
108 formBuilder.row();
109 formBuilder.row();
110 formBuilder.add("contactType", "colSpan=1 align=left");
111 formBuilder.row();
112 formBuilder.addSeparator("Address");
113 formBuilder.row();
114 formBuilder.add("address.address1");
115 formBuilder.row();
116 formBuilder.add("address.address2");
117 formBuilder.row();
118 formBuilder.add("address.address3");
119 formBuilder.row();
120 formBuilder.add("address.city", "colSpan=1 align=left");
121 formBuilder.row();
122 // formBuilder.add(getBindingFactory().createBoundComboBox( "address.state", MasterLists.STATE_CODE), "colSpan=1 align=left" );
123 formBuilder.add("address.state", "colSpan=1 align=left");
124 formBuilder.row();
125
126 // We want to make the zip code UI field smaller than the default. The add method
127 // returns an array of two components, the field label and the component bound to
128 // the property.
129 JComponent zipField = formBuilder.add("address.zip", "colSpan=1 align=left")[1];
130 ((JTextField) zipField).setColumns(8);
131 formBuilder.row();
132 */
133
134 return formBuilder.getPanel();
135 }
136
137 }