001: package com.silvermindsoftware.hitch.sample;
002:
003: /**
004: * Copyright 2007 Brandon Goodin
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import com.silvermindsoftware.hitch.Binder;
020: import com.silvermindsoftware.hitch.BinderManager;
021: import com.silvermindsoftware.hitch.HitchUtil;
022: import com.silvermindsoftware.hitch.sample.handler.FavoriteColorComponentHandler;
023: import com.silvermindsoftware.hitch.sample.model.Address;
024: import com.silvermindsoftware.hitch.sample.model.Person;
025: import com.silvermindsoftware.hitch.sample.model.Sex;
026: import com.silvermindsoftware.hitch.sample.model.Vehicle;
027: import com.silvermindsoftware.hitch.sample.dao.SexDao;
028: import com.silvermindsoftware.hitch.sample.dao.AddressDao;
029: import com.silvermindsoftware.hitch.sample.dao.PersonDao;
030: import com.silvermindsoftware.hitch.sample.dao.VehicleDao;
031: import com.silvermindsoftware.hitch.config.BinderConfig;
032: import com.silvermindsoftware.hitch.config.BoundComponentConfig;
033: import com.silvermindsoftware.hitch.config.FormConfig;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import javax.swing.*;
038: import java.awt.*;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041:
042: public class PersonBinderConfigFormPanel extends JPanel {
043:
044: private static final Log log = LogFactory
045: .getLog(PersonBinderConfigFormPanel.class);
046:
047: private final Binder binder = BinderManager.getBinder(this );
048:
049: public static final String ADDRESS = "address";
050: public static final String PERSON = "person";
051:
052: private PersonDao personDao = new PersonDao();
053: private AddressDao addressDao = new AddressDao();
054: private SexDao sexDao = new SexDao();
055: private VehicleDao vehicleDao = new VehicleDao();
056:
057: public String[] states = new String[] { "Alabama", "Alaska",
058: "Arizona", "Arkansas", "California", "Colorado",
059: "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
060: "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
061: "Kentucky", "Louisiana", "Maine", "Maryland",
062: "Massachusetts", "Michigan", "Minnesota", "Mississippi",
063: "Missouri", "Montana", "Nebraska", "Nevada",
064: "New Hampshire", "New Jersey", "New Mexico", "New York",
065: "North Carolina", "North Dakota", "Ohio", "Oklahoma",
066: "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
067: "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
068: "Virginia", "Washington", "West Virginia", "Wisconsin",
069: "Wyoming" };
070:
071: // this is the default model object
072: private Person person = personDao.getPerson();
073:
074: // Person Fields that bind to the default model object
075: private JTextField firstName, lastNameTextField,
076: middleNameTextField, age, height, income;
077:
078: private JFormattedTextField birthDate;
079:
080: private JComboBox sex, primaryVehicle, secondaryVehicle;
081:
082: // Address Model Object
083: Address address = addressDao.getAddress();
084:
085: // Address Fields that bind to the ADDRESS model object
086: private JTextField address1, address2, city, zip;
087:
088: private JList state;
089:
090: private JCheckBox cool;
091:
092: private JTextField favoriteColor;
093:
094: private JSpinner numberOfChildren;
095:
096: private JSpinner favoriteVehicle;
097:
098: private JSpinner fanciestVehicle;
099:
100: private JSlider shoeSize;
101:
102: private JTextArea notes;
103:
104: public PersonBinderConfigFormPanel() {
105: super ();
106:
107: BinderConfig binderConfig = new BinderConfig(this .getClass(),
108: binder, new FormConfig(true));
109:
110: // bind model
111: // bind
112: binderConfig.bindDefaultModel(PERSON);
113: binderConfig
114: .bindComponentToDefault(
115: new BoundComponentConfig("firstName"),
116: new BoundComponentConfig("lastNameTextField",
117: "lastName"),
118: new BoundComponentConfig("middleNameTextField",
119: "middleName"),
120: new BoundComponentConfig("age"),
121: new BoundComponentConfig("height"),
122: new BoundComponentConfig("income"),
123: new BoundComponentConfig("birthDate"),
124: new BoundComponentConfig("cool"),
125: new BoundComponentConfig("favoriteColor",
126: FavoriteColorComponentHandler.class,
127: new String[] { "colorA=RED",
128: "colorB=ORANGE" }),
129: new BoundComponentConfig("sex", new String[] {
130: "compareProperties=id",
131: "valueProperty=id" }),
132: new BoundComponentConfig(
133: "primaryVehicle",
134: new String[] { "compareProperties=id,type" }),
135: new BoundComponentConfig("secondaryVehicle"),
136: new BoundComponentConfig("favoriteVehicle"),
137: new BoundComponentConfig("numberOfChildren"),
138: new BoundComponentConfig("fanciestVehicle",
139: new String[] { "compareProperties=id",
140: "valueProperty=id" }),
141: new BoundComponentConfig("shoeSize"),
142: new BoundComponentConfig("notes"));
143:
144: binderConfig.bindModel(ADDRESS);
145: binderConfig.bindComponent(ADDRESS, new BoundComponentConfig(
146: "address1"), new BoundComponentConfig("address2"),
147: new BoundComponentConfig("city"),
148: new BoundComponentConfig("state"),
149: new BoundComponentConfig("zip"));
150:
151: GridBagLayout layout = new GridBagLayout();
152:
153: GridBagConstraints c = new GridBagConstraints();
154: c.fill = GridBagConstraints.HORIZONTAL;
155:
156: this .setLayout(layout);
157:
158: JLabel firstNameLabel = new JLabel("First:");
159: firstNameLabel.setHorizontalAlignment(JLabel.RIGHT);
160: firstName = new JTextField();
161:
162: JLabel lastNameLabel = new JLabel("Last:");
163: lastNameLabel.setHorizontalAlignment(JLabel.RIGHT);
164: lastNameTextField = new JTextField();
165:
166: JLabel middleNameLabel = new JLabel("Middle:");
167: middleNameLabel.setHorizontalAlignment(JLabel.RIGHT);
168: middleNameTextField = new JTextField();
169:
170: JLabel ageLabel = new JLabel("Age:");
171: ageLabel.setHorizontalAlignment(JLabel.RIGHT);
172: age = new JTextField();
173:
174: JLabel heightLabel = new JLabel("Height:");
175: heightLabel.setHorizontalAlignment(JLabel.RIGHT);
176: height = new JTextField();
177:
178: JLabel incomeLabel = new JLabel("Income:");
179: incomeLabel.setHorizontalAlignment(JLabel.RIGHT);
180: income = new JTextField();
181:
182: JLabel birthDateLabel = new JLabel("Birth Date:");
183: birthDateLabel.setHorizontalAlignment(JLabel.RIGHT);
184: birthDate = new JFormattedTextField();
185:
186: JLabel address1Label = new JLabel("Address1:");
187: address1Label.setHorizontalAlignment(JLabel.RIGHT);
188: address1 = new JTextField();
189:
190: JLabel address2Label = new JLabel("Address2:");
191: address2Label.setHorizontalAlignment(JLabel.RIGHT);
192: address2 = new JTextField();
193:
194: JLabel cityLabel = new JLabel("City:");
195: cityLabel.setHorizontalAlignment(JLabel.RIGHT);
196: city = new JTextField();
197:
198: JLabel stateLabel = new JLabel("State:");
199: stateLabel.setHorizontalAlignment(JLabel.RIGHT);
200: state = new JList(states);
201: state.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
202: JScrollPane stateScrollPane = new JScrollPane(state);
203: stateScrollPane.setPreferredSize(new Dimension(100, 50));
204:
205: JLabel zipLabel = new JLabel("Zip:");
206: zipLabel.setHorizontalAlignment(JLabel.RIGHT);
207: zip = new JTextField();
208:
209: JLabel sexLabel = new JLabel("Sex:");
210: sexLabel.setHorizontalAlignment(JLabel.RIGHT);
211: sex = new JComboBox(sexDao.getList().toArray(new Sex[] {}));
212:
213: cool = new JCheckBox("Cool");
214:
215: JLabel favoriteColorLabel = new JLabel("Favorite Color:");
216: favoriteColorLabel.setHorizontalAlignment(JLabel.RIGHT);
217: favoriteColor = new JTextField();
218:
219: JLabel primaryVehicleLabel = new JLabel("Primary Vehicle:");
220: primaryVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
221: primaryVehicle = new JComboBox(vehicleDao.getList().toArray(
222: new Vehicle[] {}));
223:
224: JLabel secondaryVehicleLabel = new JLabel("Secondary Vehicle:");
225: secondaryVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
226: secondaryVehicle = new JComboBox(vehicleDao.getList().toArray(
227: new Vehicle[] {}));
228:
229: JLabel favoriteVehicleLabel = new JLabel("Favorite Vehicle:");
230: favoriteVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
231: favoriteVehicle = HitchUtil.getEnhancedJSpinner(vehicleDao
232: .getList());
233:
234: JLabel fanciestVehicleLabel = new JLabel("Favorite Vehicle:");
235: fanciestVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
236: fanciestVehicle = HitchUtil.getEnhancedJSpinner(vehicleDao
237: .getList());
238:
239: JLabel numberOfChildrenLabel = new JLabel("Number of Children:");
240: numberOfChildrenLabel.setHorizontalAlignment(JLabel.RIGHT);
241: numberOfChildren = new JSpinner(new SpinnerNumberModel(0, 0,
242: 50, 1));
243:
244: JLabel shoeSizeLabel = new JLabel("Number of Children:");
245: shoeSizeLabel.setHorizontalAlignment(JLabel.RIGHT);
246: shoeSize = new JSlider(1, 17);
247:
248: JLabel notesLabel = new JLabel("Number of Children:");
249: notesLabel.setHorizontalAlignment(JLabel.RIGHT);
250: notes = new JTextArea();
251:
252: JButton submit = new JButton("Save");
253: submit.addActionListener(new ActionListener() {
254: public void actionPerformed(ActionEvent e) {
255: update();
256: }
257: });
258:
259: JPanel buttonPanel = new JPanel(new FlowLayout());
260: buttonPanel.add(submit);
261:
262: // person adds
263: c.gridx = 0;
264: c.gridy = 0;
265: c.weightx = 0.1;
266: this .add(firstNameLabel, c);
267:
268: c.gridx = 1;
269: c.gridy = 0;
270: c.weightx = 0.9;
271: this .add(firstName, c);
272:
273: c.gridx = 0;
274: c.gridy = 1;
275: c.weightx = 0.1;
276: this .add(lastNameLabel, c);
277:
278: c.gridx = 1;
279: c.gridy = 1;
280: c.weightx = 0.9;
281: this .add(lastNameTextField, c);
282:
283: c.gridx = 0;
284: c.gridy = 2;
285: c.weightx = 0.1;
286: this .add(middleNameLabel, c);
287:
288: c.gridx = 1;
289: c.gridy = 2;
290: c.weightx = 0.9;
291: this .add(middleNameTextField, c);
292:
293: c.gridx = 0;
294: c.gridy = 3;
295: c.weightx = 0.1;
296: this .add(ageLabel, c);
297:
298: c.gridx = 1;
299: c.gridy = 3;
300: c.weightx = 0.9;
301: this .add(age, c);
302:
303: c.gridx = 0;
304: c.gridy = 4;
305: c.weightx = 0.1;
306: this .add(heightLabel, c);
307:
308: c.gridx = 1;
309: c.gridy = 4;
310: c.weightx = 0.9;
311: this .add(height, c);
312:
313: c.gridx = 0;
314: c.gridy = 5;
315: c.weightx = 0.1;
316: this .add(incomeLabel, c);
317:
318: c.gridx = 1;
319: c.gridy = 5;
320: c.weightx = 0.9;
321: this .add(income, c);
322:
323: c.gridx = 0;
324: c.gridy = 6;
325: c.weightx = 0.1;
326: this .add(birthDateLabel, c);
327:
328: c.gridx = 1;
329: c.gridy = 6;
330: c.weightx = 0.9;
331: this .add(birthDate, c);
332:
333: // address add
334:
335: c.gridx = 0;
336: c.gridy = 7;
337: c.weightx = 0.1;
338: this .add(address1Label, c);
339:
340: c.gridx = 1;
341: c.gridy = 7;
342: c.weightx = 0.9;
343: this .add(address1, c);
344:
345: c.gridx = 0;
346: c.gridy = 8;
347: c.weightx = 0.1;
348: this .add(address2Label, c);
349:
350: c.gridx = 1;
351: c.gridy = 8;
352: c.weightx = 0.9;
353: this .add(address2, c);
354:
355: c.gridx = 0;
356: c.gridy = 9;
357: c.weightx = 0.1;
358: this .add(cityLabel, c);
359:
360: c.gridx = 1;
361: c.gridy = 9;
362: c.weightx = 0.9;
363: this .add(city, c);
364:
365: c.gridx = 0;
366: c.gridy = 10;
367: c.weightx = 0.1;
368: this .add(stateLabel, c);
369:
370: c.gridx = 1;
371: c.gridy = 10;
372: c.weightx = 0.9;
373: this .add(stateScrollPane, c);
374:
375: c.gridx = 0;
376: c.gridy = 11;
377: c.weightx = 0.1;
378: this .add(zipLabel, c);
379:
380: c.gridx = 1;
381: c.gridy = 11;
382: c.weightx = 0.9;
383: this .add(zip, c);
384:
385: c.gridx = 0;
386: c.gridy = 12;
387: c.weightx = 0.1;
388: this .add(sexLabel, c);
389:
390: c.gridx = 1;
391: c.gridy = 12;
392: c.weightx = 0.9;
393: this .add(sex, c);
394:
395: c.gridx = 1;
396: c.gridy = 13;
397: this .add(cool, c);
398:
399: c.gridx = 0;
400: c.gridy = 14;
401: c.weightx = 0.1;
402: this .add(favoriteColorLabel, c);
403:
404: c.gridx = 1;
405: c.gridy = 14;
406: c.weightx = 0.9;
407: this .add(favoriteColor, c);
408:
409: c.gridx = 0;
410: c.gridy = 15;
411: c.weightx = 0.1;
412: this .add(primaryVehicleLabel, c);
413:
414: c.gridx = 1;
415: c.gridy = 15;
416: c.weightx = 0.9;
417: this .add(primaryVehicle, c);
418:
419: c.gridx = 0;
420: c.gridy = 16;
421: c.weightx = 0.1;
422: this .add(secondaryVehicleLabel, c);
423:
424: c.gridx = 1;
425: c.gridy = 16;
426: c.weightx = 0.9;
427: this .add(secondaryVehicle, c);
428:
429: c.gridx = 0;
430: c.gridy = 17;
431: c.weightx = 0.1;
432: this .add(favoriteVehicleLabel, c);
433:
434: c.gridx = 1;
435: c.gridy = 17;
436: c.weightx = 0.9;
437: this .add(favoriteVehicle, c);
438:
439: c.gridx = 0;
440: c.gridy = 18;
441: c.weightx = 0.1;
442: this .add(fanciestVehicleLabel, c);
443:
444: c.gridx = 1;
445: c.gridy = 18;
446: c.weightx = 0.9;
447: this .add(fanciestVehicle, c);
448:
449: c.gridx = 0;
450: c.gridy = 19;
451: c.weightx = 0.1;
452: this .add(numberOfChildrenLabel, c);
453:
454: c.gridx = 1;
455: c.gridy = 19;
456: c.weightx = 0.9;
457: this .add(numberOfChildren, c);
458:
459: c.gridx = 0;
460: c.gridy = 20;
461: c.weightx = 0.1;
462: this .add(shoeSizeLabel, c);
463:
464: c.gridx = 1;
465: c.gridy = 20;
466: c.weightx = 0.9;
467: this .add(shoeSize, c);
468:
469: c.gridx = 0;
470: c.gridy = 21;
471: c.weightx = 0.1;
472: this .add(notesLabel, c);
473:
474: c.gridx = 1;
475: c.gridy = 21;
476: c.weightx = 0.9;
477: this .add(notes, c);
478:
479: c.gridx = 1;
480: c.gridy = 22;
481: this .add(buttonPanel, c);
482:
483: binder.populateForm(this );
484: }
485:
486: private void update() {
487: binder.updateModel(this );
488: log.info("\n\n" + person.toString() + "\n\n"
489: + address.toString());
490: }
491:
492: public Person getPerson() {
493: return person;
494: }
495:
496: public void setPerson(Person person) {
497: this.person = person;
498: }
499: }
|