CustomerBean.java
Structure
package samples.s01 CustomerBean.java PresentationModelBuild.java CustomerPresentationModel.java CustomBeanlaunch.java
---------------------------------------------------------------- CustomerBean.java -----------------------------------------------------------------
package samples.s01;
* * Copyright (c) 2008 willems piet pwProTech All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * */
import com.jgoodies.binding.beans.Model;
public class CustomerBean extends Model { public static final String NAME_PROPERTY = "name"; public static final String OCCUPATION_PROPERTY = "occupation"; private String name; private String occupation;
public String getName() { return name; }
public void setName(String name) { String oldValue = name; this.name = name; firePropertyChange(NAME_PROPERTY, oldValue, name); }
public String getOccupation() { return occupation; }
public void setOccupation(String occupation) { String oldValue = occupation; this.occupation = occupation; firePropertyChange(OCCUPATION_PROPERTY, oldValue, occupation); }
}
PresentationModelBuild.java
package samples.s01;
* * Copyright (c) 2008 willems piet pwProTech All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * */
import java.awt.Color; import java.awt.Dimension; import java.awt.TextField;
import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;
import se.datadosen.component.RiverLayout;
import com.jgoodies.binding.adapter.BasicComponentFactory;
public class PresentationModelBuild extends JPanel {
static JTextField nameInput,occupationInput; static JButton commitButton; static JButton resetButton; JButton sendButton; static CustomerPresentationModel presentationModel;
public void createComponents() {
presentationModel = new CustomerPresentationModel( new CustomerBean());
nameInput = BasicComponentFactory.createTextField(presentationModel .getBufferedModel(CustomerBean.NAME_PROPERTY)); occupationInput = BasicComponentFactory .createTextField(presentationModel .getBufferedModel(CustomerBean.OCCUPATION_PROPERTY));
commitButton = new JButton(presentationModel.getCommitAction()); resetButton = new JButton(presentationModel.getResetAction()); sendButton = new JButton(presentationModel.getSendAction()); }
public void constructGui() { JFrame frame = new JFrame(); JPanel CustomBeanFrm = new JPanel(); frame.getContentPane().setLayout(new RiverLayout()); frame.getContentPane().add(CustomBeanFrm);
CustomBeanFrmGui(CustomBeanFrm); commitButton.setEnabled(false); commitButton.setEnabled(false); frame.pack(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); }
private void CustomBeanFrmGui(JPanel CustomBeanFrm) { createComponents(); CustomBeanFrm.setLayout(new RiverLayout()); CustomBeanFrm.add("br left", new JLabel("PresentationModel")); CustomBeanFrm.add("br hfill", separator(Color.RED));
CustomBeanFrm.add("br", new JLabel("Name")); CustomBeanFrm.add("tab hfill", nameInput);
CustomBeanFrm.add("br", new JLabel("Occupation")); CustomBeanFrm.add("tab hfill", occupationInput); CustomBeanFrm.add("br hfill", separator(Color.GRAY));
CustomBeanFrm.add("p br left", resetButton); // The button ins't really located to the left side CustomBeanFrm.add("center", sendButton); CustomBeanFrm.add("right", commitButton); CustomBeanFrm.add("br hfill", separator(Color.GRAY));
}
public static void clearForm() { commitButton.setEnabled(false); nameInput.setText(""); occupationInput.setText("");
}
private static TextField separator(Color c) { TextField separator = new TextField(); separator.setBackground(c); separator.setPreferredSize(new Dimension(20, 2)); return separator; } }
CustomerPresentationModel.java
package samples.s01;
* * Copyright (c) 2008 willems piet pwProTech All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * */
import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import com.jgoodies.binding.PresentationModel;
public class CustomerPresentationModel extends PresentationModel {
private static final long serialVersionUID = 1L; CommitAction commitAction; ResetAction resetAction; SendAction sendAction; ClearAction clearAction; private final CustomerBean customerBean;
@SuppressWarnings("unchecked") public CustomerPresentationModel(CustomerBean customerBean) { super(customerBean); this.customerBean = customerBean; commitAction = new CommitAction(); resetAction = new ResetAction(); sendAction = new SendAction(); clearAction = new ClearAction();
}
public Action getCommitAction() { return commitAction; }
public Action getSendAction() { return sendAction; }
public Action getResetAction() { return resetAction; }
public Action getClearAction() { return clearAction; } private class CommitAction extends AbstractAction { /** * */ private static final long serialVersionUID = 1L;
public CommitAction() { super("Commit"); }
public void actionPerformed(ActionEvent e) {
triggerCommit(); //getBean(); displayBufferAndBean("COMMIT action:"); PresentationModelBuild.clearForm();// disable commitbutton
} }
private class SendAction extends AbstractAction {
public SendAction() { super("Send"); }
public void actionPerformed(final ActionEvent e) {
// getBean(); if (PresentationModelBuild.resetButton.isEnabled()) { PresentationModelBuild.resetButton.setEnabled(true); }
if (isBuffering()) { PresentationModelBuild.commitButton.setEnabled(true);
displayBufferAndBean("SEND Bufferd :true :: Not yet committed"); } else { PresentationModelBuild.commitButton.setEnabled(false); displayBufferAndBean("SEND Bufferd :true :: yet Committed"); //Bufferd still true so by a reset the bufferd values are still available }
}
}
private class ResetAction extends AbstractAction {
public ResetAction() { super("Reset"); }
public void actionPerformed(ActionEvent e) {
// release(): // Removes the PropertyChangeHandler from the observed bean, if the // bean is not null. Also removes all listeners from the bean that // have been registered with #addBeanPropertyChangeListener before. if (isBuffering()) { triggerFlush(); resetChanged(); System.out.println("RESET action : If changes : Previous restored"); PresentationModelBuild.commitButton.setEnabled(false); } } }
private class ClearAction extends AbstractAction { // Not Used
public ClearAction() { super("Clear Form"); }
public void actionPerformed(ActionEvent e) {
PresentationModelBuild.resetButton.setEnabled(false); PresentationModelBuild.clearForm(); System.out.println("clearform"); } }
private void displayBufferAndBean(String buffering) { System.out.println("" + "Result " + buffering + "n" + "-------------------------------------------------n" + "Bufferd values :" + "n" + "Buffered beanvalue name :" + getBufferedValue("name") + "n" + "Buffered beanvalue occupation :"+ getBufferedValue("occupation") + "n" + "" + "n" + "Bean values :" + "n" + "CustomerBean value name :" + customerBean.getName() + "n" + "CustomerBean value occupation :" + customerBean.getOccupation() + "n" + "---------------------------------------------------"); } }
CustomBeanlaunch.java --------------------------------------------------------------------------------------------
package samples.s01;
* * Copyright (c) 2008 willems piet pwProTech All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * */
public class CustomBeanlaunch {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PresentationModelBuild pme = new PresentationModelBuild(); pme.constructGui(); } }
RESULTAAT
Result SEND Bufferd :true :: Not yet committed ------------------------------------------------- Bufferd values : Buffered beanvalue name :piet Buffered beanvalue occupation :job
Bean values : CustomerBean value name :null CustomerBean value occupation :null --------------------------------------------------- RESET action : If changes : Previous restored Result SEND Bufferd :true :: Not yet committed ------------------------------------------------- Bufferd values : Buffered beanvalue name :piet Buffered beanvalue occupation :job
Bean values : CustomerBean value name :null CustomerBean value occupation :null --------------------------------------------------- Result COMMIT action: ------------------------------------------------- Bufferd values : Buffered beanvalue name :piet Buffered beanvalue occupation :job
Bean values : CustomerBean value name :piet CustomerBean value occupation :job ---------------------------------------------------
Tags :: RiverLayout , JGoodies , Valueholder , bindings, formmodel , model , sample, samples, java
|