moved action related code to new ActionList class
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.8</version>
|
<version>0.7.9</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>
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import com.sun.net.httpserver.HttpServer;
|
|||||||
import de.keawe.localconfig.Configuration;
|
import de.keawe.localconfig.Configuration;
|
||||||
import de.keawe.tools.translations.Translation;
|
import de.keawe.tools.translations.Translation;
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
|
import de.srsoftware.web4rail.actions.ActionList;
|
||||||
import de.srsoftware.web4rail.conditions.Condition;
|
import de.srsoftware.web4rail.conditions.Condition;
|
||||||
import de.srsoftware.web4rail.moving.Car;
|
import de.srsoftware.web4rail.moving.Car;
|
||||||
import de.srsoftware.web4rail.moving.Locomotive;
|
import de.srsoftware.web4rail.moving.Locomotive;
|
||||||
@@ -62,6 +63,8 @@ public class Application implements Constants{
|
|||||||
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
|
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
|
||||||
|
|
||||||
switch (realm) {
|
switch (realm) {
|
||||||
|
case REALM_ACTIONS:
|
||||||
|
return ActionList.process(params);
|
||||||
case REALM_CAR:
|
case REALM_CAR:
|
||||||
return Car.action(params);
|
return Car.action(params);
|
||||||
case REALM_CONDITION:
|
case REALM_CONDITION:
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ public interface Constants {
|
|||||||
public static final String ACTION_UPDATE = "update";
|
public static final String ACTION_UPDATE = "update";
|
||||||
|
|
||||||
public static final String REALM = "realm";
|
public static final String REALM = "realm";
|
||||||
|
public static final String REALM_ACTIONS = "actions";
|
||||||
public static final String REALM_CAR = "car";
|
public static final String REALM_CAR = "car";
|
||||||
public static final String REALM_CONDITION = "condition";
|
public static final String REALM_CONDITION = "condition";
|
||||||
public static final String REALM_CU = "cu";
|
public static final String REALM_CU = "cu";
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ public class Plan implements Constants{
|
|||||||
this.routes.clear();
|
this.routes.clear();
|
||||||
for (Tile tile : tiles.values()) tile.routes().clear();
|
for (Tile tile : tiles.values()) tile.routes().clear();
|
||||||
for (Route route : routes) {
|
for (Route route : routes) {
|
||||||
route.complete(this);
|
route.complete();
|
||||||
registerRoute(route);
|
registerRoute(route);
|
||||||
}
|
}
|
||||||
return t("Found {} routes.",routes.size());
|
return t("Found {} routes.",routes.size());
|
||||||
@@ -416,12 +416,6 @@ public class Plan implements Constants{
|
|||||||
Route route = route(Integer.parseInt(params.get(ID)));
|
Route route = route(Integer.parseInt(params.get(ID)));
|
||||||
if (route == null) return t("Unknown route: {}",params.get(ID));
|
if (route == null) return t("Unknown route: {}",params.get(ID));
|
||||||
switch (params.get(ACTION)) {
|
switch (params.get(ACTION)) {
|
||||||
case ACTION_ADD_ACTION:
|
|
||||||
return route.addActionForm(params);
|
|
||||||
case ACTION_DROP:
|
|
||||||
return route.dropAction(params);
|
|
||||||
case ACTION_MOVE:
|
|
||||||
return route.moveAction(params);
|
|
||||||
case ACTION_PROPS:
|
case ACTION_PROPS:
|
||||||
return route.properties();
|
return route.properties();
|
||||||
case ACTION_UPDATE:
|
case ACTION_UPDATE:
|
||||||
|
|||||||
@@ -25,18 +25,14 @@ import de.srsoftware.tools.Tag;
|
|||||||
import de.srsoftware.web4rail.Plan.Direction;
|
import de.srsoftware.web4rail.Plan.Direction;
|
||||||
import de.srsoftware.web4rail.actions.Action;
|
import de.srsoftware.web4rail.actions.Action;
|
||||||
import de.srsoftware.web4rail.actions.Action.Context;
|
import de.srsoftware.web4rail.actions.Action.Context;
|
||||||
|
import de.srsoftware.web4rail.actions.ActionList;
|
||||||
import de.srsoftware.web4rail.actions.ActivateRoute;
|
import de.srsoftware.web4rail.actions.ActivateRoute;
|
||||||
import de.srsoftware.web4rail.actions.ConditionalAction;
|
|
||||||
import de.srsoftware.web4rail.actions.FinishRoute;
|
import de.srsoftware.web4rail.actions.FinishRoute;
|
||||||
import de.srsoftware.web4rail.actions.SetSignalsToStop;
|
import de.srsoftware.web4rail.actions.SetSignalsToStop;
|
||||||
import de.srsoftware.web4rail.actions.SpeedReduction;
|
import de.srsoftware.web4rail.actions.SpeedReduction;
|
||||||
import de.srsoftware.web4rail.actions.TurnTrain;
|
|
||||||
import de.srsoftware.web4rail.moving.Train;
|
import de.srsoftware.web4rail.moving.Train;
|
||||||
import de.srsoftware.web4rail.tags.Button;
|
|
||||||
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.Label;
|
|
||||||
import de.srsoftware.web4rail.tags.Select;
|
|
||||||
import de.srsoftware.web4rail.tiles.Block;
|
import de.srsoftware.web4rail.tiles.Block;
|
||||||
import de.srsoftware.web4rail.tiles.Contact;
|
import de.srsoftware.web4rail.tiles.Contact;
|
||||||
import de.srsoftware.web4rail.tiles.Shadow;
|
import de.srsoftware.web4rail.tiles.Shadow;
|
||||||
@@ -55,7 +51,7 @@ public class Route implements Constants{
|
|||||||
private Vector<Signal> signals;
|
private Vector<Signal> signals;
|
||||||
private Vector<Contact> contacts;
|
private Vector<Contact> contacts;
|
||||||
private HashMap<Turnout,Turnout.State> turnouts;
|
private HashMap<Turnout,Turnout.State> turnouts;
|
||||||
private HashMap<String,Vector<Action>> triggers = new HashMap<String, Vector<Action>>();
|
private HashMap<String,ActionList> triggers = new HashMap<String, ActionList>();
|
||||||
private int id;
|
private int id;
|
||||||
private static HashMap<Integer, String> names = new HashMap<Integer, String>(); // maps id to name. needed to keep names during plan.analyze()
|
private static HashMap<Integer, String> names = new HashMap<Integer, String>(); // maps id to name. needed to keep names during plan.analyze()
|
||||||
public Train train;
|
public Train train;
|
||||||
@@ -65,31 +61,9 @@ public class Route implements Constants{
|
|||||||
|
|
||||||
public Direction startDirection;
|
public Direction startDirection;
|
||||||
private Direction endDirection;
|
private Direction endDirection;
|
||||||
private Plan plan;
|
|
||||||
|
|
||||||
private static final String TRIGGER = "trigger";
|
private static final String TRIGGER = "trigger";
|
||||||
private static final String ACTIONS = "actions";
|
private static final String ACTIONS = "actions";
|
||||||
private static final String ACTION_ID = "action_id";
|
|
||||||
private static final String TILE = Tile.class.getSimpleName();
|
|
||||||
|
|
||||||
private Tag actionTypeForm(Contact contact) {
|
|
||||||
String formId ="add-action-to-contact-"+contact.id();
|
|
||||||
Tag typeForm = new Form(formId);
|
|
||||||
new Input(REALM, REALM_ROUTE).hideIn(typeForm);
|
|
||||||
new Input(ID,id()).hideIn(typeForm);
|
|
||||||
new Input(ACTION,ACTION_ADD_ACTION).hideIn(typeForm);
|
|
||||||
new Input(CONTACT,contact.id()).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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route wurde von Zug betreten
|
* Route wurde von Zug betreten
|
||||||
@@ -117,45 +91,14 @@ public class Route implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addAction(String trigger, Action action) {
|
public void addAction(String trigger, Action action) {
|
||||||
Vector<Action> actions = triggers.get(trigger);
|
ActionList actions = triggers.get(trigger);
|
||||||
if (actions == null) {
|
if (actions == null) {
|
||||||
actions = new Vector<Action>();
|
actions = new ActionList();
|
||||||
triggers.put(trigger, actions);
|
triggers.put(trigger, actions);
|
||||||
}
|
}
|
||||||
actions.add(action);
|
actions.add(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object addActionForm(HashMap<String, String> params) {
|
|
||||||
String contactId = params.get(CONTACT);
|
|
||||||
Tile tag = plan.get(contactId, false);
|
|
||||||
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
|
||||||
Contact contact = (Contact) tag;
|
|
||||||
String type = params.get(TYPE);
|
|
||||||
Window win = new Window("add-action-form", t("Add action to contact on route"));
|
|
||||||
new Tag("div").content("Route: "+this).addTo(win);
|
|
||||||
new Tag("div").content("Contact: "+contact).addTo(win);
|
|
||||||
if (type == null) return (actionTypeForm(contact).addTo(win));
|
|
||||||
switch (type) {
|
|
||||||
case "ConditionalAction":
|
|
||||||
return ConditionalAction.propForm(params,this,contact);
|
|
||||||
case "FinishRoute":
|
|
||||||
addAction(contact.trigger(),new FinishRoute(id()));
|
|
||||||
break;
|
|
||||||
case "SpeedReduction":
|
|
||||||
return SpeedReduction.propForm(params,this,contact);
|
|
||||||
case "SetSignalsToStop":
|
|
||||||
addAction(contact.trigger(),new SetSignalsToStop(id()));
|
|
||||||
break;
|
|
||||||
case "TurnTrain":
|
|
||||||
addAction(contact.trigger(),new TurnTrain(id()));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return win;
|
|
||||||
}
|
|
||||||
plan.stream("Action added!");
|
|
||||||
return properties();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addBasicPropertiesTo(Window win) {
|
private void addBasicPropertiesTo(Window win) {
|
||||||
new Tag("h4").content(t("Origin and destination")).addTo(win);
|
new Tag("h4").content(t("Origin and destination")).addTo(win);
|
||||||
Tag list = new Tag("ul");
|
Tag list = new Tag("ul");
|
||||||
@@ -174,34 +117,15 @@ public class Route implements Constants{
|
|||||||
private void addContactsTo(Window win) {
|
private void addContactsTo(Window win) {
|
||||||
if (!contacts.isEmpty()) {
|
if (!contacts.isEmpty()) {
|
||||||
new Tag("h4").content(t("Contacts and actions")).addTo(win);
|
new Tag("h4").content(t("Contacts and actions")).addTo(win);
|
||||||
Tag list = new Tag("ul");
|
Tag list = new Tag("ol");
|
||||||
for (Contact c : contacts) {
|
for (Contact c : contacts) {
|
||||||
Tag link = Plan.addLink(c,c.toString(),list);
|
Tag link = Plan.addLink(c,c.toString(),list);
|
||||||
Map<String, Object> props = new HashMap<String, Object>(Map.of(
|
ActionList actions = triggers.get(c.trigger());
|
||||||
REALM,REALM_ROUTE,
|
if (actions == null) {
|
||||||
ID,id,
|
actions = new ActionList();
|
||||||
ACTION,ACTION_ADD_ACTION,
|
triggers.put(c.trigger(), actions);
|
||||||
CONTACT,c.id()));
|
|
||||||
new Button(t("add action"),props).addTo(link);
|
|
||||||
Vector<Action> actions = triggers.get(c.trigger());
|
|
||||||
if (actions != null && !actions.isEmpty()) {
|
|
||||||
Tag ul = new Tag("ul");
|
|
||||||
boolean first = true;
|
|
||||||
for (Action action : actions) {
|
|
||||||
props.put(ACTION_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);
|
|
||||||
}
|
}
|
||||||
|
actions.addTo(link);
|
||||||
}
|
}
|
||||||
list.addTo(win);
|
list.addTo(win);
|
||||||
}
|
}
|
||||||
@@ -254,18 +178,17 @@ public class Route implements Constants{
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void complete(Plan plan) {
|
public void complete() {
|
||||||
this.plan = plan;
|
|
||||||
if (contacts.size()>1) { // mindestens 2 Kontakte: erster Kontakt aktiviert Block, vorletzter Kontakt leitet Bremsung ein
|
if (contacts.size()>1) { // mindestens 2 Kontakte: erster Kontakt aktiviert Block, vorletzter Kontakt leitet Bremsung ein
|
||||||
addAction(contacts.firstElement().trigger(),new ActivateRoute(id()));
|
addAction(contacts.firstElement().trigger(),new ActivateRoute());
|
||||||
Contact nextToLastContact = contacts.get(contacts.size()-2);
|
Contact nextToLastContact = contacts.get(contacts.size()-2);
|
||||||
addAction(nextToLastContact.trigger(),new SpeedReduction(id(),30));
|
addAction(nextToLastContact.trigger(),new SpeedReduction(30));
|
||||||
addAction(nextToLastContact.trigger(),new SetSignalsToStop(id()));
|
addAction(nextToLastContact.trigger(),new SetSignalsToStop());
|
||||||
}
|
}
|
||||||
if (!contacts.isEmpty()) {
|
if (!contacts.isEmpty()) {
|
||||||
Contact lastContact = contacts.lastElement();
|
Contact lastContact = contacts.lastElement();
|
||||||
addAction(lastContact.trigger(), new SpeedReduction(id(), 0));
|
addAction(lastContact.trigger(), new SpeedReduction(0));
|
||||||
addAction(lastContact.trigger(), new FinishRoute(id()));
|
addAction(lastContact.trigger(), new FinishRoute());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,48 +199,16 @@ public class Route implements Constants{
|
|||||||
*/
|
*/
|
||||||
public void contact(Contact contact) {
|
public void contact(Contact contact) {
|
||||||
LOG.debug("{} on {} activated {}.",train,this,contact);
|
LOG.debug("{} on {} activated {}.",train,this,contact);
|
||||||
Vector<Action> actions = triggers.get(contact.trigger());
|
ActionList actions = triggers.get(contact.trigger());
|
||||||
if (actions == null) return;
|
if (actions == null) return;
|
||||||
LOG.debug("Triggering {}",actions);
|
|
||||||
Context context = new Context(contact);
|
Context context = new Context(contact);
|
||||||
for (Action action : actions) {
|
actions.fire(context);
|
||||||
try {
|
|
||||||
action.fire(context);
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOG.warn("Action did not fire properly: {}",action,e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector<Contact> contacts() {
|
public Vector<Contact> contacts() {
|
||||||
return new Vector<>(contacts);
|
return new Vector<>(contacts);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object dropAction(HashMap<String, String> params) {
|
|
||||||
String actionId = params.get(ACTION_ID);
|
|
||||||
if (actionId == null) {
|
|
||||||
plan.remove(this); // if id of an action is given: delete the action from the route. otherwise: delete the route
|
|
||||||
String tileId = params.get(TILE);
|
|
||||||
Tile tile = plan.get(tileId,false);
|
|
||||||
return tile.propMenu();
|
|
||||||
}
|
|
||||||
String contactId = params.get(CONTACT);
|
|
||||||
Tile tag = plan.get(contactId, false);
|
|
||||||
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
|
||||||
Contact contact = (Contact) tag;
|
|
||||||
Vector<Action> actions = triggers.get(contact.trigger());
|
|
||||||
|
|
||||||
for (int i=0; i<actions.size(); i++) {
|
|
||||||
if (actions.elementAt(i).toString().equals(actionId)) {
|
|
||||||
actions.remove(i);
|
|
||||||
plan.stream(t("removed {}.",actionId));
|
|
||||||
return properties();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
plan.stream(t("No action \"{}\" assigned with {}!",actionId,contact));
|
|
||||||
return properties();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Block endBlock() {
|
public Block endBlock() {
|
||||||
return endBlock;
|
return endBlock;
|
||||||
}
|
}
|
||||||
@@ -379,7 +270,7 @@ public class Route implements Constants{
|
|||||||
json.put(END_DIRECTION, endDirection);
|
json.put(END_DIRECTION, endDirection);
|
||||||
|
|
||||||
JSONArray jTriggers = new JSONArray();
|
JSONArray jTriggers = new JSONArray();
|
||||||
for (Entry<String, Vector<Action>> entry : triggers.entrySet()) {
|
for (Entry<String, ActionList> entry : triggers.entrySet()) {
|
||||||
JSONObject trigger = new JSONObject();
|
JSONObject trigger = new JSONObject();
|
||||||
trigger.put(TRIGGER, entry.getKey());
|
trigger.put(TRIGGER, entry.getKey());
|
||||||
|
|
||||||
@@ -401,7 +292,6 @@ public class Route implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Route load(JSONObject json,Plan plan) {
|
private Route load(JSONObject json,Plan plan) {
|
||||||
this.plan = plan;
|
|
||||||
if (json.has(ID)) id = json.getInt(ID);
|
if (json.has(ID)) id = json.getInt(ID);
|
||||||
JSONArray pathIds = json.getJSONArray(PATH);
|
JSONArray pathIds = json.getJSONArray(PATH);
|
||||||
startDirection = Direction.valueOf(json.getString(START_DIRECTION));
|
startDirection = Direction.valueOf(json.getString(START_DIRECTION));
|
||||||
@@ -477,28 +367,6 @@ public class Route implements Constants{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object moveAction(HashMap<String, String> params) {
|
|
||||||
if (!params.containsKey(ACTION_ID)) return t("No action id passed to request!");
|
|
||||||
|
|
||||||
int action_id = Integer.parseInt(params.get(ACTION_ID));
|
|
||||||
|
|
||||||
String contactId = params.get(CONTACT);
|
|
||||||
Tile tag = plan.get(contactId, false);
|
|
||||||
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
|
||||||
Contact contact = (Contact) tag;
|
|
||||||
Vector<Action> actions = triggers.get(contact.trigger());
|
|
||||||
|
|
||||||
for (int i=1; i<actions.size(); i++) {
|
|
||||||
if (action_id == actions.elementAt(i).id()) {
|
|
||||||
Action action = actions.remove(i);
|
|
||||||
actions.insertElementAt(action, i-1);
|
|
||||||
return properties();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
plan.stream(t("No action \"{}\" assigned with {}!",action_id,contact));
|
|
||||||
return properties();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Route> multiply(int size) {
|
public List<Route> multiply(int size) {
|
||||||
Vector<Route> routes = new Vector<Route>();
|
Vector<Route> routes = new Vector<Route>();
|
||||||
for (int i=0; i<size; i++) routes.add(i==0 ? this : this.clone());
|
for (int i=0; i<size; i++) routes.add(i==0 ? this : this.clone());
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ 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 static final String TYPE = "type";
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
public static class Context {
|
public static class Context {
|
||||||
@@ -63,15 +62,15 @@ public abstract class Action implements Constants {
|
|||||||
String clazz = json.getString(TYPE);
|
String clazz = json.getString(TYPE);
|
||||||
switch (clazz) {
|
switch (clazz) {
|
||||||
case "ActivateRoute":
|
case "ActivateRoute":
|
||||||
return new ActivateRoute(json.getInt(RouteAction.ROUTE));
|
return new ActivateRoute();
|
||||||
case "FinishRoute":
|
case "FinishRoute":
|
||||||
return new FinishRoute(json.getInt(RouteAction.ROUTE));
|
return new FinishRoute();
|
||||||
case "SetSignalsToStop":
|
case "SetSignalsToStop":
|
||||||
return new SetSignalsToStop(json.getInt(RouteAction.ROUTE));
|
return new SetSignalsToStop();
|
||||||
case "SpeedReduction":
|
case "SpeedReduction":
|
||||||
return new SpeedReduction(json.getInt(RouteAction.ROUTE), json.getInt(SpeedReduction.MAX_SPEED));
|
return new SpeedReduction(json.getInt(SpeedReduction.MAX_SPEED));
|
||||||
case "TurnTrain":
|
case "TurnTrain":
|
||||||
return new TurnTrain(json.getInt(RouteAction.ROUTE));
|
return new TurnTrain();
|
||||||
}
|
}
|
||||||
return null;
|
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;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ActivateRoute extends RouteAction {
|
public class ActivateRoute extends Action {
|
||||||
|
|
||||||
public ActivateRoute(int routeId) {
|
|
||||||
super(routeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fire(Context context) throws IOException {
|
public boolean fire(Context context) throws IOException {
|
||||||
|
|||||||
@@ -2,11 +2,7 @@ package de.srsoftware.web4rail.actions;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class FinishRoute extends RouteAction {
|
public class FinishRoute extends Action {
|
||||||
|
|
||||||
public FinishRoute(int routeId) {
|
|
||||||
super(routeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fire(Context context) throws IOException {
|
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;
|
import de.srsoftware.web4rail.tiles.Signal;
|
||||||
|
|
||||||
public class SetSignalsToStop extends RouteAction {
|
public class SetSignalsToStop extends Action {
|
||||||
|
|
||||||
public SetSignalsToStop(int routeId) {
|
|
||||||
super(routeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fire(Context context) throws IOException {
|
public boolean fire(Context context) throws IOException {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import java.util.HashMap;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Constants;
|
|
||||||
import de.srsoftware.web4rail.Route;
|
import de.srsoftware.web4rail.Route;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
import de.srsoftware.web4rail.tags.Button;
|
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.tags.Label;
|
||||||
import de.srsoftware.web4rail.tiles.Contact;
|
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";
|
public static final String MAX_SPEED = "max_speed";
|
||||||
private int maxSpeed;
|
private int maxSpeed;
|
||||||
|
|
||||||
public SpeedReduction(int routeId, int kmh) {
|
public SpeedReduction(int kmh) {
|
||||||
super(routeId);
|
super();
|
||||||
maxSpeed = kmh;
|
maxSpeed = kmh;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +49,7 @@ public class SpeedReduction extends RouteAction implements Constants{
|
|||||||
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) {
|
||||||
route.addAction(contact.trigger(),new SpeedReduction(route.id(), s));
|
route.addAction(contact.trigger(),new SpeedReduction(s));
|
||||||
contact.plan().stream("Action added!");
|
contact.plan().stream("Action added!");
|
||||||
return route.properties();
|
return route.properties();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
package de.srsoftware.web4rail.actions;
|
package de.srsoftware.web4rail.actions;
|
||||||
|
|
||||||
import de.srsoftware.web4rail.Constants;
|
public class TurnTrain extends Action{
|
||||||
|
|
||||||
public class TurnTrain extends RouteAction implements Constants{
|
|
||||||
|
|
||||||
public TurnTrain(int routeId) {
|
|
||||||
super(routeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fire(Context context) {
|
public boolean fire(Context context) {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class TrainSelect extends Condition {
|
|||||||
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);
|
||||||
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")).addTo(form).addTo(win);
|
new Button(t("Save"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
||||||
return win;
|
return win;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user