読者です 読者をやめる 読者になる 読者になる

Mediatorパターン

awt初めて触ったー。javascriptでも実装したいな。

% find . -name "*.java" -print0 |xargs -0 cat                                                                               []
public interface Colleague {
	public abstract void setMediator(Mediator mediator);

	public abstract void setCollegueEnabled(boolean enabled);
}
import java.awt.Button;

public class ColleagueButton extends Button implements Colleague {
	private Mediator mediator;

	public ColleagueButton(String caption) {
		super(caption);
	}

	@Override
	public void setMediator(Mediator mediator) {
		this.mediator = mediator;
	}

	@Override
	public void setCollegueEnabled(boolean enabled) {
		setEnabled(enabled);
	}

}
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ColleagueCheckBox extends Checkbox implements ItemListener,
		Colleague {
	private Mediator mediator;

	public ColleagueCheckBox(String caption, CheckboxGroup group, boolean state) {
		super(caption, group, state);
	}

	@Override
	public void setMediator(Mediator mediator) {
		this.mediator = mediator;
	}

	@Override
	public void setCollegueEnabled(boolean enabled) {
		setEnabled(enabled);
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		mediator.colleagueChanged();
	}
}
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

public class ColleagueTextFileld extends TextField implements TextListener,
		Colleague {
	private Mediator mediator;

	public ColleagueTextFileld(String text, int column) {
		super(text, column);
	}

	@Override
	public void setMediator(Mediator mediator) {
		this.mediator = mediator;
	}

	@Override
	public void setCollegueEnabled(boolean enabled) {
		setEnabled(enabled);
		setBackground(enabled ? Color.white : Color.lightGray);
	}

	@Override
	public void textValueChanged(TextEvent e) {
		mediator.colleagueChanged();
	}

}
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends Frame implements ActionListener, Mediator {

	private ColleagueCheckBox checkGuest;
	private ColleagueCheckBox checkLogin;
	private ColleagueTextFileld textUser;
	private ColleagueTextFileld textPass;
	private ColleagueButton buttonOk;
	private ColleagueButton buttonCancel;

	public LoginFrame(String title) {
		super(title);
		setBackground(Color.lightGray);
		setLayout(new GridLayout(4, 2));

		createColleagues();

		add(checkGuest);
		add(checkLogin);
		add(new Label("Username:"));
		add(textUser);
		add(new Label("Password:"));
		add(textPass);
		add(buttonOk);
		add(buttonCancel);

		colleagueChanged();

		pack();
		show();
	}

	@Override
	public void createColleagues() {
		CheckboxGroup g = new CheckboxGroup();
		checkGuest = new ColleagueCheckBox("Guest", g, true);
		checkLogin = new ColleagueCheckBox("Login", g, false);
		textUser = new ColleagueTextFileld("", 10);
		textPass = new ColleagueTextFileld("", 10);
		textPass.setEchoChar('*');
		buttonOk = new ColleagueButton("OK");
		buttonCancel = new ColleagueButton("Cancel");

		checkGuest.setMediator(this);
		checkLogin.setMediator(this);
		textUser.setMediator(this);
		textPass.setMediator(this);
		buttonOk.setMediator(this);
		buttonCancel.setMediator(this);

		checkGuest.addItemListener(checkGuest);
		checkLogin.addItemListener(checkLogin);
		textUser.addTextListener(textUser);
		textPass.addTextListener(textPass);
		buttonOk.addActionListener(this);
		buttonCancel.addActionListener(this);

	}

	@Override
	public void colleagueChanged() {
		if (checkGuest.getState()) {
			textUser.setCollegueEnabled(false);
			textPass.setCollegueEnabled(false);
			buttonOk.setCollegueEnabled(true);
		} else {
			textUser.setCollegueEnabled(true);
			userpassChanged();
		}

	}

	private void userpassChanged() {
		if (textUser.getText().length() > 0) {
			textPass.setCollegueEnabled(true);
			if (textPass.getText().length() > 0) {
				buttonOk.setCollegueEnabled(true);
			} else {
				buttonOk.setCollegueEnabled(false);
			}
		} else {
			textPass.setCollegueEnabled(false);
			buttonOk.setCollegueEnabled(false);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String label = ((ColleagueButton) e.getSource()).getLabel();
		System.out.println(label);
		if ("OK".equals(label)) {
			System.out.println("ログイン確認へ");
		}
		System.out.println(textUser.getText());
		System.out.println(textPass.getText());
		System.out.println(e.toString());
		System.exit(0);
	}

}
public class Main {

	public static void main(String[] args) {
		new LoginFrame("Mediator Sample");
	}

}
public interface Mediator {
	public abstract void createColleagues();

	public abstract void colleagueChanged();
}