overhauled registry
This commit is contained in:
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
@@ -13,6 +14,11 @@ public class BlockFree extends Condition {
|
||||
|
||||
private static final String BLOCK = Block.class.getSimpleName();
|
||||
private Block block;
|
||||
|
||||
private BlockFree block(Block block) {
|
||||
this.block = block;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
@@ -35,17 +41,17 @@ public class BlockFree extends Condition {
|
||||
formInputs.add(t("Select block"), Block.selector(block, null));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == block) block = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (block == null) return t("[Click here to select block!]");
|
||||
return t(inverted ? "Block {} is occupied":"Block {} is free",block);
|
||||
}
|
||||
|
||||
private BlockFree block(Block block) {
|
||||
this.block = block;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,9 +15,7 @@ import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.ConditionalAction;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
@@ -27,9 +25,7 @@ public abstract class Condition extends BaseClass {
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Condition.class);
|
||||
private static final String INVERTED = "inverted";
|
||||
private static final String PREFIX = Condition.class.getPackageName();
|
||||
private static HashMap<Id, Condition> conditions = new HashMap<Id, Condition>();
|
||||
public boolean inverted = false;
|
||||
private Object parent;
|
||||
|
||||
public Condition() {
|
||||
this(new Id());
|
||||
@@ -37,7 +33,7 @@ public abstract class Condition extends BaseClass {
|
||||
|
||||
public Condition(Id id) {
|
||||
this.id = id;
|
||||
conditions.put(id, this);
|
||||
register();
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params,Plan plan) {
|
||||
@@ -47,18 +43,18 @@ public abstract class Condition extends BaseClass {
|
||||
Id cid = Id.from(params);
|
||||
|
||||
if (isSet(cid)) {
|
||||
Condition condition = conditions.get(cid);
|
||||
if (condition == null) return t("No condition with id {}!",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 plan.showContext(params);
|
||||
return condition.parent().properties();
|
||||
case ACTION_DROP:
|
||||
condition.drop();
|
||||
return plan.showContext(params);
|
||||
condition.remove();
|
||||
return condition.parent().properties();
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
}
|
||||
@@ -74,27 +70,18 @@ public abstract class Condition extends BaseClass {
|
||||
|
||||
private static Object addCondition(HashMap<String, String> params) {
|
||||
String type = params.get(REALM_CONDITION);
|
||||
String context = params.get(CONTEXT);
|
||||
if (isNull(type)) return t("No type supplied to addCondition!");
|
||||
if (isNull(context)) return t("No context supplied to addCondtion!");
|
||||
|
||||
Id parentId = Id.from(params, PARENT);
|
||||
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);
|
||||
|
||||
Condition condition = Condition.create(type);
|
||||
if (isNull(condition)) return t("Unknown type \"{}\" of condition!",type);
|
||||
String[] parts = context.split(":");
|
||||
Id contextId = new Id(parts[1]);
|
||||
String realm = parts[0];
|
||||
switch (realm) {
|
||||
case REALM_ROUTE:
|
||||
Route route = plan.route(contextId);
|
||||
if (isNull(route)) return t("Unknown route: {}",contextId);
|
||||
route.add(condition);
|
||||
return route.properties();
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return t("Cannot handle context of type {} in addCondition!");
|
||||
return condition.parent(parent).properties();
|
||||
}
|
||||
|
||||
public static Condition create(String type) {
|
||||
@@ -107,18 +94,6 @@ public abstract class Condition extends BaseClass {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void drop() {
|
||||
if (parent instanceof ConditionalAction) {
|
||||
ConditionalAction ca = (ConditionalAction) parent;
|
||||
ca.remove(this);
|
||||
}
|
||||
if (parent instanceof Route) {
|
||||
Route route = (Route) parent;
|
||||
route.remove(this);
|
||||
}
|
||||
conditions.remove(this.id());
|
||||
}
|
||||
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
|
||||
public JSONObject json() {
|
||||
@@ -186,11 +161,6 @@ public abstract class Condition extends BaseClass {
|
||||
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
inverted = "on".equals(params.get(INVERTED));
|
||||
return t("updated {}.",this);
|
||||
}
|
||||
|
||||
public Condition parent(Object parent) {
|
||||
this.parent = parent;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,57 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass.Context;
|
||||
import de.srsoftware.web4rail.BaseClass.Id;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class ConditionList extends Vector<Condition> implements Constants{
|
||||
public class ConditionList extends Condition implements Iterable<Condition>{
|
||||
|
||||
private static final long serialVersionUID = 5826717120751473807L;
|
||||
private Vector<Condition> conditions = new Vector<Condition>();
|
||||
|
||||
public void add(Condition condition) {
|
||||
conditions.add(condition);
|
||||
}
|
||||
|
||||
public void addAll(ConditionList conditions) {
|
||||
this.conditions.addAll(conditions.conditions);
|
||||
}
|
||||
|
||||
|
||||
public boolean fulfilledBy(Context context) {
|
||||
for (Condition condition : this) {
|
||||
for (Condition condition : conditions) {
|
||||
if (!condition.fulfilledBy(context)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return conditions.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Condition> iterator() {
|
||||
return conditions.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
String cls = getClass().getSimpleName();
|
||||
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
|
||||
}
|
||||
|
||||
public JSONArray json() {
|
||||
public JSONArray jsonArray() {
|
||||
JSONArray json = new JSONArray();
|
||||
for (Condition condition : this) json.put(condition.json());
|
||||
for (Condition condition : conditions) json.put(condition.json());
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -42,39 +64,43 @@ public class ConditionList extends Vector<Condition> implements Constants{
|
||||
if (caption != null) new Tag("p").content(caption).addTo(fieldset);
|
||||
Tag list = new Tag("ul");
|
||||
newConditionForm().addTo(new Tag("li")).addTo(list);
|
||||
forEach(condition -> condition.link("li", condition).addTo(list));
|
||||
conditions.forEach(condition -> condition.link("li", condition).addTo(list));
|
||||
list.addTo(fieldset);
|
||||
return fieldset;
|
||||
}
|
||||
|
||||
public void load(JSONArray arr) {
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
if (condition != null) {
|
||||
condition.parent(this);
|
||||
conditions.add(condition.load(json));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Form newConditionForm() {
|
||||
Form form = new Form("add-condition-form");
|
||||
new Input(REALM, REALM_CONDITION).hideIn(form);
|
||||
new Input(ACTION,ACTION_ADD).hideIn(form);
|
||||
// new Input(CONTEXT,context).hideIn(form); TODO: add context
|
||||
new Input(PARENT,id());
|
||||
Condition.selector().addTo(form);
|
||||
new Button(t("Add condition"), form).addTo(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
public void load(JSONArray arr) {
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
if (condition != null) add(condition.parent(this).load(json));
|
||||
}
|
||||
|
||||
public boolean remove(Object condition) {
|
||||
return conditions.remove(condition);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
conditions.remove(child);
|
||||
}
|
||||
|
||||
public void removeById(Id cid) {
|
||||
for (Condition condition : this) {
|
||||
if (condition.id().equals(cid)) {
|
||||
remove(condition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String t(String tx, Object...fills) {
|
||||
return Translation.get(Application.class, tx, fills);
|
||||
public Stream<Condition> stream() {
|
||||
return conditions.stream();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,19 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
|
||||
public class OrCondition extends Condition{
|
||||
public class OrCondition extends ConditionList{
|
||||
|
||||
private static final String CONDITIONS = "conditions";
|
||||
private ConditionList conditions = new ConditionList();
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
for (Condition condition : conditions) {
|
||||
for (Condition condition : this) {
|
||||
if (condition.fulfilledBy(context)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(CONDITIONS, conditions.json());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(CONDITIONS)) conditions.load(json.getJSONArray(CONDITIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
// add conditions
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return conditions.isEmpty() ? t("Click here to select conditions") : String.join(" "+t("OR")+" ", conditions.stream().map(Object::toString).collect(Collectors.toList()));
|
||||
return isEmpty() ? t("Click here to select conditions") : String.join(" "+t("OR")+" ", stream().map(Object::toString).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class PushPullTrain extends Condition {
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
return context.train().pushPull != inverted;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
@@ -37,6 +38,11 @@ public class TrainHasTag extends Condition {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (tag == null) return t("[Click to setup tag]");
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.List;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
@@ -37,7 +38,12 @@ public class TrainLength extends Condition {
|
||||
formInputs.add(t("Maximum train length"),new Input(MAX_LENGTH, maxLength).numeric());
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t(inverted ? "train is longer than {}" : "train is shorter than {}",maxLength) ;
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -36,6 +37,11 @@ public class TrainSelect extends Condition {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == train) train = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (train == null) return t("[Click here to select train!]");
|
||||
|
||||
Reference in New Issue
Block a user