From 7b8aea89f29b3672907e00cf5b1054857b31f5fe Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 27 Oct 2020 18:29:17 +0100 Subject: [PATCH] implemented adding of locos to train --- pom.xml | 2 +- resources/css/style.css | 3 + .../de/srsoftware/web4rail/moving/Train.java | 71 +++++++++++++------ .../de/srsoftware/web4rail/tiles/Block.java | 6 +- .../de/srsoftware/web4rail/tiles/Tile.java | 4 +- .../de/srsoftware/web4rail/tiles/Turnout.java | 2 +- 6 files changed, 61 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index 5a8ae5b..1186fcd 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 de.srsoftware web4rail - 0.6.6 + 0.6.7 Web4Rail jar Java Model Railway Control diff --git a/resources/css/style.css b/resources/css/style.css index 560ce22..c66f752 100644 --- a/resources/css/style.css +++ b/resources/css/style.css @@ -210,3 +210,6 @@ fieldset{ background: red; } +ul{ + clear: both; +} \ No newline at end of file diff --git a/src/main/java/de/srsoftware/web4rail/moving/Train.java b/src/main/java/de/srsoftware/web4rail/moving/Train.java index 9e64b93..4388bc0 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Train.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Train.java @@ -96,6 +96,7 @@ public class Train implements Constants { } public static final String LOCO_ID = "locoId"; + private static final String CAR_ID = "carId"; public int speed = 0; private Autopilot autopilot = null; @@ -129,20 +130,36 @@ public class Train implements Constants { Train train = trains.get(id); if (train == null) return(t("No train with id {}!",id)); switch (action) { - case ACTION_PROPS: - return train.props(); + case ACTION_ADD: + return train.addCar(params); case ACTION_AUTO: return train.automatic(); + case ACTION_PROPS: + return train.props(); case ACTION_START: return train.start(); case ACTION_STOP: return train.stop(); + case ACTION_TURN: + return train.turn(); case ACTION_UPDATE: return train.update(params); } return t("Unknown action: {}",params.get(ACTION)); } + + private Object addCar(HashMap params) { + LOG.debug("addCar({})",params); + if (!params.containsKey(CAR_ID)) return t("No car id passed to Train.addCar!"); + Car car = Car.get(params.get(CAR_ID)); + if (car == null) return t("No car with id \"{}\" known!",params.get(CAR_ID)); + if (car instanceof Locomotive) { + locos.add((Locomotive) car); + } else cars.add(car); + return this; + } + private static Object create(HashMap params, Plan plan) { Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID)); if (loco == null) return t("unknown locomotive: {}",params.get(ID)); @@ -288,27 +305,39 @@ public class Train implements Constants { } public Tag props() { - Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName())); + Window window = new Window("train-properties",t("Properties of {}",this)); - Form form = new Form(); Fieldset fieldset = new Fieldset(t("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(fieldset); + new Input(NAME,name).addTo(fieldset); new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull).addTo(fieldset); - new Button(t("save")).addTo(fieldset).addTo(form).addTo(window); + new Button(t("save")).addTo(form).addTo(fieldset); - Tag list = new Tag("ul"); - if (!locos.isEmpty()) { - Tag locos = new Tag("li").content(t("Locomotives:")); - Tag l2 = new Tag("ul"); - for (Locomotive loco : this.locos) loco.link("li").addTo(l2); - l2.addTo(locos).addTo(list); + new Button(t("Turn"), "train("+id+",'"+ACTION_TURN+"')").addTo(fieldset).addTo(window); + + Tag propList = new Tag("ul"); + + Tag locoProp = new Tag("li").content(t("Locomotives:")); + Tag locoList = new Tag("ul"); + + for (Locomotive loco : this.locos) loco.link("li").addTo(locoList); + + Tag addLocoForm = new Form().content(t("add locomotive:")+" "); + new Input(REALM, REALM_TRAIN).hideIn(addLocoForm); + new Input(ACTION, ACTION_ADD).hideIn(addLocoForm); + new Input(ID,id).hideIn(addLocoForm); + Select select = new Select(CAR_ID); + for (Locomotive loco : Locomotive.list()) { + if (!this.locos.contains(loco)) select.addOption(loco.id(), loco); } + select.addTo(addLocoForm); + new Button(t("add")).addTo(addLocoForm).addTo(new Tag("li")).addTo(locoList).addTo(locoProp).addTo(propList); if (block != null) { - new Tag("li").content(t("Current location: {}",block)).addTo(list); + new Tag("li").content(t("Current location: {}",block)).addTo(propList); Tag actions = new Tag("li").clazz().content(t("Actions: ")); new Button(t("start"),"train("+id+",'"+ACTION_START+"')").addTo(actions); if (autopilot == null) { @@ -316,16 +345,16 @@ public class Train implements Constants { } else { new Button(t("stop"),"train("+id+",'"+ACTION_STOP+"')").addTo(actions); } - actions.addTo(list); + actions.addTo(propList); } if (route != null) { - new Tag("li").content(t("Current route: {}",route)).addTo(list); + new Tag("li").content(t("Current route: {}",route)).addTo(propList); } - if (direction != null) new Tag("li").content(t("Direction: heading {}",direction)).addTo(list); + if (direction != null) new Tag("li").content(t("Direction: heading {}",direction)).addTo(propList); - list.addTo(window); + propList.addTo(window); return window; } @@ -378,6 +407,7 @@ public class Train implements Constants { } route.unlock(); this.block.train(this); // re-set train on previous block + this.route = null; return error; } @@ -393,16 +423,17 @@ public class Train implements Constants { @Override public String toString() { - return name(); + return name != null ? name : locos.firstElement().name(); } - private void turn() throws IOException { + private Object turn() throws IOException { LOG.debug("train.turn()"); if (direction != null) { direction = direction.inverse(); for (Locomotive loco : locos) loco.turn(); } - if (block != null) block.train(this); + if (block != null) plan.place(block.train(this)); + return t("{} turned.",this); } diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Block.java b/src/main/java/de/srsoftware/web4rail/tiles/Block.java index 4d33f1b..ac0edac 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Block.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Block.java @@ -105,11 +105,11 @@ public abstract class Block extends StretchableTile{ return getClass().getSimpleName()+"("+name+") @ ("+x+","+y+")"; } - public void train(Train newTrain) throws IOException { - if (train == newTrain) return; + public Tile train(Train newTrain) throws IOException { + if (train == newTrain) return this; if (train != null) train.block(null); // vorherigen Zug rauswerfen if (newTrain != null) newTrain.block(this); - super.train(newTrain); + return super.train(newTrain); } @Override diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Tile.java b/src/main/java/de/srsoftware/web4rail/tiles/Tile.java index 6c179b8..97d0425 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Tile.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Tile.java @@ -341,9 +341,9 @@ public abstract class Tile implements Constants{ return train; } - public void train(Train train) throws IOException { + public Tile train(Train train) throws IOException { this.train = train; - plan.place(this); + return plan.place(this); } public void unlock() throws IOException { diff --git a/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java b/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java index 4b5f685..edb9c28 100644 --- a/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java +++ b/src/main/java/de/srsoftware/web4rail/tiles/Turnout.java @@ -153,7 +153,7 @@ public abstract class Turnout extends Tile implements Device{ @Override protected void onFailure(Reply reply) { super.onFailure(reply); - plan.stream(t("Unable to switch {}: {}",this,reply.message())); + plan.stream(t("Unable to switch \"{}\": {}",Turnout.this,reply.message())); } }).reply();