refactored condition management
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.srsoftware</groupId>
|
<groupId>de.srsoftware</groupId>
|
||||||
<artifactId>web4rail</artifactId>
|
<artifactId>web4rail</artifactId>
|
||||||
<version>0.7.10</version>
|
<version>0.7.11</version>
|
||||||
<name>Web4Rail</name>
|
<name>Web4Rail</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<description>Java Model Railway Control</description>
|
<description>Java Model Railway Control</description>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import java.net.InetSocketAddress;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@@ -54,6 +55,15 @@ public class Application implements Constants{
|
|||||||
Desktop.getDesktop().browse(URI.create("http://"+InetAddress.getLocalHost().getHostName()+":"+config.getInt(PORT)+"/plan"));
|
Desktop.getDesktop().browse(URI.create("http://"+InetAddress.getLocalHost().getHostName()+":"+config.getInt(PORT)+"/plan"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int createId() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return new Date().hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
private static Object handle(HashMap<String, String> params) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
private static Object handle(HashMap<String, String> params) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||||
|
|
||||||
String realm = params.get(REALM);
|
String realm = params.get(REALM);
|
||||||
@@ -64,11 +74,11 @@ public class Application implements Constants{
|
|||||||
|
|
||||||
switch (realm) {
|
switch (realm) {
|
||||||
case REALM_ACTIONS:
|
case REALM_ACTIONS:
|
||||||
return ActionList.process(params);
|
return ActionList.process(params,plan);
|
||||||
case REALM_CAR:
|
case REALM_CAR:
|
||||||
return Car.action(params);
|
return Car.action(params);
|
||||||
case REALM_CONDITION:
|
case REALM_CONDITION:
|
||||||
return Condition.action(params);
|
return Condition.action(params,plan);
|
||||||
case REALM_CU:
|
case REALM_CU:
|
||||||
return plan.controlUnit().process(params);
|
return plan.controlUnit().process(params);
|
||||||
case REALM_LOCO:
|
case REALM_LOCO:
|
||||||
|
|||||||
@@ -45,4 +45,5 @@ public interface Constants {
|
|||||||
public static final String CONTACT = "contact";
|
public static final String CONTACT = "contact";
|
||||||
public static final String TYPE = "type";
|
public static final String TYPE = "type";
|
||||||
public static final String NBSP = " ";
|
public static final String NBSP = " ";
|
||||||
|
public static final String CONTEXT = "context";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -448,6 +448,17 @@ public class Plan implements Constants{
|
|||||||
tile.position(x, y).plan(this);
|
tile.position(x, y).plan(this);
|
||||||
tiles.put(tile.id(),tile);
|
tiles.put(tile.id(),tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Window showContext(HashMap<String, String> params) {
|
||||||
|
String[] parts = params.get(CONTEXT).split(":");
|
||||||
|
String realm = parts[0];
|
||||||
|
String id = parts.length>1 ? parts[1] : null;
|
||||||
|
switch (realm) {
|
||||||
|
case REALM_ROUTE:
|
||||||
|
return route(Integer.parseInt(id)).properties();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void stream(String data) {
|
public synchronized void stream(String data) {
|
||||||
data = data.replaceAll("\n", "").replaceAll("\r", "");
|
data = data.replaceAll("\n", "").replaceAll("\r", "");
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ public class Route implements Constants{
|
|||||||
actions = new ActionList();
|
actions = new ActionList();
|
||||||
triggers.put(c.trigger(), actions);
|
triggers.put(c.trigger(), actions);
|
||||||
}
|
}
|
||||||
actions.addTo(link);
|
actions.addTo(link,REALM_ROUTE+":"+id());
|
||||||
}
|
}
|
||||||
list.addTo(win);
|
list.addTo(win);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ package de.srsoftware.web4rail.actions;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import de.keawe.tools.translations.Translation;
|
import de.keawe.tools.translations.Translation;
|
||||||
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Application;
|
import de.srsoftware.web4rail.Application;
|
||||||
import de.srsoftware.web4rail.Constants;
|
import de.srsoftware.web4rail.Constants;
|
||||||
import de.srsoftware.web4rail.Plan;
|
import de.srsoftware.web4rail.Plan;
|
||||||
@@ -20,7 +21,7 @@ import de.srsoftware.web4rail.tiles.Contact;
|
|||||||
|
|
||||||
public abstract class Action implements Constants {
|
public abstract class Action implements Constants {
|
||||||
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||||
private int id;
|
protected int id;
|
||||||
|
|
||||||
public static class Context {
|
public static class Context {
|
||||||
public Plan plan = null;
|
public Plan plan = null;
|
||||||
@@ -37,7 +38,7 @@ public abstract class Action implements Constants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Action() {
|
public Action() {
|
||||||
id = new Date().hashCode();
|
id = Application.createId();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract boolean fire(Context context) throws IOException;
|
public abstract boolean fire(Context context) throws IOException;
|
||||||
@@ -46,18 +47,18 @@ public abstract class Action implements Constants {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put(TYPE, getClass().getSimpleName());
|
json.put(TYPE, getClass().getSimpleName());
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
protected Tag link(int actionId, String context) {
|
||||||
public String toString() {
|
Map<String, String> props = Map.of(REALM,REALM_ACTIONS,ID,actionId+"/"+id,ACTION,ACTION_PROPS,CONTEXT,context);
|
||||||
return getClass().getSimpleName();
|
String action = "request("+(new JSONObject(props).toString().replace("\"", "'"))+")";
|
||||||
|
return new Tag("span").content(toString()).attr("onclick", action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action load(JSONObject json) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
|
public static Action load(JSONObject json) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
|
||||||
String clazz = json.getString(TYPE);
|
String clazz = json.getString(TYPE);
|
||||||
switch (clazz) {
|
switch (clazz) {
|
||||||
@@ -75,11 +76,20 @@ public abstract class Action implements Constants {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Window propForm(HashMap<String, String> params) {
|
public Window properties(HashMap<String, String> params) {
|
||||||
return new Window("action-props", "Action properties");
|
return new Window("action-props-"+id, t("Properties of {}",this.getClass().getSimpleName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static String t(String tex,Object...fills) {
|
protected static String t(String tex,Object...fills) {
|
||||||
return Translation.get(Application.class, tex, fills);
|
return Translation.get(Application.class, tex, fills);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getClass().getSimpleName();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Object update(HashMap<String, String> params) {
|
||||||
|
return t("Nothing changed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package de.srsoftware.web4rail.actions;
|
package de.srsoftware.web4rail.actions;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -14,6 +13,7 @@ import de.keawe.tools.translations.Translation;
|
|||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Application;
|
import de.srsoftware.web4rail.Application;
|
||||||
import de.srsoftware.web4rail.Constants;
|
import de.srsoftware.web4rail.Constants;
|
||||||
|
import de.srsoftware.web4rail.Plan;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
import de.srsoftware.web4rail.actions.Action.Context;
|
import de.srsoftware.web4rail.actions.Action.Context;
|
||||||
import de.srsoftware.web4rail.tags.Button;
|
import de.srsoftware.web4rail.tags.Button;
|
||||||
@@ -30,48 +30,78 @@ public class ActionList extends Vector<Action> implements Constants{
|
|||||||
private static final HashMap<Integer, ActionList> actionLists = new HashMap<Integer, ActionList>();
|
private static final HashMap<Integer, ActionList> actionLists = new HashMap<Integer, ActionList>();
|
||||||
|
|
||||||
public ActionList() {
|
public ActionList() {
|
||||||
id = new Date().hashCode();
|
id = Application.createId();
|
||||||
actionLists.put(id,this);
|
actionLists.put(id,this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fire(Context context) {
|
private static Integer actionId(HashMap<String, String> params) {
|
||||||
LOG.debug("Firing {}",this);
|
if (!params.containsKey(ID)) return null;
|
||||||
|
String[] parts = params.get(ID).split("/");
|
||||||
for (Action action : this) {
|
if (parts.length<2) return null;
|
||||||
try {
|
return Integer.parseInt(parts[1]);
|
||||||
action.fire(context);
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOG.warn("Action did not fire properly: {}",action,e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean drop(int actionId) {
|
private static Integer actionListId(HashMap<String, String> params) {
|
||||||
for (Action action : this) {
|
if (!params.containsKey(ID)) return null;
|
||||||
if (action.id() == actionId) {
|
String[] parts = params.get(ID).split("/");
|
||||||
this.remove(action);
|
return Integer.parseInt(parts[0]);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean moveUp(int actionId) {
|
private Object actionTypeForm(Window win, String context) {
|
||||||
for (int i=1; i<size(); i++) {
|
String formId ="add-action-to-"+id;
|
||||||
if (actionId == elementAt(i).id()) {
|
Tag typeForm = new Form(formId);
|
||||||
Action action = remove(i);
|
new Input(REALM, REALM_ACTIONS).hideIn(typeForm);
|
||||||
insertElementAt(action, i-1);
|
new Input(ID,id).hideIn(typeForm);
|
||||||
return true;
|
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
|
||||||
}
|
new Input(CONTEXT,context).hideIn(typeForm);
|
||||||
}
|
Select select = new Select(TYPE);
|
||||||
return false;
|
List<Class<? extends Action>> classes = List.of(
|
||||||
|
SpeedReduction.class,
|
||||||
|
SetSignalsToStop.class,
|
||||||
|
FinishRoute.class,
|
||||||
|
TurnTrain.class,
|
||||||
|
ConditionalAction.class);
|
||||||
|
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||||
|
select.addTo(new Label("Action type:")).addTo(typeForm);
|
||||||
|
return new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(typeForm).addTo(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTo(Tag link) {
|
private Object addActionForm(HashMap<String, String> params, Plan plan) {
|
||||||
|
Window win = new Window("add-action-form", t("Add action to action list"));
|
||||||
|
String type = params.get(TYPE);
|
||||||
|
String context = params.get(CONTEXT);
|
||||||
|
if (type == null) return actionTypeForm(win,context);
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case "ConditionalAction":
|
||||||
|
add(new ConditionalAction());
|
||||||
|
break;
|
||||||
|
case "FinishRoute":
|
||||||
|
add(new FinishRoute());
|
||||||
|
break;
|
||||||
|
case "SetSignalsToStop":
|
||||||
|
add(new SetSignalsToStop());
|
||||||
|
break;
|
||||||
|
case "SpeedReduction":
|
||||||
|
add(new SpeedReduction(0));
|
||||||
|
break;
|
||||||
|
case "TurnTrain":
|
||||||
|
add(new TurnTrain());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
actionTypeForm(win,context);
|
||||||
|
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win);
|
||||||
|
return win;
|
||||||
|
}
|
||||||
|
return plan.showContext(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTo(Tag link, String context) {
|
||||||
Map<String, Object> props = new HashMap<String, Object>(Map.of(
|
Map<String, Object> props = new HashMap<String, Object>(Map.of(
|
||||||
REALM,REALM_ACTIONS,
|
REALM,REALM_ACTIONS,
|
||||||
ID,id,
|
ID,id,
|
||||||
ACTION,ACTION_ADD));
|
ACTION,ACTION_ADD,
|
||||||
|
CONTEXT,context));
|
||||||
new Button(t("add action"),props).addTo(link);
|
new Button(t("add action"),props).addTo(link);
|
||||||
|
|
||||||
props.put(ACTION,ACTION_PROPS);
|
props.put(ACTION,ACTION_PROPS);
|
||||||
@@ -80,7 +110,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
|||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (Action action : this) {
|
for (Action action : this) {
|
||||||
props.put(ID, id+"/"+action.id());
|
props.put(ID, id+"/"+action.id());
|
||||||
Tag act = new Tag("li").content(action.toString());
|
Tag act = action.link(id,context).addTo(new Tag("li"));
|
||||||
if (!first) {
|
if (!first) {
|
||||||
props.put(ACTION, ACTION_MOVE);
|
props.put(ACTION, ACTION_MOVE);
|
||||||
new Button("↑",props).addTo(act);
|
new Button("↑",props).addTo(act);
|
||||||
@@ -94,79 +124,92 @@ public class ActionList extends Vector<Action> implements Constants{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String t(String text,Object...fills) {
|
public boolean drop(int actionId) {
|
||||||
return Translation.get(Application.class, text, fills);
|
for (Action action : this) {
|
||||||
}
|
if (action.id() == actionId) {
|
||||||
|
this.remove(action);
|
||||||
public static Object process(HashMap<String, String> params) {
|
return true;
|
||||||
if (!params.containsKey(ID)) return t("No action list id passed to ActionList.process()!");
|
}
|
||||||
String[] parts = params.get(ID).split("/");
|
|
||||||
int listId = Integer.parseInt(parts[0]);
|
|
||||||
int actionId = parts.length>1 ? Integer.parseInt(parts[1]) : 0;
|
|
||||||
ActionList actionList = actionLists.get(listId);
|
|
||||||
if (actionList == null) return t("No action list with id {} found!",listId);
|
|
||||||
String action = params.get(ACTION);
|
|
||||||
if (action == null) return t("No action passed to ActionList.process()!");
|
|
||||||
switch (action) {
|
|
||||||
case ACTION_ADD:
|
|
||||||
return actionList.addActionForm(params);
|
|
||||||
case ACTION_DROP:
|
|
||||||
return actionList.drop(actionId) ? t("Action removed") : t("No action with id {} found!",actionId);
|
|
||||||
case ACTION_MOVE:
|
|
||||||
return actionList.moveUp(actionId) ? t("Action moved") : t("No action with id {} found!",actionId);
|
|
||||||
}
|
}
|
||||||
return t("Unknown action: {}",action);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void fire(Context context) {
|
||||||
|
LOG.debug("Firing {}",this);
|
||||||
|
|
||||||
private Object addActionForm(HashMap<String, String> params) {
|
for (Action action : this) {
|
||||||
Window win = new Window("add-action-form", t("Add action to action list"));
|
try {
|
||||||
String formId ="add-action-to-"+id;
|
action.fire(context);
|
||||||
Tag typeForm = new Form(formId);
|
} catch (IOException e) {
|
||||||
new Input(REALM, REALM_ACTIONS).hideIn(typeForm);
|
LOG.warn("Action did not fire properly: {}",action,e);
|
||||||
new Input(ID,id).hideIn(typeForm);
|
}
|
||||||
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
|
|
||||||
String type = params.get(TYPE);
|
|
||||||
if (type == null) return actionTypeForm(win);
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "FinishRoute":
|
|
||||||
add(new FinishRoute());
|
|
||||||
break;
|
|
||||||
case "SetSignalsToStop":
|
|
||||||
add(new SetSignalsToStop());
|
|
||||||
break;
|
|
||||||
case "SpeedReduction":
|
|
||||||
return SpeedReduction.propForm(this,params);
|
|
||||||
case "TurnTrain":
|
|
||||||
add(new TurnTrain());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
actionTypeForm(win);
|
|
||||||
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win);
|
|
||||||
return win;
|
|
||||||
}
|
}
|
||||||
return t("Action added!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object actionTypeForm(Window win) {
|
private Action getAction(int actionId) {
|
||||||
String formId ="add-action-to-"+id;
|
for (Action action : this) {
|
||||||
Tag typeForm = new Form(formId);
|
if (action.id == actionId) return action;
|
||||||
new Input(REALM, REALM_ACTIONS).hideIn(typeForm);
|
}
|
||||||
new Input(ID,id).hideIn(typeForm);
|
return null;
|
||||||
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
|
|
||||||
Select select = new Select(TYPE);
|
|
||||||
List<Class<? extends Action>> classes = List.of(
|
|
||||||
SpeedReduction.class,
|
|
||||||
SetSignalsToStop.class,
|
|
||||||
FinishRoute.class,
|
|
||||||
TurnTrain.class,
|
|
||||||
ConditionalAction.class);
|
|
||||||
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
|
||||||
select.addTo(new Label("Action type:")).addTo(typeForm);
|
|
||||||
return new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(typeForm).addTo(win);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int id() {
|
public int id() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean moveUp(int actionId) {
|
||||||
|
for (int i=1; i<size(); i++) {
|
||||||
|
if (actionId == elementAt(i).id()) {
|
||||||
|
Action action = remove(i);
|
||||||
|
insertElementAt(action, i-1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object process(HashMap<String, String> params, Plan plan) {
|
||||||
|
Integer listId = actionListId(params);
|
||||||
|
if (listId == null) return t("No action list id passed to ActionList.process()!");
|
||||||
|
ActionList actionList = actionLists.get(listId);
|
||||||
|
if (actionList == null) return t("No action list with id {} found!",listId);
|
||||||
|
|
||||||
|
Integer actionId = actionId(params);
|
||||||
|
String action = params.get(ACTION);
|
||||||
|
if (action == null) return t("No action passed to ActionList.process()!");
|
||||||
|
|
||||||
|
switch (action) {
|
||||||
|
case ACTION_ADD:
|
||||||
|
return actionList.addActionForm(params,plan);
|
||||||
|
case ACTION_DROP:
|
||||||
|
return actionList.drop(actionId) ? plan.showContext(params) : t("No action with id {} found!",actionId);
|
||||||
|
case ACTION_MOVE:
|
||||||
|
return actionList.moveUp(actionId) ? plan.showContext(params) : t("No action with id {} found!",actionId);
|
||||||
|
case ACTION_PROPS:
|
||||||
|
return actionList.propsOf(params);
|
||||||
|
case ACTION_UPDATE:
|
||||||
|
return actionList.update(actionId,params,plan);
|
||||||
|
}
|
||||||
|
return t("Unknown action: {}",action);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object propsOf(HashMap<String, String> params) {
|
||||||
|
int actionId = actionId(params);
|
||||||
|
Action action = getAction(actionId);
|
||||||
|
if (action != null) return action.properties(params);
|
||||||
|
return t("No action with id {} found!",actionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String t(String text,Object...fills) {
|
||||||
|
return Translation.get(Application.class, text, fills);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object update(int actionId, HashMap<String, String> params, Plan plan) {
|
||||||
|
Action action = getAction(actionId);
|
||||||
|
if (action != null) {
|
||||||
|
plan.stream(action.update(params).toString());
|
||||||
|
return plan.showContext(params);
|
||||||
|
}
|
||||||
|
return t("No action with id {} found.",actionId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +1,47 @@
|
|||||||
package de.srsoftware.web4rail.actions;
|
package de.srsoftware.web4rail.actions;
|
||||||
|
|
||||||
import static de.srsoftware.web4rail.Constants.TYPE;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Route;
|
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
import de.srsoftware.web4rail.conditions.Condition;
|
import de.srsoftware.web4rail.conditions.Condition;
|
||||||
import de.srsoftware.web4rail.conditions.TrainSelect;
|
import de.srsoftware.web4rail.conditions.TrainSelect;
|
||||||
import de.srsoftware.web4rail.tags.Button;
|
import de.srsoftware.web4rail.tags.Button;
|
||||||
|
import de.srsoftware.web4rail.tags.Fieldset;
|
||||||
import de.srsoftware.web4rail.tags.Form;
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
import de.srsoftware.web4rail.tags.Select;
|
import de.srsoftware.web4rail.tags.Select;
|
||||||
import de.srsoftware.web4rail.tiles.Contact;
|
|
||||||
|
|
||||||
public class ConditionalAction extends Action {
|
public class ConditionalAction extends Action {
|
||||||
|
|
||||||
private Vector<Condition> conditions = new Vector<Condition>();
|
private Vector<Condition> conditions = new Vector<Condition>();
|
||||||
private Vector<Action> actions = new Vector<Action>();
|
private ActionList actions = new ActionList();
|
||||||
|
|
||||||
private ConditionalAction addCondition(Condition condition) {
|
private Tag conditionForm(HashMap<String, String> params) {
|
||||||
conditions.add(new TrainSelect());
|
Fieldset fieldset = new Fieldset("Conditions");
|
||||||
return this;
|
|
||||||
}
|
if (!conditions.isEmpty()) {
|
||||||
|
Tag list = new Tag("ul");
|
||||||
private static void addToContact(Route route, Contact contact, String conditionType) {
|
for (Condition condition : conditions) condition.link("li",params.get(CONTEXT)).addTo(list);
|
||||||
Condition condition = null;
|
list.addTo(fieldset);
|
||||||
switch (conditionType) {
|
|
||||||
case "TrainSelect":
|
|
||||||
condition = new TrainSelect();
|
|
||||||
break;
|
|
||||||
default: return;
|
|
||||||
}
|
}
|
||||||
route.addAction(contact.trigger(), new ConditionalAction().addCondition(condition));
|
String formId = "action-prop-form-"+id;
|
||||||
|
Form form = new Form(formId);
|
||||||
|
new Input(REALM,REALM_ACTIONS).hideIn(form);
|
||||||
|
new Input(ID,params.get(ID)).hideIn(form);
|
||||||
|
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||||
|
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||||
|
|
||||||
|
Select select = new Select(REALM_CONDITION);
|
||||||
|
List<Class<? extends Condition>> classes = List.of(TrainSelect.class);
|
||||||
|
for (Class<? extends Condition> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||||
|
select.addTo(form);
|
||||||
|
return new Button(t("Add condition"),"return submitForm('"+formId+"');").addTo(form).addTo(fieldset);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fire(Context context) throws IOException {
|
public boolean fire(Context context) throws IOException {
|
||||||
for (Condition condition : conditions) {
|
for (Condition condition : conditions) {
|
||||||
@@ -55,39 +58,38 @@ public class ConditionalAction extends Action {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Window propForm(HashMap<String, String> params, Route route, Contact contact) {
|
@Override
|
||||||
String condition = params.get(REALM_CONDITION);
|
public Window properties(HashMap<String, String> params) {
|
||||||
if (condition != null) {
|
Window win = super.properties(params);
|
||||||
addToContact(route,contact,condition);
|
conditionForm(params).addTo(win);
|
||||||
return route.properties();
|
|
||||||
}
|
|
||||||
Window win = Action.propForm(params);
|
|
||||||
String formId = "add-action-to-contact-"+contact.id();
|
|
||||||
Tag form = new Form(formId);
|
|
||||||
new Tag("div").content(t("Add Action {} to contact {} on route {}:",ConditionalAction.class.getSimpleName(),contact,route)).addTo(win);
|
|
||||||
new Input(REALM, REALM_ROUTE).hideIn(form);
|
|
||||||
new Input(ID,route.id()).hideIn(form);
|
|
||||||
new Input(ACTION,ACTION_ADD_ACTION).hideIn(form);
|
|
||||||
new Input(CONTACT,contact.id()).hideIn(form);
|
|
||||||
new Input(TYPE,ConditionalAction.class.getSimpleName()).hideIn(form);
|
|
||||||
Select select = new Select(REALM_CONDITION);
|
|
||||||
List<Class<? extends Condition>> conditionTypes = List.of(TrainSelect.class);
|
|
||||||
for (Class<? extends Condition> clazz : conditionTypes) select.addOption(clazz.getSimpleName());
|
|
||||||
select.addTo(form);
|
|
||||||
new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
|
||||||
return win;
|
return win;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (conditions.isEmpty()) return t("Invalid condition");
|
if (conditions.isEmpty()) return t("Invalid condition");
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (int i = 0; i<conditions.size(); i++) {
|
for (int i = 0; i<conditions.size(); i++) {
|
||||||
Condition condition = conditions.get(i);
|
if (i>0) sb.append(t(" or "));
|
||||||
Tag link = condition.link("span");
|
sb.append(conditions.get(i).toString());
|
||||||
sb.append(link);
|
|
||||||
}
|
}
|
||||||
return t("if ({}):",sb);
|
return t("if ({}):",sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object update(HashMap<String, String> params) {
|
||||||
|
String conditionClass = params.get(REALM_CONDITION);
|
||||||
|
if (conditionClass != null) {
|
||||||
|
switch (conditionClass) {
|
||||||
|
case "TrainSelect":
|
||||||
|
conditions.add(new TrainSelect());
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return t("Unknown type of condition: {}",conditionClass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.update(params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,30 @@ public class SpeedReduction extends Action{
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object propForm(ActionList actionList, HashMap<String, String> params) {
|
@Override
|
||||||
|
public Window properties(HashMap<String, String> params) {
|
||||||
|
Window win = super.properties(params);
|
||||||
|
String formId = "action-prop-form-"+id;
|
||||||
|
Form form = new Form(formId);
|
||||||
|
new Input(REALM,REALM_ACTIONS).hideIn(form);
|
||||||
|
new Input(ID,params.get(ID)).hideIn(form);
|
||||||
|
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||||
|
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||||
|
Label label = new Label(t("Set speed to")+NBSP);
|
||||||
|
new Input(MAX_SPEED, maxSpeed).addTo(label).content(NBSP+t("km/h"));
|
||||||
|
label.addTo(form);
|
||||||
|
new Button(t("Save"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
||||||
|
return win;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return t("Reduce speed to {} km/h",maxSpeed);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object update(HashMap<String, String> params) {
|
||||||
|
LOG.debug("update: {}",params);
|
||||||
String error = null;
|
String error = null;
|
||||||
String ms = params.get(MAX_SPEED);
|
String ms = params.get(MAX_SPEED);
|
||||||
if (ms == null) {
|
if (ms == null) {
|
||||||
@@ -47,28 +70,14 @@ public class SpeedReduction extends Action{
|
|||||||
int s = Integer.parseInt(ms);
|
int s = Integer.parseInt(ms);
|
||||||
if (s<0) error = t("Speed must not be less than zero!");
|
if (s<0) error = t("Speed must not be less than zero!");
|
||||||
if (error == null) {
|
if (error == null) {
|
||||||
actionList.add(new SpeedReduction(s));
|
this.maxSpeed = s;
|
||||||
return t("Action added!");
|
return t("Action updated!");
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
error = t("Not a valid number!");
|
error = t("Not a valid number!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Window win = Action.propForm(params);
|
Window win = properties(params);
|
||||||
String formId = "edit-speedreduction";
|
return new Tag("span").content(error).addTo(win);
|
||||||
Tag form = new Form(formId);
|
|
||||||
new Input(REALM, REALM_ACTIONS).hideIn(form);
|
|
||||||
new Input(ID,actionList.id()).hideIn(form);
|
|
||||||
new Input(ACTION,ACTION_ADD).hideIn(form);
|
|
||||||
new Input(TYPE,SpeedReduction.class.getSimpleName()).hideIn(form);
|
|
||||||
new Input(MAX_SPEED, ms).addTo(new Label("new speed")).addTo(form);
|
|
||||||
//if (error != null) new Tag("div").content(error).addTo(form);
|
|
||||||
new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
|
||||||
return win;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return t("Reduce speed to {} km/h",maxSpeed);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package de.srsoftware.web4rail.conditions;
|
package de.srsoftware.web4rail.conditions;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -10,6 +9,7 @@ import de.keawe.tools.translations.Translation;
|
|||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Application;
|
import de.srsoftware.web4rail.Application;
|
||||||
import de.srsoftware.web4rail.Constants;
|
import de.srsoftware.web4rail.Constants;
|
||||||
|
import de.srsoftware.web4rail.Plan;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
import de.srsoftware.web4rail.actions.Action.Context;
|
import de.srsoftware.web4rail.actions.Action.Context;
|
||||||
|
|
||||||
@@ -19,8 +19,17 @@ public abstract class Condition implements Constants {
|
|||||||
|
|
||||||
public abstract boolean fulfilledBy(Context context);
|
public abstract boolean fulfilledBy(Context context);
|
||||||
protected int id;
|
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!");
|
if (!params.containsKey(ID)) return t("No id passed to Condition.action!");
|
||||||
int cid = Integer.parseInt(params.get(ID));
|
int cid = Integer.parseInt(params.get(ID));
|
||||||
Condition condition = conditions.get(cid);
|
Condition condition = conditions.get(cid);
|
||||||
@@ -31,39 +40,29 @@ public abstract class Condition implements Constants {
|
|||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case ACTION_PROPS:
|
case ACTION_PROPS:
|
||||||
return condition.properties();
|
return condition.properties(params);
|
||||||
case ACTION_UPDATE:
|
case ACTION_UPDATE:
|
||||||
return condition.update(params);
|
condition.update(params);
|
||||||
|
return plan.showContext(params);
|
||||||
}
|
}
|
||||||
return t("Unknown action: {}",action);
|
return t("Unknown action: {}",action);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Window properties();
|
|
||||||
|
|
||||||
|
|
||||||
public Condition() {
|
|
||||||
this(new Date().hashCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Condition(int id) {
|
public Tag link(String tagClass,String context) {
|
||||||
this.id = id;
|
String json = new JSONObject(Map.of(REALM,REALM_CONDITION,ID,id,ACTION,ACTION_PROPS,CONTEXT,context)).toString().replace("\"", "'");
|
||||||
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());
|
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract Window properties(HashMap<String, String> params);
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return t("invalid condition");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String t(String text, Object...fills) {
|
public static String t(String text, Object...fills) {
|
||||||
return Translation.get(Application.class, text, fills);
|
return Translation.get(Application.class, text, fills);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return t("invalid condition");
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract Object update(HashMap<String, String> params);
|
protected abstract Object update(HashMap<String, String> params);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,13 +22,14 @@ public class TrainSelect extends Condition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Window properties() {
|
protected Window properties(HashMap<String, String> params) {
|
||||||
Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName()));
|
Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName()));
|
||||||
String formId = "conditional-props-"+id;
|
String formId = "conditional-props-"+id;
|
||||||
Form form = new Form(formId);
|
Form form = new Form(formId);
|
||||||
new Input(REALM,REALM_CONDITION).hideIn(form);
|
new Input(REALM,REALM_CONDITION).hideIn(form);
|
||||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||||
new Input(ID,id).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);
|
Train.selector(train, null).addTo(new Label(t("Select train:")+NBSP)).addTo(form);
|
||||||
new Button(t("Save"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
new Button(t("Save"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
||||||
return win;
|
return win;
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import java.io.FileReader;
|
|||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -99,7 +98,7 @@ public class Train implements Constants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Train(Locomotive loco, Integer id) {
|
public Train(Locomotive loco, Integer id) {
|
||||||
if (id == null) id = new Date().hashCode();
|
if (id == null) id = Application.createId();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
add(loco);
|
add(loco);
|
||||||
trains.put(id, this);
|
trains.put(id, this);
|
||||||
|
|||||||
Reference in New Issue
Block a user