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

@@ -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);