added new action SetDisplayText

This commit is contained in:
Stephan Richter
2020-11-20 01:16:05 +01:00
parent e7a6349c7c
commit 9e3556c05c
7 changed files with 116 additions and 3 deletions

View File

@@ -40,6 +40,14 @@ public abstract class Action extends BaseClass {
public Block block = null;
public Direction direction = null;
private Context(Contact c, Route r, Train t, Block b, Direction d) {
contact = c;
route = r;
train = t;
block = b;
direction = d;
}
public Context(Contact c) {
contact = c;
setRoute(contact.route());
@@ -52,6 +60,10 @@ public abstract class Action extends BaseClass {
public Context(Route route) {
setRoute(route);
}
protected Context clone() {
return new Context(contact, route, train, block, direction);
}
private void setRoute(Route route) {
this.route = route;
@@ -127,6 +139,7 @@ public abstract class Action extends BaseClass {
DetermineTrainInBlock.class,
FinishRoute.class,
SendCommand.class,
SetDisplayText.class,
SetPower.class,
SetRelay.class,
SetSignal.class,

View File

@@ -72,7 +72,7 @@ public class ConditionalAction extends Action {
for (Condition condition : conditions) {
if (!condition.fulfilledBy(context)) return true;
}
return actions.fire(context);
return actions.fire(context.clone()); // actions, that happen within the conditional action list must not modify the global context.
}
@Override

View File

@@ -55,7 +55,7 @@ public class DetermineTrainInBlock extends Action {
}
public String toString() {
return isSet(block) ? t("Determine, which train is in {}",block) : "["+t("click here to setup block")+"]";
return isSet(block) ? t("Determine, which train is in {}",block) : t("[Click here to select block!]");
};
@Override

View File

@@ -0,0 +1,78 @@
package de.srsoftware.web4rail.actions;
import java.util.HashMap;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Label;
import de.srsoftware.web4rail.tags.Select;
import de.srsoftware.web4rail.tiles.TextDisplay;
public class SetDisplayText extends TextAction{
private TextDisplay display;
private static final String DISPLAY = "display";
@Override
public boolean fire(Context context) {
plan.place(display.text(fill(text, context)));
return true;
}
@Override
public JSONObject json() {
return super.json().put(DISPLAY, display.id());
}
@Override
protected Label label() {
return new Label(t("Text to show on display:")+NBSP);
}
@Override
public Action load(JSONObject json) {
if (json.has(DISPLAY)) {
new Thread() { // load asynchronously, as referred tile may not be available,yet
public void run() {
try {
sleep(1000);
display = (TextDisplay) plan.get(json.getString(DISPLAY), false);
} catch (InterruptedException e) {}
};
}.start();
}
return super.load(json);
}
@Override
public Window properties(HashMap<String, String> params) {
Window win = super.properties(params);
Select select = TextDisplay.selector(display, null);
Tag label = select.addTo(new Label(t("Select display:")+NBSP));
for (Tag tag : win.children()) {
if (tag instanceof Form) {
tag.children().insertElementAt(label, 1);
break;
}
}
return win;
}
@Override
public String toString() {
return isNull(display) ? t("[Click here to select display!]") : t("Display \"{}\" on {}.",text,display);
}
@Override
protected Object update(HashMap<String, String> params) {
super.update(params);
String displayId = params.get(TextDisplay.class.getSimpleName());
if (isSet(displayId)) display = (TextDisplay) plan.get(displayId, false);
return properties(params);
}
}