improved actions and conditions code,
added TrainLength condition, added FreeStartBlock action
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.9.6</version>
|
||||
<version>0.9.7</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -1,17 +1,29 @@
|
||||
Actions : Aktionen
|
||||
Action type\: : Aktions-Typ
|
||||
Added {} : {} hinzugefügt
|
||||
add action : Aktion hinzufügen
|
||||
Add condition : Bedingung hinzufügen
|
||||
Add tile : Kachel hinzufügen
|
||||
Analyze plan : Plan analysieren
|
||||
Apply : Übernehmen
|
||||
ConditionalAction : bedingte Aktion
|
||||
Conditions : Bedingungen
|
||||
Contacts and actions : Kontakte und Aktionen
|
||||
Actions and contacts : Aktionen und Kontakte
|
||||
Create action : Aktion erzeugen
|
||||
DelayedAction : verzögerte Aktion
|
||||
delete route : Route löschen
|
||||
Destination\: {} from {} : Ziel: {} von {}
|
||||
Emergency : Notfall
|
||||
FinishRoute : Route abschließen
|
||||
FreeStartBlock : Start-Block freigeben
|
||||
inverted : invertiert
|
||||
length\: : Länge:
|
||||
Maximum train length\: : maximale Zug-Länge
|
||||
name\: : Name:
|
||||
Origin and destination : Start und Ziel
|
||||
Origin\: {} to {} : Start: {} nach {}
|
||||
Plan saved as "{}". : Plan als „{}“ gespeichert.
|
||||
PowerOff : Strom abschalten
|
||||
Properties : Eigenschaften
|
||||
Properties of {} : Eigenschaften von {}
|
||||
Properties of {} @ ({},{}) : Eigenschaften von {} @ ({},{})
|
||||
@@ -19,6 +31,19 @@ Reduce speed to {} km/h : Geschwindigkeit auf {} km/h reduzieren
|
||||
Routes using this tile\: : Fahrstraßen, die diesen Abschnitt verwenden:
|
||||
save : speichern
|
||||
Save plan : Plan speichern
|
||||
SetRelay : Relais schalten
|
||||
SetSignalsToStop : Signale auf Halt stellen
|
||||
SetSpeed : Geschwindigkeit ändern
|
||||
Setup actions : Aktivierungs-Aktionen
|
||||
Signals : Signale
|
||||
StopAuto : Automatikmodus abschalten
|
||||
Train\: : Zug:
|
||||
train is longer than {} : Zug ist länger als {}
|
||||
train is shorter than {} : Zug ist kürzer als {}
|
||||
Trains\: : Züge:
|
||||
TrainLength : Zuglänge
|
||||
TrainSelect : Zug-Auswahl
|
||||
Turn allowed : Wenden erlaubt
|
||||
Turnouts : Weichen
|
||||
TurnTrain : Fahrtrichtung umkehren
|
||||
Unknown action\: {} : Unbekannte Aktion: {}
|
||||
@@ -26,15 +26,14 @@ import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.actions.ActionList;
|
||||
import de.srsoftware.web4rail.actions.ActivateRoute;
|
||||
import de.srsoftware.web4rail.actions.FinishRoute;
|
||||
import de.srsoftware.web4rail.actions.FreeStartBlock;
|
||||
import de.srsoftware.web4rail.actions.SetSignalsToStop;
|
||||
import de.srsoftware.web4rail.actions.SetSpeed;
|
||||
import de.srsoftware.web4rail.conditions.Condition;
|
||||
import de.srsoftware.web4rail.conditions.TrainSelect;
|
||||
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.Select;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
import de.srsoftware.web4rail.tiles.Shadow;
|
||||
@@ -77,6 +76,7 @@ public class Route implements Constants{
|
||||
private static final String ACTION_LISTS = "action_lists";
|
||||
private static final String ROUTES = "routes";
|
||||
private static final String CONDITIONS = "conditions";
|
||||
private static final String DROP_CONDITION = "drop_condition";
|
||||
|
||||
/**
|
||||
* process commands from the client
|
||||
@@ -103,10 +103,26 @@ public class Route implements Constants{
|
||||
return route.properties(params);
|
||||
case ACTION_UPDATE:
|
||||
return route.update(params,plan);
|
||||
case DROP_CONDITION:
|
||||
return route.dropCodition(params);
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
|
||||
private Object dropCodition(HashMap<String, String> params) {
|
||||
String condId = params.get(REALM_CONDITION);
|
||||
if (condId != null) {
|
||||
int cid = Integer.parseInt(condId);
|
||||
for (Condition condition : conditions) {
|
||||
if (condition.id() == cid) {
|
||||
conditions.remove(condition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return properties(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Route wurde von Zug betreten
|
||||
* @throws IOException
|
||||
@@ -167,18 +183,16 @@ public class Route implements Constants{
|
||||
}
|
||||
}
|
||||
|
||||
private void addCondition(String type) {
|
||||
switch (type) {
|
||||
case "TrainSelect":
|
||||
conditions.add(new TrainSelect());
|
||||
}
|
||||
}
|
||||
|
||||
private void addConditionsTo(Window win) {
|
||||
new Tag("h4").content(t("Conditions")).addTo(win);
|
||||
if (!conditions.isEmpty()) {
|
||||
Tag list = new Tag("ul");
|
||||
for (Condition condition : conditions) condition.link("li",REALM_ROUTE+":"+id).addTo(list);
|
||||
for (Condition condition : conditions) {
|
||||
Tag li = new Tag("li");
|
||||
condition.link("span",REALM_ROUTE+":"+id).addTo(li);
|
||||
Map<String, Object> params = Map.of(REALM,REALM_ROUTE,ID,id(),ACTION,DROP_CONDITION,REALM_CONDITION,condition.id());
|
||||
new Button(t("delete"), params).addTo(li).addTo(list);
|
||||
}
|
||||
list.addTo(win);
|
||||
}
|
||||
|
||||
@@ -187,19 +201,16 @@ public class Route implements Constants{
|
||||
new Input(ID,id()).hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).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);
|
||||
Condition.selector().addTo(form);
|
||||
new Button(t("Add condition"),form).addTo(form).addTo(win);
|
||||
}
|
||||
|
||||
private void addContactsTo(Window win) {
|
||||
if (!contacts.isEmpty()) {
|
||||
new Tag("h4").content(t("Actions and Contacts")).addTo(win);
|
||||
new Tag("h4").content(t("Actions and contacts")).addTo(win);
|
||||
Tag list = new Tag("ol");
|
||||
|
||||
Tag setup = new Tag("li").content("Setup actions");
|
||||
Tag setup = new Tag("li").content(t("Setup actions"));
|
||||
setupActions.addTo(setup, context());
|
||||
setup.addTo(list);
|
||||
for (Contact c : contacts) {
|
||||
@@ -277,12 +288,13 @@ public class Route implements Constants{
|
||||
if (contacts.size()>1) { // mindestens 2 Kontakte: erster Kontakt aktiviert Block, vorletzter Kontakt leitet Bremsung ein
|
||||
add(contacts.firstElement().trigger(),new ActivateRoute());
|
||||
Contact nextToLastContact = contacts.get(contacts.size()-2);
|
||||
add(nextToLastContact.trigger(),new SetSpeed(30));
|
||||
add(nextToLastContact.trigger(),new SetSpeed().speed(30));
|
||||
add(nextToLastContact.trigger(),new SetSignalsToStop());
|
||||
}
|
||||
if (!contacts.isEmpty()) {
|
||||
Contact lastContact = contacts.lastElement();
|
||||
add(lastContact.trigger(), new SetSpeed(0));
|
||||
add(lastContact.trigger(), new SetSpeed());
|
||||
add(lastContact.trigger(), new FreeStartBlock());
|
||||
add(lastContact.trigger(), new FinishRoute());
|
||||
}
|
||||
}
|
||||
@@ -313,7 +325,6 @@ public class Route implements Constants{
|
||||
}
|
||||
|
||||
public void finish() throws IOException {
|
||||
startBlock.train(null);
|
||||
train.route = null;
|
||||
unlock();
|
||||
endBlock.train(train.heading(endDirection.inverse()));
|
||||
@@ -332,6 +343,11 @@ public class Route implements Constants{
|
||||
return true;
|
||||
}
|
||||
|
||||
public Route freeStartBlock() throws IOException {
|
||||
startBlock.train(null);
|
||||
return this;
|
||||
}
|
||||
|
||||
private String generateName() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i=0; i<path.size();i++) {
|
||||
@@ -599,9 +615,9 @@ public class Route implements Constants{
|
||||
String name = params.get(NAME);
|
||||
if (name != null) name(name);
|
||||
|
||||
String condition = params.get(REALM_CONDITION);
|
||||
Condition condition = Condition.create(params.get(REALM_CONDITION));
|
||||
if (condition != null) {
|
||||
addCondition(condition);
|
||||
conditions.add(condition);
|
||||
return properties(params);
|
||||
}
|
||||
String message = t("{} updated.",this);
|
||||
|
||||
@@ -2,7 +2,10 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
@@ -16,11 +19,14 @@ import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
|
||||
public abstract class Action implements Constants {
|
||||
private static final HashMap<Integer,Action> actions = new HashMap<Integer, Action>();
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||
private static final String PREFIX = Action.class.getPackageName();
|
||||
protected int id;
|
||||
|
||||
public static class Context {
|
||||
@@ -47,31 +53,20 @@ public abstract class Action implements Constants {
|
||||
}
|
||||
|
||||
public static Action create(String type) {
|
||||
switch (type) { // TODO: das kann man mit Reflection generischer lösen
|
||||
case "ConditionalAction":
|
||||
return new ConditionalAction();
|
||||
case "FinishRoute":
|
||||
return new FinishRoute();
|
||||
case "SetSignalsToStop":
|
||||
return new SetSignalsToStop();
|
||||
case "SetSpeed":
|
||||
return new SetSpeed(0);
|
||||
case "TurnTrain":
|
||||
return new TurnTrain();
|
||||
case "StopAuto":
|
||||
return new StopAuto();
|
||||
case "PowerOff":
|
||||
return new PowerOff();
|
||||
case "SetRelay":
|
||||
return new SetRelay();
|
||||
case "DelayedAction":
|
||||
return new DelayedAction();
|
||||
try {
|
||||
return (Action) Class.forName(PREFIX+"."+type).getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract boolean fire(Context context) throws IOException;
|
||||
|
||||
public static Action get(int actionId) {
|
||||
return actions.get(actionId);
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
@@ -86,30 +81,23 @@ public abstract class Action implements Constants {
|
||||
return new Tag("span").content(toString()+NBSP).attr("onclick", action);
|
||||
}
|
||||
|
||||
public static Action load(JSONObject json) {
|
||||
String clazz = json.getString(TYPE);
|
||||
switch (clazz) { // TODO: das kann generisch mittels Reflection implementiert werden!
|
||||
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 "SetSpeed":
|
||||
return SetSpeed.load(json);
|
||||
case "SetRelay":
|
||||
return SetRelay.load(json);
|
||||
case "TurnTrain":
|
||||
return new TurnTrain();
|
||||
case "DelayedAction":
|
||||
return DelayedAction.load(json);
|
||||
}
|
||||
LOG.error("Found unknwon action \"{}\" in json!",clazz);
|
||||
return null;
|
||||
public static List<Class<? extends Action>> list() {
|
||||
return List.of(
|
||||
ConditionalAction.class,
|
||||
SetSpeed.class,
|
||||
SetSignalsToStop.class,
|
||||
FreeStartBlock.class,
|
||||
FinishRoute.class,
|
||||
TurnTrain.class,
|
||||
StopAuto.class,
|
||||
PowerOff.class,
|
||||
SetRelay.class,
|
||||
DelayedAction.class
|
||||
);
|
||||
}
|
||||
|
||||
public Action load(JSONObject json) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Window properties(HashMap<String, String> params) {
|
||||
@@ -129,7 +117,16 @@ public abstract class Action implements Constants {
|
||||
return t("Nothing changed");
|
||||
}
|
||||
|
||||
public static Action get(int actionId) {
|
||||
return actions.get(actionId);
|
||||
public static Tag selector() {
|
||||
Select select = new Select(TYPE);
|
||||
TreeMap<String, String> names = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
for (Class<? extends Action> clazz : Action.list()) {
|
||||
String s = t(clazz.getSimpleName());
|
||||
names.put(s, clazz.getSimpleName());
|
||||
}
|
||||
|
||||
for (Entry<String, String> entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey());
|
||||
return select.addTo(new Label(t("Action type:")+NBSP));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,6 @@ 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{
|
||||
|
||||
@@ -55,20 +53,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
new Input(ID,id).hideIn(typeForm);
|
||||
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
|
||||
new Input(CONTEXT,context).hideIn(typeForm);
|
||||
Select select = new Select(TYPE);
|
||||
List<Class<? extends Action>> classes = List.of(
|
||||
ConditionalAction.class,
|
||||
SetSpeed.class,
|
||||
SetSignalsToStop.class,
|
||||
FinishRoute.class,
|
||||
TurnTrain.class,
|
||||
StopAuto.class,
|
||||
PowerOff.class,
|
||||
SetRelay.class,
|
||||
DelayedAction.class
|
||||
);
|
||||
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||
select.addTo(new Label(t("Action type:")+NBSP)).addTo(typeForm);
|
||||
Action.selector().addTo(typeForm);
|
||||
return new Button(t("Create action"),typeForm).addTo(typeForm).addTo(win);
|
||||
}
|
||||
|
||||
@@ -161,8 +146,9 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
ActionList actionList = new ActionList();
|
||||
for (Object o : list) {
|
||||
if (o instanceof JSONObject) {
|
||||
Action action = Action.load((JSONObject) o);
|
||||
if (action != null) actionList.add(action);
|
||||
JSONObject json = (JSONObject) o;
|
||||
Action action = Action.create(json.getString(TYPE));
|
||||
if (action != null) actionList.add(action.load(json));
|
||||
}
|
||||
}
|
||||
return actionList;
|
||||
|
||||
@@ -2,7 +2,6 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.json.JSONArray;
|
||||
@@ -11,12 +10,10 @@ import org.json.JSONObject;
|
||||
import de.srsoftware.tools.Tag;
|
||||
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.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
|
||||
public class ConditionalAction extends Action {
|
||||
|
||||
@@ -50,10 +47,7 @@ public class ConditionalAction extends Action {
|
||||
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);
|
||||
Condition.selector().addTo(form);
|
||||
return new Button(t("Add condition"),form).addTo(form).addTo(fieldset);
|
||||
}
|
||||
|
||||
@@ -75,16 +69,17 @@ public class ConditionalAction extends Action {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static ConditionalAction load(JSONObject json) {
|
||||
ConditionalAction action = new ConditionalAction();
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
for (Object o : json.getJSONArray(CONDITIONS)) {
|
||||
if (o instanceof JSONObject) {
|
||||
Condition condition = Condition.load((JSONObject)o);
|
||||
if (condition != null) action.conditions.add(condition);
|
||||
if (condition != null) conditions.add(condition);
|
||||
}
|
||||
}
|
||||
action.actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return action;
|
||||
actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,16 +104,9 @@ public class ConditionalAction extends Action {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
Condition condition = Condition.create(conditionClass);
|
||||
if (condition == null) return t("Unknown type of condition: {}",conditionClass);
|
||||
conditions.add(condition);
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,11 +70,11 @@ public class DelayedAction extends Action {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static DelayedAction load(JSONObject json) {
|
||||
DelayedAction action = new DelayedAction();
|
||||
action.delay = json.getInt(DELAY);
|
||||
if (json.has(ACTIONS)) action.actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return action;
|
||||
public DelayedAction load(JSONObject json) {
|
||||
super.load(json);
|
||||
delay = json.getInt(DELAY);
|
||||
if (json.has(ACTIONS)) actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class FreeStartBlock extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
context.route.freeStartBlock();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -39,14 +39,15 @@ public class SetRelay extends Action {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static SetRelay load(JSONObject json) {
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
String relayId = json.getString(RELAY);
|
||||
SetRelay result = new SetRelay();
|
||||
if (relayId != null) {
|
||||
result.relay = Relay.get(relayId);
|
||||
result.state = json.getBoolean(Relay.STATE);
|
||||
relay = Relay.get(relayId);
|
||||
state = json.getBoolean(Relay.STATE);
|
||||
}
|
||||
return result;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,12 +14,7 @@ import de.srsoftware.web4rail.tags.Label;
|
||||
public class SetSpeed extends Action{
|
||||
|
||||
public static final String MAX_SPEED = "max_speed";
|
||||
private int maxSpeed = -1;
|
||||
|
||||
public SetSpeed(int kmh) {
|
||||
super();
|
||||
maxSpeed = kmh;
|
||||
}
|
||||
private int maxSpeed = 0;
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
@@ -37,9 +32,12 @@ 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 Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
maxSpeed = json.getInt(MAX_SPEED);
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,6 +59,11 @@ public class SetSpeed extends Action{
|
||||
public String toString() {
|
||||
return t("Reduce speed to {} km/h",maxSpeed);
|
||||
}
|
||||
|
||||
public SetSpeed speed(int kmh) {
|
||||
maxSpeed = kmh;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
@@ -18,10 +21,13 @@ import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
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 abstract class Condition implements Constants {
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Condition.class);
|
||||
private static final String INVERTED = "inverted";
|
||||
private static final String PREFIX = Condition.class.getPackageName();
|
||||
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>();
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
public boolean inverted = false;
|
||||
@@ -55,6 +61,15 @@ public abstract class Condition implements Constants {
|
||||
return t("Unknown action: {}",action);
|
||||
}
|
||||
|
||||
public static Condition create(String type) {
|
||||
try {
|
||||
return (Condition) Class.forName(PREFIX+"."+type).getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public JSONObject json() {
|
||||
JSONObject json = new JSONObject().put(TYPE, getClass().getSimpleName());
|
||||
if (inverted) json.put(INVERTED, true);
|
||||
@@ -63,12 +78,7 @@ public abstract class Condition implements Constants {
|
||||
|
||||
public static Condition load(JSONObject json) {
|
||||
String type = json.getString(TYPE);
|
||||
Condition condition = null;
|
||||
switch (type) {
|
||||
case "TrainSelect":
|
||||
condition = TrainSelect.load(json);
|
||||
break;
|
||||
}
|
||||
Condition condition = Condition.create(type);
|
||||
if (condition != null) condition.inverted = json.has(INVERTED) && json.getBoolean(INVERTED);
|
||||
return condition;
|
||||
}
|
||||
@@ -95,6 +105,23 @@ public abstract class Condition implements Constants {
|
||||
return win;
|
||||
}
|
||||
|
||||
public static Tag selector() {
|
||||
Select select = new Select(REALM_CONDITION);
|
||||
TreeMap<String, String> names = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
for (Class<? extends Condition> clazz : list()) {
|
||||
String s = t(clazz.getSimpleName());
|
||||
names.put(s, clazz.getSimpleName());
|
||||
}
|
||||
|
||||
for (Entry<String, String> entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey());
|
||||
return select.addTo(new Label(t("Action type:")+NBSP));
|
||||
}
|
||||
|
||||
private static List<Class<? extends Condition>> list() {
|
||||
return List.of(TrainSelect.class,TrainLength.class);
|
||||
}
|
||||
|
||||
public static String t(String text, Object...fills) {
|
||||
return Translation.get(Application.class, text, fills);
|
||||
}
|
||||
@@ -108,4 +135,8 @@ public abstract class Condition implements Constants {
|
||||
inverted = "on".equals(params.get(INVERTED));
|
||||
return t("updated {}.",this);
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class TrainLength extends Condition {
|
||||
|
||||
private static final String MAX_LENGTH = "max_length";
|
||||
private int maxLength = 0;
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
return context.train.length() < maxLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(MAX_LENGTH, maxLength);
|
||||
}
|
||||
|
||||
public static TrainLength load(JSONObject json) {
|
||||
TrainLength tl = new TrainLength();
|
||||
if (json.has(MAX_LENGTH)) tl.maxLength = json.getInt(MAX_LENGTH);
|
||||
return tl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag propForm(HashMap<String, String> params) {
|
||||
return new Input(MAX_LENGTH, maxLength).addTo(new Label(t("Maximum train length:")+NBSP)).addTo(super.propForm(params));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t(inverted ? "train is longer than {}" : "train is shorter than {}",maxLength) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (params.containsKey(MAX_LENGTH)) try {
|
||||
int ml = Integer.parseInt(params.get(MAX_LENGTH));
|
||||
if (ml < 1) throw new NumberFormatException(t("length must be larger than zero!"));
|
||||
maxLength = ml;
|
||||
} catch (NumberFormatException nfe) {
|
||||
Window win = properties(params);
|
||||
win.children().insertElementAt(new Tag("div").content(nfe.getMessage()),1);
|
||||
return win;
|
||||
}
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
@@ -151,19 +151,6 @@ public class Train implements Constants {
|
||||
} else cars.add(car);
|
||||
return this;
|
||||
}
|
||||
|
||||
private static Object create(HashMap<String, String> params, Plan plan) {
|
||||
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
|
||||
if (loco == null) return t("unknown locomotive: {}",params.get(ID));
|
||||
Train train = new Train(loco).plan(plan);
|
||||
if (params.containsKey(NAME)) train.name(params.get(NAME));
|
||||
return train;
|
||||
}
|
||||
|
||||
private Train name(String newName) {
|
||||
this.name = newName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void add(Car car) {
|
||||
if (car == null) return;
|
||||
@@ -189,6 +176,14 @@ public class Train implements Constants {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
private static Object create(HashMap<String, String> params, Plan plan) {
|
||||
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
|
||||
if (loco == null) return t("unknown locomotive: {}",params.get(ID));
|
||||
Train train = new Train(loco).plan(plan);
|
||||
if (params.containsKey(NAME)) train.name(params.get(NAME));
|
||||
return train;
|
||||
}
|
||||
|
||||
public static Train get(int id) {
|
||||
return trains.get(id);
|
||||
}
|
||||
@@ -321,6 +316,11 @@ public class Train implements Constants {
|
||||
return result;
|
||||
}
|
||||
|
||||
private Train name(String newName) {
|
||||
this.name = newName;
|
||||
return this;
|
||||
}
|
||||
|
||||
private Train plan(Plan plan) {
|
||||
this.plan = plan;
|
||||
return this;
|
||||
|
||||
@@ -67,7 +67,7 @@ public abstract class Block extends StretchableTile{
|
||||
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
|
||||
|
||||
Select select = Train.selector(train, null);
|
||||
select.addTo(new Label(t("Trains:")+" ")).addTo(new Tag("p")).addTo(form);
|
||||
select.addTo(new Label(t("Trains:")+NBSP)).addTo(new Tag("p")).addTo(form);
|
||||
|
||||
return form;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user