Mediator Pattern 2 : Mediator Pattern « Design Pattern « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Design Pattern » Mediator PatternScreenshots 
Mediator Pattern 2
Mediator Pattern 2

//[C] 2002 Sun Microsystems, Inc.---



import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class RunMediatorPattern {
    public static void main(String [] arguments){
        System.out.println("Example for the Mediator pattern");
        System.out.println("In this demonstration, the ContactMediatorImpl class will");
        System.out.println(" coordinate updates between three controls in a GUI - the");
        System.out.println(" ContactDisplayPanel, the ContactEditorPanel, and the");
        System.out.println(" ContactSelectorPanel. As its name suggests, the Mediator");
        System.out.println(" mediates the activity between the elements of the GUI,");
        System.out.println(" translating method calls from one panel into the appropriate");
        System.out.println(" method calls on the other GUI components.");
        
        Contact contact = new ContactImpl("""""""");
        Contact contact1 = new ContactImpl("Duke""""Java Advocate""The Patterns Guild");
        ContactMediatorImpl mediator = new ContactMediatorImpl();
        mediator.addContact(contact);
        mediator.addContact(contact1);
        MediatorGui gui = new MediatorGui();
        gui.setContactMediator(mediator);
        gui.createGui();
        
        
    }
}

class ContactEditorPanel extends JPanel implements ActionListener{
    private ContactMediator mediator;
    private JTextField firstName, lastName, title, organization;
    private JButton create, update;
    
    public ContactEditorPanel(){
        createGui();
    }
    public ContactEditorPanel(ContactMediator newMediator){
        setContactMediator(newMediator);
        createGui();
    }
    public void createGui(){
        setLayout(new BorderLayout());
        
        JPanel editor = new JPanel();
        editor.setLayout(new GridLayout(42));
        editor.add(new JLabel("First Name:"));
        firstName = new JTextField(20);
        editor.add(firstName);
        editor.add(new JLabel("Last Name:"));
        lastName = new JTextField(20);
        editor.add(lastName);
        editor.add(new JLabel("Title:"));
        title = new JTextField(20);
        editor.add(title);
        editor.add(new JLabel("Organization:"));
        organization = new JTextField(20);
        editor.add(organization);
        add(editor, BorderLayout.CENTER);
        
        JPanel control = new JPanel();
        create new JButton("Create Contact");
        update new JButton("Update Contact");
        create.addActionListener(this);
        update.addActionListener(this);
        control.add(create);
        control.add(update);
        add(control, BorderLayout.SOUTH);
    }
    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();
        if (source == create){
            createContact();
        }
        else if (source == update){
            updateContact();
        }
    }
    
    public void createContact(){
        mediator.createContact(firstName.getText(), lastName.getText(),
            title.getText(), organization.getText());
    }
    public void updateContact(){
        mediator.updateContact(firstName.getText(), lastName.getText(),
            title.getText(), organization.getText());
    }
    
    public void setContactFields(Contact contact){
        firstName.setText(contact.getFirstName());
        lastName.setText(contact.getLastName());
        title.setText(contact.getTitle());
        organization.setText(contact.getOrganization());
    }
    public void setContactMediator(ContactMediator newMediator){
        mediator = newMediator;
    }
}
class ContactMediatorImpl implements ContactMediator{
    private ContactDisplayPanel display;
    private ContactEditorPanel editor;
    private ContactSelectorPanel selector;
    private ArrayList contacts = new ArrayList();
    private int contactIndex;
    
    public void setContactDisplayPanel(ContactDisplayPanel displayPanel){
        display = displayPanel;
    }
    public void setContactEditorPanel(ContactEditorPanel editorPanel){
        editor = editorPanel;
    }
    public void setContactSelectorPanel(ContactSelectorPanel selectorPanel){
        selector = selectorPanel;
    }
    
    public void createContact(String firstName, String lastName, String title, String organization){
        Contact newContact = new ContactImpl(firstName, lastName, title, organization);
        addContact(newContact);
        selector.addContact(newContact);
        display.contactChanged(newContact);
    }
    public void updateContact(String firstName, String lastName, String title, String organization){
        Contact updateContact = (Contact)contacts.get(contactIndex);
        if (updateContact != null){
            updateContact.setFirstName(firstName);
            updateContact.setLastName(lastName);
            updateContact.setTitle(title);
            updateContact.setOrganization(organization);
            display.contactChanged(updateContact);
        }
    }
    public void selectContact(Contact contact){
        if (contacts.contains(contact)){
            contactIndex = contacts.indexOf(contact);
            display.contactChanged(contact);
            editor.setContactFields(contact);
        }
    }
    public Contact [] getAllContacts(){
        return (Contact [])contacts.toArray(new Contact[1]);
    }
    public void addContact(Contact contact){
        if (!contacts.contains(contact)){
            contacts.add(contact);
        }
    }
}

class ContactDisplayPanel extends JPanel{
    private ContactMediator mediator;
    private JTextArea displayRegion;
    
    public ContactDisplayPanel(){
        createGui();
    }
    public ContactDisplayPanel(ContactMediator newMediator){
        setContactMediator(newMediator);
        createGui();
    }
    public void createGui(){
        setLayout(new BorderLayout());
        displayRegion = new JTextArea(1040);
        displayRegion.setEditable(false);
        add(new JScrollPane(displayRegion));
    }
    public void contactChanged(Contact contact){
        displayRegion.setText(
            "Contact\n\tName: " + contact.getFirstName() +
            " " + contact.getLastName() "\n\tTitle: " +
            contact.getTitle() "\n\tOrganization: " +
            contact.getOrganization());
    }
    public void setContactMediator(ContactMediator newMediator){
        mediator = newMediator;
    }
}
class ContactSelectorPanel extends JPanel implements ActionListener{
    private ContactMediator mediator;
    private JComboBox selector;
    
    public ContactSelectorPanel(){
        createGui();
    }
    public ContactSelectorPanel(ContactMediator newMediator){
        setContactMediator(newMediator);
        createGui();
    }
    
    public void createGui(){
        selector = new JComboBox(mediator.getAllContacts());
        selector.addActionListener(this);
        add(selector);
    }
    
    public void actionPerformed(ActionEvent evt){
        mediator.selectContact((Contact)selector.getSelectedItem());
    }
    public void addContact(Contact contact){
        selector.addItem(contact);
        selector.setSelectedItem(contact);
    }
    public void setContactMediator(ContactMediator newMediator){
        mediator = newMediator;
    }
}
class MediatorGui{
    private ContactMediator mediator;
    
    public void setContactMediator(ContactMediator newMediator){ mediator = newMediator; }
    
    public void createGui(){
        JFrame mainFrame = new JFrame("Mediator example");
        Container content = mainFrame.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        ContactSelectorPanel select new ContactSelectorPanel(mediator);
        ContactDisplayPanel display = new ContactDisplayPanel(mediator);
        ContactEditorPanel edit = new ContactEditorPanel(mediator);
        content.add(select);
        content.add(display);
        content.add(edit);
        mediator.setContactSelectorPanel(select);
        mediator.setContactDisplayPanel(display);
        mediator.setContactEditorPanel(edit);
        mainFrame.addWindowListener(new WindowCloseManager());
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
    private class WindowCloseManager extends WindowAdapter{
        public void windowClosing(WindowEvent evt){
            System.exit(0);
        }
    }
}




interface ContactMediator{
    public void setContactDisplayPanel(ContactDisplayPanel displayPanel);
    public void setContactEditorPanel(ContactEditorPanel editorPanel);
    public void setContactSelectorPanel(ContactSelectorPanel selectorPanel);
    public void createContact(String firstName, String lastName, String title, String organization);
    public void updateContact(String firstName, String lastName, String title, String organization);
    public Contact [] getAllContacts();
    public void selectContact(Contact contact);
}
interface Contact extends Serializable{
    public static final String SPACE = " ";
    public String getFirstName();
    public String getLastName();
    public String getTitle();
    public String getOrganization();
    
    public void setFirstName(String newFirstName);
    public void setLastName(String newLastName);
    public void setTitle(String newTitle);
    public void setOrganization(String newOrganization);
}

class ContactImpl implements Contact{
    private String firstName;
    private String lastName;
    private String title;
    private String organization;
    
    public ContactImpl(){}
    public ContactImpl(String newFirstName, String newLastName,
        String newTitle, String newOrganization){
            firstName = newFirstName;
            lastName = newLastName;
            title = newTitle;
            organization = newOrganization;
    }
    
    public String getFirstName(){ return firstName; }
    public String getLastName(){ return lastName; }
    public String getTitle(){ return title; }
    public String getOrganization(){ return organization; }
    
    public void setFirstName(String newFirstName){ firstName = newFirstName; }
    public void setLastName(String newLastName){ lastName = newLastName; }
    public void setTitle(String newTitle){ title = newTitle; }
    public void setOrganization(String newOrganization){ organization = newOrganization; }
    
    public String toString(){
        return firstName + SPACE + lastName;
    }
}

           
       
Related examples in the same category
1. Mediator pattern in JavaMediator pattern in Java
2. Design Patterns By Example in Java- Mediator Pattern Design Patterns By Example in Java- Mediator Pattern
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.