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

@@ -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;
}