PersonBinderConfigFormPanel.java :  » Swing-Library » Hitch » com » silvermindsoftware » hitch » sample » Java Open Source

Java Open Source » Swing Library » Hitch 
Hitch » com » silvermindsoftware » hitch » sample » PersonBinderConfigFormPanel.java
package com.silvermindsoftware.hitch.sample;

/**
 * Copyright 2007 Brandon Goodin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.silvermindsoftware.hitch.Binder;
import com.silvermindsoftware.hitch.BinderManager;
import com.silvermindsoftware.hitch.HitchUtil;
import com.silvermindsoftware.hitch.sample.handler.FavoriteColorComponentHandler;
import com.silvermindsoftware.hitch.sample.model.Address;
import com.silvermindsoftware.hitch.sample.model.Person;
import com.silvermindsoftware.hitch.sample.model.Sex;
import com.silvermindsoftware.hitch.sample.model.Vehicle;
import com.silvermindsoftware.hitch.sample.dao.SexDao;
import com.silvermindsoftware.hitch.sample.dao.AddressDao;
import com.silvermindsoftware.hitch.sample.dao.PersonDao;
import com.silvermindsoftware.hitch.sample.dao.VehicleDao;
import com.silvermindsoftware.hitch.config.BinderConfig;
import com.silvermindsoftware.hitch.config.BoundComponentConfig;
import com.silvermindsoftware.hitch.config.FormConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PersonBinderConfigFormPanel extends JPanel {

  private static final Log log = LogFactory.getLog(PersonBinderConfigFormPanel.class);

  private final Binder binder = BinderManager.getBinder(this);

  public static final String ADDRESS = "address";
  public static final String PERSON = "person";

  private PersonDao personDao = new PersonDao();
  private AddressDao addressDao = new AddressDao();
  private SexDao sexDao = new SexDao();
  private VehicleDao vehicleDao = new VehicleDao();

  public String[] states = new String[]{
      "Alabama", "Alaska", "Arizona", "Arkansas", "California",
      "Colorado", "Connecticut", "Delaware", "Florida", "Georgia",
      "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
      "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
      "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
      "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
      "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
      "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
      "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
      "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"
  };

  // this is the default model object
  private Person person = personDao.getPerson();

  // Person Fields that bind to the default model object
  private JTextField firstName, lastNameTextField, middleNameTextField, age, height, income;

  private JFormattedTextField birthDate;

  private JComboBox sex, primaryVehicle, secondaryVehicle;

  // Address Model Object
  Address address = addressDao.getAddress();

  // Address Fields that bind to the ADDRESS model object
  private JTextField address1, address2, city, zip;

  private JList state;

  private JCheckBox cool;

  private JTextField favoriteColor;

  private JSpinner numberOfChildren;

  private JSpinner favoriteVehicle;

  private JSpinner fanciestVehicle;

  private JSlider shoeSize;

  private JTextArea notes;

  public PersonBinderConfigFormPanel() {
    super();

    BinderConfig binderConfig = new BinderConfig(this.getClass(), binder, new FormConfig(true));

    // bind model
    // bind
    binderConfig.bindDefaultModel(PERSON);
    binderConfig.bindComponentToDefault(
        new BoundComponentConfig("firstName"),
        new BoundComponentConfig("lastNameTextField", "lastName"),
        new BoundComponentConfig("middleNameTextField", "middleName"),
        new BoundComponentConfig("age"),
        new BoundComponentConfig("height"),
        new BoundComponentConfig("income"),
        new BoundComponentConfig("birthDate"),
        new BoundComponentConfig("cool"),
        new BoundComponentConfig(
            "favoriteColor",
            FavoriteColorComponentHandler.class,
            new String[]{"colorA=RED", "colorB=ORANGE"}),
        new BoundComponentConfig(
            "sex",
            new String[]{"compareProperties=id", "valueProperty=id"}),
        new BoundComponentConfig("primaryVehicle", new String[]{"compareProperties=id,type"}),
        new BoundComponentConfig("secondaryVehicle"),
        new BoundComponentConfig("favoriteVehicle"),
        new BoundComponentConfig("numberOfChildren"),
        new BoundComponentConfig("fanciestVehicle", new String[]{"compareProperties=id", "valueProperty=id"}),
        new BoundComponentConfig("shoeSize"),
        new BoundComponentConfig("notes"));


    binderConfig.bindModel(ADDRESS);
    binderConfig.bindComponent(ADDRESS,
        new BoundComponentConfig("address1"),
        new BoundComponentConfig("address2"),
        new BoundComponentConfig("city"),
        new BoundComponentConfig("state"),
        new BoundComponentConfig("zip"));

    GridBagLayout layout = new GridBagLayout();

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    this.setLayout(layout);

    JLabel firstNameLabel = new JLabel("First:");
    firstNameLabel.setHorizontalAlignment(JLabel.RIGHT);
    firstName = new JTextField();

    JLabel lastNameLabel = new JLabel("Last:");
    lastNameLabel.setHorizontalAlignment(JLabel.RIGHT);
    lastNameTextField = new JTextField();

    JLabel middleNameLabel = new JLabel("Middle:");
    middleNameLabel.setHorizontalAlignment(JLabel.RIGHT);
    middleNameTextField = new JTextField();

    JLabel ageLabel = new JLabel("Age:");
    ageLabel.setHorizontalAlignment(JLabel.RIGHT);
    age = new JTextField();

    JLabel heightLabel = new JLabel("Height:");
    heightLabel.setHorizontalAlignment(JLabel.RIGHT);
    height = new JTextField();

    JLabel incomeLabel = new JLabel("Income:");
    incomeLabel.setHorizontalAlignment(JLabel.RIGHT);
    income = new JTextField();

    JLabel birthDateLabel = new JLabel("Birth Date:");
    birthDateLabel.setHorizontalAlignment(JLabel.RIGHT);
    birthDate = new JFormattedTextField();

    JLabel address1Label = new JLabel("Address1:");
    address1Label.setHorizontalAlignment(JLabel.RIGHT);
    address1 = new JTextField();

    JLabel address2Label = new JLabel("Address2:");
    address2Label.setHorizontalAlignment(JLabel.RIGHT);
    address2 = new JTextField();

    JLabel cityLabel = new JLabel("City:");
    cityLabel.setHorizontalAlignment(JLabel.RIGHT);
    city = new JTextField();

    JLabel stateLabel = new JLabel("State:");
    stateLabel.setHorizontalAlignment(JLabel.RIGHT);
    state = new JList(states);
    state.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane stateScrollPane = new JScrollPane(state);
    stateScrollPane.setPreferredSize(new Dimension(100, 50));

    JLabel zipLabel = new JLabel("Zip:");
    zipLabel.setHorizontalAlignment(JLabel.RIGHT);
    zip = new JTextField();

    JLabel sexLabel = new JLabel("Sex:");
    sexLabel.setHorizontalAlignment(JLabel.RIGHT);
    sex = new JComboBox(sexDao.getList().toArray(new Sex[]{}));

    cool = new JCheckBox("Cool");

    JLabel favoriteColorLabel = new JLabel("Favorite Color:");
    favoriteColorLabel.setHorizontalAlignment(JLabel.RIGHT);
    favoriteColor = new JTextField();

    JLabel primaryVehicleLabel = new JLabel("Primary Vehicle:");
    primaryVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
    primaryVehicle = new JComboBox(vehicleDao.getList().toArray(new Vehicle[]{}));

    JLabel secondaryVehicleLabel = new JLabel("Secondary Vehicle:");
    secondaryVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
    secondaryVehicle = new JComboBox(vehicleDao.getList().toArray(new Vehicle[]{}));

    JLabel favoriteVehicleLabel = new JLabel("Favorite Vehicle:");
    favoriteVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
    favoriteVehicle = HitchUtil.getEnhancedJSpinner(vehicleDao.getList());

    JLabel fanciestVehicleLabel = new JLabel("Favorite Vehicle:");
    fanciestVehicleLabel.setHorizontalAlignment(JLabel.RIGHT);
    fanciestVehicle = HitchUtil.getEnhancedJSpinner(vehicleDao.getList());

    JLabel numberOfChildrenLabel = new JLabel("Number of Children:");
    numberOfChildrenLabel.setHorizontalAlignment(JLabel.RIGHT);
    numberOfChildren = new JSpinner(new SpinnerNumberModel(0, 0, 50, 1));

    JLabel shoeSizeLabel = new JLabel("Number of Children:");
    shoeSizeLabel.setHorizontalAlignment(JLabel.RIGHT);
    shoeSize = new JSlider(1, 17);

    JLabel notesLabel = new JLabel("Number of Children:");
    notesLabel.setHorizontalAlignment(JLabel.RIGHT);
    notes = new JTextArea();

    JButton submit = new JButton("Save");
    submit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        update();
      }
    });

    JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(submit);

    // person adds
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.1;
    this.add(firstNameLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 0.9;
    this.add(firstName, c);

    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 0.1;
    this.add(lastNameLabel, c);

    c.gridx = 1;
    c.gridy = 1;
    c.weightx = 0.9;
    this.add(lastNameTextField, c);

    c.gridx = 0;
    c.gridy = 2;
    c.weightx = 0.1;
    this.add(middleNameLabel, c);

    c.gridx = 1;
    c.gridy = 2;
    c.weightx = 0.9;
    this.add(middleNameTextField, c);

    c.gridx = 0;
    c.gridy = 3;
    c.weightx = 0.1;
    this.add(ageLabel, c);

    c.gridx = 1;
    c.gridy = 3;
    c.weightx = 0.9;
    this.add(age, c);

    c.gridx = 0;
    c.gridy = 4;
    c.weightx = 0.1;
    this.add(heightLabel, c);

    c.gridx = 1;
    c.gridy = 4;
    c.weightx = 0.9;
    this.add(height, c);

    c.gridx = 0;
    c.gridy = 5;
    c.weightx = 0.1;
    this.add(incomeLabel, c);

    c.gridx = 1;
    c.gridy = 5;
    c.weightx = 0.9;
    this.add(income, c);

    c.gridx = 0;
    c.gridy = 6;
    c.weightx = 0.1;
    this.add(birthDateLabel, c);

    c.gridx = 1;
    c.gridy = 6;
    c.weightx = 0.9;
    this.add(birthDate, c);

    // address add

    c.gridx = 0;
    c.gridy = 7;
    c.weightx = 0.1;
    this.add(address1Label, c);

    c.gridx = 1;
    c.gridy = 7;
    c.weightx = 0.9;
    this.add(address1, c);


    c.gridx = 0;
    c.gridy = 8;
    c.weightx = 0.1;
    this.add(address2Label, c);

    c.gridx = 1;
    c.gridy = 8;
    c.weightx = 0.9;
    this.add(address2, c);


    c.gridx = 0;
    c.gridy = 9;
    c.weightx = 0.1;
    this.add(cityLabel, c);

    c.gridx = 1;
    c.gridy = 9;
    c.weightx = 0.9;
    this.add(city, c);

    c.gridx = 0;
    c.gridy = 10;
    c.weightx = 0.1;
    this.add(stateLabel, c);

    c.gridx = 1;
    c.gridy = 10;
    c.weightx = 0.9;
    this.add(stateScrollPane, c);

    c.gridx = 0;
    c.gridy = 11;
    c.weightx = 0.1;
    this.add(zipLabel, c);

    c.gridx = 1;
    c.gridy = 11;
    c.weightx = 0.9;
    this.add(zip, c);

    c.gridx = 0;
    c.gridy = 12;
    c.weightx = 0.1;
    this.add(sexLabel, c);

    c.gridx = 1;
    c.gridy = 12;
    c.weightx = 0.9;
    this.add(sex, c);

    c.gridx = 1;
    c.gridy = 13;
    this.add(cool, c);

    c.gridx = 0;
    c.gridy = 14;
    c.weightx = 0.1;
    this.add(favoriteColorLabel, c);

    c.gridx = 1;
    c.gridy = 14;
    c.weightx = 0.9;
    this.add(favoriteColor, c);

    c.gridx = 0;
    c.gridy = 15;
    c.weightx = 0.1;
    this.add(primaryVehicleLabel, c);

    c.gridx = 1;
    c.gridy = 15;
    c.weightx = 0.9;
    this.add(primaryVehicle, c);

    c.gridx = 0;
    c.gridy = 16;
    c.weightx = 0.1;
    this.add(secondaryVehicleLabel, c);

    c.gridx = 1;
    c.gridy = 16;
    c.weightx = 0.9;
    this.add(secondaryVehicle, c);

    c.gridx = 0;
    c.gridy = 17;
    c.weightx = 0.1;
    this.add(favoriteVehicleLabel, c);

    c.gridx = 1;
    c.gridy = 17;
    c.weightx = 0.9;
    this.add(favoriteVehicle, c);

    c.gridx = 0;
    c.gridy = 18;
    c.weightx = 0.1;
    this.add(fanciestVehicleLabel, c);

    c.gridx = 1;
    c.gridy = 18;
    c.weightx = 0.9;
    this.add(fanciestVehicle, c);

    c.gridx = 0;
    c.gridy = 19;
    c.weightx = 0.1;
    this.add(numberOfChildrenLabel, c);

    c.gridx = 1;
    c.gridy = 19;
    c.weightx = 0.9;
    this.add(numberOfChildren, c);

    c.gridx = 0;
    c.gridy = 20;
    c.weightx = 0.1;
    this.add(shoeSizeLabel, c);

    c.gridx = 1;
    c.gridy = 20;
    c.weightx = 0.9;
    this.add(shoeSize, c);

    c.gridx = 0;
    c.gridy = 21;
    c.weightx = 0.1;
    this.add(notesLabel, c);

    c.gridx = 1;
    c.gridy = 21;
    c.weightx = 0.9;
    this.add(notes, c);

    c.gridx = 1;
    c.gridy = 22;
    this.add(buttonPanel, c);

    binder.populateForm(this);
  }

  private void update() {
    binder.updateModel(this);
    log.info("\n\n" + person.toString() + "\n\n" + address.toString());
  }

  public Person getPerson() {
    return person;
  }

  public void setPerson(Person person) {
    this.person = person;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.