diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElement.java b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElement.java index a1264f4..9696e1f 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElement.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlElement.java @@ -52,7 +52,7 @@ stylesMap.insert(prop, new Constant(styles.get(prop))); } res.insert("\"styles\"", stylesMap); - String classTmp = String.join(" ", classes); + String classTmp = "\"" + String.join(" ", classes) + "\""; res.insert("\"class\"", new Constant(classTmp)); return res; } diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlPresenter.java b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlPresenter.java index b6c4736..d892e7d 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlPresenter.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/interfaces/html/HtmlPresenter.java @@ -15,6 +15,7 @@ import models.dataFlowModel.DataTransferChannel; import simulator.Event; import simulator.Resource; +import simulator.ResourceIdentifier; import simulator.Simulator; import simulator.SystemState; import simulator.interfaces.INativeReceiver; @@ -50,8 +51,8 @@ setTextChannel = (DataTransferChannel) simulator.getModel().getChannel(setTextChannelName); textEventChannel = (DataTransferChannel) simulator.getModel().getInputChannel(textEventChannelName); simulator.addNativeReceiver(this, screenUpdateChannel); - elements = elems; + initScreen(); } @@ -90,7 +91,7 @@ } if (!newWidSet.isEmpty()) { - addNewWidgets(newWidSet, nextSystemState, event, newWidgets, addElements); + addNewWidgets(newWidSet, nextSystemState, event.getInputResource().getResourceIdentifier(), newWidgets, addElements); } } @@ -102,15 +103,69 @@ } } + private void initScreen() { + Expression initScExp = simulator.getCurState().getResource("screen").getState().getValue(); + if (initScExp instanceof JsonTerm) { + JsonTerm sendData = new JsonTerm(); + sendData.addMember("\"method\"", new Constant("\"updateHtml\"")); + JsonTerm sendElements = new JsonTerm(); + ListTerm addElements = new ListTerm(); + ListTerm delElements = new ListTerm(); - private void addNewWidgets(Set newWidSet, SystemState nextSystemState, Event event, Expression newWidgets, ListTerm addElements) { + JsonTerm nextSc = (JsonTerm) initScExp; + Expression newWidgets = nextSc.get("widgets"); - Resource screenResource = nextSystemState.getResource(event.getInputResource().getResourceIdentifier()); + if (newWidgets instanceof MapTerm) { + + Set newWidSet = new HashSet<>(((MapTerm) newWidgets).keySet()); + + if (!newWidSet.isEmpty()) { + addNewWidgets(newWidSet, simulator.getCurState().getResource("screen"), newWidgets, addElements); + } + } + + sendElements.addMember("\"add\"", addElements); + sendElements.addMember("\"delete\"", delElements); + sendData.addMember("\"data\"", sendElements); + ws.send(sendData.toString()); + } + } + + private void addNewWidgets(Set newWidSet, Resource screenResource, Expression newWidgets, ListTerm addElements) { + Resource widgetsResource = screenResource.getChildrenMap().get("widgets"); for(String newWid: newWidSet) { Expression value = ((MapTerm) newWidgets).get(newWid); - if(value instanceof JsonTerm) { - JsonTerm widget = (JsonTerm) value; + if(value instanceof MapTerm) { + MapTerm widget = (MapTerm) value; + Resource widgetResource = widgetsResource.getChildrenMap().get(newWid); + Expression type = widget.get("type"); + + switch(type.toString()) { + case "\"button\"": + addButton(widget, newWid, addElements, widgetResource); + break; + case "\"label\"": + addLabel(widget, newWid, addElements, widgetResource); + break; + case "\"textInput\"": + addTextInput(widget, newWid, addElements, widgetResource); + break; + } + } + } + + } + + + private void addNewWidgets(Set newWidSet, SystemState nextSystemState, ResourceIdentifier ri, Expression newWidgets, ListTerm addElements) { + + Resource screenResource = nextSystemState.getResource(ri); + Resource widgetsResource = screenResource.getChildrenMap().get("widgets"); + for(String newWid: newWidSet) { + Expression value = ((MapTerm) newWidgets).get(newWid); + if(value instanceof MapTerm) { + MapTerm widget = (MapTerm) value; Resource widgetResource = widgetsResource.getChildrenMap().get(newWid); Expression type = widget.get("\"type\""); @@ -130,10 +185,25 @@ } - private void addButton(JsonTerm widget, String wid, ListTerm addElements, Resource widgetResource) { - String text = widget.get("\"text\"").toString(); + private void addButton(MapTerm widget, String wid, ListTerm addElements, Resource widgetResource) { + String text = widget.get("text").toString(); HtmlElement button = new HtmlElement("button", wid, text); + Set keySet = widget.keySet(); + button.setStyle("\"position\"", "\"absolute\""); + if(keySet.contains("x")) { + button.setStyle("\"left\"", "\"" + widget.get("x") + "px\""); + } + if(keySet.contains("y")) { + button.setStyle("\"top\"", "\"" + widget.get("y") + "px\""); + } + if(keySet.contains("width")) { + button.setStyle("\"width\"", "\"" + widget.get("width") + "px\""); + } + if(keySet.contains("height")) { + button.setStyle("\"height\"", "\"" + widget.get("height") + "px\""); + } + //ボタンが押されたり離されたりした時を検知 ResourcePath resPath = mouseEventChannel.getOutputResources().iterator().next(); button.setRestEventListener(new HtmlElementMouseSender(simulator, mouseEventChannel, resPath, widgetResource)); @@ -162,10 +232,25 @@ addElements.append(button.toMap()); } - private void addLabel(JsonTerm widget, String wid, ListTerm addElements, Resource widgetResource) { - String text = widget.get("\"text\"").toString(); + private void addLabel(MapTerm widget, String wid, ListTerm addElements, Resource widgetResource) { + String text = widget.get("text").toString(); HtmlElement label = new HtmlElement("label", wid, text); + Set keySet = widget.keySet(); + label.setStyle("\"position\"", "\"absolute\""); + if(keySet.contains("x")) { + label.setStyle("\"left\"", "\"" + widget.get("x") + "px\""); + } + if(keySet.contains("y")) { + label.setStyle("\"top\"", "\"" + widget.get("y") + "px\""); + } + if(keySet.contains("width")) { + label.setStyle("\"width\"", "\"" + widget.get("width") + "px\""); + } + if(keySet.contains("height")) { + label.setStyle("\"height\"", "\"" + widget.get("height") + "px\""); + } + //ボタンが透明になったことを通知 HtmlElementVisibilityReceiver nativeVisibilityReceiver = new HtmlElementVisibilityReceiver(label, ws); simulator.addNativeReceiver(nativeVisibilityReceiver, setVisibleChannel, widgetResource); @@ -190,10 +275,25 @@ addElements.append(label.toMap()); } - private void addTextInput(JsonTerm widget, String wid, ListTerm addElements, Resource widgetResource) { - String text = widget.get("\"text\"").toString(); + private void addTextInput(MapTerm widget, String wid, ListTerm addElements, Resource widgetResource) { + String text = widget.get("text").toString(); HtmlElement textInput = new HtmlElement("textInput", wid, text); + Set keySet = widget.keySet(); + textInput.setStyle("\"position\"", "\"absolute\""); + if(keySet.contains("x")) { + textInput.setStyle("\"left\"", "\"" + widget.get("x") + "px\""); + } + if(keySet.contains("y")) { + textInput.setStyle("\"top\"", "\"" + widget.get("y") + "px\""); + } + if(keySet.contains("width")) { + textInput.setStyle("\"width\"", "\"" + widget.get("width") + "px\""); + } + if(keySet.contains("height")) { + textInput.setStyle("\"height\"", "\"" + widget.get("height") + "px\""); + } + //文字の変更を検知 ResourcePath resPath = textEventChannel.getOutputResources().iterator().next(); textInput.setRestEventListener(new HtmlElementTextSender(simulator, textEventChannel, resPath, widgetResource));