working on conditional actions
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
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.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
|
||||
public abstract class Condition implements Constants {
|
||||
|
||||
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>();
|
||||
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
protected int id;
|
||||
|
||||
public static Object action(HashMap<String, String> params) {
|
||||
if (!params.containsKey(ID)) return t("No id passed to Condition.action!");
|
||||
int cid = Integer.parseInt(params.get(ID));
|
||||
Condition condition = conditions.get(cid);
|
||||
if (condition == null) return t("No condition with id {}!",cid);
|
||||
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) return t("No action passed to Condition.action!");
|
||||
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return condition.properties();
|
||||
case ACTION_UPDATE:
|
||||
return condition.update(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("\"", "'");
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(toString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("invalid condition");
|
||||
}
|
||||
|
||||
public static String t(String text, Object...fills) {
|
||||
return Translation.get(Application.class, text, fills);
|
||||
}
|
||||
|
||||
protected abstract Object update(HashMap<String, String> params);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class TrainSelect extends Condition {
|
||||
|
||||
private static final Object TRAIN = Train.class.getSimpleName();
|
||||
private Train train;
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties() {
|
||||
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);
|
||||
Train.selector(train, null).addTo(new Label(t("Select train:")+NBSP)).addTo(form);
|
||||
new Button(t("Save")).addTo(form).addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (train == null) return super.toString();
|
||||
return t("Train = {}",train);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (!params.containsKey(TRAIN)) return t("No train id passed to TrainSelect.update()!");
|
||||
int tid = Integer.parseInt(params.get(TRAIN));
|
||||
Train train = Train.get(tid);
|
||||
if (train == null) return t("No train with id {} found!",tid);
|
||||
this.train = train;
|
||||
return t("Updated condition");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user