/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.jgoodies.binding.PresentationModel;
import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.Model;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class PresentationBeanChannelExample extends JPanel {
private PersonBean personBean1;
private PersonBean personBean2;
private PresentationModel presentationModel;
public PresentationBeanChannelExample() {
DefaultFormBuilder defaultFormBuilder = new DefaultFormBuilder(new FormLayout("p, 2dlu, p:g"));
defaultFormBuilder.setDefaultDialogBorder();
this.personBean1 = new PersonBean("Scott", "Delap");
this.personBean2 = new PersonBean("Matt", "Raible");
this.presentationModel = new PresentationModel(this.personBean1);
ValueModel firstNameAdapter = presentationModel.getModel("firstName");
ValueModel lastNameAdapter = presentationModel.getModel("lastName");
JTextField firstNameTextField = BasicComponentFactory.createTextField(firstNameAdapter);
JTextField lastNameTextField = BasicComponentFactory.createTextField(lastNameAdapter);
defaultFormBuilder.append("First Name: ", firstNameTextField);
defaultFormBuilder.append("Last Name: ", lastNameTextField);
defaultFormBuilder.append(new JButton(new ChangeBeanAction()), 3);
defaultFormBuilder.append(new JButton(new ConvertValueToUpperCaseAction()), 3);
defaultFormBuilder.append(new JButton(new ConvertValueToLowerCaseAction()), 3);
defaultFormBuilder.append(new JButton(new ShowValueHolderValueAction()), 3);
add(defaultFormBuilder.getPanel());
}
private class ChangeBeanAction extends AbstractAction {
public ChangeBeanAction() {
super("Change PersonBean");
}
public void actionPerformed(ActionEvent event) {
if (presentationModel.getBean() == personBean1) {
presentationModel.setBean(personBean2);
} else {
presentationModel.setBean(personBean1);
}
}
}
private class ConvertValueToUpperCaseAction extends AbstractAction {
public ConvertValueToUpperCaseAction() {
super("Convert Value To UpperCase");
}
public void actionPerformed(ActionEvent event) {
PersonBean personBean = (PersonBean) presentationModel.getBean();
personBean.setFirstName(personBean.getFirstName().toUpperCase());
personBean.setLastName(personBean.getLastName().toUpperCase());
}
}
private class ConvertValueToLowerCaseAction extends AbstractAction {
public ConvertValueToLowerCaseAction() {
super("Convert Value To LowerCase");
}
public void actionPerformed(ActionEvent event) {
PersonBean personBean = (PersonBean) presentationModel.getBean();
personBean.setFirstName(personBean.getFirstName().toLowerCase());
personBean.setLastName(personBean.getLastName().toLowerCase());
}
}
private class ShowValueHolderValueAction extends AbstractAction {
public ShowValueHolderValueAction() {
super("Show Value");
}
public void actionPerformed(ActionEvent event) {
PersonBean personBean = (PersonBean) presentationModel.getBean();
StringBuffer message = new StringBuffer();
message.append("<html>");
message.append("<b>First Name:</b> ");
message.append(personBean.getFirstName());
message.append("<br><b>Last Name:</b> ");
message.append(personBean.getLastName());
message.append("</html>");
JOptionPane.showMessageDialog(null, message.toString());
}
}
public class PersonBean extends Model {
private String firstName;
private String lastName;
public static final String FIRST_NAME_PROPERTY = "firstName";
public static final String LAST_NAME_PROPERTY = "lastName";
public PersonBean(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
String oldValue = this.firstName;
this.firstName = firstName;
firePropertyChange(FIRST_NAME_PROPERTY, oldValue, this.firstName);
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
String oldValue = this.lastName;
this.lastName = lastName;
firePropertyChange(LAST_NAME_PROPERTY, oldValue, this.lastName);
}
}
public static void main(String[] a){
JFrame f = new JFrame("Presentation Bean Channel Example");
f.setDefaultCloseOperation(2);
f.add(new PresentationBeanChannelExample());
f.pack();
f.setVisible(true);
}
}
|