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