package test.swing; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JSpinner; import javax.swing.JTextField; import com.planetalia.dynject.injection.Shell; import com.planetalia.dynject.metamodel.Injectable; import com.planetalia.dynject.sources.SwingSource; import com.planetalia.dynject.validation.ErrorMap; public class InputForm extends JFrame implements ActionListener { private JLabel label1; private JLabel label2; private JLabel label3; private JTextField customer; private JTextField birthdate; private JSpinner discount; private JButton ok; public InputForm() { setBounds(0,0,340,330); setTitle("Sample Swing Form"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(null); label1 = new JLabel("Customer Name:"); label1.setBounds(20, 50,100,20); getContentPane().add(label1); label2 = new JLabel("Birth date :"); label2.setBounds(20, 100,100,20); getContentPane().add(label2); label3 = new JLabel("Discount rate :"); label3.setBounds(20, 150,100,20); getContentPane().add(label3); customer = new JTextField(); customer.setBounds(120,50,100,20); getContentPane().add(customer); birthdate = new JTextField(); birthdate.setBounds(120,100,100,20); getContentPane().add(birthdate); discount = new JSpinner(); discount.setBounds(120,150,100,20); getContentPane().add(discount); ok = new JButton("Ok"); ok.setBounds(220,200,50,20); getContentPane().add(ok); ok.addActionListener(this); } public void actionPerformed(ActionEvent e) { Customer c = new Customer(); ErrorMap errorMap= Shell.assign(c, new SwingSource(this)); String errors=""; for(String field : errorMap.keySet()) { // Append the first error message for each field. errors+=field +" field : " + errorMap.get(field).get(0).getMessage()+"\n"; } JOptionPane.showMessageDialog(null, "These are the errors:\n"+ errors+"\n\n"+ "And the current values of the model fields:\n"+ "Customer =["+c.customer+"]\n"+ ((c.birthdate!=null)?("BirthDate =["+c.birthdate.getTime()+"]\n"):"BirthDate =null\n")+ "Disctount =["+c.discount+"]"); } public static void main(String[] args) { InputForm frm = new InputForm(); frm.setVisible(true); } }