Java Reference
In-Depth Information
39.
40. JPanel control = new JPanel();
41. create = new JButton("Create Contact");
42. update = new JButton("Update Contact");
43. create.addActionListener(this);
44. update.addActionListener(this);
45. control.add(create);
46. control.add(update);
47. add(control, BorderLayout.SOUTH);
48. }
49. public void actionPerformed(ActionEvent evt){
50. Object source = evt.getSource();
51. if (source == create){
52. createContact();
53. }
54. else if (source == update){
55. updateContact();
56. }
57. }
58.
59. public void createContact(){
60. mediator.createContact(firstName.getText(), lastName.getText(),
61. title.getText(), organization.getText());
62. }
63. public void updateContact(){
64. mediator.updateContact(firstName.getText(), lastName.getText(),
65. title.getText(), organization.getText());
66. }
67.
68. public void setContactFields(Contact contact){
69. firstName.setText(contact.getFirstName());
70. lastName.setText(contact.getLastName());
71. title.setText(contact.getTitle());
72. organization.setText(contact.getOrganization());
73. }
74. public void setContactMediator(ContactMediator newMediator){
75. mediator = newMediator;
76. }
77. }
The ContactMediator interface defines set methods for each of the GUI components, and for the business
methods createContact , updateContact , selectContact and getAllContacts .
Example 2.29 ContactMediator.java
1. public interface ContactMediator{
2. public void setContactDisplayPanel(ContactDisplayPanel displayPanel);
3. public void setContactEditorPanel(ContactEditorPanel editorPanel);
4. public void setContactSelectorPanel(ContactSelectorPanel selectorPanel);
5. public void createContact(String firstName, String lastName, String title, String
organization);
6. public void updateContact(String firstName, String lastName, String title, String
organization);
7. public Contact [] getAllContacts();
8. public void selectContact(Contact contact);
9. }
ContactMediatorImpl is the implementer of ContactMediator . It maintains a collection of Contacts, and
methods that notify the panels of changes within the GUI.
Example 2.30 ContactMediatorImpl.java
1. import java.util.ArrayList;
2. public class ContactMediatorImpl implements ContactMediator{
3. private ContactDisplayPanel display;
4. private ContactEditorPanel editor;
5. private ContactSelectorPanel selector;
6. private ArrayList contacts = new ArrayList();
7. private int contactIndex;
8.
9. public void setContactDisplayPanel(ContactDisplayPanel displayPanel){
10. display = displayPanel;
11. }
12. public void setContactEditorPanel(ContactEditorPanel editorPanel){
13. editor = editorPanel;
14. }
15. public void setContactSelectorPanel(ContactSelectorPanel selectorPanel){
Search WWH ::




Custom Search