improved processing of display texts
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>1.5.38</version>
|
||||
<version>1.5.39</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -177,8 +177,8 @@ Effect : Effekt
|
||||
else\: : falls nicht:
|
||||
Emergency : Notfall
|
||||
empty train : leerer Zug
|
||||
enable : aktivieren
|
||||
enable {} : {} aktivieren
|
||||
enable : entsperren
|
||||
enable {} : {} entsperren
|
||||
Engage {} : {} aktivieren
|
||||
EngageDecoupler : Entkuppler aktivieren
|
||||
Enter new name for plan : Neuen Namen für den Plan eingeben
|
||||
|
||||
@@ -12,7 +12,7 @@ import de.srsoftware.web4rail.tags.Select;
|
||||
public class Store {
|
||||
|
||||
public interface Listener{
|
||||
public void storeUpdated(Store store);
|
||||
public void storeUpdated();
|
||||
}
|
||||
|
||||
private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(Store.class);
|
||||
@@ -28,8 +28,9 @@ public class Store {
|
||||
stores.put(name, this);
|
||||
}
|
||||
|
||||
public void addListener(Listener listener) {
|
||||
public Store addListener(Listener listener) {
|
||||
listeners.add(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Store get(String name) {
|
||||
@@ -80,7 +81,7 @@ public class Store {
|
||||
intValue = BaseClass.isNull(intValue) ? add : 10*intValue + add;
|
||||
}
|
||||
}
|
||||
listeners.forEach(listener -> listener.storeUpdated(this));
|
||||
listeners.forEach(listener -> listener.storeUpdated());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -129,6 +129,7 @@ public class AddRemoveDestination extends Action {
|
||||
if (isNull(destination)) return t("Clear destinations of train");
|
||||
String suffix = destination.turn() ? t("Turn") : null;
|
||||
if (destination.shunting()) suffix = (isSet(suffix) ? suffix+" + " : "")+t("Shunting");
|
||||
if (isSet(destinationTrigger)) suffix = (isSet(suffix) ? suffix+" + " : "")+t("Trigger {}",destinationTrigger);
|
||||
return t("Add {} to destinations of train",destination)+(isSet(suffix) ? " ("+suffix+")" : "");
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,6 @@ public class DisableEnableTile extends Action {
|
||||
if (newTile instanceof Shadow) newTile = ((Shadow)newTile).overlay();
|
||||
if (isSet(newTile)) tile = newTile;
|
||||
disable = !"enable".equals(params.get(STATE));
|
||||
return parent().properties();
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,23 +96,23 @@ public class RoutePrepper extends BaseClass implements Runnable{
|
||||
context = c;
|
||||
}
|
||||
|
||||
private static PriorityQueue<Trail> availableRoutes(Context c){
|
||||
private static PriorityQueue<Trail> availableRoutes(Context context){
|
||||
boolean error = false;
|
||||
|
||||
Block startBlock = c.block();
|
||||
Block startBlock = context.block();
|
||||
if (isNull(startBlock) && (error=true)) LOG.warn("RoutePrepper.availableRoutes(…) called without a startBlock!");
|
||||
|
||||
Train train = c.train();
|
||||
Train train = context.train();
|
||||
if (isNull(train) && (error=true)) LOG.warn("RoutePrepper.availableRoutes(…) called without a startBlock!");
|
||||
|
||||
if (error) return new PriorityQueue<>();
|
||||
|
||||
Destination destination = train.destination();
|
||||
|
||||
Direction startDirection = c.direction();
|
||||
Direction startDirection = context.direction();
|
||||
LOG.debug("RoutePrepper.availableRoutes({},{},{}), dest = {}",startBlock,startDirection,train,destination);
|
||||
|
||||
PriorityQueue<Trail> candidates = routesFrom(c); // map: route → score
|
||||
PriorityQueue<Trail> candidates = routesFrom(context); // map: route → score
|
||||
|
||||
if (isNull(destination) || train.isShunting()) {
|
||||
LOG.debug("{} has no destination, returning {}",train,candidates);
|
||||
@@ -121,14 +121,14 @@ public class RoutePrepper extends BaseClass implements Runnable{
|
||||
|
||||
LOG.debug("{} is heading for {}, starting breadth-first search…",train,destination);
|
||||
|
||||
for (int depth=1; depth<99; depth++) {
|
||||
for (int depth=1; depth<33; depth++) {
|
||||
if (candidates.stream().filter(trail -> trail.endsAt(destination)).count() > 0) return candidates; // return connectingTrails + other routes, in case all connecting trails are occupied
|
||||
|
||||
PriorityQueue<Trail> nextLevelCandidates = new PriorityQueue<Trail>();
|
||||
for (Trail trail : candidates) {
|
||||
Route lastRoute = trail.lastElement();
|
||||
c.block(lastRoute.endBlock()).direction(lastRoute.endDirection);
|
||||
PriorityQueue<Trail> ongoing = routesFrom(c);
|
||||
context.block(lastRoute.endBlock()).direction(lastRoute.endDirection);
|
||||
PriorityQueue<Trail> ongoing = routesFrom(context);
|
||||
if (!ongoing.isEmpty()) nextLevelCandidates.addAll(trail.derive(ongoing));
|
||||
}
|
||||
candidates = nextLevelCandidates;
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
@@ -14,15 +15,26 @@ import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Params;
|
||||
import de.srsoftware.web4rail.Store;
|
||||
import de.srsoftware.web4rail.Store.Listener;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
|
||||
public class TextDisplay extends StretchableTile implements Store.Listener {
|
||||
public class TextDisplay extends StretchableTile implements Listener {
|
||||
private static final String TEXT = "text";
|
||||
private String text = "Hello, world!";
|
||||
private String displayText = text;
|
||||
private HashSet<Store> stores = new HashSet<>();
|
||||
|
||||
private String fillPlaceholders() {
|
||||
String result = text;
|
||||
for (Store store : stores) {
|
||||
String val = store.value();
|
||||
if (isNull(val)) continue;
|
||||
result = result.replace("{"+store.name()+"}", val);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
@@ -54,11 +66,10 @@ public class TextDisplay extends StretchableTile implements Store.Listener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeUpdated(Store store) {
|
||||
displayText = text.replace("{"+store.name()+"}", store.value());
|
||||
public void storeUpdated() {
|
||||
plan.place(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String stretchType() {
|
||||
return t("Width");
|
||||
@@ -68,26 +79,22 @@ public class TextDisplay extends StretchableTile implements Store.Listener {
|
||||
@Override
|
||||
public Tag tag(Map<String, Object> replacements) throws IOException {
|
||||
if (isNull(replacements)) replacements = new HashMap<String, Object>();
|
||||
replacements.put("%text%",displayText);
|
||||
replacements.put("%text%",fillPlaceholders());
|
||||
Tag tag = super.tag(replacements);
|
||||
return tag.clazz(tag.get("class")+" fill");
|
||||
}
|
||||
|
||||
|
||||
public TextDisplay text(String tx) {
|
||||
if (isNull(text) || !text.equals(tx)) {
|
||||
displayText = tx;
|
||||
}
|
||||
text = tx;
|
||||
stores.clear();
|
||||
int pos = text.indexOf("{");
|
||||
Store.removeListener(this);
|
||||
while (pos > -1) {
|
||||
int end = text.indexOf("}",pos);
|
||||
if (end < 0) break;
|
||||
String storeName = text.substring(pos+1, end);
|
||||
Store.get(storeName).addListener(this);
|
||||
stores.add(Store.get(text.substring(pos+1, end)).addListener(this));
|
||||
pos = text.indexOf("{",end);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user