Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / simulator / interfaces / html / HtmlElementVisibilityReceiver.java
Sakoda2269 on 17 Sep 1 KB websocket 作業中
package simulator.interfaces.html;

import models.algebra.Constant;
import models.algebra.Expression;
import models.algebra.Term;
import models.dataConstraintModel.DataConstraintModel;
import models.dataConstraintModel.JsonTerm;
import models.dataConstraintModel.MapTerm;
import simulator.Event;
import simulator.interfaces.INativeReceiver;

public class HtmlElementVisibilityReceiver implements INativeReceiver{
	
	//HtmlElementにwebsocketのインスタンスを渡さない場合こちらに必要
	//見えるかどうかの変更があった場合、html(クライアント側)へwebsocket messageを送る必要がある
	//
	protected HtmlElement element;
	
	private final IWebSocketMessageSender ws;
	
	public HtmlElementVisibilityReceiver(HtmlElement elem, IWebSocketMessageSender ws){
		this.element = elem;
		this.ws = ws;
	}
	
	@Override
	public void onReceiveFromModel(Event event) {
		Expression message = event.getMessage();
		if (message instanceof Term) {
			Expression visible = ((Term) message).getChild(0);
			if (visible instanceof Constant) {
				if (((Constant) visible).getSymbol() == DataConstraintModel.true_) {
					element.setStyle("display", "none");
					
					JsonTerm json = new JsonTerm();
					json.addMember("method", new Constant("changeStyle"));
					json.addMember("id", new Constant(element.getId()));
					MapTerm datas = new MapTerm();
					datas.insert("display", new Constant("none"));
					json.addMember("datas", datas);
					
					ws.send(json.toString());
					
					
				} else {
					element.resetStyle("display");
					
					JsonTerm json = new JsonTerm();
					json.addMember("method", new Constant("changeStyle"));
					json.addMember("id", new Constant(element.getId()));
					MapTerm datas = new MapTerm();
					datas.insert("display", new Constant("null"));
					json.addMember("datas", datas);
					
					ws.send(json.toString());
				}
			}
		}
	}

}