diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElement.java b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElement.java new file mode 100644 index 0000000..0ffb191 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElement.java @@ -0,0 +1,54 @@ +package simulator.interfaces.html; + +import java.util.HashMap; +import java.util.Map; + +public class HtmlElement { + + private final String type; + private final String id; + private String text; + private Map styles; + //多分ここにもwebsocketのインスタンスが必要になりそう? + //styleなどが変更されたときに通知しなければならないから + //それかそれぞれのreceiverのほうにその処理を作るか + + HtmlElement(String type, String id, String text){ + this.type = type; + this.id = id; + this.text = text; + styles = new HashMap<>(); + } + + public void setStyle(String property, String value) { + styles.put(property, value); + /* + * こちら側にwebsocketの処理を書く場合 + * ws.send("styleChange", id, { property : value } ); + * みたいな感じになりそう + */ + } + + public void resetStyle(String property) { + styles.remove(property); + // ws.send("styleChange", id, { property : null }); + } + + + public String getType() { + return type; + } + + public String getId() { + return id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + +} diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElementVisibilityReceiver.java b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElementVisibilityReceiver.java new file mode 100644 index 0000000..986e170 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElementVisibilityReceiver.java @@ -0,0 +1,38 @@ +package simulator.interfaces.html; + +import models.algebra.Constant; +import models.algebra.Expression; +import models.algebra.Term; +import models.dataConstraintModel.DataConstraintModel; +import simulator.Event; +import simulator.interfaces.INativeReceiver; + +public class HtmlElementVisibilityReceiver implements INativeReceiver{ + + //HtmlElementにwebsocketのインスタンスを渡さない場合こちらに必要 + //見えるかどうかの変更があった場合、html(クライアント側)へwebsocket messageを送る必要がある + // + protected HtmlElement element; + + public HtmlElementVisibilityReceiver(HtmlElement elem){ + this.element = elem; + } + + @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"); + //ws.send("styleChange", element.id, "{\"display\" : \"none\"}");みたいな? + } else { + element.resetStyle("display"); + //ws.send("styleChange", element.id, "{\"display\" : \"null\" }"); + } + } + } + } + +} diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlPresenter.java b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlPresenter.java new file mode 100644 index 0000000..b6fa822 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlPresenter.java @@ -0,0 +1,119 @@ +package simulator.interfaces.html; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import models.algebra.Expression; +import models.dataConstraintModel.JsonTerm; +import models.dataConstraintModel.MapTerm; +import models.dataFlowModel.DataTransferChannel; +import simulator.Event; +import simulator.Resource; +import simulator.Simulator; +import simulator.interfaces.INativeReceiver; + +public class HtmlPresenter implements INativeReceiver{ + + public final String screenUpdateChannelName = "ScreenUpdate"; + public final String setVisibleChannelName = "SetVisible"; + public final String mouseEventChannelName = "MouseEvent"; + public final String textEventChannelName = "TextEvent"; + + protected Simulator simulator; + protected DataTransferChannel screenUpdateChannel; + protected DataTransferChannel setVisibleChannel; + protected DataTransferChannel mouseEventChannel; + protected DataTransferChannel textEventChannel; + protected Map> channelAndResourcesForReceiving = new HashMap<>();// ??? + + protected Map elements; + + public HtmlPresenter(Simulator simulator) { + this.simulator = simulator; + screenUpdateChannel = (DataTransferChannel) simulator.getModel().getChannel(screenUpdateChannelName); + setVisibleChannel = (DataTransferChannel) simulator.getModel().getChannel(setVisibleChannelName); + mouseEventChannel = (DataTransferChannel) simulator.getModel().getChannel(mouseEventChannelName); + textEventChannel = (DataTransferChannel) simulator.getModel().getChannel(textEventChannelName); + simulator.addNativeReceiver(this, screenUpdateChannel); + } + + @Override + public void onReceiveFromModel(Event event) { + //websocketでjavascriptにinsertAdjacentHTMLを実行するように指示するのが簡単そう + //どうやってwebsocketを送るようにサーバーに指示するかが微妙 + //websocketのインスタンスをもらってきて送るみたいな? + //Javaでhtmlのエレメントをどう扱うかも考える必要がありそう + //エレメントを表すクラスが必要? + + + Expression message = event.getMessage(); + + // Remove old native receivers. + if (message instanceof JsonTerm) { + + for (DataTransferChannel channel : channelAndResourcesForReceiving.keySet()) { + for (Resource resource : channelAndResourcesForReceiving.get(channel)) { + simulator.removeNativeReceiver(channel, resource); + } + } + + channelAndResourcesForReceiving.clear(); + + JsonTerm screenContent = (JsonTerm) message; //受け取ったmessageをjsonとして扱う + Resource screenResource = simulator.getCurState().getResource(event.getInputResource().getResourceIdentifier()); //出力チャンネルに接続されたresourceを取得? + Expression widgets = screenContent.get("\"widgets\""); //json化したmessageから"widgets"を取得 + Resource widgetsResource = screenResource.getChildrenMap().get("\"widgets\"");//??? + + if(widgets instanceof MapTerm) { + for(String key : ((MapTerm) widgets).keySet()) { // keyはなに? どうやって決められる? + Expression value = ((MapTerm) widgets).get(key); //widgetの構成要素(text, typeなど)を持つjson, 一つのwidgetを表す? + if(value instanceof JsonTerm) { + JsonTerm widget = (JsonTerm) value; //jsonとして扱えるように + Resource widgetResouce = widgetsResource.getChildrenMap().get(key); //??? + Expression type = widget.get("\"type\""); //widgetのtypeを取得 + if(type.toString().equals("button")) { + + //こんな感じになりそう + Expression text = widget.get("\"text\""); + HtmlElement button = new HtmlElement(type.toString(), key, text.toString()); + elements.put(key, button); + + //ボタンが押された時の処理 + //ボタンが押されたかどうかをどうやって確認するか + //そもそもボタンが押されたらREST API が叩かれるのでは? + + //可視化、不可視化のやつ + HtmlElementVisibilityReceiver nativeReceiver = new HtmlElementVisibilityReceiver(button); + simulator.addNativeReceiver(nativeReceiver, setVisibleChannel, widgetResouce); + Set resources = channelAndResourcesForReceiving.get(setVisibleChannel);//この辺謎 + if (resources == null) { + resources = new HashSet<>(); + channelAndResourcesForReceiving.put(setVisibleChannel, resources); + } + resources.add(widgetsResource); + + } else if(type.toString().equals("label")) { + + Expression text = widget.get("\"text\""); + HtmlElement elem = new HtmlElement(type.toString(), key, text.toString()); + elements.put(key, elem); + + } else if(type.toString().equals("inputText")) { + + HtmlElement elem = new HtmlElement(type.toString(), key, ""); + elements.put(key, elem); + + } + } + } + + + } + + + } + } + +}