/Users/lyon/j4p/src/gui/dialogs/AddressDialog.java

1    package gui.dialogs; 
2     
3    import classUtils.reflection.MethodList; 
4    import gui.In; 
5     
6    import javax.swing.*; 
7    import java.lang.reflect.Method; 
8     
9    /** 
10    * This class demonstrates the usage of the StandardDialog class. 
11    */ 
12   public class AddressDialog extends ModalJDialog { 
13    
14       Method writeMethods[] = null; 
15       MethodList ml = null; 
16    
17       private JTextField tf[] = null; 
18    
19       private LabelledItemPanel myContentPane = new LabelledItemPanel(); 
20    
21       public AddressDialog(Class c) { 
22           ml = new MethodList(c); 
23           writeMethods = ml.getWriteMethods(); 
24           init(); 
25       } 
26    
27       private void init() { 
28           setTitle("Customer Dialog (StandardDialog)"); 
29           tf = new JTextField[writeMethods.length]; 
30           String propNames[] = ml.getPropertyNames(); 
31           for (int i = 0; i < propNames.length; i++) { 
32    
33               tf[i] = new JTextField(20); 
34               myContentPane.addItem(propNames[i], tf[i]); 
35               if (i > 5) break; 
36    
37           } 
38           myContentPane.setBorder(BorderFactory.createEtchedBorder()); 
39    
40           setContentPane(myContentPane); 
41       } 
42    
43       /** 
44        * This method gets the values of the panel entry fields. 
45        * 
46        * @return an object containing the Customer data 
47        */ 
48       public CustomerData getData() { 
49           CustomerData customerData = new CustomerData(); 
50           customerData.setCustomerCode(tf[1].getText()); 
51           return customerData; 
52       } 
53    
54       /** 
55        * This method sets the values of the panel entry fields. 
56        * 
57        * @param customerData The object containing the Customer data 
58        */ 
59       public void setCustomerData(CustomerData customerData) { 
60           tf[1].setText(customerData.getCustomerCode()); 
61    
62       } 
63    
64       /** 
65        * This method checks that the data entered is valid. 
66        * To be valid, the following must be met: 
67        * <LI>Customer Code field is not blank 
68        * <LI>Name field is not blank 
69        * 
70        * @return <code>true</code> if the data is valid, otherwise 
71        *         <code>false</code> 
72        */ 
73       protected boolean isValidData() { 
74           if (tf[1].getText().equals("")) { 
75               In.message("Please enter an address"); 
76    
77               tf[1].requestFocus(); 
78    
79               return false; 
80           } 
81    
82    
83           return true; 
84       } 
85    
86       public static void main(String[] args) { 
87           AddressDialog dialog = new AddressDialog(StudentData.class); 
88           dialog.pack(); 
89           dialog.show(); 
90           CustomerData customerData = dialog.getData(); 
91    
92           customerData.printData(); 
93    
94           System.exit(0); 
95       } 
96   }