working on conditional actions
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.6</version>
|
<version>0.7.7</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.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;
|
||||||
import de.srsoftware.web4rail.moving.Train;
|
import de.srsoftware.web4rail.moving.Train;
|
||||||
@@ -63,6 +64,8 @@ public class Application implements Constants{
|
|||||||
switch (realm) {
|
switch (realm) {
|
||||||
case REALM_CAR:
|
case REALM_CAR:
|
||||||
return Car.action(params);
|
return Car.action(params);
|
||||||
|
case REALM_CONDITION:
|
||||||
|
return Condition.action(params);
|
||||||
case REALM_CU:
|
case REALM_CU:
|
||||||
return plan.controlUnit().process(params);
|
return plan.controlUnit().process(params);
|
||||||
case REALM_LOCO:
|
case REALM_LOCO:
|
||||||
|
|||||||
@@ -29,13 +29,14 @@ public interface Constants {
|
|||||||
public static final String ACTION_TURN = "turn";
|
public static final String ACTION_TURN = "turn";
|
||||||
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_CAR = "car";
|
public static final String REALM_CAR = "car";
|
||||||
public static final String REALM_CU = "cu";
|
public static final String REALM_CONDITION = "condition";
|
||||||
public static final String REALM_LOCO = "loco";
|
public static final String REALM_CU = "cu";
|
||||||
public static final String REALM_ROUTE = "route";
|
public static final String REALM_LOCO = "loco";
|
||||||
public static final String REALM_PLAN = "plan";
|
public static final String REALM_ROUTE = "route";
|
||||||
public static final String REALM_TRAIN = "train";
|
public static final String REALM_PLAN = "plan";
|
||||||
|
public static final String REALM_TRAIN = "train";
|
||||||
|
|
||||||
public static final String ID = "id";
|
public static final String ID = "id";
|
||||||
public static final String PORT = "port";
|
public static final String PORT = "port";
|
||||||
@@ -43,5 +44,4 @@ 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 = " ";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ 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.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;
|
||||||
@@ -82,7 +83,8 @@ public class Route implements Constants{
|
|||||||
SpeedReduction.class,
|
SpeedReduction.class,
|
||||||
SetSignalsToStop.class,
|
SetSignalsToStop.class,
|
||||||
FinishRoute.class,
|
FinishRoute.class,
|
||||||
TurnTrain.class);
|
TurnTrain.class,
|
||||||
|
ConditionalAction.class);
|
||||||
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||||
select.addTo(new Label("Action type:")).addTo(typeForm);
|
select.addTo(new Label("Action type:")).addTo(typeForm);
|
||||||
return new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(typeForm);
|
return new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(typeForm);
|
||||||
@@ -127,13 +129,14 @@ public class Route implements Constants{
|
|||||||
Tile tag = plan.get(contactId, false);
|
Tile tag = plan.get(contactId, false);
|
||||||
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
||||||
Contact contact = (Contact) tag;
|
Contact contact = (Contact) tag;
|
||||||
|
String type = params.get(TYPE);
|
||||||
Window win = new Window("add-action-form", t("Add action to contact on route"));
|
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("Route: "+this).addTo(win);
|
||||||
new Tag("div").content("Contact: "+contact).addTo(win);
|
new Tag("div").content("Contact: "+contact).addTo(win);
|
||||||
|
|
||||||
String type = params.get(TYPE);
|
|
||||||
if (type == null) return (actionTypeForm(contact).addTo(win));
|
if (type == null) return (actionTypeForm(contact).addTo(win));
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
case "ConditionalAction":
|
||||||
|
return ConditionalAction.propForm(params,this,contact);
|
||||||
case "FinishRoute":
|
case "FinishRoute":
|
||||||
addAction(contact.trigger(),new FinishRoute(id()));
|
addAction(contact.trigger(),new FinishRoute(id()));
|
||||||
break;
|
break;
|
||||||
@@ -184,7 +187,7 @@ public class Route implements Constants{
|
|||||||
Tag ul = new Tag("ul");
|
Tag ul = new Tag("ul");
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (Action action : actions) {
|
for (Action action : actions) {
|
||||||
props.put(ACTION_ID, action.toString());
|
props.put(ACTION_ID, action.id());
|
||||||
|
|
||||||
Tag act = new Tag("li").content(action.toString());
|
Tag act = new Tag("li").content(action.toString());
|
||||||
if (!first) {
|
if (!first) {
|
||||||
@@ -469,8 +472,10 @@ public class Route implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Object moveAction(HashMap<String, String> params) {
|
public Object moveAction(HashMap<String, String> params) {
|
||||||
String action_id = params.get(ACTION_ID);
|
if (!params.containsKey(ACTION_ID)) return t("No action id passed to request!");
|
||||||
if (action_id == null) return t("No action id passed to request!");
|
|
||||||
|
int action_id = Integer.parseInt(params.get(ACTION_ID));
|
||||||
|
|
||||||
String contactId = params.get(CONTACT);
|
String contactId = params.get(CONTACT);
|
||||||
Tile tag = plan.get(contactId, false);
|
Tile tag = plan.get(contactId, false);
|
||||||
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
if (!(tag instanceof Contact)) return t("No contact id passed to request!");
|
||||||
@@ -478,7 +483,7 @@ public class Route implements Constants{
|
|||||||
Vector<Action> actions = triggers.get(contact.trigger());
|
Vector<Action> actions = triggers.get(contact.trigger());
|
||||||
|
|
||||||
for (int i=1; i<actions.size(); i++) {
|
for (int i=1; i<actions.size(); i++) {
|
||||||
if (actions.elementAt(i).toString().equals(action_id)) {
|
if (action_id == actions.elementAt(i).id()) {
|
||||||
Action action = actions.remove(i);
|
Action action = actions.remove(i);
|
||||||
actions.insertElementAt(action, i-1);
|
actions.insertElementAt(action, i-1);
|
||||||
return properties();
|
return properties();
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ 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 org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@@ -10,16 +11,17 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import de.keawe.tools.translations.Translation;
|
import de.keawe.tools.translations.Translation;
|
||||||
import de.srsoftware.web4rail.Application;
|
import de.srsoftware.web4rail.Application;
|
||||||
|
import de.srsoftware.web4rail.Constants;
|
||||||
import de.srsoftware.web4rail.Plan;
|
import de.srsoftware.web4rail.Plan;
|
||||||
import de.srsoftware.web4rail.Route;
|
import de.srsoftware.web4rail.Route;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
import de.srsoftware.web4rail.moving.Train;
|
import de.srsoftware.web4rail.moving.Train;
|
||||||
import de.srsoftware.web4rail.tiles.Contact;
|
import de.srsoftware.web4rail.tiles.Contact;
|
||||||
|
|
||||||
public abstract class Action {
|
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 static final String TYPE = "type";
|
||||||
|
private int id;
|
||||||
|
|
||||||
public static class Context {
|
public static class Context {
|
||||||
public Plan plan = null;
|
public Plan plan = null;
|
||||||
@@ -34,8 +36,17 @@ public abstract class Action {
|
|||||||
train = route.train;
|
train = route.train;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Action() {
|
||||||
|
id = new Date().hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean fire(Context context) throws IOException;
|
||||||
|
|
||||||
|
public int id() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void fire(Context context) throws IOException;
|
|
||||||
|
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ public class ActivateRoute extends RouteAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fire(Context context) throws IOException {
|
public boolean fire(Context context) throws IOException {
|
||||||
context.route.activate();
|
context.route.activate();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
package de.srsoftware.web4rail.actions;
|
||||||
|
|
||||||
|
import static de.srsoftware.web4rail.Constants.TYPE;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import de.srsoftware.tools.Tag;
|
||||||
|
import de.srsoftware.web4rail.Route;
|
||||||
|
import de.srsoftware.web4rail.Window;
|
||||||
|
import de.srsoftware.web4rail.conditions.Condition;
|
||||||
|
import de.srsoftware.web4rail.conditions.TrainSelect;
|
||||||
|
import de.srsoftware.web4rail.tags.Button;
|
||||||
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
|
import de.srsoftware.web4rail.tags.Select;
|
||||||
|
import de.srsoftware.web4rail.tiles.Contact;
|
||||||
|
|
||||||
|
public class ConditionalAction extends Action {
|
||||||
|
|
||||||
|
private Vector<Condition> conditions = new Vector<Condition>();
|
||||||
|
private Vector<Action> actions = new Vector<Action>();
|
||||||
|
|
||||||
|
private ConditionalAction addCondition(Condition condition) {
|
||||||
|
conditions.add(new TrainSelect());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addToContact(Route route, Contact contact, String conditionType) {
|
||||||
|
Condition condition = null;
|
||||||
|
switch (conditionType) {
|
||||||
|
case "TrainSelect":
|
||||||
|
condition = new TrainSelect();
|
||||||
|
break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
route.addAction(contact.trigger(), new ConditionalAction().addCondition(condition));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean fire(Context context) throws IOException {
|
||||||
|
for (Condition condition : conditions) {
|
||||||
|
if (condition.fulfilledBy(context)) return fireActions(context);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean fireActions(Context context) {
|
||||||
|
for (Action action : actions) try {
|
||||||
|
action.fire(context);
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.warn("Was not able to fire {}",action);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Window propForm(HashMap<String, String> params, Route route, Contact contact) {
|
||||||
|
String condition = params.get(REALM_CONDITION);
|
||||||
|
if (condition != null) {
|
||||||
|
addToContact(route,contact,condition);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if (conditions.isEmpty()) return t("Invalid condition");
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (int i = 0; i<conditions.size(); i++) {
|
||||||
|
Condition condition = conditions.get(i);
|
||||||
|
Tag link = condition.link("span");
|
||||||
|
sb.append(link);
|
||||||
|
}
|
||||||
|
return t("if ({}):",sb);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,8 @@ public class FinishRoute extends RouteAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fire(Context context) throws IOException {
|
public boolean fire(Context context) throws IOException {
|
||||||
context.route.finish();
|
context.route.finish();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ public class SetSignalsToStop extends RouteAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fire(Context context) throws IOException {
|
public boolean fire(Context context) throws IOException {
|
||||||
context.route.setSignals(Signal.STOP);
|
context.route.setSignals(Signal.STOP);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,8 +25,12 @@ public class SpeedReduction extends RouteAction implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fire(Context context) {
|
public boolean fire(Context context) {
|
||||||
if (context.train != null && context.train.speed > maxSpeed) context.train.setSpeed(maxSpeed);
|
if (context.train != null && context.train.speed > maxSpeed) {
|
||||||
|
context.train.setSpeed(maxSpeed);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -9,7 +9,11 @@ public class TurnTrain extends RouteAction implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fire(Context context) {
|
public boolean fire(Context context) {
|
||||||
if (context.train != null) context.train.turn();
|
if (context.train != null) {
|
||||||
|
context.train.turn();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,10 +39,10 @@ import de.srsoftware.web4rail.tiles.Signal;
|
|||||||
public class Train implements Constants {
|
public class Train implements Constants {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
||||||
|
|
||||||
private static final HashMap<Long, Train> trains = new HashMap<>();
|
private static final HashMap<Integer, Train> trains = new HashMap<>();
|
||||||
|
|
||||||
public static final String ID = "id";
|
public static final String ID = "id";
|
||||||
public long id;
|
public int id;
|
||||||
|
|
||||||
private static final String NAME = "name";
|
private static final String NAME = "name";
|
||||||
private String name = null;
|
private String name = null;
|
||||||
@@ -98,8 +98,8 @@ public class Train implements Constants {
|
|||||||
this(loco,null);
|
this(loco,null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Train(Locomotive loco, Long id) {
|
public Train(Locomotive loco, Integer id) {
|
||||||
if (id == null) id = new Date().getTime();
|
if (id == null) id = new Date().hashCode();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
add(loco);
|
add(loco);
|
||||||
trains.put(id, this);
|
trains.put(id, this);
|
||||||
@@ -117,7 +117,7 @@ public class Train implements Constants {
|
|||||||
}
|
}
|
||||||
return t("No train id passed!");
|
return t("No train id passed!");
|
||||||
}
|
}
|
||||||
long id = Long.parseLong(params.get(Train.ID));
|
int id = Integer.parseInt(params.get(Train.ID));
|
||||||
Train train = trains.get(id);
|
Train train = trains.get(id);
|
||||||
if (train == null) return(t("No train with id {}!",id));
|
if (train == null) return(t("No train with id {}!",id));
|
||||||
switch (action) {
|
switch (action) {
|
||||||
@@ -189,7 +189,7 @@ public class Train implements Constants {
|
|||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Train get(long id) {
|
public static Train get(int id) {
|
||||||
return trains.get(id);
|
return trains.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ public class Train implements Constants {
|
|||||||
while (line != null) {
|
while (line != null) {
|
||||||
JSONObject json = new JSONObject(line);
|
JSONObject json = new JSONObject(line);
|
||||||
|
|
||||||
long id = json.getLong(ID);
|
int id = json.getInt(ID);
|
||||||
|
|
||||||
Train train = new Train(null,id);
|
Train train = new Train(null,id);
|
||||||
train.load(json).plan(plan);
|
train.load(json).plan(plan);
|
||||||
@@ -378,12 +378,24 @@ public class Train implements Constants {
|
|||||||
|
|
||||||
public static void saveAll(String filename) throws IOException {
|
public static void saveAll(String filename) throws IOException {
|
||||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||||
for (Entry<Long, Train> entry:trains.entrySet()) {
|
for (Entry<Integer, Train> entry:trains.entrySet()) {
|
||||||
Train train = entry.getValue();
|
Train train = entry.getValue();
|
||||||
file.write(train.json()+"\n");
|
file.write(train.json()+"\n");
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Select selector(Train preselected,Collection<Train> exclude) {
|
||||||
|
if (exclude == null) exclude = new Vector<Train>();
|
||||||
|
Select select = new Select(Train.class.getSimpleName());
|
||||||
|
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
||||||
|
for (Train train : Train.list()) {
|
||||||
|
if (exclude.contains(train)) continue;
|
||||||
|
Tag opt = select.addOption(train.id, train);
|
||||||
|
if (train == preselected) opt.attr("selected", "selected");
|
||||||
|
}
|
||||||
|
return select;
|
||||||
|
}
|
||||||
|
|
||||||
public void setSpeed(int v) {
|
public void setSpeed(int v) {
|
||||||
LOG.debug("Setting speed to {} kmh.",v);
|
LOG.debug("Setting speed to {} kmh.",v);
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import de.srsoftware.web4rail.tags.Button;
|
|||||||
import de.srsoftware.web4rail.tags.Checkbox;
|
import de.srsoftware.web4rail.tags.Checkbox;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
import de.srsoftware.web4rail.tags.Label;
|
import de.srsoftware.web4rail.tags.Label;
|
||||||
|
import de.srsoftware.web4rail.tags.Select;
|
||||||
|
|
||||||
public abstract class Block extends StretchableTile{
|
public abstract class Block extends StretchableTile{
|
||||||
private static final String NAME = "name";
|
private static final String NAME = "name";
|
||||||
@@ -22,7 +23,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
private static final String ALLOW_TURN = "allowTurn";
|
private static final String ALLOW_TURN = "allowTurn";
|
||||||
public boolean turnAllowed = false;
|
public boolean turnAllowed = false;
|
||||||
|
|
||||||
private static final String TRAIN = "train";
|
private static final String TRAIN = Train.class.getSimpleName();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject config() {
|
public JSONObject config() {
|
||||||
@@ -51,7 +52,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
name = json.has(NAME) ? json.getString(NAME) : "Block";
|
name = json.has(NAME) ? json.getString(NAME) : "Block";
|
||||||
turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN);
|
turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN);
|
||||||
if (json.has(TRAIN)) {
|
if (json.has(TRAIN)) {
|
||||||
Train tr = Train.get(json.getLong(TRAIN));
|
Train tr = Train.get(json.getInt(TRAIN));
|
||||||
train(tr);
|
train(tr);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@@ -65,13 +66,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
|
|
||||||
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
|
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
|
||||||
|
|
||||||
Tag select = new Tag("select").attr("name", TRAIN);
|
Select select = Train.selector(train, null);
|
||||||
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
|
||||||
for (Train train : Train.list()) {
|
|
||||||
Tag opt = new Tag("option").attr("value", ""+train.id);
|
|
||||||
if (this.train == train) opt.attr("selected", "selected");
|
|
||||||
opt.content(train.toString()).addTo(select);
|
|
||||||
}
|
|
||||||
select.addTo(new Label(t("Trains:")+" ")).addTo(new Tag("p")).addTo(form);
|
select.addTo(new Label(t("Trains:")+" ")).addTo(new Tag("p")).addTo(form);
|
||||||
|
|
||||||
return form;
|
return form;
|
||||||
@@ -118,7 +113,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
super.update(params);
|
super.update(params);
|
||||||
if (params.containsKey(NAME)) name=params.get(NAME);
|
if (params.containsKey(NAME)) name=params.get(NAME);
|
||||||
if (params.containsKey(TRAIN)) {
|
if (params.containsKey(TRAIN)) {
|
||||||
long trainId = Long.parseLong(params.get(TRAIN));
|
int trainId = Integer.parseInt(params.get(TRAIN));
|
||||||
Train t = Train.get(trainId);
|
Train t = Train.get(trainId);
|
||||||
if (t != null) {
|
if (t != null) {
|
||||||
Block oldBlock = t.block();
|
Block oldBlock = t.block();
|
||||||
|
|||||||
Reference in New Issue
Block a user