Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

20.add(new JScrollPane(displayRegion));

21.}

22.public void contactChanged(Contact contact){

23.displayRegion.setText(

24. "Contact\n\tName: " + contact.getFirstName() +

25. " " + contact.getLastName() + "\n\tTitle: " +

26. contact.getTitle() + "\n\tOrganization: " +

27. contact.getOrganization());

28.}

29.public void setContactMediator(ContactMediator newMediator){

30.mediator = newMediator;

31.}

32.}

ContactSelectorPanel allows the user to choose a Contact for display and edit in the MediatorGui.

Example A.76 ContactSelectorPanel.java

1.import java.awt.event.ActionEvent;

2.import java.awt.event.ActionListener;

3.import javax.swing.JComboBox;

4.import javax.swing.JPanel;

5.

6.public class ContactSelectorPanel extends JPanel implements ActionListener{

7.private ContactMediator mediator;

8.private JComboBox selector;

9.

10.public ContactSelectorPanel(){

11.createGui();

12.}

13.public ContactSelectorPanel(ContactMediator newMediator){

14.setContactMediator(newMediator);

15.createGui();

16.}

17.

18.public void createGui(){

19.selector = new JComboBox(mediator.getAllContacts());

20.selector.addActionListener(this);

21.add(selector);

22.}

23.

24.public void actionPerformed(ActionEvent evt){

25.mediator.selectContact((Contact)selector.getSelectedItem());

26.}

27.public void addContact(Contact contact){

28.selector.addItem(contact);

29.selector.setSelectedItem(contact);

30.}

31.public void setContactMediator(ContactMediator newMediator){

32.mediator = newMediator;

33.}

34.}

The ContactEditorPanel provides an editing interface for the currently selected Contact. It has buttons that allow a user to add or update a Contact.

Example A.77 ContactEditorPanel.java

1.import java.awt.BorderLayout;

2.import java.awt.GridLayout;

3.import java.awt.event.ActionEvent;

4.import java.awt.event.ActionListener;

5.import javax.swing.JButton;

6.import javax.swing.JLabel;

7.import javax.swing.JPanel;

8.import javax.swing.JTextField;

9.public class ContactEditorPanel extends JPanel implements ActionListener{

10.private ContactMediator mediator;

11.private JTextField firstName, lastName, title, organization;

12.private JButton create, update;

13.

14.public ContactEditorPanel(){

15.createGui();

16.}

17.public ContactEditorPanel(ContactMediator newMediator){

18.setContactMediator(newMediator);

19.createGui();

258

20.}

21.public void createGui(){

22.setLayout(new BorderLayout());

24.JPanel editor = new JPanel();

25.editor.setLayout(new GridLayout(4, 2));

26.editor.add(new JLabel("First Name:"));

27.firstName = new JTextField(20);

28.editor.add(firstName);

29.editor.add(new JLabel("Last Name:"));

30.lastName = new JTextField(20);

31.editor.add(lastName);

32.editor.add(new JLabel("Title:"));

33.title = new JTextField(20);

34.editor.add(title);

35.editor.add(new JLabel("Organization:"));

36.organization = new JTextField(20);

37.editor.add(organization);

38.add(editor, BorderLayout.CENTER);

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.}

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 A.78 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.}

259

ContactMediatorImpl is the implementer of ContactMediator. It maintains a collection of Contacts, and methods that notify the panels of changes within the GUI.

Example A.79 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){

16.selector = selectorPanel;

17.}

18.

19.public void createContact(String firstName, String lastName, String title, String

organization){

20.Contact newContact = new ContactImpl(firstName, lastName, title, organization);

21.addContact(newContact);

22.selector.addContact(newContact);

23.display.contactChanged(newContact);

24.}

25.public void updateContact(String firstName, String lastName, String title, String

organization){

26.Contact updateContact = (Contact)contacts.get(contactIndex);

27.if (updateContact != null){

28.

updateContact.setFirstName(firstName);

29.

updateContact.setLastName(lastName);

30.

updateContact.setTitle(title);

31.

updateContact.setOrganization(organization);

32.

display.contactChanged(updateContact);

33.}

34.}

