refactored condition management
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -10,6 +9,7 @@ import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
|
||||
@@ -19,8 +19,17 @@ public abstract class Condition implements Constants {
|
||||
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
protected int id;
|
||||
|
||||
public Condition() {
|
||||
this(Application.createId());
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params) {
|
||||
public Condition(int id) {
|
||||
this.id = id;
|
||||
conditions.put(id, this);
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params,Plan plan) {
|
||||
if (!params.containsKey(ID)) return t("No id passed to Condition.action!");
|
||||
int cid = Integer.parseInt(params.get(ID));
|
||||
Condition condition = conditions.get(cid);
|
||||
@@ -31,39 +40,29 @@ public abstract class Condition implements Constants {
|
||||
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return condition.properties();
|
||||
return condition.properties(params);
|
||||
case ACTION_UPDATE:
|
||||
return condition.update(params);
|
||||
condition.update(params);
|
||||
return plan.showContext(params);
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
}
|
||||
|
||||
protected abstract Window properties();
|
||||
|
||||
|
||||
public Condition() {
|
||||
this(new Date().hashCode());
|
||||
}
|
||||
|
||||
public Condition(int id) {
|
||||
this.id = id;
|
||||
conditions.put(id, this);
|
||||
}
|
||||
|
||||
public Tag link(String tagClass) {
|
||||
String json = new JSONObject(Map.of(REALM,REALM_CONDITION,ID,id,ACTION,ACTION_PROPS)).toString().replace("\"", "'");
|
||||
public Tag link(String tagClass,String context) {
|
||||
String json = new JSONObject(Map.of(REALM,REALM_CONDITION,ID,id,ACTION,ACTION_PROPS,CONTEXT,context)).toString().replace("\"", "'");
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(toString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("invalid condition");
|
||||
}
|
||||
protected abstract Window properties(HashMap<String, String> params);
|
||||
|
||||
public static String t(String text, Object...fills) {
|
||||
return Translation.get(Application.class, text, fills);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("invalid condition");
|
||||
}
|
||||
|
||||
protected abstract Object update(HashMap<String, String> params);
|
||||
}
|
||||
|
||||
@@ -22,13 +22,14 @@ public class TrainSelect extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties() {
|
||||
protected Window properties(HashMap<String, String> params) {
|
||||
Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName()));
|
||||
String formId = "conditional-props-"+id;
|
||||
Form form = new Form(formId);
|
||||
new Input(REALM,REALM_CONDITION).hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(ID,id).hideIn(form);
|
||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||
Train.selector(train, null).addTo(new Label(t("Select train:")+NBSP)).addTo(form);
|
||||
new Button(t("Save"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
||||
return win;
|
||||
|
||||
Reference in New Issue
Block a user