overhauled storing and loading code for routes, actions and conditions

This commit is contained in:
Stephan Richter
2020-10-30 11:03:39 +01:00
parent 05784f94ce
commit b68b1168c7
8 changed files with 72 additions and 31 deletions

View File

@@ -1,7 +1,6 @@
package de.srsoftware.web4rail.actions;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
@@ -57,17 +56,21 @@ public abstract class Action implements Constants {
return new Tag("span").content(toString()+NBSP).attr("onclick", action);
}
public static Action load(JSONObject json) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
public static Action load(JSONObject json) {
String clazz = json.getString(TYPE);
switch (clazz) {
case "ActivateRoute":
return new ActivateRoute();
case "ConditionalAction":
return ConditionalAction.load(json);
case "FinishRoute":
return new FinishRoute();
case "PowerOff":
return new PowerOff();
case "SetSignalsToStop":
return new SetSignalsToStop();
case "SpeedReduction":
return new SetSpeed(json.getInt(SetSpeed.MAX_SPEED));
case "SetSpeed":
return SetSpeed.load(json);
case "TurnTrain":
return new TurnTrain();
}

View File

@@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -176,6 +177,17 @@ public class ActionList extends Vector<Action> implements Constants{
for (Action action : this) result.put(action.json());
return result;
}
public static ActionList load(JSONArray list) {
ActionList actionList = new ActionList();
for (Object o : list) {
if (o instanceof JSONObject) {
Action action = Action.load((JSONObject) o);
if (action != null) actionList.add(action);
}
}
return actionList;
}
public boolean moveUp(int actionId) {
for (int i=1; i<size(); i++) {

View File

@@ -83,6 +83,18 @@ public class ConditionalAction extends Action {
json.put(ACTIONS, actions.json());
return json;
}
public static ConditionalAction load(JSONObject json) {
ConditionalAction action = new ConditionalAction();
for (Object o : json.getJSONArray(CONDITIONS)) {
if (o instanceof JSONObject) {
Condition condition = Condition.load((JSONObject)o);
if (condition != null) action.conditions.add(condition);
}
}
action.actions = ActionList.load(json.getJSONArray(ACTIONS));
return action;
}
@Override
public Window properties(HashMap<String, String> params) {

View File

@@ -37,6 +37,11 @@ public class SetSpeed extends Action{
return json;
}
public static SetSpeed load(JSONObject json) {
int s = json.getInt(MAX_SPEED);
return new SetSpeed(s);
}
@Override
public Window properties(HashMap<String, String> params) {
Window win = super.properties(params);