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 { ...@@ -62,6 +62,12 @@ public class HtmlTemplate implements VariableBased {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
public boolean dependsOnVariables() {
return !this.identifiers.isEmpty();
}
// ---------------------------------------------------------------------------
public String render() { public String render() {
return Interpolator.render(chunks); return Interpolator.render(chunks);
} }
......
...@@ -17,14 +17,9 @@ import lu.list.itis.dkd.tui.cps.InputChangeListener; ...@@ -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.InputEvent;
import lu.list.itis.dkd.tui.cps.system.VariableBased; import lu.list.itis.dkd.tui.cps.system.VariableBased;
import lu.list.itis.dkd.tui.cps.variable.Variable; 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.HtmlTemplate;
import lu.list.itis.dkd.tui.utility.interpolation.Interpolator;
import lu.list.itis.dkd.tui.widget.corona.builder.BaseVariableHtmlBoxBuilder; 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.List;
import java.util.Map; import java.util.Map;
...@@ -41,10 +36,7 @@ import java.util.Map; ...@@ -41,10 +36,7 @@ import java.util.Map;
public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChangeListener { public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChangeListener {
private String htmlTemplate; private HtmlTemplate htmlTemplate;
private List<Chunk<?>> htmlChunks;
private List<String> identifiers;
private Map<String, Variable<?>> parameters;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// *************************************************************************** // ***************************************************************************
...@@ -58,16 +50,9 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan ...@@ -58,16 +50,9 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
public VariableHtmlBox(BaseVariableHtmlBoxBuilder<?> builder) { public VariableHtmlBox(BaseVariableHtmlBoxBuilder<?> builder) {
super(builder); super(builder);
htmlTemplate = builder.htmlTemplate; htmlTemplate = new HtmlTemplate(builder.htmlTemplate);
if (!htmlTemplate.dependsOnVariables()) {
this.parameters = new HashMap<>(); this.setInformation(builder.htmlTemplate);
if (!Strings.isNullOrEmpty(this.htmlTemplate)) {
identifiers = Interpolator.extractVariableIdentifiers(this.htmlTemplate);
if (identifiers.isEmpty()) {
this.htmlContent = this.htmlTemplate;
this.setInformation(this.htmlContent);
}
} }
} }
...@@ -87,21 +72,7 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan ...@@ -87,21 +72,7 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
@Override @Override
public List<Variable<?>> connectWithSystemVariables(Map<String, Variable<?>> systemVariables) { public List<Variable<?>> connectWithSystemVariables(Map<String, Variable<?>> systemVariables) {
List<Variable<?>> connected = new ArrayList<>(); return htmlTemplate.connectWithSystemVariables(systemVariables);
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;
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
...@@ -110,9 +81,9 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan ...@@ -110,9 +81,9 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
@Override @Override
public void inputChanged(InputEvent input) { public void inputChanged(InputEvent input) {
if (parameters.containsKey(input.getSource().getName())) { String identifier = input.getSource().getName();
this.htmlContent = Interpolator.render(htmlChunks); if (htmlTemplate.dependsOn(identifier)) {
this.setInformation(this.htmlContent); this.setInformation(htmlTemplate.render());
} }
} }
...@@ -122,11 +93,7 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan ...@@ -122,11 +93,7 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
@Override @Override
public List<Variable<?>> getDeclaredVariables() { public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>(); return htmlTemplate.getDeclaredVariables();
for (Variable<?> parameter : this.parameters.values()) {
declared.add(parameter);
}
return declared;
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
......
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