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

1    package gui.dialogs; 
2     
3    import javax.swing.*; 
4     
5    /** 
6     * This class demonstrates the usage of the StandardDialog class. 
7     */ 
8    public class CustomerDialog extends ModalJDialog { 
9        // Constants 
10    
11       private static final String[] COUNTRY_LIST = 
12               { 
13                   "Australia", "England", "Japan", "USA" 
14               }; 
15    
16       // Attributes 
17    
18       private JTextField myCustomerCodeField = new JTextField(); 
19       private JTextField myNameField = new JTextField(); 
20       private JTextArea myAddressField = new JTextArea(3, 20); 
21       private JTextField myCityField = new JTextField(); 
22       private JTextField myStateField = new JTextField(); 
23       private JTextField myPostcodeField = new JTextField(); 
24       private JComboBox myCountryField = new JComboBox(COUNTRY_LIST); 
25       private JTextField myContactField = new JTextField(); 
26       private JTextField myPhoneField = new JTextField(); 
27       private JTextField myFaxField = new JTextField(); 
28       private JTextField myEmailField = new JTextField(); 
29    
30       private LabelledItemPanel myContentPane = new LabelledItemPanel(); 
31    
32       // Methods 
33    
34       /** 
35        * This method is the default constructor. 
36        */ 
37       public CustomerDialog() { 
38           init(); 
39       } 
40    
41       /** 
42        * This method initialises the components on the panel. 
43        */ 
44       private void init() { 
45           setTitle("Customer Dialog (StandardDialog)"); 
46    
47           myContentPane.setBorder(BorderFactory.createEtchedBorder()); 
48    
49           myContentPane.addItem("Customer Code", myCustomerCodeField); 
50           myContentPane.addItem("Name", myNameField); 
51           myContentPane.addItem("Address", new JScrollPane(myAddressField, 
52                   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
53                   JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); 
54           myContentPane.addItem("City", myCityField); 
55           myContentPane.addItem("State", myStateField); 
56           myContentPane.addItem("Postcode", myPostcodeField); 
57           myContentPane.addItem("Country", myCountryField); 
58           myContentPane.addItem("Contact", myContactField); 
59           myContentPane.addItem("Phone", myPhoneField); 
60           myContentPane.addItem("Fax", myFaxField); 
61           myContentPane.addItem("Email", myEmailField); 
62    
63           setContentPane(myContentPane); 
64       } 
65    
66       /** 
67        * This method gets the values of the panel entry fields. 
68        * 
69        * @return an object containing the Customer data 
70        */ 
71       public CustomerData getCustomerData() { 
72           CustomerData customerData = new CustomerData(); 
73    
74           customerData.setCustomerCode(myCustomerCodeField.getText()); 
75           customerData.setName(myNameField.getText()); 
76           customerData.setAddress(myAddressField.getText()); 
77           customerData.setCity(myCityField.getText()); 
78           customerData.setState(myStateField.getText()); 
79           customerData.setZip(myPostcodeField.getText()); 
80           customerData.setCountry(myCountryField.getSelectedItem().toString()); 
81           customerData.setContact(myContactField.getText()); 
82           customerData.setPhone1(myPhoneField.getText()); 
83           customerData.setFax(myFaxField.getText()); 
84           customerData.setEmail(myEmailField.getText()); 
85    
86           return customerData; 
87       } 
88    
89       /** 
90        * This method sets the values of the panel entry fields. 
91        * 
92        * @param customerData The object containing the Customer data 
93        */ 
94       public void setCustomerData(CustomerData customerData) { 
95           myCustomerCodeField.setText(customerData.getCustomerCode()); 
96           myNameField.setText(customerData.getName()); 
97           myAddressField.setText(customerData.getAddress()); 
98           myCityField.setText(customerData.getCity()); 
99           myStateField.setText(customerData.getState()); 
100          myPostcodeField.setText(customerData.getZip()); 
101          myCountryField.setSelectedItem(customerData.getCountry()); 
102          myContactField.setText(customerData.getContact()); 
103          myPhoneField.setText(customerData.getPhone1()); 
104          myFaxField.setText(customerData.getFax()); 
105          myEmailField.setText(customerData.getEmail()); 
106      } 
107   
108      /** 
109       * This method checks that the data entered is valid. 
110       * To be valid, the following must be met: 
111       * <LI>Customer Code field is not blank 
112       * <LI>Name field is not blank 
113       * 
114       * @return <code>true</code> if the data is valid, otherwise 
115       * <code>false</code> 
116       */ 
117      protected boolean isValidData() { 
118          if (myCustomerCodeField.getText().equals("")) { 
119              JOptionPane.showMessageDialog(this, 
120                      "Please enter a Customer Code", 
121                      "Blank Customer Code", 
122                      JOptionPane.WARNING_MESSAGE); 
123   
124              myCustomerCodeField.requestFocus(); 
125   
126              return false; 
127          } 
128   
129          if (myNameField.getText().equals("")) { 
130              JOptionPane.showMessageDialog(this, 
131                      "Please enter a Name", 
132                      "Blank Name", 
133                      JOptionPane.WARNING_MESSAGE); 
134   
135              myNameField.requestFocus(); 
136   
137              return false; 
138          } 
139   
140          return true; 
141      } 
142   
143      /** 
144       * This method demonstrates the usage of the CustomerDialog class. 
145       */ 
146      public static void main(String[] args) { 
147          CustomerDialog dialog = new CustomerDialog(); 
148   
149          dialog.pack(); 
150   
151          // Present it to a User 
152   
153          dialog.show(); 
154   
155          // Get the data and display it 
156   
157          if (!dialog.hasUserCancelled()) { 
158              CustomerData customerData = dialog.getCustomerData(); 
159   
160              customerData.printData(); 
161          } 
162   
163          System.exit(0); 
164      } 
165  }