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

1    package gui.dialogs; 
2     
3    import javax.swing.*; 
4    import java.awt.*; 
5     
6    public class JBuilderPanel extends JPanel { 
7        // Constants 
8     
9        private static final String[] COUNTRY_LIST = 
10               { 
11                   "Australia", "England", "Japan", "USA" 
12               }; 
13    
14       // Attributes 
15    
16       GridBagLayout gridBagLayout1 = new GridBagLayout(); 
17       private JLabel myCustomerCodeLabel = new JLabel(); 
18       private JTextField myCustomerCodeField = new JTextField(); 
19       private JLabel myNameLabel = new JLabel(); 
20       private JTextField myNameField = new JTextField(); 
21       private JLabel myAddressLabel = new JLabel(); 
22       private JScrollPane myAddressScrollpane = new JScrollPane(); 
23       private JTextArea myAddressField = new JTextArea(); 
24       private JLabel myCityLabel = new JLabel(); 
25       private JTextField myCityField = new JTextField(); 
26       private JLabel myStateLabel = new JLabel(); 
27       private JTextField myStateField = new JTextField(); 
28       private JLabel myPostcodeLabel = new JLabel(); 
29       private JTextField myPostcodeField = new JTextField(); 
30       private JLabel myCountryLabel = new JLabel(); 
31       private JComboBox myCountryField = new JComboBox(COUNTRY_LIST); 
32       private JLabel myContactLabel = new JLabel(); 
33       private JTextField myContactField = new JTextField(); 
34       private JLabel myPhoneLabel = new JLabel(); 
35       private JTextField myPhoneField = new JTextField(); 
36       private JLabel myFaxLabel = new JLabel(); 
37       private JTextField myFaxField = new JTextField(); 
38       private JLabel myEmailLabel = new JLabel(); 
39       private JTextField myEmailField = new JTextField(); 
40       private JLabel myFillLabel = new JLabel(); 
41    
42       // Methods 
43    
44       /** 
45        * This method is the default constructor. 
46        */ 
47       public JBuilderPanel() { 
48           try { 
49               jbInit(); 
50           } catch (Exception e) { 
51               e.printStackTrace(); 
52           } 
53       } 
54    
55       /** 
56        * This method initialises the components on the panel. 
57        */ 
58       private void jbInit() throws Exception { 
59           this.setLayout(gridBagLayout1); 
60           this.setBorder(BorderFactory.createEtchedBorder()); 
61           myCustomerCodeLabel.setText("Customer Code"); 
62           myNameLabel.setText("Name"); 
63           myAddressLabel.setText("Address"); 
64           myAddressField.setRows(3); 
65           myAddressField.setColumns(20); 
66           myAddressScrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
67           myAddressScrollpane.getViewport().add(myAddressField, null); 
68           myCityLabel.setText("City"); 
69           myStateLabel.setText("State"); 
70           myPostcodeLabel.setText("Postcode"); 
71           myCountryLabel.setText("Country"); 
72           myContactLabel.setText("Contact"); 
73           myPhoneLabel.setText("Phone"); 
74           myFaxLabel.setText("Fax"); 
75           myEmailLabel.setText("Email"); 
76           this.add(myCustomerCodeLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 
77                   , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
78           this.add(myCustomerCodeField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0 
79                   , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
80           this.add(myNameLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 
81                   , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0)); 
82           this.add(myNameField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0 
83                   , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
84           this.add(myAddressLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 
85                   , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
86           this.add(myAddressScrollpane, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0 
87                   , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
88           this.add(myCityLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 
89                   , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
90           this.add(myCityField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0 
91                   , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
92           this.add(myStateLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0 
93                   , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
94           this.add(myStateField, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0 
95                   , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
96           this.add(myPostcodeLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0 
97                   , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
98           this.add(myPostcodeField, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0 
99                   , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
100          this.add(myCountryLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0 
101                  , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
102          this.add(myCountryField, new GridBagConstraints(1, 6, 1, 1, 1.0, 0.0 
103                  , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
104          this.add(myContactLabel, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0 
105                  , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
106          this.add(myContactField, new GridBagConstraints(1, 7, 1, 1, 1.0, 0.0 
107                  , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
108          this.add(myPhoneLabel, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0 
109                  , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
110          this.add(myPhoneField, new GridBagConstraints(1, 8, 1, 1, 1.0, 0.0 
111                  , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
112          this.add(myFaxLabel, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0 
113                  , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
114          this.add(myFaxField, new GridBagConstraints(1, 9, 1, 1, 1.0, 0.0 
115                  , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
116          this.add(myEmailLabel, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0 
117                  , GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); 
118          this.add(myEmailField, new GridBagConstraints(1, 10, 1, 1, 1.0, 0.0 
119                  , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 10), 0, 0)); 
120          this.add(myFillLabel, new GridBagConstraints(0, 99, 1, 1, 0.0, 1.0 
121                  , GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0)); 
122      } 
123   
124      /** 
125       * This method gets the values of the panel entry fields. 
126       * 
127       * @return an object containing the Customer data 
128       */ 
129      public CustomerData getCustomerData() { 
130          CustomerData customerData = new CustomerData(); 
131   
132          customerData.setCustomerCode(myCustomerCodeField.getText()); 
133          customerData.setName(myNameField.getText()); 
134          customerData.setAddress(myAddressField.getText()); 
135          customerData.setCity(myCityField.getText()); 
136          customerData.setState(myStateField.getText()); 
137          customerData.setZip(myPostcodeField.getText()); 
138          customerData.setCountry(myCountryField.getSelectedItem().toString()); 
139          customerData.setContact(myContactField.getText()); 
140          customerData.setPhone1(myPhoneField.getText()); 
141          customerData.setFax(myFaxField.getText()); 
142          customerData.setEmail(myEmailField.getText()); 
143   
144          return customerData; 
145      } 
146   
147      /** 
148       * This method sets the values of the panel entry fields. 
149       * 
150       * @param customerData The object containing the Customer data 
151       */ 
152      public void setCustomerData(CustomerData customerData) { 
153          myCustomerCodeField.setText(customerData.getCustomerCode()); 
154          myNameField.setText(customerData.getName()); 
155          myAddressField.setText(customerData.getAddress()); 
156          myCityField.setText(customerData.getCity()); 
157          myStateField.setText(customerData.getState()); 
158          myPostcodeField.setText(customerData.getZip()); 
159          myCountryField.setSelectedItem(customerData.getCountry()); 
160          myContactField.setText(customerData.getContact()); 
161          myPhoneField.setText(customerData.getPhone1()); 
162          myFaxField.setText(customerData.getFax()); 
163          myEmailField.setText(customerData.getEmail()); 
164      } 
165   
166      /** 
167       * This method demonstrates the usage of the JBuilderPanel class. 
168       */ 
169      public static void main(String[] args) { 
170          // Create an instance of the panel 
171   
172          JBuilderPanel panel = new JBuilderPanel(); 
173   
174          // Create a dialog to hold the panel 
175   
176          ModalJDialog dialog = new 
177                  ModalJDialog((Frame) null, 
178                          "Customer Details (JBuilderPanel)"); 
179   
180          dialog.setContentPane(panel); 
181   
182          dialog.pack(); 
183   
184          // Present it to a User 
185   
186          dialog.show(); 
187   
188          // Get the data and display it 
189   
190          if (!dialog.hasUserCancelled()) { 
191              CustomerData customerData = panel.getCustomerData(); 
192   
193              customerData.printData(); 
194          } 
195   
196          System.exit(0); 
197      } 
198  }