35.public void selectContact(Contact contact){

36.if (contacts.contains(contact)){

37.

contactIndex = contacts.indexOf(contact);

38.

display.contactChanged(contact);

39.

editor.setContactFields(contact);

40.}

41.}

42.public Contact [] getAllContacts(){

43.return (Contact [])contacts.toArray(new Contact[1]);

44.}

45.public void addContact(Contact contact){

46.if (!contacts.contains(contact)){

47. contacts.add(contact);

48.}

49.}

50.}

The ContactMediatorImpl interacts with each of the panels differently. For the ContactDisplayPanel, the mediator calls its contactChanged method for the create, update and select operations. For the ContactSelectorPanel, the mediator provides the list of Contacts with the getAllContacts method, receives select notifications, and adds a new Contact object to the panel when one is created. The mediator receives create and update method calls from the ContactEditorPanel, and notifies the panel of select actions from the

ContactSelectorPanel.

Contact and ContactImpl define the business class used in this example.

Example A.80 Contact.java

1.import java.io.Serializable;

2.public interface Contact extends Serializable{

3.public static final String SPACE = " ";

4.public String getFirstName();

5.public String getLastName();

6.public String getTitle();

260

7. public String getOrganization();

8.

9.public void setFirstName(String newFirstName);

10.public void setLastName(String newLastName);

11.public void setTitle(String newTitle);

12.public void setOrganization(String newOrganization);

13.}

Example A.81 ContactImpl.java

1.public class ContactImpl implements Contact{

2.private String firstName;

3.private String lastName;

4.private String title;

5.private String organization;

6.

7.public ContactImpl(){}

8.public ContactImpl(String newFirstName, String newLastName,

9.String newTitle, String newOrganization){

10. firstName = newFirstName;

11. lastName = newLastName;

12. title = newTitle;

13. organization = newOrganization;

14. }

15.

16.public String getFirstName(){ return firstName; }

17.public String getLastName(){ return lastName; }

18.public String getTitle(){ return title; }

19.public String getOrganization(){ return organization; }

21.public void setFirstName(String newFirstName){ firstName = newFirstName; }

22.public void setLastName(String newLastName){ lastName = newLastName; }

23.public void setTitle(String newTitle){ title = newTitle; }

24.public void setOrganization(String newOrganization){ organization = newOrganization; }

26.public String toString(){

27.return firstName + SPACE + lastName;

28.}

29.}

 

 

 

 

Y

RunPattern creates the GUI and its mediator, and loads a sampleLcontact.

Example A.82 RunPattern.java

 

 

F

 

 

M

1.

public class RunPattern{

 

A

2.

public static void main(String [] arguments){

3.

 

E

 

System.out.println("Example for the Mediator pattern");

4.

 

T

 

 

System.out.println("In this demonstration, the ContactMediatorImpl class will");

5.

System.out.println(" coordinate updates between three controls in a GUI the");

6.

System.out.println(" ContactDisplayPanel, the ContactEditorPanel, and the");

7.

System.out.println(" ContactSelectorPanel. As its name suggests, the Mediator");

8.

System.out.println(" mediates the activity between the elements of the GUI,");

9.

System.out.println(" translating method calls from one panel into the appropriate");

10.

System.out.println(" method calls on the other GUI components.");

11.

 

 

 

 

12.Contact contact = new ContactImpl("", "", "", "");

13.Contact contact1 = new ContactImpl("Duke", "", "Java Advocate", "The Patterns Guild");

14.ContactMediatorImpl mediator = new ContactMediatorImpl();

15.mediator.addContact(contact);

16.mediator.addContact(contact1);

17.MediatorGui gui = new MediatorGui();

18.gui.setContactMediator(mediator);

19.gui.createGui();

20.

21.

22.}

23.}

TEAM FLY PRESENTS

261