more refactoring
This commit is contained in:
@@ -16,9 +16,9 @@ import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
|
||||
public abstract class Condition extends BaseClass {
|
||||
@@ -40,48 +40,36 @@ public abstract class Condition extends BaseClass {
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) return t("No action passed to Condition.action!");
|
||||
|
||||
Id cid = Id.from(params);
|
||||
|
||||
if (isSet(cid)) {
|
||||
Condition condition = BaseClass.get(cid);
|
||||
if (isNull(condition)) return t("No condition with id {}!",cid);
|
||||
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return condition.properties();
|
||||
case ACTION_UPDATE:
|
||||
condition.update(params);
|
||||
return condition.parent().properties();
|
||||
case ACTION_DROP:
|
||||
condition.remove();
|
||||
return condition.parent().properties();
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
}
|
||||
Id id = Id.from(params);
|
||||
Condition condition = BaseClass.get(id);
|
||||
|
||||
switch (action) {
|
||||
case ACTION_ADD:
|
||||
return addCondition(params);
|
||||
|
||||
case ACTION_DROP:
|
||||
BaseClass context = condition.context();
|
||||
condition.remove();
|
||||
return context.properties();
|
||||
case ACTION_PROPS:
|
||||
return condition.properties();
|
||||
case ACTION_UPDATE:
|
||||
condition.update(params);
|
||||
return condition.context().properties();
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
|
||||
}
|
||||
|
||||
private static Object addCondition(HashMap<String, String> params) {
|
||||
String type = params.get(REALM_CONDITION);
|
||||
if (isNull(type)) return t("No type supplied to addCondition!");
|
||||
|
||||
Id parentId = Id.from(params, PARENT);
|
||||
Id parentId = Id.from(params);
|
||||
if (isNull(parentId)) return t("No parent id supplied to addCondition");
|
||||
|
||||
BaseClass parent = BaseClass.get(parentId);
|
||||
if (isNull(parent)) return t("No condition with id {} found!",parentId);
|
||||
ConditionList conditionList = BaseClass.get(parentId);
|
||||
if (isNull(conditionList)) return t("No condition list with id {} found!",parentId);
|
||||
|
||||
Condition condition = Condition.create(type);
|
||||
if (isNull(condition)) return t("Unknown type \"{}\" of condition!",type);
|
||||
|
||||
return condition.parent(parent).properties();
|
||||
return conditionList.add(Condition.create(type)).properties();
|
||||
}
|
||||
|
||||
public static Condition create(String type) {
|
||||
@@ -94,6 +82,14 @@ public abstract class Condition extends BaseClass {
|
||||
return null;
|
||||
}
|
||||
|
||||
public BaseClass context() {
|
||||
BaseClass context = this;
|
||||
while (isSet(context.parent()) && (context instanceof Condition || context instanceof Action)) {
|
||||
context = context.parent();
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
|
||||
public JSONObject json() {
|
||||
@@ -137,7 +133,7 @@ public abstract class Condition extends BaseClass {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
public static Tag selector() {
|
||||
public static Select selector() {
|
||||
Select select = new Select(REALM_CONDITION);
|
||||
TreeMap<String, String> names = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
@@ -147,7 +143,7 @@ public abstract class Condition extends BaseClass {
|
||||
}
|
||||
|
||||
for (Entry<String, String> entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey());
|
||||
return select.addTo(new Label(t("Condition type:")+NBSP));
|
||||
return select;
|
||||
}
|
||||
|
||||
public static String t(String text, Object...fills) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -9,6 +10,7 @@ import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
@@ -18,8 +20,10 @@ public class ConditionList extends Condition implements Iterable<Condition>{
|
||||
|
||||
private Vector<Condition> conditions = new Vector<Condition>();
|
||||
|
||||
public void add(Condition condition) {
|
||||
public ConditionList add(Condition condition) {
|
||||
conditions.add(condition);
|
||||
condition.parent(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void addAll(ConditionList conditions) {
|
||||
@@ -34,6 +38,10 @@ public class ConditionList extends Condition implements Iterable<Condition>{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected String glue() {
|
||||
return t("and");
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return conditions.isEmpty();
|
||||
}
|
||||
@@ -62,14 +70,26 @@ public class ConditionList extends Condition implements Iterable<Condition>{
|
||||
public Fieldset list(String caption) {
|
||||
Fieldset fieldset = new Fieldset(t("Conditions"));
|
||||
if (caption != null) new Tag("p").content(caption).addTo(fieldset);
|
||||
Tag list = new Tag("ul");
|
||||
newConditionForm().addTo(new Tag("li")).addTo(list);
|
||||
conditions.forEach(condition -> condition.link("li", condition).addTo(list));
|
||||
list.addTo(fieldset);
|
||||
listInternal().addTo(fieldset);
|
||||
return fieldset;
|
||||
}
|
||||
|
||||
private Tag listInternal() {
|
||||
Tag list = new Tag("ul");
|
||||
for (Condition condition : conditions) {
|
||||
Tag item = new Tag("li");
|
||||
condition.link("span", condition).addTo(item);
|
||||
condition.button(t("delete"), Map.of(ACTION,ACTION_DROP)).addTo(item.content(NBSP)).addTo(list);
|
||||
if (condition instanceof ConditionList) {
|
||||
((ConditionList)condition).listInternal().addTo(item);
|
||||
}
|
||||
}
|
||||
newConditionForm().addTo(new Tag("li")).addTo(list);
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
public void load(JSONArray arr) {
|
||||
public ConditionList load(JSONArray arr) {
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
@@ -78,18 +98,26 @@ public class ConditionList extends Condition implements Iterable<Condition>{
|
||||
conditions.add(condition.load(json));
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private Form newConditionForm() {
|
||||
Form form = new Form("add-condition-form");
|
||||
Form form = new Form("new-condition-form-"+id());
|
||||
new Input(REALM, REALM_CONDITION).hideIn(form);
|
||||
new Input(ACTION,ACTION_ADD).hideIn(form);
|
||||
new Input(PARENT,id());
|
||||
new Input(ID,id()).hideIn(form);
|
||||
Condition.selector().addTo(form);
|
||||
new Button(t("Add condition"), form).addTo(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Window properties() {
|
||||
BaseClass parent = parent();
|
||||
if (isSet(parent)) return parent.properties();
|
||||
return super.properties();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseClass remove() {
|
||||
super.remove();
|
||||
@@ -110,4 +138,13 @@ public class ConditionList extends Condition implements Iterable<Condition>{
|
||||
public Stream<Condition> stream() {
|
||||
return conditions.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (conditions.isEmpty()) return "["+t("Click here to add conditions")+"]";
|
||||
StringBuffer sb = new StringBuffer(conditions.firstElement().toString());
|
||||
String glue = glue();
|
||||
for (int i=1; i<conditions.size(); i++) sb.append(" ").append(glue).append(" "+conditions.get(i));
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class OrCondition extends ConditionList{
|
||||
|
||||
@Override
|
||||
@@ -11,9 +9,9 @@ public class OrCondition extends ConditionList{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return isEmpty() ? t("Click here to select conditions") : String.join(" "+t("OR")+" ", stream().map(Object::toString).collect(Collectors.toList()));
|
||||
}
|
||||
protected String glue() {
|
||||
return t("or");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class TrainHasTag extends Condition {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (tag == null) return t("[Click to setup tag]");
|
||||
if (tag == null) return "["+t("Click to setup tag")+"]";
|
||||
return t(inverted ? "train does not have tag \"{}\"" : "train has tag \"{}\"",tag) ;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ public class TrainSelect extends Condition {
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select train"),Train.selector(train, null));
|
||||
formInputs.add(t("Select train")+":",Train.selector(train, null));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public class TrainSelect extends Condition {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (train == null) return t("[Click here to select train!]");
|
||||
if (train == null) return "["+t("Click here to select train!")+"]";
|
||||
return t("Train")+ (inverted?"≠":"=") + train;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user