Commit 764c0e36 authored by Nico Mack's avatar Nico Mack

Modified VariableHtmlBox to make use of HtmlTemplate helper class.

parent 594515b2
......@@ -62,6 +62,12 @@ public class HtmlTemplate implements VariableBased {
// ---------------------------------------------------------------------------
public boolean dependsOnVariables() {
return !this.identifiers.isEmpty();
}
// ---------------------------------------------------------------------------
public String render() {
return Interpolator.render(chunks);
}
......
......@@ -17,14 +17,9 @@ import lu.list.itis.dkd.tui.cps.InputChangeListener;
import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.system.VariableBased;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import lu.list.itis.dkd.tui.utility.interpolation.Chunk;
import lu.list.itis.dkd.tui.utility.interpolation.Interpolator;
import lu.list.itis.dkd.tui.utility.HtmlTemplate;
import lu.list.itis.dkd.tui.widget.corona.builder.BaseVariableHtmlBoxBuilder;
import org.python.google.common.base.Strings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -41,10 +36,7 @@ import java.util.Map;
public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChangeListener {
private String htmlTemplate;
private List<Chunk<?>> htmlChunks;
private List<String> identifiers;
private Map<String, Variable<?>> parameters;
private HtmlTemplate htmlTemplate;
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -58,16 +50,9 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
public VariableHtmlBox(BaseVariableHtmlBoxBuilder<?> builder) {
super(builder);
htmlTemplate = builder.htmlTemplate;
this.parameters = new HashMap<>();
if (!Strings.isNullOrEmpty(this.htmlTemplate)) {
identifiers = Interpolator.extractVariableIdentifiers(this.htmlTemplate);
if (identifiers.isEmpty()) {
this.htmlContent = this.htmlTemplate;
this.setInformation(this.htmlContent);
}
htmlTemplate = new HtmlTemplate(builder.htmlTemplate);
if (!htmlTemplate.dependsOnVariables()) {
this.setInformation(builder.htmlTemplate);
}
}
......@@ -87,21 +72,7 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
@Override
public List<Variable<?>> connectWithSystemVariables(Map<String, Variable<?>> systemVariables) {
List<Variable<?>> connected = new ArrayList<>();
for (String identifier : identifiers) {
if ((systemVariables != null) && systemVariables.containsKey(identifier)) {
Variable<?> variable = systemVariables.get(identifier);
this.parameters.put(identifier, variable);
connected.add(variable);
variable.addListener(this);
}
}
if (!connected.isEmpty()) {
htmlChunks = Interpolator.decompose(this.htmlTemplate, this.parameters);
}
return connected;
return htmlTemplate.connectWithSystemVariables(systemVariables);
}
// ---------------------------------------------------------------------------
......@@ -110,9 +81,9 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
@Override
public void inputChanged(InputEvent input) {
if (parameters.containsKey(input.getSource().getName())) {
this.htmlContent = Interpolator.render(htmlChunks);
this.setInformation(this.htmlContent);
String identifier = input.getSource().getName();
if (htmlTemplate.dependsOn(identifier)) {
this.setInformation(htmlTemplate.render());
}
}
......@@ -122,11 +93,7 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
for (Variable<?> parameter : this.parameters.values()) {
declared.add(parameter);
}
return declared;
return htmlTemplate.getDeclaredVariables();
}
// ---------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment