added new tiles and actions:

* Tile displaying text
* TextDisplay: an action that sends text to clients
* DetermineTrainInBlock: action that alters the context by requesting the train in a specific block
This commit is contained in:
Stephan Richter
2020-11-19 19:07:23 +01:00
parent 6f5b2f677c
commit 9ea3d7a65b
14 changed files with 266 additions and 16 deletions

View File

@@ -50,6 +50,7 @@ import de.srsoftware.web4rail.tiles.SignalS;
import de.srsoftware.web4rail.tiles.SignalW;
import de.srsoftware.web4rail.tiles.StraightH;
import de.srsoftware.web4rail.tiles.StraightV;
import de.srsoftware.web4rail.tiles.TextDisplay;
import de.srsoftware.web4rail.tiles.Tile;
import de.srsoftware.web4rail.tiles.Turnout.State;
import de.srsoftware.web4rail.tiles.Turnout3E;
@@ -804,6 +805,7 @@ public class Plan extends BaseClass{
new Turnout3E().tag(null).addTo(tiles);
new Relay().setLabel(true,"RL").tag(null).addTo(tiles);
new Contact().tag(null).addTo(tiles);
new TextDisplay().text("tx").tag(null).addTo(tiles);
new Eraser().tag(null).addTo(tiles);
return tiles.addTo(tileMenu);
}

View File

@@ -125,12 +125,14 @@ public abstract class Action extends BaseClass {
return List.of(
ConditionalAction.class,
DelayedAction.class,
DetermineTrainInBlock.class,
FinishRoute.class,
SendCommand.class,
SetPower.class,
SetRelay.class,
SetSignal.class,
SetSpeed.class,
ShowText.class,
StopAllTrains.class,
StopAuto.class,
TriggerContact.class,

View File

@@ -0,0 +1,70 @@
package de.srsoftware.web4rail.actions;
import java.io.IOException;
import java.util.HashMap;
import org.json.JSONObject;
import de.srsoftware.web4rail.Window;
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;
import de.srsoftware.web4rail.tiles.Block;
public class DetermineTrainInBlock extends Action {
private Block block = null;
@Override
public boolean fire(Context context) throws IOException {
context.block = block;
context.train = block.train();
return true;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
if (isSet(block)) json.put(BLOCK, block.id());
return json;
}
@Override
public Action load(JSONObject json) {
super.load(json);
String blockId = json.getString(BLOCK);
if (isSet(blockId)) block = Block.get(blockId);
return this;
}
@Override
public Window properties(HashMap<String, String> params) {
Window win = super.properties(params);
Form form = new Form("action-prop-form-"+id);
new Input(REALM,REALM_ACTIONS).hideIn(form);
new Input(ID,params.get(ID)).hideIn(form);
new Input(ACTION,ACTION_UPDATE).hideIn(form);
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
Select select = Block.selector(block, null);
select.addTo(new Label(t("Select block:")+NBSP)).addTo(form);
new Button(t("Apply"),form).addTo(form).addTo(win);
return win;
}
public String toString() {
return isSet(block) ? t("Determine, which train is in {}",block) : "["+t("click here to setup block")+"]";
};
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
String blockId = params.get(Block.class.getSimpleName());
if (isSet(blockId)) block = Block.get(blockId);
return properties(params);
}
}

View File

@@ -28,7 +28,7 @@ public class SendCommand extends Action{
}
});
return false;
return true;
}
@Override

View File

@@ -0,0 +1,23 @@
package de.srsoftware.web4rail.actions;
import de.srsoftware.web4rail.tags.Label;
public class ShowText extends TextAction{
@Override
public boolean fire(Context context) {
plan.stream(fill(text,context));
return true;
}
@Override
protected Label label() {
return new Label(t("Text to display on clients:")+NBSP);
}
@Override
public String toString() {
return t("Display \"{}\" on clients.",text);
}
}

View File

@@ -0,0 +1,66 @@
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.Button;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Label;
public abstract class TextAction extends Action {
public static final String TEXT = "text";
protected String text = "Hello, world!";
public String fill(String tx,Context context) {
if (isSet(context.block)) tx = tx.replace("%block%", context.block.name);
if (isSet(context.contact)) tx = tx.replace("%contact%", context.contact.id());
if (isSet(context.direction)) tx = tx.replace("%dir%", context.direction.name());
if (isSet(context.route)) tx = tx.replace("%route%", context.route.name());
if (isSet(context.train)) tx = tx.replace("%train%", context.train.name());
return tx;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
json.put(TEXT, text);
return json;
}
protected abstract Label label();
@Override
public Action load(JSONObject json) {
super.load(json);
text = json.getString(TEXT);
return this;
}
@Override
public Window properties(HashMap<String, String> params) {
Window win = super.properties(params);
Form form = new Form("action-prop-form-"+id);
new Input(REALM,REALM_ACTIONS).hideIn(form);
new Input(ID,params.get(ID)).hideIn(form);
new Input(ACTION,ACTION_UPDATE).hideIn(form);
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
new Input(TEXT, text).addTo(label()).addTo(form);
new Button(t("Apply"),form).addTo(form).addTo(win);
return win;
}
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
String error = null;
text = params.get(TEXT);
Window win = properties(params);
return new Tag("span").content(error).addTo(win);
}
}

