implemented AddDestination action and evaluation of destination tags of trains.
This commit is contained in:
@@ -41,6 +41,7 @@ public abstract class Action extends BaseClass {
|
||||
|
||||
public static List<Class<? extends Action>> classes() {
|
||||
return List.of(
|
||||
AddDestination.class,
|
||||
AddRemoveTag.class,
|
||||
AlterDirection.class,
|
||||
BrakeCancel.class,
|
||||
|
||||
118
src/main/java/de/srsoftware/web4rail/actions/AddDestination.java
Normal file
118
src/main/java/de/srsoftware/web4rail/actions/AddDestination.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class AddDestination extends Action {
|
||||
|
||||
private static final String TURN = "turn";
|
||||
private Block destination;
|
||||
private boolean turnAtDestination;
|
||||
|
||||
public AddDestination(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
Train train = context.train();
|
||||
if (isNull(train)) return false;
|
||||
if (isNull(destination)) { // clear destinations!
|
||||
Iterator<String> it = train.tags().iterator();
|
||||
while (it.hasNext()) {
|
||||
String tag = it.next();
|
||||
if (tag.startsWith("@")) it = train.removeTag(tag);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
String dest = "@"+destination.id()+(turnAtDestination?"+turn":"");
|
||||
for (String tag: train.tags()) {
|
||||
if (tag.startsWith("@")) {
|
||||
train.removeTag(tag);
|
||||
dest = tag+dest;
|
||||
break;
|
||||
}
|
||||
}
|
||||
train.addTag(dest);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (isSet(destination)) json.put(Train.DESTINATION,destination.id().toString());
|
||||
if (turnAtDestination) json.put(TURN,true);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
if (json.has(TURN)) turnAtDestination = json.getBoolean(TURN);
|
||||
if (json.has(Train.DESTINATION)) {
|
||||
Id blockId = new Id(json.getString(Train.DESTINATION));
|
||||
destination = BaseClass.get(blockId);
|
||||
if (isNull(destination)) {
|
||||
Application.threadPool.execute(new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
destination = BaseClass.get(blockId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
Tag span = new Tag("span");
|
||||
button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,Train.DESTINATION)).addTo(span);
|
||||
button(t("Clear destinations"),Map.of(ACTION,ACTION_UPDATE,Train.DESTINATION,"0")).addTo(span);
|
||||
formInputs.add(t("Destination")+": "+(isNull(destination) ? t("Clear destinations") : destination),span);
|
||||
formInputs.add(t("Turn at destination"),new Checkbox(TURN, t("Turn"), turnAtDestination));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return isSet(destination) ? t("Add {} to destinations of train",destination) : t("Clear destinations of train");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (params.containsKey(Train.DESTINATION)) {
|
||||
String destId = params.get(Train.DESTINATION);
|
||||
if ("0".equals(destId)) {
|
||||
destination = null;
|
||||
} else {
|
||||
Tile tile = plan.get(new Id(destId), true);
|
||||
if (tile instanceof Block) {
|
||||
destination = (Block) tile;
|
||||
} else {
|
||||
return t("Clicked tile is not a {}!",t("block"));
|
||||
}
|
||||
}
|
||||
}
|
||||
turnAtDestination = "on".equals(params.get(TURN));
|
||||
return context().properties();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -10,6 +11,7 @@ import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class DetermineTrainInBlock extends Action {
|
||||
|
||||
@@ -57,7 +59,7 @@ public class DetermineTrainInBlock extends Action {
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select block"),Block.selector(block, null));
|
||||
formInputs.add(t("Block")+": "+(isNull(block) ? t("unset") : block),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,BLOCK)));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -74,8 +76,12 @@ public class DetermineTrainInBlock extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
Id blockId = Id.from(params,Block.class.getSimpleName());
|
||||
if (isSet(blockId)) block = Block.get(blockId);
|
||||
return properties();
|
||||
if (params.containsKey(BLOCK)) {
|
||||
Tile tile = plan.get(new Id(params.get(BLOCK)), true);
|
||||
if (tile instanceof Block) {
|
||||
block = (Block) tile;
|
||||
} else return t("Clicked tile is not a {}!",t("block"));
|
||||
}
|
||||
return context().properties();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -11,6 +12,7 @@ import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tiles.TextDisplay;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class SetDisplayText extends TextAction{
|
||||
|
||||
@@ -29,7 +31,9 @@ public class SetDisplayText extends TextAction{
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(DISPLAY, display.id());
|
||||
JSONObject json = super.json();
|
||||
if (isSet(display)) json.put(DISPLAY, display.id());
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +64,7 @@ public class SetDisplayText extends TextAction{
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select display"),TextDisplay.selector(display, null));
|
||||
formInputs.add(t("Display")+": "+(isNull(display) ? t("unset") : display),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,DISPLAY)));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -70,9 +74,14 @@ public class SetDisplayText extends TextAction{
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
String displayId = params.get(TextDisplay.class.getSimpleName());
|
||||
if (isSet(displayId)) display = (TextDisplay) plan.get(new Id(displayId), false);
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (params.containsKey(DISPLAY)) {
|
||||
Tile object = plan.get(new Id(params.get(DISPLAY)), true);
|
||||
if (object instanceof TextDisplay) {
|
||||
display = (TextDisplay) object;
|
||||
} else return t("Clicked tile is not a {}!",t("display"));
|
||||
|
||||
}
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -73,8 +74,7 @@ public class SetTurnout extends Action {
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
|
||||
formInputs.add(t("Select turnout"),Turnout.selector(turnout,null));
|
||||
|
||||
formInputs.add(t("Turnout")+": "+(isNull(turnout) ? t("unset") : turnout),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,TURNOUT)));
|
||||
if (isSet(turnout)) {
|
||||
Select select = new Select(Turnout.STATE);
|
||||
|
||||
@@ -112,8 +112,12 @@ public class SetTurnout extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
Id turnoutId = new Id(params.get(TURNOUT));
|
||||
turnout = BaseClass.get(turnoutId);
|
||||
if (params.containsKey(TURNOUT)) {
|
||||
BaseClass object = BaseClass.get(new Id(params.get(TURNOUT)));
|
||||
if (object instanceof Turnout) {
|
||||
turnout = (Turnout) object;
|
||||
} else return t("Clicked tile is not a {}!",t("turnout"));
|
||||
}
|
||||
String st = params.get(Turnout.STATE);
|
||||
if (isSet(st)) state = Turnout.State.valueOf(st);
|
||||
return super.update(params);
|
||||
|
||||
@@ -54,7 +54,7 @@ public abstract class TextAction extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
text = params.get(TEXT);
|
||||
if (params.containsKey(TEXT)) text = params.get(TEXT);
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user