Design Pattern Java

//[C] 2002 Sun Microsystems, Inc.---
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Properties;
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.JTextField;
public class RunFacadePattern {
    public static void main(String [] arguments){
        System.out.println("Example for the Facade pattern");
        System.out.println();
        System.out.println("This code sample uses an InternationalizatgionWizard (a Facade)");
        System.out.println(" to manage communication between the rest of the application and");
        System.out.println(" a series of other classes.");
        System.out.println();
        System.out.println("The InternationalizatgionWizard maintains a colleciton of Nation");
        System.out.println(" objects. When the setNation method is called, the wizard sets the");
        System.out.println(" default nation, updating the Currency, PhoneNumber and localized");
        System.out.println(" String resources (InternationalizedText) available.");
        System.out.println();
        System.out.println("Calls to get Strings for the GUI, the currency symbol or the dialing");
        System.out.println(" prefix are routed through the Facade, the InternationalizationWizard.");
        System.out.println();
        
        if (!(new File("data.ser").exists())){
            DataCreator.serialize("data.ser");
        }
        
        System.out.println("Creating the InternationalizationWizard and setting the nation to US.");
        System.out.println();
        InternationalizationWizard wizard = new InternationalizationWizard();
        wizard.setNation("US");
        
        System.out.println("Creating the FacadeGui.");
        System.out.println();
        FacadeGui application = new FacadeGui(wizard);
        application.createGui();
        application.setNation(wizard.getNation("US"));
    }
}
class FacadeGui implements ActionListener, ItemListener{
    private static final String GUI_TITLE = "title";
    private static final String EXIT_CAPTION = "exit";
    private static final String COUNTRY_LABEL = "country";
    private static final String CURRENCY_LABEL = "currency";
    private static final String PHONE_LABEL = "phone";
    
    private JFrame mainFrame;
    private JButton exit;
    private JComboBox countryChooser;
    private JPanel controlPanel, displayPanel;
    private JLabel countryLabel, currencyLabel, phoneLabel;
    private JTextField currencyTextField, phoneTextField;
    private InternationalizationWizard nationalityFacade;
    
    public FacadeGui(InternationalizationWizard wizard){
        nationalityFacade = wizard;
    }
    
    public void createGui(){
        mainFrame = new JFrame(nationalityFacade.getProperty(GUI_TITLE));
        Container content = mainFrame.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        
        displayPanel = new JPanel();
        displayPanel.setLayout(new GridLayout(3, 2));
        
        countryLabel = new JLabel(nationalityFacade.getProperty(COUNTRY_LABEL));
        countryChooser = new JComboBox(nationalityFacade.getNations());
        currencyLabel = new JLabel(nationalityFacade.getProperty(CURRENCY_LABEL));
        currencyTextField = new JTextField();
        phoneLabel = new JLabel(nationalityFacade.getProperty(PHONE_LABEL));
        phoneTextField = new JTextField();
        
        currencyTextField.setEditable(false);
        phoneTextField.setEditable(false);
        
        displayPanel.add(countryLabel);
        displayPanel.add(countryChooser);
        displayPanel.add(currencyLabel);
        displayPanel.add(currencyTextField);
        displayPanel.add(phoneLabel);
        displayPanel.add(phoneTextField);
        content.add(displayPanel);
        
        controlPanel = new JPanel();
        exit = new JButton(nationalityFacade.getProperty(EXIT_CAPTION));
        controlPanel.add(exit);
        content.add(controlPanel);
        
        exit.addActionListener(this);
        countryChooser.addItemListener(this);
        
        mainFrame.addWindowListener(new WindowCloseManager());
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
    
    private void updateGui(){
        nationalityFacade.setNation(countryChooser.getSelectedItem().toString());
        mainFrame.setTitle(nationalityFacade.getProperty(GUI_TITLE));
        countryLabel.setText(nationalityFacade.getProperty(COUNTRY_LABEL));
        currencyLabel.setText(nationalityFacade.getProperty(CURRENCY_LABEL));
        phoneLabel.setText(nationalityFacade.getProperty(PHONE_LABEL));
        exit.setText(nationalityFacade.getProperty(EXIT_CAPTION));
        currencyTextField.setText(nationalityFacade.getCurrencySymbol() + " " +
            nationalityFacade.getNumberFormat().format(5280.50));
        phoneTextField.setText(nationalityFacade.getPhonePrefix());
        mainFrame.invalidate();
        countryLabel.invalidate();
        currencyLabel.invalidate();
        phoneLabel.invalidate();
        exit.invalidate();
        mainFrame.validate();
    }
    
    public void actionPerformed(ActionEvent evt){
        Object originator = evt.getSource();
        if (originator == exit){
            exitApplication();
        }
    }
    public void itemStateChanged(ItemEvent evt){
        Object originator = evt.getSource();
        if (originator == countryChooser){
            updateGui();
        }
    }
    
    public void setNation(Nation nation){
        countryChooser.setSelectedItem(nation);
    }
    
    private class WindowCloseManager extends WindowAdapter{
        public void windowClosing(WindowEvent evt){
            exitApplication();
        }
    }
    
    private void exitApplication(){
        System.exit(0);
    }
}
class Nation {
    private char symbol;
    private String name;
    private String dialingPrefix;
    private String propertyFileName;
    private NumberFormat numberFormat;
    
