package simulator.interfaces.swing;

import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.text.JTextComponent;

import models.algebra.Constant;
import models.algebra.Expression;
import models.algebra.Term;
import simulator.Event;
import simulator.SystemState;
import simulator.interfaces.INativeReceiver;

public class ComponentTextReceiver implements INativeReceiver {
	protected Component component;
	
	public ComponentTextReceiver(Component component) {
		this.component = component;
	}

	@Override
	public void onReceiveFromModel(Event event, SystemState nextSystemState) {
		Expression message = event.getMessage();
		if (message instanceof Term) {
			Expression text = ((Term) message).getChild(0);
			if (text instanceof Constant) {
				if (component instanceof JTextComponent) {
					((JTextComponent) component).setText(((Constant) text).getSymbol().getName());
				} else if (component instanceof JLabel) {
					((JLabel) component).setText(((Constant) text).getSymbol().getName());
				} else if (component instanceof JButton) {
					((JButton) component).setText(((Constant) text).getSymbol().getName());
				}
			}
		}
	}

}
