/Users/lyon/j4p/src/gui/dialogs/CustomerPanel.java
|
1 package gui.dialogs;
2
3 import gui.run.RunCheckBox;
4
5 import javax.swing.*;
6 import java.awt.*;
7
8
9 /**
10 * This class implements a LabelledItemPanel for entering Customer data.
11 */
12 public class CustomerPanel extends LabelledItemPanel {
13 // Constants
14
15 private static final String[] COUNTRY_LIST =
16 {
17 "Australia", "England", "Japan", "USA"
18 };
19
20 // Attributes
21
22 private JTextField myCustomerCodeField = new JTextField();
23 private JTextField myNameField = new JTextField();
24 private JTextArea myAddressField = new JTextArea(3, 20);
25 private JTextField myCityField = new JTextField();
26 private JTextField myStateField = new JTextField();
27 private JTextField myPostcodeField = new JTextField();
28 private JComboBox myCountryField = new JComboBox(COUNTRY_LIST);
29 private JTextField myContactField = new JTextField();
30 private JTextField myPhoneField = new JTextField();
31 private JTextField myFaxField = new JTextField();
32 private JTextField myEmailField = new JTextField();
33
34 // Methods
35
36 /**
37 * This method is the default constructor.
38 */
39 public CustomerPanel() {
40 init();
41 }
42
43 /**
44 * This method initialises the components on the panel.
45 */
46 private void init() {
47 setBorder(BorderFactory.createEtchedBorder());
48
49 addItem("Customer Code", myCustomerCodeField);
50 addItem("Name", myNameField);
51 addItem("Address", new JScrollPane(myAddressField,
52 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
53 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
54 addItem("City", myCityField);
55 addItem("State", myStateField);
56 addItem("Postcode", myPostcodeField);
57 addItem("Country", myCountryField);
58 addItem("Contact", myContactField);
59 addItem("Phone", myPhoneField);
60 addItem("Fax", myFaxField);
61 addItem("Email", myEmailField);
62 addItem("checkMe",new RunCheckBox(){
63 public void run(){
64 System.out.println("checkstate="+this.isSelected());
65 }
66 });
67 }
68
69 /**
70 * This method gets the values of the panel entry fields.
71 *
72 * @return an object containing the Customer data
73 */
74 public CustomerData getCustomerData() {
75 CustomerData customerData = new CustomerData();
76
77 customerData.setCustomerCode(myCustomerCodeField.getText());
78 customerData.setName(myNameField.getText());
79 customerData.setAddress(myAddressField.getText());
80 customerData.setCity(myCityField.getText());
81 customerData.setState(myStateField.getText());
82 customerData.setZip(myPostcodeField.getText());
83 customerData.setCountry(myCountryField.getSelectedItem().toString());
84 customerData.setContact(myContactField.getText());
85 customerData.setPhone1(myPhoneField.getText());
86 customerData.setFax(myFaxField.getText());
87 customerData.setEmail(myEmailField.getText());
88
89 return customerData;
90 }
91
92 /**
93 * This method sets the values of the panel entry fields.
94 *
95 * @param customerData The object containing the Customer data
96 */
97 public void setCustomerData(CustomerData customerData) {
98 myCustomerCodeField.setText(customerData.getCustomerCode());
99 myNameField.setText(customerData.getName());
100 myAddressField.setText(customerData.getAddress());
101 myCityField.setText(customerData.getCity());
102 myStateField.setText(customerData.getState());
103 myPostcodeField.setText(customerData.getZip());
104 myCountryField.setSelectedItem(customerData.getCountry());
105 myContactField.setText(customerData.getContact());
106 myPhoneField.setText(customerData.getPhone1());
107 myFaxField.setText(customerData.getFax());
108 myEmailField.setText(customerData.getEmail());
109 }
110
111 /**
112 * This method demonstrates the usage of the CustomerPanel class.
113 */
114 public static void main(String[] args) {
115 // Create an instance of the panel
116
117 CustomerPanel panel = new CustomerPanel();
118
119 // Create a dialog to hold the panel
120
121 ModalJDialog dialog = new ModalJDialog((Frame) null,
122 "Customer Details (LabelledItemPanel)");
123
124 dialog.setContentPane(panel);
125
126 dialog.pack();
127
128 // Present it to a User
129
130 dialog.show();
131
132 // Get the data and display it
133
134 if (!dialog.hasUserCancelled()) {
135 CustomerData customerData = panel.getCustomerData();
136
137 customerData.printData();
138 }
139
140 System.exit(0);
141 }
142 }