    public Nation(String newName, char newSymbol, String newDialingPrefix,
        String newPropertyFileName, NumberFormat newNumberFormat) {
        name = newName;
        symbol = newSymbol;
        dialingPrefix = newDialingPrefix;
        propertyFileName = newPropertyFileName;
        numberFormat = newNumberFormat;
    }
    
    public String getName(){ return name; }
    public char getSymbol(){ return symbol; }
    public String getDialingPrefix(){ return dialingPrefix; }
    public String getPropertyFileName(){ return propertyFileName; }
    public NumberFormat getNumberFormat(){ return numberFormat; }
    
    public String toString(){ return name; }
}
class Currency{
    private char currencySymbol;
    private NumberFormat numberFormat;
    
    public void setCurrencySymbol(char newCurrencySymbol){ currencySymbol = newCurrencySymbol; }
    public void setNumberFormat(NumberFormat newNumberFormat){ numberFormat = newNumberFormat; }
    
    public char getCurrencySymbol(){ return currencySymbol; }
    public NumberFormat getNumberFormat(){ return numberFormat; }
}
class DataCreator{
    private static final String GUI_TITLE = "title";
    private static final String EXIT_CAPTION = "exit";
    private static final String COUNTRY_LABEL = "country";
    private static final String CURRENCY_LABEL = "currency";
    private static final String PHONE_LABEL = "phone";
    
    public static void serialize(String fileName){
        saveFrData();
        saveUsData();
        saveNlData();
    }
    
    private static void saveFrData(){
        try{
            Properties textSettings = new Properties();
            textSettings.setProperty(GUI_TITLE, "Demonstration du Pattern Facade");
            textSettings.setProperty(EXIT_CAPTION, "Sortir");
            textSettings.setProperty(COUNTRY_LABEL, "Pays");
            textSettings.setProperty(CURRENCY_LABEL, "Monnaie");
            textSettings.setProperty(PHONE_LABEL, "Numero de Telephone");
            textSettings.store(new FileOutputStream("french.properties"), "French Settings");
        }
        catch (IOException exc){
            System.err.println("Error storing settings to output");
            exc.printStackTrace();
        }
    }
    private static void saveUsData(){
        try{
            Properties textSettings = new Properties();
            textSettings.setProperty(GUI_TITLE, "Facade Pattern Demonstration");
            textSettings.setProperty(EXIT_CAPTION, "Exit");
            textSettings.setProperty(COUNTRY_LABEL, "Country");
            textSettings.setProperty(CURRENCY_LABEL, "Currency");
            textSettings.setProperty(PHONE_LABEL, "Phone Number");
            textSettings.store(new FileOutputStream("us.properties"), "US Settings");
        }
        catch (IOException exc){
            System.err.println("Error storing settings to output");
            exc.printStackTrace();
        }
    }
    private static void saveNlData(){
        try{
            Properties textSettings = new Properties();
            textSettings.setProperty(GUI_TITLE, "Facade Pattern voorbeeld");
            textSettings.setProperty(EXIT_CAPTION, "Exit");
            textSettings.setProperty(COUNTRY_LABEL, "Land");
            textSettings.setProperty(CURRENCY_LABEL, "Munt eenheid");
            textSettings.setProperty(PHONE_LABEL, "Telefoonnummer");
            textSettings.store(new FileOutputStream("dutch.properties"), "Dutch Settings");
        }
        catch (IOException exc){
            System.err.println("Error storing settings to output");
            exc.printStackTrace();
        }
    }
}
class PhoneNumber {
    private static String selectedInterPrefix;
    private String internationalPrefix;
    private String areaNumber;
    private String netNumber;
    public PhoneNumber(String intPrefix, String areaNumber, String netNumber) {
        this.internationalPrefix = intPrefix;
        this.areaNumber = areaNumber;
        this.netNumber = netNumber;
    }
    
