Browse Source
* Tile displaying text * TextDisplay: an action that sends text to clients * DetermineTrainInBlock: action that alters the context by requesting the train in a specific blocklookup-tables
14 changed files with 266 additions and 16 deletions
@ -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); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue