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