    public String getInternationalPrefix(){ return internationalPrefix; }
    public String getAreaNumber(){ return areaNumber; }
    public String getNetNumber(){ return netNumber; }
    public static String getSelectedInterPrefix(){ return selectedInterPrefix; }
    
    public void setInternationalPrefix(String newPrefix){ internationalPrefix = newPrefix; }
    public void setAreaNumber(String newAreaNumber){ areaNumber = newAreaNumber; }
    public void setNetNumber(String newNetNumber){ netNumber = newNetNumber; }
    public static void setSelectedInterPrefix(String prefix) { selectedInterPrefix = prefix; }
    
    public String toString(){
        return internationalPrefix + areaNumber + netNumber;
    }
}
class InternationalizedText{
    private static final String DEFAULT_FILE_NAME = "";
    private Properties textProperties = new Properties();
    
    public InternationalizedText(){
        this(DEFAULT_FILE_NAME);
    }
    public InternationalizedText(String fileName){
        loadProperties(fileName);
    }
    
    public void setFileName(String newFileName){
        if (newFileName != null){
            loadProperties(newFileName);
        }
    }
    public String getProperty(String key){
        return getProperty(key, "");
    }
    public String getProperty(String key, String defaultValue){
        return textProperties.getProperty(key, defaultValue);
    }
    
    private void loadProperties(String fileName){
        try{
            FileInputStream input = new FileInputStream(fileName);
            textProperties.load(input);
        }
        catch (IOException exc){
            textProperties = new Properties();
        }
    }
}
class InternationalizationWizard{
    private HashMap map;
    private Currency currency = new Currency();
    private InternationalizedText propertyFile = new InternationalizedText();
    
    public InternationalizationWizard() {
        map = new HashMap();
        Nation[] nations = {
            new Nation("US", '$', "+1", "us.properties", NumberFormat.getInstance(Locale.US)),
            new Nation("The Netherlands", 'f', "+31", "dutch.properties", NumberFormat.getInstance(Locale.GERMANY)),
            new Nation("France", 'f', "+33", "french.properties", NumberFormat.getInstance(Locale.FRANCE))
        };
        for (int i = 0; i < nations.length; i++) {
            map.put(nations[i].getName(), nations[i]);
        }
    }
    
    public void setNation(String name) {
        Nation nation = (Nation)map.get(name);
        if (nation != null) {
            currency.setCurrencySymbol(nation.getSymbol());
            currency.setNumberFormat(nation.getNumberFormat());
            PhoneNumber.setSelectedInterPrefix(nation.getDialingPrefix());
            propertyFile.setFileName(nation.getPropertyFileName());
        }
    }
    
    public Object[] getNations(){
        return map.values().toArray();
    }
    public Nation getNation(String name){
        return (Nation)map.get(name);
    }
    public char getCurrencySymbol(){
        return currency.getCurrencySymbol();
    }
    public NumberFormat getNumberFormat(){
        return currency.getNumberFormat();
    }
    public String getPhonePrefix(){
        return PhoneNumber.getSelectedInterPrefix();
    }
    public String getProperty(String key){
        return propertyFile.getProperty(key);
    }
    public String getProperty(String key, String defaultValue){
        return propertyFile.getProperty(key, defaultValue);
    }
}