started major refactoring
This commit is contained in:
@@ -13,7 +13,6 @@ import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -23,7 +22,6 @@ import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import de.keawe.localconfig.Configuration;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.actions.ActionList;
|
||||
import de.srsoftware.web4rail.conditions.Condition;
|
||||
@@ -81,19 +79,6 @@ public class Application extends BaseClass{
|
||||
Desktop.getDesktop().browse(URI.create("http://"+InetAddress.getLocalHost().getHostName()+":"+config.getInt(PORT)+"/plan"));
|
||||
}
|
||||
|
||||
/**
|
||||
* helper class creating unique ids for use throuout the application
|
||||
* @return
|
||||
*/
|
||||
public static int createId() {
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new Date().hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* handles request from clients by delegating them to respective classes
|
||||
* @param params
|
||||
@@ -275,14 +260,4 @@ public class Application extends BaseClass{
|
||||
OutputStreamWriter sseWriter = new OutputStreamWriter(client.getResponseBody());
|
||||
plan.addClient(sseWriter);
|
||||
}
|
||||
|
||||
/**
|
||||
* shorthand for Translations.get(text,fills)
|
||||
* @param text
|
||||
* @param fills
|
||||
* @return
|
||||
*/
|
||||
private static String t(String text, Object...fills) {
|
||||
return Translation.get(Application.class, text, fills);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,25 @@ package de.srsoftware.web4rail;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.actions.Action;
|
||||
import de.srsoftware.web4rail.conditions.Condition;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
import de.srsoftware.web4rail.moving.Locomotive;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public abstract class BaseClass implements Constants{
|
||||
protected static Plan plan; // the track layout in use
|
||||
@@ -17,18 +28,162 @@ public abstract class BaseClass implements Constants{
|
||||
public static String speedUnit = DEFAULT_SPEED_UNIT;
|
||||
public static String lengthUnit = DEFAULT_LENGTH_UNIT;
|
||||
private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
|
||||
public static Button contextButton(String context,String text) {
|
||||
String[] parts = context.split(":");
|
||||
String realm = parts[0];
|
||||
String id = parts.length>1 ? parts[1] : null;
|
||||
return new Button(text,Map.of(REALM,realm,ID,id,ACTION,ACTION_PROPS));
|
||||
protected Id id = null;
|
||||
|
||||
public static class Context {
|
||||
private BaseClass main = null;
|
||||
private Tile tile;
|
||||
private Block block;
|
||||
private Train train;
|
||||
private Route route;
|
||||
private Action action;
|
||||
private Condition condition;
|
||||
private Car car;
|
||||
private Contact contact;
|
||||
private Direction direction = null;
|
||||
|
||||
public Context(BaseClass object) {
|
||||
main = object;
|
||||
if (main instanceof Tile) this.tile = (Tile) main;
|
||||
if (main instanceof Contact) this.contact = (Contact) main;
|
||||
if (main instanceof Block) this.block = (Block) main;
|
||||
if (main instanceof Train) this.train = (Train) main;
|
||||
if (main instanceof Route) this.route = (Route) main;
|
||||
if (main instanceof Action) this.action = (Action) main;
|
||||
if (main instanceof Condition) this.condition = (Condition) main;
|
||||
if (main instanceof Car) this.car = (Car) main;
|
||||
}
|
||||
|
||||
public Action action() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public Block block() {
|
||||
return block;
|
||||
}
|
||||
|
||||
public Context block(Block newBlock) {
|
||||
block = newBlock;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Car car() {
|
||||
return car;
|
||||
}
|
||||
|
||||
public Context clone() {
|
||||
return new Context(main);
|
||||
}
|
||||
|
||||
public Condition condition() {
|
||||
return condition;
|
||||
}
|
||||
|
||||
public Contact contact() {
|
||||
return contact;
|
||||
}
|
||||
|
||||
public Direction direction() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public Context direction(Direction newDirection) {
|
||||
direction = newDirection;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Route route() {
|
||||
return route;
|
||||
}
|
||||
|
||||
public Context route(Route newRoute) {
|
||||
route = newRoute;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Tile tile() {
|
||||
return tile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuffer sb = new StringBuffer(getClass().getSimpleName());
|
||||
sb.append("(");
|
||||
sb.append(t("Train: {}",train));
|
||||
if (isSet(route)) sb.append(", "+t("Route: {}",route));
|
||||
if (isSet(contact)) sb.append(", "+t("Contact: {}",contact));
|
||||
sb.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public Train train() {
|
||||
return train;
|
||||
}
|
||||
|
||||
public void train(Train newTrain) {
|
||||
train = newTrain;
|
||||
}
|
||||
}
|
||||
|
||||
public static Tag link(String tagClass,Map<String,Object> params,Object caption) {
|
||||
String json = new JSONObject(params).toString().replace("\"", "'");
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(caption.toString());
|
||||
}
|
||||
public static class Id implements Comparable<Id>{
|
||||
private String internalId;
|
||||
|
||||
public Id() {
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
internalId = md5sum(new Date());
|
||||
}
|
||||
|
||||
|
||||
public Id(String id) {
|
||||
internalId = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return internalId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Id other) {
|
||||
return internalId.compareTo(other.internalId);
|
||||
}
|
||||
|
||||
public static Id from(JSONObject json) {
|
||||
return Id.from(json,ID);
|
||||
}
|
||||
|
||||
public static Id from(JSONObject json,String key) {
|
||||
return json.has(key) ? new Id(""+json.get(key)) : null;
|
||||
}
|
||||
|
||||
public static Id from(Map<String,String> params) {
|
||||
return Id.from(params,ID);
|
||||
}
|
||||
|
||||
|
||||
public static Id from(Map<String, String> params, String key) {
|
||||
String sid = params.get(key);
|
||||
return sid == null ? null : new Id(sid);
|
||||
}
|
||||
}
|
||||
|
||||
public Button button(String text,Map<String,String> additionalProps) {
|
||||
return new Button(text,props(additionalProps));
|
||||
}
|
||||
|
||||
public Button button(String text) {
|
||||
return button(text,null);
|
||||
}
|
||||
|
||||
public Id id() {
|
||||
if (isNull(id)) id = new Id();
|
||||
return id;
|
||||
}
|
||||
|
||||
public static boolean isNull(Object o) {
|
||||
return o==null;
|
||||
}
|
||||
@@ -37,6 +192,16 @@ public abstract class BaseClass implements Constants{
|
||||
return o != null;
|
||||
}
|
||||
|
||||
public Tag link(String tagClass,Object caption) {
|
||||
return link(tagClass,caption,null);
|
||||
}
|
||||
|
||||
public Tag link(String tagClass,Object caption,Map<String,String> additionalProps) {
|
||||
String json = new JSONObject(props(additionalProps)).toString().replace("\"", "'");
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(caption.toString());
|
||||
}
|
||||
|
||||
|
||||
public static String md5sum(Object o) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("MD5");
|
||||
@@ -57,4 +222,30 @@ public abstract class BaseClass implements Constants{
|
||||
overlay.entrySet().stream().forEach(entry -> merged.put(entry.getKey(), entry.getValue()));
|
||||
return merged;
|
||||
}
|
||||
|
||||
public Window properties() {
|
||||
return new Window(getClass().getSimpleName()+"-properties", t("Properties of {}",this));
|
||||
}
|
||||
|
||||
public Map<String,String> props(Map<String,String> additionalProps){
|
||||
String realm = null;
|
||||
if (this instanceof Tile) realm = REALM_PLAN;
|
||||
if (this instanceof Contact) realm = REALM_CONTACT;
|
||||
|
||||
if (this instanceof Car) realm = REALM_CAR;
|
||||
if (this instanceof Locomotive) realm = REALM_LOCO;
|
||||
|
||||
if (this instanceof Train) realm = REALM_TRAIN;
|
||||
if (this instanceof Route) realm = REALM_ROUTE;
|
||||
if (this instanceof Action) realm = REALM_ACTIONS;
|
||||
if (this instanceof Condition) realm = REALM_CONDITION;
|
||||
|
||||
HashMap<String,String> props = new HashMap<String, String>(Map.of(REALM, realm, ACTION, ACTION_PROPS, ID, id().toString()));
|
||||
if (isSet(additionalProps)) props.putAll(additionalProps);
|
||||
return props;
|
||||
}
|
||||
|
||||
protected static String t(String txt, Object...fills) {
|
||||
return Translation.get(Application.class, txt, fills);
|
||||
}
|
||||
}
|
||||
|
||||
5
src/main/java/de/srsoftware/web4rail/Context.java
Normal file
5
src/main/java/de/srsoftware/web4rail/Context.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package de.srsoftware.web4rail;
|
||||
|
||||
public class Context {
|
||||
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
|
||||
@@ -24,12 +23,12 @@ public class PathFinder extends BaseClass{
|
||||
for (int i=0; i<visitedRoutes.size(); i++) inset+=" ";
|
||||
|
||||
boolean error = false;
|
||||
Block block = context.block;
|
||||
Block block = context.block();
|
||||
if (isNull(block)) {
|
||||
LOG.warn("{} → {}.availableRoutes called without context.block!",inset,Train.class.getSimpleName());
|
||||
error = true;
|
||||
}
|
||||
Train train = context.train;
|
||||
Train train = context.train();
|
||||
if (isNull(train)) {
|
||||
LOG.warn("{}→ {}.availableRoutes called without context.train!",inset,Train.class.getSimpleName());
|
||||
error = true;
|
||||
@@ -37,7 +36,7 @@ public class PathFinder extends BaseClass{
|
||||
if (error) return availableRoutes;
|
||||
|
||||
Block destination = train.destination();
|
||||
Direction direction = context.direction;
|
||||
Direction direction = context.direction();
|
||||
/* if (isSet(direction)) {
|
||||
LOG.debug("{}Looking for {}-bound routes from {}",inset,direction,block);
|
||||
} else {
|
||||
@@ -45,7 +44,7 @@ public class PathFinder extends BaseClass{
|
||||
}*/
|
||||
if (isSet(destination) && visitedRoutes.isEmpty()) LOG.debug("{}- Destination: {}",inset,destination);
|
||||
|
||||
Route currentRoute = context.route;
|
||||
Route currentRoute = context.route();
|
||||
|
||||
for (Route routeCandidate : block.routes()) {
|
||||
if (routeCandidate.path().firstElement() != block) continue; // Routen, die nicht vom aktuellen Block starten sind bubu
|
||||
@@ -70,9 +69,10 @@ public class PathFinder extends BaseClass{
|
||||
} else {
|
||||
LOG.debug("{}- Candidate: {}",inset,routeCandidate.shortName());
|
||||
Context forwardContext = new Context(train);
|
||||
forwardContext.block = routeCandidate.endBlock();
|
||||
forwardContext.direction = routeCandidate.endDirection;
|
||||
forwardContext.route = null;
|
||||
forwardContext
|
||||
.block(routeCandidate.endBlock())
|
||||
.direction(routeCandidate.endDirection)
|
||||
.route(null);
|
||||
visitedRoutes.add(routeCandidate);
|
||||
TreeMap<Integer, List<Route>> forwardRoutes = availableRoutes(forwardContext,visitedRoutes);
|
||||
visitedRoutes.remove(routeCandidate);
|
||||
@@ -112,5 +112,4 @@ public class PathFinder extends BaseClass{
|
||||
|
||||
return selectetRoute;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.actions.Action;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
@@ -141,10 +140,10 @@ public class Plan extends BaseClass{
|
||||
private static final String SPEED_UNIT = "speed_unit";
|
||||
private static final String LENGTH_UNIT = "length_unit";
|
||||
|
||||
public HashMap<String,Tile> tiles = new HashMap<String,Tile>(); // The list of tiles of this plan, i.e. the Track layout
|
||||
public HashMap<Id,Tile> tiles = new HashMap<Id,Tile>(); // The list of tiles of this plan, i.e. the Track layout
|
||||
private HashSet<Block> blocks = new HashSet<Block>(); // the list of tiles, that are blocks
|
||||
private HashSet<Signal> signals = new HashSet<Signal>(); // the list of tiles, that are signals
|
||||
private HashMap<Integer, Route> routes = new HashMap<Integer, Route>(); // the list of routes of the track layout
|
||||
private HashMap<Id, Route> routes = new HashMap<Id, Route>(); // the list of routes of the track layout
|
||||
private ControlUnit controlUnit = new ControlUnit(this); // the control unit, to which the plan is connected
|
||||
private Contact learningContact;
|
||||
|
||||
@@ -175,13 +174,13 @@ public class Plan extends BaseClass{
|
||||
case ACTION_ANALYZE:
|
||||
return analyze();
|
||||
case ACTION_CLICK:
|
||||
return click(get(params.get(ID),true));
|
||||
return click(get(Id.from(params),true));
|
||||
case ACTION_CONNECT:
|
||||
Tile tile = get(params.get(ID), false);
|
||||
Tile tile = get(Id.from(params), false);
|
||||
if (tile instanceof Bridge) return ((Bridge)tile).requestConnect();
|
||||
break;
|
||||
case ACTION_MOVE:
|
||||
return moveTile(params.get(DIRECTION),params.get(ID));
|
||||
return moveTile(params.get(DIRECTION),Id.from(params));
|
||||
case ACTION_PROPS:
|
||||
return properties(params);
|
||||
case ACTION_SAVE:
|
||||
@@ -341,7 +340,7 @@ public class Plan extends BaseClass{
|
||||
* @param resolveShadows if this is set to true, this function will return the overlaying tiles, if the id belongs to a shadow tile.
|
||||
* @return the tile belonging to the id, or the overlaying tile if the respective tile is a shadow tile.
|
||||
*/
|
||||
public Tile get(String tileId,boolean resolveShadows) {
|
||||
public Tile get(Id tileId,boolean resolveShadows) {
|
||||
if (isNull(tileId)) return null;
|
||||
Tile tile = tiles.get(tileId);
|
||||
if (resolveShadows && tile instanceof Shadow) tile = ((Shadow)tile).overlay();
|
||||
@@ -508,7 +507,7 @@ public class Plan extends BaseClass{
|
||||
* @throws NumberFormatException
|
||||
* @throws IOException
|
||||
*/
|
||||
private String moveTile(String direction, String tileId) throws NumberFormatException, IOException {
|
||||
private String moveTile(String direction, Id tileId) throws NumberFormatException, IOException {
|
||||
switch (direction) {
|
||||
case "south":
|
||||
return moveTile(get(tileId,false),Direction.SOUTH);
|
||||
@@ -611,7 +610,7 @@ public class Plan extends BaseClass{
|
||||
|
||||
private Window properties(HashMap<String, String> params) {
|
||||
if (params.containsKey(ID)) {
|
||||
Tile tile = get(params.get(ID), true);
|
||||
Tile tile = get(Id.from(params), true);
|
||||
if (isSet(tile)) return tile.propMenu();
|
||||
}
|
||||
|
||||
@@ -659,7 +658,7 @@ public class Plan extends BaseClass{
|
||||
*/
|
||||
Route registerRoute(Route newRoute) {
|
||||
newRoute.path().stream().filter(Tile::isSet).forEach(tile -> tile.add(newRoute));
|
||||
int newRouteId = newRoute.id();
|
||||
Id newRouteId = newRoute.id();
|
||||
Route existingRoute = routes.get(newRouteId);
|
||||
if (isSet(existingRoute)) newRoute.addPropertiesFrom(existingRoute);
|
||||
routes.put(newRouteId, newRoute);
|
||||
@@ -707,7 +706,7 @@ public class Plan extends BaseClass{
|
||||
* @param routeId the id of the route requestd
|
||||
* @return
|
||||
*/
|
||||
public Route route(int routeId) {
|
||||
public Route route(Id routeId) {
|
||||
return routes.get(routeId);
|
||||
}
|
||||
|
||||
@@ -798,15 +797,15 @@ public class Plan extends BaseClass{
|
||||
public Tag showContext(HashMap<String, String> params) {
|
||||
String[] parts = params.get(CONTEXT).split(":");
|
||||
String realm = parts[0];
|
||||
String id = parts.length>1 ? parts[1] : null;
|
||||
Id id = parts.length>1 ? new Id(parts[1]) : null;
|
||||
switch (realm) {
|
||||
case REALM_ROUTE:
|
||||
return route(Integer.parseInt(id)).properties(params);
|
||||
return route(id).properties(params);
|
||||
case REALM_PLAN:
|
||||
Tile tile = get(id, false);
|
||||
return isNull(tile) ? null : tile.propMenu();
|
||||
case REALM_ACTIONS:
|
||||
Action action = Action.get(Integer.parseInt(id));
|
||||
Action action = Action.get(id);
|
||||
return (isSet(action)) ? action.properties(params) : null;
|
||||
|
||||
}
|
||||
@@ -849,16 +848,6 @@ public class Plan extends BaseClass{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* shorthand for Translations.get(message,fills)
|
||||
* @param message
|
||||
* @param fills
|
||||
* @return
|
||||
*/
|
||||
private String t(String message, Object...fills) {
|
||||
return Translation.get(Application.class, message, fills);
|
||||
}
|
||||
|
||||
/**
|
||||
* generates the menu for selecting tiles to be added to the layout
|
||||
* @return
|
||||
@@ -931,7 +920,7 @@ public class Plan extends BaseClass{
|
||||
* @throws IOException
|
||||
*/
|
||||
private Object update(HashMap<String, String> params) throws IOException {
|
||||
Tile tile = get(params.get(ID),true);
|
||||
Tile tile = get(Id.from(params),true);
|
||||
if (isSet(tile)) return tile.update(params);
|
||||
|
||||
if (params.containsKey(LENGTH_UNIT)) lengthUnit = params.get(LENGTH_UNIT);
|
||||
@@ -942,7 +931,7 @@ public class Plan extends BaseClass{
|
||||
}
|
||||
|
||||
private Object updateTimes(HashMap<String, String> params) throws IOException {
|
||||
Tile tile = get(params.get(ID),false);
|
||||
Tile tile = get(Id.from(params),false);
|
||||
if (tile instanceof Block) {
|
||||
Block block = (Block) tile;
|
||||
place(block.updateTimes(params));
|
||||
|
||||
@@ -23,7 +23,6 @@ import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.actions.Action;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.actions.ActionList;
|
||||
import de.srsoftware.web4rail.actions.BrakeStart;
|
||||
import de.srsoftware.web4rail.actions.BrakeStop;
|
||||
@@ -72,7 +71,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
static final String SIGNALS = "signals";
|
||||
static final String TURNOUTS = "turnouts";
|
||||
|
||||
private static HashMap<Integer, String> names = new HashMap<Integer, String>(); // maps id to name. needed to keep names during plan.analyze()
|
||||
private static HashMap<Id, String> names = new HashMap<Id, String>(); // maps id to name. needed to keep names during plan.analyze()
|
||||
|
||||
private class BrakeProcessor extends Thread {
|
||||
private int startSpeed;
|
||||
@@ -144,7 +143,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
private boolean disabled = false;
|
||||
private Block endBlock = null;
|
||||
public Direction endDirection;
|
||||
private int id;
|
||||
private Id id;
|
||||
private Vector<Tile> path;
|
||||
private Vector<Signal> signals;
|
||||
public Train train;
|
||||
@@ -163,12 +162,12 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Object action(HashMap<String, String> params) throws IOException {
|
||||
Route route = plan.route(Integer.parseInt(params.get(ID)));
|
||||
Route route = plan.route(Id.from(params));
|
||||
if (isNull(route)) return t("Unknown route: {}",params.get(ID));
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_DROP:
|
||||
String message = plan.remove(route);
|
||||
String tileId = params.get(Tile.class.getSimpleName());
|
||||
Id tileId = Id.from(params,Tile.class.getSimpleName());
|
||||
if (isSet(tileId)) {
|
||||
Tile tile = plan.get(tileId, false);
|
||||
if (isSet(tile)) {
|
||||
@@ -228,7 +227,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
}
|
||||
|
||||
private void addBasicPropertiesTo(Window win) {
|
||||
if (isSet(train)) link("span",Map.of(REALM,REALM_TRAIN,ID,train.id,ACTION,ACTION_PROPS),t("Train: {}",train)).addTo(win);
|
||||
if (isSet(train)) link("span",t("Train: {}",train)).addTo(win);
|
||||
new Tag("h4").content(t("Origin and destination")).addTo(win);
|
||||
Tag list = new Tag("ul");
|
||||
Plan.addLink(startBlock, t("Origin: {} to {}",startBlock.name,startDirection), list);
|
||||
@@ -441,8 +440,8 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
}
|
||||
|
||||
private Object dropCodition(HashMap<String, String> params) {
|
||||
String condId = params.get(REALM_CONDITION);
|
||||
if (isSet(condId)) conditions.removeById(Integer.parseInt(condId));
|
||||
Id condId = Id.from(params,REALM_CONDITION);
|
||||
if (isSet(condId)) conditions.removeById(condId);
|
||||
return properties(params);
|
||||
}
|
||||
|
||||
@@ -492,8 +491,8 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
public int id() {
|
||||
if (id == 0) id = generateName().hashCode();
|
||||
public Id id() {
|
||||
if (id == null) id = new Id(generateName());
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -512,11 +511,11 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
json.put(ID, id());
|
||||
Vector<String> tileIds = new Vector<String>();
|
||||
Vector<Id> tileIds = new Vector<Id>();
|
||||
for (Tile t : this.path) tileIds.add(t.id());
|
||||
json.put(PATH, tileIds);
|
||||
|
||||
Vector<String> signalIds = new Vector<String>(); // list all signals affecting this route
|
||||
Vector<Id> signalIds = new Vector<Id>(); // list all signals affecting this route
|
||||
for (Tile t : this.signals) signalIds.add(t.id());
|
||||
json.put(SIGNALS, signalIds);
|
||||
|
||||
@@ -556,13 +555,13 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
}
|
||||
|
||||
private Route load(JSONObject json,Plan plan) {
|
||||
if (json.has(ID)) id = json.getInt(ID);
|
||||
if (json.has(ID)) id = Id.from(json);
|
||||
if (json.has(NAME)) name(json.getString(NAME));
|
||||
JSONArray pathIds = json.getJSONArray(PATH);
|
||||
startDirection = Direction.valueOf(json.getString(START_DIRECTION));
|
||||
endDirection = Direction.valueOf(json.getString(END_DIRECTION));
|
||||
for (Object tileId : pathIds) {
|
||||
Tile tile = plan.get((String) tileId,false);
|
||||
Tile tile = plan.get(new Id((String) tileId),false);
|
||||
if (isNull(tile)) {
|
||||
continue;
|
||||
}
|
||||
@@ -582,12 +581,12 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
JSONArray turnouts = json.getJSONArray(TURNOUTS);
|
||||
for (int i=0; i<turnouts.length();i++) {
|
||||
JSONObject jTurnout = turnouts.getJSONObject(i);
|
||||
Turnout turnout = (Turnout) plan.get(jTurnout.getString(Turnout.ID), false);
|
||||
Turnout turnout = (Turnout) plan.get(new Id(jTurnout.getString(Turnout.ID)), false);
|
||||
addTurnout(turnout, Turnout.State.valueOf(jTurnout.getString(Turnout.STATE)));
|
||||
}
|
||||
}
|
||||
if (json.has(SIGNALS)) {
|
||||
for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get((String) signalId, false));
|
||||
for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get(new Id((String) signalId), false));
|
||||
}
|
||||
if (json.has(ACTION_LISTS)) loadActions(json.getJSONArray(ACTION_LISTS));
|
||||
if (json.has(CONDITIONS)) conditions.load(json.getJSONArray(CONDITIONS));
|
||||
|
||||
@@ -13,14 +13,9 @@ import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
|
||||
/**
|
||||
* Base Class for all other actions
|
||||
@@ -28,81 +23,12 @@ import de.srsoftware.web4rail.tiles.Contact;
|
||||
*
|
||||
*/
|
||||
public abstract class Action extends BaseClass {
|
||||
private static final HashMap<Integer,Action> actions = new HashMap<Integer, Action>();
|
||||
private static final HashMap<Id,Action> actions = new HashMap<Id, Action>();
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||
private static final String PREFIX = Action.class.getPackageName();
|
||||
protected int id;
|
||||
|
||||
public static class Context {
|
||||
public Contact contact = null;
|
||||
public Route route = null;
|
||||
public Train train = null;
|
||||
public Block block = null;
|
||||
public Direction direction = null;
|
||||
|
||||
public Context(Contact c, Route r, Train t, Block b, Direction d) {
|
||||
contact = c;
|
||||
route = r;
|
||||
train = t;
|
||||
block = b;
|
||||
direction = d;
|
||||
}
|
||||
|
||||
public Context(Contact c) {
|
||||
contact = c;
|
||||
setRoute(contact.route());
|
||||
}
|
||||
|
||||
public Context(Train train) {
|
||||
setTrain(train);
|
||||
}
|
||||
|
||||
public Context(Route route) {
|
||||
setRoute(route);
|
||||
}
|
||||
|
||||
protected Context clone() {
|
||||
return new Context(contact, route, train, block, direction);
|
||||
}
|
||||
|
||||
private void setRoute(Route route) {
|
||||
this.route = route;
|
||||
if (isSet(route)) setTrain(route.train);
|
||||
|
||||
}
|
||||
|
||||
private void setTrain(Train train) {
|
||||
this.train = train;
|
||||
if (isSet(train)) {
|
||||
if (isNull(route)) route = train.route;
|
||||
setBlock(train.currentBlock());
|
||||
setDirection(train.direction());
|
||||
}
|
||||
}
|
||||
|
||||
private void setDirection(Direction dir) {
|
||||
direction = dir;
|
||||
}
|
||||
|
||||
private void setBlock(Block block) {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuffer sb = new StringBuffer(getClass().getSimpleName());
|
||||
sb.append("(");
|
||||
sb.append(t("Train: {}",train));
|
||||
if (isSet(route)) sb.append(", "+t("Route: {}",route));
|
||||
if (isSet(contact)) sb.append(", "+t("Contact: {}",contact));
|
||||
sb.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Action() {
|
||||
id = Application.createId();
|
||||
actions.put(id, this);
|
||||
actions.put(id(), this);
|
||||
}
|
||||
|
||||
public static Action create(String type) {
|
||||
@@ -120,13 +46,9 @@ public abstract class Action extends BaseClass {
|
||||
|
||||
public abstract boolean fire(Context context);
|
||||
|
||||
public static Action get(int actionId) {
|
||||
public static Action get(Id actionId) {
|
||||
return actions.get(actionId);
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public JSONObject json() {
|
||||
return new JSONObject().put(TYPE, getClass().getSimpleName());
|
||||
|
||||
@@ -13,11 +13,11 @@ import org.slf4j.LoggerFactory;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.BaseClass.Context;
|
||||
import de.srsoftware.web4rail.BaseClass.Id;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
@@ -26,25 +26,25 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
||||
|
||||
private static final long serialVersionUID = 4862000041987682112L;
|
||||
private static final HashMap<Integer, ActionList> actionLists = new HashMap<Integer, ActionList>();
|
||||
private int id;
|
||||
private static final HashMap<Id, ActionList> actionLists = new HashMap<Id, ActionList>();
|
||||
private Id id;
|
||||
|
||||
public ActionList() {
|
||||
id = Application.createId();
|
||||
id = new Id();
|
||||
actionLists.put(id,this);
|
||||
}
|
||||
|
||||
private static Integer actionId(HashMap<String, String> params) {
|
||||
private static Id actionId(HashMap<String, String> params) {
|
||||
if (!params.containsKey(ID)) return null;
|
||||
String[] parts = params.get(ID).split("/");
|
||||
if (parts.length<2) return null;
|
||||
return Integer.parseInt(parts[1]);
|
||||
return new Id(parts[1]);
|
||||
}
|
||||
|
||||
private static Integer actionListId(HashMap<String, String> params) {
|
||||
private static Id actionListId(HashMap<String, String> params) {
|
||||
if (!params.containsKey(ID)) return null;
|
||||
String[] parts = params.get(ID).split("/");
|
||||
return Integer.parseInt(parts[0]);
|
||||
return new Id(parts[0]);
|
||||
}
|
||||
|
||||
private Object actionTypeForm(Window win, String context) {
|
||||
@@ -110,7 +110,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
boolean first = true;
|
||||
for (Action action : this) {
|
||||
props.put(ID, id+"/"+action.id());
|
||||
Tag act = BaseClass.link("span", Map.of(REALM,REALM_ACTIONS,ID,id+"/"+action.id(),ACTION,ACTION_PROPS,CONTEXT,context), action+NBSP).addTo(new Tag("li"));;
|
||||
Tag act = action.link("span", action+NBSP, Map.of(CONTEXT,context,ID,id+"/"+action.id())).addTo(new Tag("li"));
|
||||
if (!first) {
|
||||
props.put(ACTION, ACTION_MOVE);
|
||||
new Button("↑",props).addTo(act);
|
||||
@@ -132,9 +132,9 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
}
|
||||
}
|
||||
|
||||
public boolean drop(int actionId) {
|
||||
public boolean drop(Id actionId) {
|
||||
for (Action action : this) {
|
||||
if (action.id() == actionId) {
|
||||
if (action.id().equals(actionId)) {
|
||||
this.remove(action);
|
||||
return true;
|
||||
}
|
||||
@@ -150,7 +150,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
return true;
|
||||
}
|
||||
|
||||
public int id() {
|
||||
public Id id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -172,9 +172,9 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
return actionList;
|
||||
}
|
||||
|
||||
public boolean moveUp(int actionId) {
|
||||
public boolean moveUp(Id actionId) {
|
||||
for (int i=1; i<size(); i++) {
|
||||
if (actionId == elementAt(i).id()) {
|
||||
if (actionId.equals(elementAt(i).id())) {
|
||||
Action action = remove(i);
|
||||
insertElementAt(action, i-1);
|
||||
return true;
|
||||
@@ -184,11 +184,11 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
}
|
||||
|
||||
public static Object process(HashMap<String, String> params, Plan plan) {
|
||||
Integer listId = actionListId(params);
|
||||
Id listId = actionListId(params);
|
||||
if (listId == null) return t("No action list id passed to ActionList.process()!");
|
||||
ActionList actionList = actionLists.get(listId);
|
||||
|
||||
Integer actionId = actionId(params);
|
||||
Id actionId = actionId(params);
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) return t("No action passed to ActionList.process()!");
|
||||
if (actionList == null && !List.of(ACTION_UPDATE,ACTION_PROPS).contains(action)) return t("No action list with id {} found!",listId);
|
||||
@@ -209,7 +209,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
}
|
||||
|
||||
private static Object propsOf(HashMap<String, String> params) {
|
||||
int actionId = actionId(params);
|
||||
Id actionId = actionId(params);
|
||||
Action action = Action.get(actionId);
|
||||
if (action != null) return action.properties(params);
|
||||
return t("No action with id {} found!",actionId);
|
||||
@@ -219,7 +219,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
return Translation.get(Application.class, text, fills);
|
||||
}
|
||||
|
||||
private static Object update(int actionId, HashMap<String, String> params, Plan plan) {
|
||||
private static Object update(Id actionId, HashMap<String, String> params, Plan plan) {
|
||||
Action action = Action.get(actionId);
|
||||
if (action != null) {
|
||||
plan.stream(action.update(params).toString());
|
||||
|
||||
@@ -4,8 +4,8 @@ public class BrakeCancel extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (isNull(context.route)) return false;
|
||||
context.route.brakeCancel();
|
||||
if (isNull(context.route())) return false;
|
||||
context.route().brakeCancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ public class BrakeStart extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (isNull(context.route)) return false;
|
||||
context.route.brakeStart();
|
||||
if (isNull(context.route())) return false;
|
||||
context.route().brakeStart();
|
||||
LOG.debug("Started brake process...");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ public class BrakeStop extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (isNull(context.route)) return false;
|
||||
context.route.brakeStop();
|
||||
if (isNull(context.route())) return false;
|
||||
context.route().brakeStop();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,10 +49,8 @@ public class ConditionalAction extends Action {
|
||||
if (!conditions.isEmpty()) {
|
||||
Tag list = new Tag("ul");
|
||||
for (Condition condition : conditions) {
|
||||
HashMap<String,Object> props = new HashMap<String, Object>(Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_PROPS,CONTEXT, context));
|
||||
Tag li = link("span", props, condition+NBSP).addTo(new Tag("li"));
|
||||
props.put(ACTION, ACTION_DROP);
|
||||
props.put(CONTEXT,REALM_ACTIONS+":"+id());
|
||||
Tag li = link("span", condition+NBSP,Map.of(CONTEXT,context)).addTo(new Tag("li"));
|
||||
HashMap<String,Object> props = new HashMap<String, Object>(Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_DROP,CONTEXT, REALM_ACTIONS+":"+id()));
|
||||
new Button(t("delete"), props).addTo(li).addTo(list);
|
||||
}
|
||||
list.addTo(fieldset);
|
||||
@@ -67,7 +65,7 @@ public class ConditionalAction extends Action {
|
||||
Condition.selector().addTo(form);
|
||||
|
||||
new Button(t("Add condition"),form).addTo(form);
|
||||
return contextButton(context,t("Back")).addTo(form).addTo(fieldset);
|
||||
return button(t("Back")).addTo(form).addTo(fieldset);
|
||||
}
|
||||
|
||||
public boolean equals(ConditionalAction other) {
|
||||
|
||||
@@ -18,8 +18,8 @@ public class DetermineTrainInBlock extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
context.block = block;
|
||||
context.train = block.train();
|
||||
context.block(block);
|
||||
context.train(block.train());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class DetermineTrainInBlock extends Action {
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
String blockId = json.getString(BLOCK);
|
||||
Id blockId = Id.from(json,BLOCK);
|
||||
if (isSet(blockId)) block = Block.get(blockId);
|
||||
return this;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class DetermineTrainInBlock extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
String blockId = params.get(Block.class.getSimpleName());
|
||||
Id blockId = Id.from(params,Block.class.getSimpleName());
|
||||
if (isSet(blockId)) block = Block.get(blockId);
|
||||
return properties(params);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public class FinishRoute extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
Route route = context.route;
|
||||
Route route = context.route();
|
||||
if (isSet(route)) route.finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8,20 +8,20 @@ public class PreserveRoute extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
Train train = context.train;
|
||||
Route route = context.route;
|
||||
Train train = context.train();
|
||||
Route route = context.route();
|
||||
// These are errors:
|
||||
if (isNull(train)) return false;
|
||||
if (isNull(route)) return false;
|
||||
|
||||
Range waitTime = context.route.endBlock().getWaitTime(context.train,context.route.endDirection);
|
||||
Range waitTime = route.endBlock().getWaitTime(train,route.endDirection);
|
||||
|
||||
// These are NOT errors:
|
||||
if (!context.train.usesAutopilot()) return true;
|
||||
if (!train.usesAutopilot()) return true;
|
||||
if (waitTime.max > 0) return true; // train is expected to wait in next block.
|
||||
if (train.destination() == route.endBlock()) return true;
|
||||
|
||||
context.train.reserveNext();
|
||||
train.reserveNext();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,14 +18,14 @@ public class SetContextTrain extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
context.train = train;
|
||||
context.train(train);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (isSet(train)) json.put(REALM_TRAIN, train.id);
|
||||
if (isSet(train)) json.put(REALM_TRAIN, train.id());
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class SetContextTrain extends Action {
|
||||
public void run() {
|
||||
try {
|
||||
sleep(1000);
|
||||
int trainId = json.getInt(REALM_TRAIN);
|
||||
Id trainId = Id.from(json,REALM_TRAIN);
|
||||
if (isSet(trainId)) train = Train.get(trainId);
|
||||
} catch (InterruptedException e) {}
|
||||
};
|
||||
@@ -69,8 +69,8 @@ public class SetContextTrain extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
String trainId = params.get(Train.class.getSimpleName());
|
||||
if (isSet(trainId)) train = Train.get(Integer.parseInt(trainId));
|
||||
Id trainId = Id.from(params,Train.class.getSimpleName());
|
||||
if (isSet(trainId)) train = Train.get(trainId);
|
||||
return properties(params);
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class SetDisplayText extends TextAction{
|
||||
public void run() {
|
||||
try {
|
||||
sleep(1000);
|
||||
display = (TextDisplay) plan.get(json.getString(DISPLAY), false);
|
||||
display = (TextDisplay) plan.get(Id.from(json,DISPLAY), false);
|
||||
} catch (InterruptedException e) {}
|
||||
};
|
||||
}.start();
|
||||
@@ -72,7 +72,7 @@ public class SetDisplayText extends TextAction{
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
super.update(params);
|
||||
String displayId = params.get(TextDisplay.class.getSimpleName());
|
||||
if (isSet(displayId)) display = (TextDisplay) plan.get(displayId, false);
|
||||
if (isSet(displayId)) display = (TextDisplay) plan.get(new Id(displayId), false);
|
||||
return properties(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class SetRelay extends Action {
|
||||
super.load(json);
|
||||
String relayId = json.getString(RELAY);
|
||||
if (isSet(relayId)) {
|
||||
relay = Relay.get(relayId);
|
||||
relay = Relay.get(new Id(relayId));
|
||||
state = json.getBoolean(Relay.STATE);
|
||||
}
|
||||
return this;
|
||||
@@ -81,7 +81,7 @@ public class SetRelay extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
String relayId = params.get(RELAY);
|
||||
Id relayId = new Id(params.get(RELAY));
|
||||
relay = Relay.get(relayId);
|
||||
String st = params.get(Relay.STATE);
|
||||
if (isSet(st)) state = st.equals("true");
|
||||
|
||||
@@ -39,7 +39,7 @@ public class SetSignal extends Action {
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
Tile tile = plan.get(json.getString(SIGNAL), false);
|
||||
Tile tile = plan.get(new Id(json.getString(SIGNAL)), false);
|
||||
if (tile instanceof Signal) signal = (Signal) tile;
|
||||
state = json.getString(Signal.STATE);
|
||||
return this;
|
||||
@@ -91,7 +91,7 @@ public class SetSignal extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
Tile tile = plan.get(params.get(SIGNAL), false);
|
||||
Tile tile = plan.get(new Id(params.get(SIGNAL)), false);
|
||||
if (tile instanceof Signal) signal = (Signal) tile;
|
||||
String st = params.get(Signal.STATE);
|
||||
if (isSet(st)) state = st;
|
||||
|
||||
@@ -18,8 +18,8 @@ public class SetSpeed extends Action{
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (isNull(context.train)) return false;
|
||||
context.train.setSpeed(speed);
|
||||
if (isNull(context.train())) return false;
|
||||
context.train().setSpeed(speed);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ public class StopAuto extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (isNull(context.train)) return false;
|
||||
context.train.quitAutopilot();
|
||||
if (isNull(context.train())) return false;
|
||||
context.train().quitAutopilot();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,11 @@ public abstract class TextAction extends Action {
|
||||
|
||||
|
||||
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());
|
||||
if (isSet(context.block())) tx = tx.replace("%block%", context.block().name);
|
||||
if (isSet(context.contact())) tx = tx.replace("%contact%", context.contact().id().toString());
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public class TriggerContact extends Action {
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
String contactId = json.getString(CONTACT);
|
||||
Id contactId = Id.from(json,CONTACT);
|
||||
if (isSet(contactId)) contact = Contact.get(contactId);
|
||||
return this;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ public class TriggerContact extends Action {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
String contactId = params.get(CONTACT);
|
||||
Id contactId = Id.from(params,CONTACT);
|
||||
if (isSet(contactId)) contact = Contact.get(contactId);
|
||||
return properties(params);
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ public class TurnTrain extends Action{
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (context.train != null) {
|
||||
context.train.turn();
|
||||
if (context.train() != null) {
|
||||
context.train().turn();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.HashMap;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
|
||||
@@ -26,7 +25,7 @@ public class BlockFree extends Condition {
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
block(Block.get(json.getString(BLOCK)));
|
||||
block(Block.get(new Id(json.getString(BLOCK))));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -52,7 +51,7 @@ public class BlockFree extends Condition {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (!params.containsKey(BLOCK)) return t("No block id passed to BlockFree.update()!");
|
||||
String bid = params.get(BLOCK);
|
||||
Id bid = new Id(params.get(BLOCK));
|
||||
Block block = Block.get(bid);
|
||||
if (block == null) return t("No block with id {} found!",bid);
|
||||
this.block = block;
|
||||
|
||||
@@ -17,7 +17,6 @@ import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.actions.ConditionalAction;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
@@ -30,16 +29,15 @@ public abstract class Condition extends BaseClass {
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Condition.class);
|
||||
private static final String INVERTED = "inverted";
|
||||
private static final String PREFIX = Condition.class.getPackageName();
|
||||
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>();
|
||||
private static HashMap<Id, Condition> conditions = new HashMap<Id, Condition>();
|
||||
public boolean inverted = false;
|
||||
protected int id;
|
||||
private Object parent;
|
||||
|
||||
public Condition() {
|
||||
this(Application.createId());
|
||||
this(new Id());
|
||||
}
|
||||
|
||||
public Condition(int id) {
|
||||
public Condition(Id id) {
|
||||
this.id = id;
|
||||
conditions.put(id, this);
|
||||
}
|
||||
@@ -48,7 +46,7 @@ public abstract class Condition extends BaseClass {
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) return t("No action passed to Condition.action!");
|
||||
|
||||
Integer cid = (params.containsKey(ID)) ? Integer.parseInt(params.get(ID)) : null;
|
||||
Id cid = Id.from(params);
|
||||
|
||||
if (isSet(cid)) {
|
||||
Condition condition = conditions.get(cid);
|
||||
@@ -85,14 +83,14 @@ public abstract class Condition extends BaseClass {
|
||||
Condition condition = Condition.create(type);
|
||||
if (isNull(condition)) return t("Unknown type \"{}\" of condition!",type);
|
||||
String[] parts = context.split(":");
|
||||
String contextId = parts[1];
|
||||
Id contextId = new Id(parts[1]);
|
||||
String realm = parts[0];
|
||||
switch (realm) {
|
||||
case REALM_ROUTE:
|
||||
Route route = plan.route(Integer.parseInt(contextId));
|
||||
Route route = plan.route(contextId);
|
||||
if (isNull(route)) return t("Unknown route: {}",contextId);
|
||||
route.add(condition);
|
||||
return route.properties(new HashMap<String,String>(Map.of(REALM,REALM_ROUTE,ACTION,ACTION_PROPS,ID,contextId)));
|
||||
return route.properties(new HashMap<String,String>(Map.of(REALM,REALM_ROUTE,ACTION,ACTION_PROPS,ID,contextId.toString())));
|
||||
|
||||
default:
|
||||
break;
|
||||
@@ -124,10 +122,6 @@ public abstract class Condition extends BaseClass {
|
||||
}
|
||||
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public JSONObject json() {
|
||||
JSONObject json = new JSONObject().put(TYPE, getClass().getSimpleName());
|
||||
@@ -146,7 +140,7 @@ public abstract class Condition extends BaseClass {
|
||||
String tx = args.length<1 ? toString()+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
String context = args.length<3 ? null : args[2];
|
||||
return link(type, Map.of(REALM,REALM_CONDITION,ID,id(),ACTION,ACTION_PROPS,CONTEXT,context), tx);
|
||||
return link(type, tx,Map.of(CONTEXT,context));
|
||||
}
|
||||
|
||||
private static List<Class<? extends Condition>> list() {
|
||||
|
||||
@@ -8,8 +8,9 @@ import org.json.JSONObject;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass.Context;
|
||||
import de.srsoftware.web4rail.BaseClass.Id;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
@@ -39,9 +40,9 @@ public class ConditionList extends Vector<Condition> implements Constants{
|
||||
}
|
||||
}
|
||||
|
||||
public void removeById(int cid) {
|
||||
public void removeById(Id cid) {
|
||||
for (Condition condition : this) {
|
||||
if (condition.id() == cid) {
|
||||
if (condition.id().equals(cid)) {
|
||||
remove(condition);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.util.stream.Collectors;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
|
||||
public class OrCondition extends Condition{
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
|
||||
public class PushPullTrain extends Condition {
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
return context.train.pushPull != inverted;
|
||||
return context.train().pushPull != inverted;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.HashMap;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
@@ -17,7 +16,7 @@ public class TrainHasTag extends Condition {
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
if (tag == null) return true;
|
||||
return context.train.tags().contains(tag) != inverted;
|
||||
return context.train().tags().contains(tag) != inverted;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
@@ -17,8 +16,8 @@ public class TrainLength extends Condition {
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
if (isNull(context.train)) return false;
|
||||
return (context.train.length() < maxLength) != inverted;
|
||||
if (isNull(context.train())) return false;
|
||||
return (context.train().length() < maxLength) != inverted;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.HashMap;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
@@ -16,17 +15,17 @@ public class TrainSelect extends Condition {
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
return (context.train == train) != inverted;
|
||||
return (context.train() == train) != inverted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(REALM_TRAIN, train.id);
|
||||
return super.json().put(REALM_TRAIN, train.id());
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
train(Train.get(json.getInt(REALM_TRAIN)));
|
||||
train(Train.get(new Id(json.getString(REALM_TRAIN))));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -52,7 +51,7 @@ public class TrainSelect extends Condition {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (!params.containsKey(TRAIN)) return t("No train id passed to TrainSelect.update()!");
|
||||
int tid = Integer.parseInt(params.get(TRAIN));
|
||||
Id tid = new Id(params.get(TRAIN));
|
||||
Train train = Train.get(tid);
|
||||
if (train == null) return t("No train with id {} found!",tid);
|
||||
this.train = train;
|
||||
|
||||
@@ -32,7 +32,7 @@ import de.srsoftware.web4rail.tags.TextArea;
|
||||
|
||||
public class Car extends BaseClass implements Comparable<Car>{
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
|
||||
static HashMap<Integer,Car> cars = new HashMap<Integer, Car>();
|
||||
static HashMap<Id,Car> cars = new HashMap<Id, Car>();
|
||||
|
||||
public static final String NAME = "name";
|
||||
private static final String LENGTH = "length";
|
||||
@@ -41,7 +41,6 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
private static final String MAX_SPEED = "max_speed";
|
||||
protected HashSet<String> tags = new HashSet<String>();
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
public int length;
|
||||
protected String stockId = "";
|
||||
@@ -54,14 +53,9 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
this(name,null);
|
||||
}
|
||||
|
||||
public Car(String name, Integer id) {
|
||||
public Car(String name, Id id) {
|
||||
this.name = name;
|
||||
if (id == null) {
|
||||
try { // make sure multiple consecutive creations get different ids
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {}
|
||||
id = Application.createId();
|
||||
}
|
||||
if (isNull(id)) id = new Id();
|
||||
this.id = id;
|
||||
cars.put(id, this);
|
||||
}
|
||||
@@ -100,12 +94,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
}
|
||||
|
||||
public static Car get(Object id) {
|
||||
return isNull(id) ? null : cars.get(Integer.parseInt(""+id)); // try to get by id
|
||||
}
|
||||
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
return isNull(id) ? null : cars.get(new Id(""+id)); // try to get by id
|
||||
}
|
||||
|
||||
public JSONObject json() {
|
||||
@@ -130,7 +119,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
public Tag link(String...args) {
|
||||
String tx = args.length<1 ? name()+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
return link(type, Map.of(REALM,REALM_CAR,ID,id,ACTION,ACTION_PROPS), tx);
|
||||
return link(type, tx);
|
||||
}
|
||||
|
||||
static Vector<Car> list() {
|
||||
@@ -148,7 +137,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
while (line != null) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
String name = json.getString(Car.NAME);
|
||||
int id = json.getInt(ID);
|
||||
Id id = Id.from(json);
|
||||
Car car = json.has(Locomotive.LOCOMOTIVE) ? new Locomotive(name, id) : new Car(name,id);
|
||||
car.load(json).plan(plan);
|
||||
|
||||
@@ -158,7 +147,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
}
|
||||
|
||||
protected Car load(JSONObject json) {
|
||||
if (json.has(ID)) id = json.getInt(ID);
|
||||
if (json.has(ID)) id = Id.from(json);
|
||||
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
||||
if (json.has(MAX_SPEED)) maxSpeed = json.getInt(MAX_SPEED);
|
||||
if (json.has(NOTES)) notes = json.getString(NOTES);
|
||||
@@ -249,7 +238,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Entry<Integer, Car> entry: cars.entrySet()) file.write(entry.getValue().json()+"\n");
|
||||
for (Entry<Id, Car> entry: cars.entrySet()) file.write(entry.getValue().json()+"\n");
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
super(name);
|
||||
}
|
||||
|
||||
public Locomotive(String name, Integer id) {
|
||||
public Locomotive(String name, Id id) {
|
||||
super(name,id);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
}
|
||||
|
||||
public static Tag cockpit(Object locoOrTrain) {
|
||||
int id = 0;
|
||||
Id id = null;
|
||||
int speed = 0;
|
||||
String realm = null;
|
||||
Train train = null;
|
||||
@@ -95,7 +95,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
} else if (locoOrTrain instanceof Train) {
|
||||
train = (Train)locoOrTrain;
|
||||
realm = REALM_TRAIN;
|
||||
id = train.id;
|
||||
id = train.id();
|
||||
speed = train.speed;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,7 @@ import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.PathFinder;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
@@ -30,7 +28,6 @@ import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Range;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -49,9 +46,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
private static final String CAR_ID = "carId";
|
||||
public static final String LOCO_ID = "locoId";
|
||||
private static final String TRACE = "trace";
|
||||
private static final HashMap<Integer, Train> trains = new HashMap<>();
|
||||
private static final HashMap<Id, Train> trains = new HashMap<>();
|
||||
public static final String ID = "id";
|
||||
public int id;
|
||||
|
||||
private static final String NAME = "name";
|
||||
private String name = null;
|
||||
@@ -117,8 +113,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
this(loco,null);
|
||||
}
|
||||
|
||||
public Train(Locomotive loco, Integer id) {
|
||||
if (isNull(id)) id = Application.createId();
|
||||
public Train(Locomotive loco, Id id) {
|
||||
if (isNull(id)) id = new Id();
|
||||
this.id = id;
|
||||
add(loco);
|
||||
trains.put(id, this);
|
||||
@@ -136,7 +132,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
return t("No train id passed!");
|
||||
}
|
||||
int id = Integer.parseInt(params.get(Train.ID));
|
||||
Id id = Id.from(params);
|
||||
Train train = trains.get(id);
|
||||
if (isNull(train)) return(t("No train with id {}!",id));
|
||||
switch (action) {
|
||||
@@ -320,7 +316,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return properties();
|
||||
}
|
||||
|
||||
public static Train get(int id) {
|
||||
public static Train get(Id id) {
|
||||
return trains.get(id);
|
||||
}
|
||||
|
||||
@@ -344,15 +340,15 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (isSet(route)) json.put(ROUTE, route.id());
|
||||
if (isSet(direction)) json.put(DIRECTION, direction);
|
||||
|
||||
Vector<Integer> locoIds = new Vector<Integer>();
|
||||
Vector<Id> locoIds = new Vector<Id>();
|
||||
for (Locomotive loco : locos) locoIds.add(loco.id());
|
||||
json.put(LOCOS, locoIds);
|
||||
|
||||
Vector<Integer> carIds = new Vector<Integer>();
|
||||
Vector<Id> carIds = new Vector<Id>();
|
||||
for (Car car : cars) carIds.add(car.id());
|
||||
json.put(CARS,carIds);
|
||||
|
||||
Vector<String> tileIds = new Vector<String>();
|
||||
Vector<Id> tileIds = new Vector<Id>();
|
||||
for (Tile tile : trace) tileIds.add(tile.id());
|
||||
json.put(TRACE, tileIds);
|
||||
|
||||
@@ -377,7 +373,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
public Tag link(String...args) {
|
||||
String tx = args.length<1 ? name()+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
return link(type, Map.of(REALM,REALM_TRAIN,ID,id,ACTION,ACTION_PROPS), tx);
|
||||
return link(type, tx);
|
||||
}
|
||||
|
||||
public static TreeSet<Train> list() {
|
||||
@@ -390,9 +386,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
while (isSet(line)) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
|
||||
int id = json.getInt(ID);
|
||||
|
||||
Train train = new Train(null,id);
|
||||
Train train = new Train(null,Id.from(json));
|
||||
train.plan(plan).load(json);
|
||||
|
||||
line = file.readLine();
|
||||
@@ -405,8 +399,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (json.has(DIRECTION)) direction = Direction.valueOf(json.getString(DIRECTION));
|
||||
if (json.has(NAME)) name = json.getString(NAME);
|
||||
if (json.has(TAGS)) json.getJSONArray(TAGS ).forEach(elem -> { tags.add(elem.toString()); });
|
||||
if (json.has(TRACE)) json.getJSONArray(TRACE).forEach(elem -> { trace.add(plan.get(elem.toString(), false).set(this)); });
|
||||
if (json.has(BLOCK)) currentBlock = (Block) plan.get(json.getString(BLOCK), false).set(this); // do not move this up! during set, other fields will be referenced!
|
||||
if (json.has(TRACE)) json.getJSONArray(TRACE).forEach(elem -> { trace.add(plan.get(new Id(elem.toString()), false).set(this)); });
|
||||
if (json.has(BLOCK)) currentBlock = (Block) plan.get(new Id(json.getString(BLOCK)), false).set(this); // do not move this up! during set, other fields will be referenced!
|
||||
for (Object id : json.getJSONArray(CARS)) add(Car.get(id));
|
||||
for (Object id : json.getJSONArray(LOCOS)) add((Locomotive) Car.get(id));
|
||||
return this;
|
||||
@@ -516,7 +510,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Tag properties() {
|
||||
@Override
|
||||
public Window properties() {
|
||||
Window window = new Window("train-properties",t("Properties of {}",this));
|
||||
|
||||
Locomotive.cockpit(this).addTo(window);
|
||||
@@ -547,14 +542,12 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (isNull(destination)) {
|
||||
new Button(t("Select from plan"),"return selectDest("+id+");").addTo(dest);
|
||||
} else {
|
||||
link("span",Map.of(REALM,REALM_PLAN,ID,destination.id(),ACTION,ACTION_CLICK),destination.toString()).addTo(dest);
|
||||
link("span",destination,Map.of(REALM,REALM_PLAN,ID,destination.id().toString(),ACTION,ACTION_CLICK)).addTo(dest);
|
||||
new Button(t("Drop"),Map.of(REALM,REALM_TRAIN,ID,id,ACTION,ACTION_MOVE,DESTINATION,"")).addTo(dest);
|
||||
}
|
||||
|
||||
dest.addTo(propList);
|
||||
if (isSet(route)) {
|
||||
link("li", Map.of(REALM,REALM_ROUTE,ID,route.id(),ACTION,ACTION_PROPS), route).addTo(propList);
|
||||
}
|
||||
if (isSet(route)) link("li", route).addTo(propList);
|
||||
int ms = maxSpeed();
|
||||
if (ms < Integer.MAX_VALUE) new Tag("li").content(t("Max. Speed")+": "+maxSpeed()+NBSP+speedUnit).addTo(propList);
|
||||
|
||||
@@ -594,7 +587,10 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
public void reserveNext() {
|
||||
Context context = new Context(null, route, this, route.endBlock(), route.endDirection);
|
||||
Context context = new Context(this)
|
||||
.route(route)
|
||||
.block(route.endBlock())
|
||||
.direction(route.endDirection);
|
||||
Route nextRoute = PathFinder.chooseRoute(context);
|
||||
if (isNull(nextRoute)) return;
|
||||
|
||||
@@ -622,7 +618,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Entry<Integer, Train> entry:trains.entrySet()) {
|
||||
for (Entry<Id, Train> entry:trains.entrySet()) {
|
||||
Train train = entry.getValue();
|
||||
file.write(train.json()+"\n");
|
||||
}
|
||||
@@ -653,7 +649,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
destination = null;
|
||||
return t("Dropped destination of {}.",this);
|
||||
}
|
||||
Tile tile = plan.get(dest, true);
|
||||
Tile tile = plan.get(new Id(dest), true);
|
||||
if (isNull(tile)) return t("Tile {} not known!",dest);
|
||||
if (tile instanceof Block) {
|
||||
destination = (Block) tile;
|
||||
@@ -781,11 +777,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
return properties();
|
||||
}
|
||||
|
||||
private static String t(String message, Object...fills) {
|
||||
return Translation.get(Application.class, message, fills);
|
||||
}
|
||||
|
||||
|
||||
public SortedSet<String> tags() {
|
||||
TreeSet<String> list = new TreeSet<String>(tags);
|
||||
|
||||
@@ -151,7 +151,7 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Block get(String blockId) {
|
||||
public static Block get(Id blockId) {
|
||||
Tile tile = plan.get(blockId, false);
|
||||
if (tile instanceof Block) return (Block) tile;
|
||||
return null;
|
||||
@@ -195,7 +195,7 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
|
||||
public Tag link(String...args) {
|
||||
String tx = args.length<1 ? name+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
return link(type, Map.of(REALM,REALM_PLAN,ID,id(),ACTION,ACTION_PROPS), tx).clazz("link","block");
|
||||
return link(type, tx).clazz("link","block");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -267,7 +267,7 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
|
||||
new Input("min."+wt.tag+"."+dB,wt.get(dB).min).numeric().addTo(new Tag("td")).addTo(row);
|
||||
new Input("max."+wt.tag+"."+dB,wt.get(dB).max).numeric().addTo(new Tag("td")).addTo(row);
|
||||
Tag actions = new Tag("td");
|
||||
Map<String, String> props = Map.of(REALM,REALM_PLAN,ID,id(),ACTION,ACTION_TIMES);
|
||||
Map<String, String> props = Map.of(REALM,REALM_PLAN,ID,id().toString(),ACTION,ACTION_TIMES);
|
||||
switch (count) {
|
||||
case 1:
|
||||
actions.content(""); break;
|
||||
@@ -348,8 +348,8 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
|
||||
public Tile update(HashMap<String, String> params) throws IOException {
|
||||
if (params.containsKey(NAME)) name=params.get(NAME);
|
||||
if (params.containsKey(Train.class.getSimpleName())) {
|
||||
int trainId = Integer.parseInt(params.get(Train.class.getSimpleName()));
|
||||
if (trainId == 0) {
|
||||
Id trainId = Id.from(params,Train.class.getSimpleName());
|
||||
if (trainId == null) { // TODO: this is rubbish
|
||||
if (isSet(train)) train.dropTrace();
|
||||
train = null;
|
||||
} else {
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.util.Map;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
|
||||
public abstract class Bridge extends Tile {
|
||||
private static Bridge pendingConnection = null;
|
||||
@@ -37,10 +36,9 @@ public abstract class Bridge extends Tile {
|
||||
@Override
|
||||
public Window propMenu() {
|
||||
Window win = super.propMenu();
|
||||
Map<String, String> props = Map.of(REALM,REALM_PLAN,ID,id(),ACTION,ACTION_CONNECT);
|
||||
new Tag("h4").content("Counterpart").addTo(win);
|
||||
new Tag("p").content(isSet(counterpart) ? t("Connected to {}.",counterpart) : t("Not connected to other bridge part!")).addTo(win);
|
||||
new Button(t("Select counterpart"), props).addTo(win);
|
||||
button(t("Select counterpart"),Map.of(ACTION,ACTION_CONNECT)).addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,7 @@ import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.actions.ActionList;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
@@ -22,7 +20,7 @@ import de.srsoftware.web4rail.tags.Select;
|
||||
public class Contact extends Tile{
|
||||
|
||||
private static final String ADDRESS = "address";
|
||||
private static final HashMap<String, Contact> contactsById = new HashMap<String, Contact>();
|
||||
private static final HashMap<Id, Contact> contactsById = new HashMap<Id, Contact>();
|
||||
private static final HashMap<Integer, Contact> contactsByAddr = new HashMap<Integer, Contact>();
|
||||
private boolean state = false;
|
||||
private String trigger = null;
|
||||
@@ -101,7 +99,7 @@ public class Contact extends Tile{
|
||||
return contactsByAddr.get(addr);
|
||||
}
|
||||
|
||||
public static Contact get(String contactId) {
|
||||
public static Contact get(Id contactId) {
|
||||
return contactsById.get(contactId);
|
||||
}
|
||||
|
||||
@@ -135,7 +133,7 @@ public class Contact extends Tile{
|
||||
|
||||
public static Object process(HashMap<String, String> params) {
|
||||
String action = params.get(ACTION);
|
||||
String id = params.get(ID);
|
||||
Id id = Id.from(params);
|
||||
if (action == null) return t("Missing ACTION on call to {}.process()",Contact.class.getSimpleName());
|
||||
Contact contact;
|
||||
switch (action) {
|
||||
@@ -157,9 +155,7 @@ public class Contact extends Tile{
|
||||
Form form = super.propForm(formId);
|
||||
new Tag("h4").content(t("Hardware settings")).addTo(form);
|
||||
Tag label = new Input(ADDRESS, addr).numeric().addTo(new Label(t("Address:")+NBSP));
|
||||
Map<String, String> props = Map.of(REALM,REALM_CONTACT,ID,id(),ACTION,ACTION_ANALYZE);
|
||||
new Button(t("learn"), props).addTo(label).addTo(form);
|
||||
|
||||
button(t("learn"),Map.of(ACTION,ACTION_ANALYZE)).addTo(label).addTo(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ public class Relay extends Tile implements Device{
|
||||
private String name = t("Relay");
|
||||
protected boolean state = true;
|
||||
|
||||
private static final HashMap<String,Relay> relays = new HashMap<String, Relay>();
|
||||
private static final HashMap<Id,Relay> relays = new HashMap<Id, Relay>();
|
||||
public static final boolean STATE_A = true,STATE_B=false;
|
||||
private static final String LABEL_A = "label_a";
|
||||
private static final String LABEL_B = "label_b";
|
||||
@@ -242,7 +242,7 @@ public class Relay extends Tile implements Device{
|
||||
return relays.values();
|
||||
}
|
||||
|
||||
public static Relay get(String relayId) {
|
||||
public static Relay get(Id relayId) {
|
||||
return relays.get(relayId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +31,8 @@ public abstract class Signal extends Tile implements Comparable<Signal>{
|
||||
|
||||
@Override
|
||||
public int compareTo(Signal other) {
|
||||
String tid = this.id();
|
||||
String oid = other.id();
|
||||
Id tid = this.id();
|
||||
Id oid = other.id();
|
||||
return tid.compareTo(oid);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,9 +19,7 @@ import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
@@ -101,12 +99,12 @@ public abstract class Tile extends BaseClass{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public String id() {
|
||||
public Id id() {
|
||||
return Tile.id(x, y);
|
||||
}
|
||||
|
||||
public static String id(int x, int y) {
|
||||
return x+"-"+y;
|
||||
public static Id id(int x, int y) {
|
||||
return new Id(x+"-"+y);
|
||||
}
|
||||
|
||||
private static void inflate(String clazz, JSONObject json, Plan plan) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IOException {
|
||||
@@ -131,7 +129,7 @@ public abstract class Tile extends BaseClass{
|
||||
if (isSet(route)) json.put(ROUTE, route.id());
|
||||
if (isSet(oneWay)) json.put(ONEW_WAY, oneWay);
|
||||
if (disabled) json.put(DISABLED, true);
|
||||
if (isSet(train)) json.put(REALM_TRAIN, train.id);
|
||||
if (isSet(train)) json.put(REALM_TRAIN, train.id());
|
||||
json.put(LENGTH, length);
|
||||
return json;
|
||||
}
|
||||
@@ -155,7 +153,7 @@ public abstract class Tile extends BaseClass{
|
||||
public Tag link(String...args) {
|
||||
String tx = args.length<1 ? id()+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
return link(type, Map.of(REALM,REALM_PLAN,ID,id(),ACTION,ACTION_CLICK), tx);
|
||||
return super.link(type, tx, Map.of(ACTION,ACTION_CLICK));
|
||||
}
|
||||
|
||||
|
||||
@@ -216,7 +214,7 @@ public abstract class Tile extends BaseClass{
|
||||
Window window = new Window("tile-properties",t("Properties of {} @ ({},{})",title(),x,y));
|
||||
|
||||
if (isSet(train)) {
|
||||
HashMap<String, Object> props = new HashMap<String,Object>(Map.of(REALM,REALM_TRAIN,ID,train.id));
|
||||
HashMap<String, Object> props = new HashMap<String,Object>(Map.of(REALM,REALM_TRAIN,ID,train.id()));
|
||||
if (isSet(train.route)) {
|
||||
props.put(ACTION, ACTION_STOP);
|
||||
window.children().insertElementAt(new Button(t("stop"),props), 1);
|
||||
@@ -236,7 +234,7 @@ public abstract class Tile extends BaseClass{
|
||||
window.children().insertElementAt(new Tag("h4").content(t("Train:")), 1);
|
||||
}
|
||||
|
||||
if (isSet(route)) link("p",Map.of(REALM,REALM_ROUTE,ID,route.id(),ACTION,ACTION_PROPS),t("Locked by {}",route)).addTo(window);
|
||||
if (isSet(route)) link("p",t("Locked by {}",route)).addTo(window);
|
||||
|
||||
Form form = propForm("tile-properties-"+id());
|
||||
if (isTrack) {
|
||||
@@ -295,7 +293,7 @@ public abstract class Tile extends BaseClass{
|
||||
return routes;
|
||||
}
|
||||
|
||||
public static void saveAll(HashMap<String, Tile> tiles ,String filename) throws IOException {
|
||||
public static void saveAll(HashMap<Id, Tile> tiles ,String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Tile tile : tiles.values()) {
|
||||
if (isNull(tile) || tile instanceof Shadow) continue;
|
||||
@@ -317,10 +315,6 @@ public abstract class Tile extends BaseClass{
|
||||
return plan.place(this);
|
||||
}
|
||||
|
||||
protected static String t(String txt, Object...fills) {
|
||||
return Translation.get(Application.class, txt, fills);
|
||||
}
|
||||
|
||||
public Tag tag(Map<String,Object> replacements) throws IOException {
|
||||
int width = 100*width();
|
||||
int height = 100*height();
|
||||
@@ -329,7 +323,7 @@ public abstract class Tile extends BaseClass{
|
||||
replacements.put("%height%",height);
|
||||
String style = "";
|
||||
Tag svg = new Tag("svg")
|
||||
.id(isSet(x) && isSet(y) ? id() : getClass().getSimpleName())
|
||||
.id(isSet(x) && isSet(y) ? id().toString() : getClass().getSimpleName())
|
||||
.clazz(classes())
|
||||
.size(100,100)
|
||||
.attr("name", getClass().getSimpleName())
|
||||
|
||||
Reference in New Issue
Block a user