still overhauling class hierarchy

This commit is contained in:
Stephan Richter
2020-12-02 00:14:46 +01:00
parent b7effda44f
commit c4f48ebb63
19 changed files with 111 additions and 96 deletions

View File

@@ -17,6 +17,10 @@ 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.tags.Fieldset;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.TextArea;
import de.srsoftware.web4rail.tiles.Block;
import de.srsoftware.web4rail.tiles.Contact;
import de.srsoftware.web4rail.tiles.Tile;
@@ -28,6 +32,8 @@ public abstract class BaseClass implements Constants{
public static String lengthUnit = DEFAULT_LENGTH_UNIT;
private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
protected Id id = null;
protected String notes;
public static class Context {
private BaseClass main = null;
@@ -199,20 +205,22 @@ public abstract class BaseClass implements Constants{
return button(text,null);
}
public Map<String,String> contextAction(String action){
String realm = REALM_PLAN;
if (this instanceof Tile) realm = REALM_PLAN;
if (this instanceof Contact) realm = REALM_CONTACT;
public String realm() {
if (this instanceof Tile) return REALM_PLAN;
if (this instanceof Contact) return REALM_CONTACT;
if (this instanceof Car) realm = REALM_CAR;
if (this instanceof Locomotive) realm = REALM_LOCO;
if (this instanceof Car) return REALM_CAR;
if (this instanceof Locomotive) return REALM_LOCO;
if (this instanceof Action) realm = REALM_ACTIONS;
if (this instanceof Condition) realm = REALM_CONDITION;
if (this instanceof Route) realm = REALM_ROUTE;
if (this instanceof Train) realm = REALM_TRAIN;
return Map.of(ACTION,action,CONTEXT,realm+":"+id());
if (this instanceof Action) return REALM_ACTIONS;
if (this instanceof Condition) return REALM_CONDITION;
if (this instanceof Route) return REALM_ROUTE;
if (this instanceof Train) return REALM_TRAIN;
return REALM_PLAN;
}
public Map<String,String> contextAction(String action){
return Map.of(ACTION,action,CONTEXT,realm()+":"+id());
}
public Id id() {
@@ -229,7 +237,10 @@ public abstract class BaseClass implements Constants{
}
public JSONObject json() {
return new JSONObject().put(ID, id().toString());
JSONObject json = new JSONObject();
if (isSet(id)) json.put(ID, id().toString());
if (isSet(notes) && !notes.isEmpty()) json.put(NOTES, notes);
return json;
}
public Tag link(String tagClass,Object caption) {
@@ -264,7 +275,27 @@ public abstract class BaseClass implements Constants{
}
public Window properties() {
return new Window(getClass().getSimpleName()+"-properties", t("Properties of {}",this));
Window win = new Window(getClass().getSimpleName()+"-properties", t("Properties of {}",this));
Form form = propertyForm();
if (form!=null && form.children().size()>2) {
new Button(t("Apply"),form).addTo(form).addTo(win);
} else win.content(t("This tile ({}) has no editable properties",getClass().getSimpleName()));
return win;
}
public Form propertyForm() {
Form form = new Form(getClass().getSimpleName()+"-prop-form");
new Input(ACTION, ACTION_UPDATE).hideIn(form);
new Input(REALM,realm()).hideIn(form);
new Input(ID,id()).hideIn(form);
Fieldset fieldset = new Fieldset("Basic properties");
fieldset.addTo(form);
fieldset = new Fieldset(t("Notes"));
new TextArea(NOTES,notes).addTo(fieldset.clazz("notes")).addTo(form);
return form;
}
public Map<String,String> props(Map<String,String> additionalProps){
@@ -288,4 +319,15 @@ public abstract class BaseClass implements Constants{
protected static String t(String txt, Object...fills) {
return Translation.get(Application.class, txt, fills);
}
protected Object update(HashMap<String, String> params) {
if (params.containsKey(NOTES)) notes = params.get(NOTES).trim();
return this;
}
public BaseClass load(JSONObject json) {
if (json.has(ID)) id = Id.from(json);
if (json.has(NOTES)) notes = json.getString(NOTES);
return this;
}
}

View File

@@ -69,10 +69,7 @@ public class PathFinder extends BaseClass{
} else {
LOG.debug("{}- Candidate: {}",inset,routeCandidate.shortName());
Context forwardContext = new Context(train);
forwardContext
.block(routeCandidate.endBlock())
.direction(routeCandidate.endDirection)
.route(null);
forwardContext.block(routeCandidate.endBlock()).route(null);
visitedRoutes.add(routeCandidate);
TreeMap<Integer, List<Route>> forwardRoutes = availableRoutes(forwardContext,visitedRoutes);
visitedRoutes.remove(routeCandidate);

View File

@@ -920,7 +920,8 @@ public class Plan extends BaseClass{
* @return
* @throws IOException
*/
private Object update(HashMap<String, String> params) throws IOException {
protected Object update(HashMap<String, String> params) {
super.update(params);
Tile tile = get(Id.from(params),true);
if (isSet(tile)) return tile.update(params);

View File

@@ -780,7 +780,7 @@ public class Route extends BaseClass implements Comparable<Route>{
return this;
}
public Object update(HashMap<String, String> params,Plan plan) {
protected Object update(HashMap<String, String> params,Plan plan) {
LOG.debug("update({})",params);
String name = params.get(NAME);
if (isSet(name)) name(name);

View File

@@ -65,5 +65,4 @@ public class DetermineTrainInBlock extends Action {
if (isSet(blockId)) block = Block.get(blockId);
return properties(params);
}
}

View File

@@ -28,7 +28,6 @@ import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Label;
import de.srsoftware.web4rail.tags.Table;
import de.srsoftware.web4rail.tags.TextArea;
public class Car extends BaseClass implements Comparable<Car>{
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
@@ -47,7 +46,6 @@ public class Car extends BaseClass implements Comparable<Car>{
private Train train;
protected Plan plan;
protected int maxSpeed = 0;
private String notes;
public Car(String name) {
this(name,null);
@@ -102,7 +100,6 @@ public class Car extends BaseClass implements Comparable<Car>{
json.put(NAME, name);
json.put(LENGTH, length);
if (maxSpeed != 0) json.put(MAX_SPEED, maxSpeed);
if (isSet(notes) && !notes.isEmpty()) json.put(NOTES, notes);
json.put(STOCK_ID, stockId);
if (!tags.isEmpty()) json.put(TAGS, tags);
return json;
@@ -145,11 +142,10 @@ public class Car extends BaseClass implements Comparable<Car>{
file.close();
}
protected Car load(JSONObject json) {
if (json.has(ID)) id = Id.from(json);
public Car load(JSONObject json) {
super.load(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);
if (json.has(STOCK_ID)) stockId = json.getString(STOCK_ID);
if (json.has(TAGS)) json.getJSONArray(TAGS).forEach(elem -> { tags.add(elem.toString()); });
return this;
@@ -202,32 +198,20 @@ public class Car extends BaseClass implements Comparable<Car>{
}
public Form propertyForm() {
Form form = new Form("car-prop-form");
new Input(ACTION, ACTION_UPDATE).hideIn(form);
new Input(REALM,REALM_CAR).hideIn(form);
new Input(ID,id()).hideIn(form);
Fieldset fieldset = new Fieldset("Basic properties");
Form form = super.propertyForm();
Fieldset fieldset = form.children().stream().filter(tag -> tag instanceof Fieldset).map(tag -> (Fieldset)tag).findFirst().get();
new Input(NAME,name).addTo(new Label(t("Name")+NBSP)).addTo(fieldset);
new Input(STOCK_ID,stockId).addTo(new Label(t("Stock ID")+NBSP)).addTo(fieldset);
new Input(LENGTH,length).attr("type", "number").addTo(new Label(t("Length")+NBSP)).content(NBSP+lengthUnit).addTo(fieldset);
new Input(TAGS,String.join(", ", tags)).addTo(new Label(t("Tags")+NBSP)).addTo(fieldset);
new Input(MAX_SPEED, maxSpeed).numeric().addTo(new Label(t("Maximum speed")+":"+NBSP)).content(NBSP+speedUnit).addTo(fieldset);
fieldset.addTo(form);
fieldset = new Fieldset(t("Notes"));
new TextArea(NOTES,notes).addTo(fieldset.clazz("notes")).addTo(form);
return form;
}
public Window properties() {
Window win = new Window("car-props", t("Properties of {}",this));
Form form = propertyForm();
if (form!=null && form.children().size()>2) {
new Button(t("Apply"),form).addTo(form).addTo(win);
} else {
win.content(t("This tile ({}) has no editable properties",getClass().getSimpleName()));
}
Window win = super.properties();
Tag list = new Tag("ul");
if (train != null) train.link().addTo(new Tag("li").content(t("Train:")+NBSP)).addTo(list);
@@ -262,9 +246,9 @@ public class Car extends BaseClass implements Comparable<Car>{
this.train = train;
}
public Car update(HashMap<String, String> params) {
protected Car update(HashMap<String, String> params) {
super.update(params);
if (params.containsKey(NAME)) name = params.get(NAME).trim();
if (params.containsKey(NOTES)) notes = params.get(NOTES).trim();
if (params.containsKey(LENGTH)) length = Integer.parseInt(params.get(LENGTH));
if (params.containsKey(MAX_SPEED)) maxSpeed = Integer.parseInt(params.get(MAX_SPEED));
if (params.containsKey(STOCK_ID)) stockId = params.get(STOCK_ID);

View File

@@ -220,7 +220,7 @@ public class Locomotive extends Car implements Constants,Device{
}
@Override
protected Car load(JSONObject json) {
public Car load(JSONObject json) {
super.load(json);
if (json.has(LOCOMOTIVE)) {
JSONObject loco = json.getJSONObject(LOCOMOTIVE);
@@ -353,7 +353,7 @@ public class Locomotive extends Car implements Constants,Device{
}
@Override
public Car update(HashMap<String, String> params) {
protected Car update(HashMap<String, String> params) {
super.update(params);
if (params.containsKey(PROTOCOL)) proto = Protocol.valueOf(params.get(PROTOCOL));
if (params.containsKey(ADDRESS)) address = Integer.parseInt(params.get(ADDRESS));

View File

@@ -386,7 +386,7 @@ public class Train extends BaseClass implements Comparable<Train> {
file.close();
}
private Train load(JSONObject json) {
public Train load(JSONObject json) {
pushPull = json.getBoolean(PUSH_PULL);
if (json.has(DIRECTION)) direction = Direction.valueOf(json.getString(DIRECTION));
if (json.has(NAME)) name = json.getString(NAME);
@@ -395,6 +395,7 @@ public class Train extends BaseClass implements Comparable<Train> {
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));
super.load(json);
return this;
}
@@ -504,23 +505,11 @@ public class Train extends BaseClass implements Comparable<Train> {
@Override
public Window properties() {
Window window = new Window("train-properties",t("Properties of {}",this));
Window window = super.properties();
Locomotive.cockpit(this).addTo(window);
window.children().insertElementAt(Locomotive.cockpit(this), 2);
Fieldset fieldset = new Fieldset(t("editable train properties"));
Form form = new Form();
new Input(ACTION,ACTION_UPDATE).hideIn(form);
new Input(REALM,REALM_TRAIN).hideIn(form);
new Input(ID,id).hideIn(form);
new Input(NAME,name).addTo(form);
new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull).addTo(form);
new Input(TAGS,String.join(", ", tags)).addTo(new Label(t("Tags")+NBSP)).addTo(form);
new Button(t("Apply")).addTo(form).addTo(fieldset);
fieldset.addTo(window);
fieldset = new Fieldset(t("other train properties"));
Fieldset fieldset = new Fieldset(t("other train properties"));
Tag propList = new Tag("ul").clazz("proplist");
@@ -561,6 +550,16 @@ public class Train extends BaseClass implements Comparable<Train> {
propList.addTo(fieldset).addTo(window);
return window;
}
@Override
public Form propertyForm() {
Form form = super.propertyForm();
Fieldset fieldset = form.children().stream().filter(tag -> tag instanceof Fieldset).map(tag -> (Fieldset)tag).findFirst().get();
new Input(NAME,name).addTo(fieldset);
new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull).addTo(fieldset);
new Input(TAGS,String.join(", ", tags)).addTo(new Label(t("Tags")+NBSP)).addTo(fieldset);
return form;
}
public Object quitAutopilot() {
if (isSet(nextRoute)) {
@@ -579,10 +578,7 @@ public class Train extends BaseClass implements Comparable<Train> {
}
public void reserveNext() {
Context context = new Context(this)
.route(route)
.block(route.endBlock())
.direction(route.endDirection);
Context context = new Context(this).route(route).block(route.endBlock());
Route nextRoute = PathFinder.chooseRoute(context);
if (isNull(nextRoute)) return;
@@ -794,7 +790,7 @@ public class Train extends BaseClass implements Comparable<Train> {
return properties();
}
public Train update(HashMap<String, String> params) {
protected Train update(HashMap<String, String> params) {
LOG.debug("update({})",params);
pushPull = params.containsKey(PUSH_PULL) && params.get(PUSH_PULL).equals("on");
if (params.containsKey(NAME)) name = params.get(NAME);

View File

@@ -199,8 +199,7 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
}
@Override
protected Tile load(JSONObject json) throws IOException {
super.load(json);
public Tile load(JSONObject json) {
name = json.has(NAME) ? json.getString(NAME) : "Block";
turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN);
if (json.has(WAIT_TIMES)) {
@@ -210,7 +209,7 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
if (object instanceof JSONObject) waitTimes.add(new WaitTime(null).load((JSONObject) object));
});
}
return this;
return super.load(json);
}
@Override
@@ -345,7 +344,7 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
if (params.containsKey(NAME)) name=params.get(NAME);
if (params.containsKey(Train.class.getSimpleName())) {
Id trainId = Id.from(params,Train.class.getSimpleName());

View File

@@ -46,7 +46,7 @@ public abstract class Bridge extends Tile {
}
@Override
protected Tile load(JSONObject json) throws IOException {
public Tile load(JSONObject json) {
if (json.has(COUNTERPART)) {
new Thread() {
@Override

View File

@@ -117,11 +117,10 @@ public class Contact extends Tile{
@Override
protected Tile load(JSONObject json) throws IOException {
super.load(json);
public Tile load(JSONObject json) {
if (json.has(ADDRESS)) addr(json.getInt(ADDRESS));
if (json.has(REALM_ACTIONS)) actions = ActionList.load(json.getJSONArray(REALM_ACTIONS));
return this;
return super.load(json);
}
@Override
@@ -213,7 +212,7 @@ public class Contact extends Tile{
return true;
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
if (params.containsKey(ADDRESS)) addr(Integer.parseInt(params.get(ADDRESS)));
return super.update(params);
}

View File

@@ -109,7 +109,7 @@ public class Relay extends Tile implements Device{
}
@Override
protected Tile load(JSONObject json) throws IOException {
public Tile load(JSONObject json) {
if (json.has(ADDRESS)) address = json.getInt(ADDRESS);
if (json.has(PORT_A)) portA = json.getInt(PORT_A);
if (json.has(PORT_B)) portB = json.getInt(PORT_B);
@@ -227,7 +227,7 @@ public class Relay extends Tile implements Device{
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
if (params.containsKey(PROTOCOL)) protocol = Protocol.valueOf(params.get(PROTOCOL));
if (params.containsKey(ADDRESS)) address = Integer.parseInt(params.get(ADDRESS));
if (params.containsKey(PORT_A)) portA = Integer.parseInt(params.get(PORT_A));

View File

@@ -1,6 +1,5 @@
package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
@@ -30,10 +29,9 @@ public abstract class StretchableTile extends Tile {
}
@Override
protected Tile load(JSONObject json) throws IOException {
super.load(json);
public Tile load(JSONObject json) {
if (json.has(STRETCH_LENGTH)) stretch = json.getInt(STRETCH_LENGTH);
return this;
return super.load(json);
}
@Override
@@ -61,7 +59,7 @@ public abstract class StretchableTile extends Tile {
protected abstract String stretchType();
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
for (Entry<String, String> entry : params.entrySet()) {
switch (entry.getKey()) {
case STRETCH_LENGTH:

View File

@@ -25,10 +25,9 @@ public class TextDisplay extends StretchableTile {
}
@Override
protected Tile load(JSONObject json) throws IOException {
super.load(json);
public Tile load(JSONObject json) {
if (json.has(TEXT)) text = json.getString(TEXT);
return this;
return super.load(json);
}
@Override
@@ -73,7 +72,7 @@ public class TextDisplay extends StretchableTile {
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
for (Entry<String, String> entry : params.entrySet()) {
switch (entry.getKey()) {
case TEXT:

View File

@@ -169,7 +169,8 @@ public abstract class Tile extends BaseClass{
}
}
protected Tile load(JSONObject json) throws IOException {
public Tile load(JSONObject json) {
super.load(json);
JSONObject pos = json.getJSONObject(POS);
x = pos.getInt(X);
y = pos.getInt(Y);
@@ -394,7 +395,7 @@ public abstract class Tile extends BaseClass{
plan.place(this);
}
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
LOG.debug("{}.update({})",getClass().getSimpleName(),params);
String oneWayDir = params.get("oneway");
if (isSet(oneWayDir)) {
@@ -407,7 +408,7 @@ public abstract class Tile extends BaseClass{
disabled = "on".equals(params.get(DISABLED));
String len = params.get(LENGTH);
if (isSet(len)) length(Integer.parseInt(len));
plan.stream(tag(null).toString());
plan.place(this);
return this;
}

View File

@@ -103,7 +103,7 @@ public abstract class Turnout extends Tile implements Device{
}
@Override
protected Tile load(JSONObject json) throws IOException {
public Tile load(JSONObject json) {
if (json.has(ADDRESS)) address = json.getInt(ADDRESS);
if (json.has(PORT_A)) portA = json.getInt(PORT_A);
if (json.has(PORT_B)) portB = json.getInt(PORT_B);
@@ -201,7 +201,7 @@ public abstract class Turnout extends Tile implements Device{
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
if (params.containsKey(PROTOCOL)) protocol = Protocol.valueOf(params.get(PROTOCOL));
if (params.containsKey(ADDRESS)) {
int newAddress = Integer.parseInt(params.get(ADDRESS));

View File

@@ -50,7 +50,7 @@ public class TurnoutL extends Turnout {
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
if (params.containsKey(STRAIGHT)) portA = Integer.parseInt(params.get(STRAIGHT));
if (params.containsKey(LEFT)) portB = Integer.parseInt(params.get(LEFT));
return super.update(params);

View File

@@ -51,7 +51,7 @@ public class TurnoutR extends Turnout {
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
public Tile update(HashMap<String, String> params) {
if (params.containsKey(STRAIGHT)) portA = Integer.parseInt(params.get(STRAIGHT));
if (params.containsKey(RIGHT)) portB = Integer.parseInt(params.get(RIGHT));
return super.update(params);