View File

@@ -19,10 +19,7 @@ public class TriggerContact extends Action {
@Override
public boolean fire(Context context) throws IOException {
if (isSet(contact)) {
System.err.println("triggering "+contact);
contact.trigger(200);
}
if (isSet(contact)) return contact.trigger(200);
return false;
}

View File

@@ -304,7 +304,7 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
return this;
}
public static Tag selector(Block preselected,Collection<Block> exclude) {
public static Select selector(Block preselected,Collection<Block> exclude) {
if (isNull(exclude)) exclude = new Vector<Block>();
Select select = new Select(Block.class.getSimpleName());
new Tag("option").attr("value","0").content(t("unset")).addTo(select);

View File

@@ -204,7 +204,7 @@ public class Contact extends Tile{
return trigger;
}
public void trigger(int duration) throws IOException {
public boolean trigger(int duration) throws IOException {
activate(true);
new Thread() {
public void run() {
@@ -214,6 +214,7 @@ public class Contact extends Tile{
} catch (Exception e) {}
}
}.start();
return true;
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {

View File

@@ -0,0 +1,75 @@
package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Label;
public class TextDisplay extends StretchableTile {
private static final String TEXT = "text";
private String text = "Hello, world!";
@Override
public JSONObject json() {
return super.json().put(TEXT, text);
}
@Override
protected Tile load(JSONObject json) throws IOException {
super.load(json);
if (json.has(TEXT)) text = json.getString(TEXT);
return this;
}
@Override
public Tag tag(Map<String, Object> replacements) throws IOException {
if (isNull(replacements)) replacements = new HashMap<String, Object>();
replacements.put("%text%",text);
return super.tag(replacements);
}
@Override
public Form propForm(String id) {
noTrack();
Form form = super.propForm(id);
new Tag("h4").content(t("Text")).addTo(form);
new Input(TEXT, text).addTo(new Label(t("Text")+":"+NBSP)).addTo(new Tag("p")).addTo(form);
return form;
}
@Override
protected String stretchType() {
return t("Width");
}
public TextDisplay text(String tx) {
text = tx;
return this;
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
for (Entry<String, String> entry : params.entrySet()) {
switch (entry.getKey()) {
case TEXT:
text(entry.getValue());
break;
}
}
return super.update(params);
}
@Override
public int width() {
return stretch;
}
}

View File

@@ -58,6 +58,7 @@ public abstract class Tile extends BaseClass{
private static final String Y = "y";
private boolean disabled = false;
private boolean isTrack = true;
private int length = DEFAUT_LENGTH;
protected Direction oneWay = null;
protected Route route = null;
@@ -142,7 +143,7 @@ public abstract class Tile extends BaseClass{
}
public Tile length(int newLength) {
length = newLength;
length = Math.max(0, newLength);
return this;
}
@@ -187,6 +188,10 @@ public abstract class Tile extends BaseClass{
if (json.has(ONEW_WAY)) oneWay = Direction.valueOf(json.getString(ONEW_WAY));
return this;
}
protected void noTrack() {
isTrack = false;
}
public Tile position(int x, int y) {
this.x = x;
@@ -242,12 +247,14 @@ public abstract class Tile extends BaseClass{
if (isSet(route)) link("p",Map.of(REALM,REALM_ROUTE,ID,route.id(),ACTION,ACTION_PROPS),t("Locked by {}",route)).addTo(window);
Form form = propForm("tile-properties-"+id());
new Tag("h4").content(t("Length")).addTo(form);
new Input(LENGTH,length).numeric().addTo(new Label(t("Length")+":"+NBSP)).addTo(form);
new Tag("h4").content(t("Availability")).addTo(form);
Checkbox cb = new Checkbox(DISABLED, t("disabled"), disabled);
if (disabled) cb.clazz("disabled");
cb.addTo(form);
if (isTrack) {
new Tag("h4").content(t("Length")).addTo(form);
new Input(LENGTH,length).numeric().addTo(new Label(t("Length")+":"+NBSP)).addTo(form);
new Tag("h4").content(t("Availability")).addTo(form);
Checkbox cb = new Checkbox(DISABLED, t("disabled"), disabled);
if (disabled) cb.clazz("disabled");
cb.addTo(form);
}
new Button(t("Apply"),form).addTo(form);
form.addTo(window);