moved action related code to new ActionList class
This commit is contained in:
@@ -20,7 +20,6 @@ import de.srsoftware.web4rail.tiles.Contact;
|
||||
|
||||
public abstract class Action implements Constants {
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||
private static final String TYPE = "type";
|
||||
private int id;
|
||||
|
||||
public static class Context {
|
||||
@@ -63,15 +62,15 @@ public abstract class Action implements Constants {
|
||||
String clazz = json.getString(TYPE);
|
||||
switch (clazz) {
|
||||
case "ActivateRoute":
|
||||
return new ActivateRoute(json.getInt(RouteAction.ROUTE));
|
||||
return new ActivateRoute();
|
||||
case "FinishRoute":
|
||||
return new FinishRoute(json.getInt(RouteAction.ROUTE));
|
||||
return new FinishRoute();
|
||||
case "SetSignalsToStop":
|
||||
return new SetSignalsToStop(json.getInt(RouteAction.ROUTE));
|
||||
return new SetSignalsToStop();
|
||||
case "SpeedReduction":
|
||||
return new SpeedReduction(json.getInt(RouteAction.ROUTE), json.getInt(SpeedReduction.MAX_SPEED));
|
||||
return new SpeedReduction(json.getInt(SpeedReduction.MAX_SPEED));
|
||||
case "TurnTrain":
|
||||
return new TurnTrain(json.getInt(RouteAction.ROUTE));
|
||||
return new TurnTrain();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
166
src/main/java/de/srsoftware/web4rail/actions/ActionList.java
Normal file
166
src/main/java/de/srsoftware/web4rail/actions/ActionList.java
Normal file
@@ -0,0 +1,166 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
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;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
|
||||
public class ActionList extends Vector<Action> implements Constants{
|
||||
|
||||
private static final long serialVersionUID = 4862000041987682112L;
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
||||
private int id;
|
||||
private static final HashMap<Integer, ActionList> actionLists = new HashMap<Integer, ActionList>();
|
||||
|
||||
public ActionList() {
|
||||
id = new Date().hashCode();
|
||||
actionLists.put(id,this);
|
||||
}
|
||||
|
||||
public void fire(Context context) {
|
||||
LOG.debug("Firing {}",this);
|
||||
|
||||
for (Action action : this) {
|
||||
try {
|
||||
action.fire(context);
|
||||
} catch (IOException e) {
|
||||
LOG.warn("Action did not fire properly: {}",action,e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean drop(int actionId) {
|
||||
for (Action action : this) {
|
||||
if (action.id() == actionId) {
|
||||
this.remove(action);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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 void addTo(Tag link) {
|
||||
Map<String, Object> props = new HashMap<String, Object>(Map.of(
|
||||
REALM,REALM_ACTIONS,
|
||||
ID,id,
|
||||
ACTION,ACTION_ADD));
|
||||
new Button(t("add action"),props).addTo(link);
|
||||
|
||||
props.put(ACTION,ACTION_PROPS);
|
||||
if (!isEmpty()) {
|
||||
Tag ul = new Tag("ol");
|
||||
boolean first = true;
|
||||
for (Action action : this) {
|
||||
props.put(ID, id+"/"+action.id());
|
||||
Tag act = new Tag("li").content(action.toString());
|
||||
if (!first) {
|
||||
props.put(ACTION, ACTION_MOVE);
|
||||
new Button("↑",props).addTo(act);
|
||||
}
|
||||
props.put(ACTION, ACTION_DROP);
|
||||
new Button("-",props).addTo(act);
|
||||
act.addTo(ul);
|
||||
first = false;
|
||||
}
|
||||
ul.addTo(link);
|
||||
}
|
||||
}
|
||||
|
||||
private static String t(String text,Object...fills) {
|
||||
return Translation.get(Application.class, text, fills);
|
||||
}
|
||||
|
||||
public static Object process(HashMap<String, String> params) {
|
||||
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);
|
||||
}
|
||||
|
||||
private Object addActionForm(HashMap<String, String> params) {
|
||||
Window win = new Window("add-action-form", t("Add action to action list"));
|
||||
String formId ="add-action-to-"+id;
|
||||
Tag typeForm = new Form(formId);
|
||||
new Input(REALM, REALM_ACTIONS).hideIn(typeForm);
|
||||
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 "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) {
|
||||
String formId ="add-action-to-"+id;
|
||||
Tag typeForm = new Form(formId);
|
||||
new Input(REALM, REALM_ACTIONS).hideIn(typeForm);
|
||||
new Input(ID,id).hideIn(typeForm);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ActivateRoute extends RouteAction {
|
||||
|
||||
public ActivateRoute(int routeId) {
|
||||
super(routeId);
|
||||
}
|
||||
public class ActivateRoute extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
|
||||
@@ -2,11 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class FinishRoute extends RouteAction {
|
||||
|
||||
public FinishRoute(int routeId) {
|
||||
super(routeId);
|
||||
}
|
||||
public class FinishRoute extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
public abstract class RouteAction extends Action {
|
||||
|
||||
static final String ROUTE = "route";
|
||||
protected int routeId;
|
||||
|
||||
public RouteAction(int routeId) {
|
||||
this.routeId = routeId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(ROUTE, routeId);
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,7 @@ import java.io.IOException;
|
||||
|
||||
import de.srsoftware.web4rail.tiles.Signal;
|
||||
|
||||
public class SetSignalsToStop extends RouteAction {
|
||||
|
||||
public SetSignalsToStop(int routeId) {
|
||||
super(routeId);
|
||||
}
|
||||
public class SetSignalsToStop extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.HashMap;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
@@ -14,13 +13,13 @@ import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
|
||||
public class SpeedReduction extends RouteAction implements Constants{
|
||||
public class SpeedReduction extends Action{
|
||||
|
||||
public static final String MAX_SPEED = "max_speed";
|
||||
private int maxSpeed;
|
||||
|
||||
public SpeedReduction(int routeId, int kmh) {
|
||||
super(routeId);
|
||||
public SpeedReduction(int kmh) {
|
||||
super();
|
||||
maxSpeed = kmh;
|
||||
}
|
||||
|
||||
@@ -50,7 +49,7 @@ public class SpeedReduction extends RouteAction implements Constants{
|
||||
int s = Integer.parseInt(ms);
|
||||
if (s<0) error = t("Speed must not be less than zero!");
|
||||
if (error == null) {
|
||||
route.addAction(contact.trigger(),new SpeedReduction(route.id(), s));
|
||||
route.addAction(contact.trigger(),new SpeedReduction(s));
|
||||
contact.plan().stream("Action added!");
|
||||
return route.properties();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
|
||||
public class TurnTrain extends RouteAction implements Constants{
|
||||
|
||||
public TurnTrain(int routeId) {
|
||||
super(routeId);
|
||||
}
|
||||
public class TurnTrain extends Action{
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
|
||||
Reference in New Issue
Block a user