diff --git a/src/main/java/de/srsoftware/web4rail/moving/Car.java b/src/main/java/de/srsoftware/web4rail/moving/Car.java index de9bcbb..addbb4d 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Car.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Car.java @@ -72,6 +72,8 @@ public class Car extends BaseClass implements Comparable{ car.clone(); } else new Car(params.get(Car.NAME)).parent(plan); return Car.manager(); + case ACTION_MOVE: + return car.moveUp(); case ACTION_PROPS: return car == null ? Car.manager() : car.properties(); case ACTION_UPDATE: @@ -199,6 +201,11 @@ public class Car extends BaseClass implements Comparable{ return maxSpeedForward; } + protected Window moveUp() { + if (!isSet(train())) return properties(); + return train().moveUp(this); + } + String name(){ return name; } diff --git a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java index b105ae3..a75232f 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java @@ -51,6 +51,8 @@ public class Locomotive extends Car implements Constants,Device{ return Locomotive.manager(); case ACTION_FASTER10: return loco.faster(10); + case ACTION_MOVE: + return loco.moveUp(); case ACTION_PROPS: return loco == null ? Locomotive.manager() : loco.properties(); case ACTION_SLOWER10: @@ -74,7 +76,7 @@ public class Locomotive extends Car implements Constants,Device{ return t("Unknown action: {}",params.get(ACTION)); } - + @Override public int address() { return address; diff --git a/src/main/java/de/srsoftware/web4rail/moving/Train.java b/src/main/java/de/srsoftware/web4rail/moving/Train.java index 0336335..871b0c9 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Train.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Train.java @@ -65,10 +65,8 @@ public class Train extends BaseClass implements Comparable { public boolean pushPull = false; private static final String CARS = "cars"; + private static final String LOCOS = "locomotives"; private Vector cars = new Vector(); - - private static final String LOCOS = "locomotives"; - private Vector locos = new Vector(); private static final String TAGS = "tags"; @@ -188,9 +186,7 @@ public class Train extends BaseClass implements Comparable { public void add(Car car) { if (isNull(car)) return; - if (car instanceof Locomotive) { - locos.add((Locomotive) car); - } else cars.add(car); + cars.add(car); car.train(this); } @@ -209,41 +205,54 @@ public class Train extends BaseClass implements Comparable { public String brakeId(boolean reversed) { TreeSet carIds = new TreeSet(); - locos.stream().map(loco -> loco.id()+":"+(reversed != loco.orientation?"r":"f")).forEach(carIds::add); - cars.stream().map(car -> ""+car.id()).forEach(carIds::add); + cars.stream().map(car -> car.id()+":"+(car.orientation == reversed ? "r":"f")).forEach(carIds::add); String brakeId = md5sum(carIds); LOG.debug("generated new brake id for {}: {}",this,brakeId); return brakeId; } private Tag carList() { - Tag locoProp = new Tag("li").content(t("Cars:")); - Tag locoList = new Tag("ul").clazz("carlist").content(""); + Tag locoProp = new Tag("li").content(t("Locomotives and cars:")); + Tag carList = new Tag("ul").clazz("carlist"); - for (Car car : this.cars) { + for (Car loco : this.cars) { Tag li = new Tag("li"); - car.link(car.name()+(car.stockId.isEmpty() ? "" : " ("+car.stockId+")")).addTo(li).content(NBSP); - button(t("delete"),Map.of(ACTION,ACTION_DROP,CAR_ID,car.id().toString())).addTo(li); - li.addTo(locoList); - } - - Form addCarForm = new Form("append-car-form"); - addCarForm.content(t("add car:")+" "); - new Input(REALM, REALM_TRAIN).hideIn(addCarForm); - new Input(ACTION, ACTION_ADD).hideIn(addCarForm); - new Input(ID,id).hideIn(addCarForm); - Select select = new Select(CAR_ID); - for (Car car : BaseClass.listElements(Car.class)) { - if (car instanceof Locomotive) continue; - if (isSet(car.train())) continue; - select.addOption(car.id(), car+(car.stockId.isEmpty()?"":" ("+car.stockId+")")); - } - if (!select.children().isEmpty()) { - select.addTo(addCarForm); - new Button(t("add"),addCarForm).addTo(addCarForm); - addCarForm.addTo(new Tag("li")).addTo(locoList); + loco.link(loco.name()+(loco.stockId.isEmpty() ? "" : " ("+loco.stockId+")")).addTo(li).content(NBSP); + loco.button(t("turn within train"),Map.of(ACTION,ACTION_TURN)).addTo(li); + loco.button("↑",Map.of(ACTION,ACTION_MOVE)).addTo(li); + button(t("delete"),Map.of(ACTION,ACTION_DROP,LOCO_ID,loco.id().toString())).addTo(li); + li.addTo(carList); + } + + List locos = BaseClass.listElements(Locomotive.class).stream().filter(loco -> isNull(loco.train())).collect(Collectors.toList()); + if (!locos.isEmpty()) { + Form addLocoForm = new Form("append-loco-form"); + addLocoForm.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 (Car loco : locos) select.addOption(loco.id(), loco); + select.addTo(addLocoForm); + new Button(t("add"),addLocoForm).addTo(addLocoForm); + addLocoForm.addTo(new Tag("li")).addTo(carList); } - return locoList.addTo(locoProp); + + List cars = BaseClass.listElements(Car.class).stream().filter(car -> !(car instanceof Locomotive)).filter(loco -> isNull(loco.train())).collect(Collectors.toList()); + if (!cars.isEmpty()) { + Form addCarForm = new Form("append-car-form"); + addCarForm.content(t("add car:")+" "); + new Input(REALM, REALM_TRAIN).hideIn(addCarForm); + new Input(ACTION, ACTION_ADD).hideIn(addCarForm); + new Input(ID,id).hideIn(addCarForm); + Select select = new Select(CAR_ID); + for (Car car : cars) select.addOption(car.id(), car+(car.stockId.isEmpty()?"":" ("+car.stockId+")")); + select.addTo(addCarForm); + new Button(t("add"),addCarForm).addTo(addCarForm); + addCarForm.addTo(new Tag("li")).addTo(carList); + } + return carList.addTo(locoProp); + } public List cars(){ @@ -298,15 +307,11 @@ public class Train extends BaseClass implements Comparable { private Object dropCar(HashMap params) { Car car = Car.get(params.get(CAR_ID)); + if (isSet(car)) { cars.remove(car); car.train(null); } - Locomotive loco = Locomotive.get(params.get(LOCO_ID)); - if (isSet(loco)) { - locos.remove(loco); - loco.train(null); - } return properties(); } @@ -342,7 +347,6 @@ public class Train extends BaseClass implements Comparable { if (isSet(route)) json.put(ROUTE, route.id()); if (isSet(direction)) json.put(DIRECTION, direction); - json.put(LOCOS, locos.stream().map(l -> l.id().toString()).collect(Collectors.toList())); json.put(CARS,cars.stream().map(c -> c.id().toString()).collect(Collectors.toList())); json.put(TRACE, trace.stream().map(t -> t.id().toString()).collect(Collectors.toList())); @@ -352,7 +356,6 @@ public class Train extends BaseClass implements Comparable { public int length() { int result = 0; - for (Locomotive loco : locos) result += loco.length; for (Car car : cars) result += car.length; return result; } @@ -397,46 +400,14 @@ public class Train extends BaseClass implements Comparable { 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(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! + if (json.has(LOCOS)) { // for downward compatibility + for (Object id : json.getJSONArray(LOCOS)) add((Locomotive) Car.get(id)); + } 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; } - private Tag locoList() { - Tag locoProp = new Tag("li").content(t("Locomotives:")); - Tag locoList = new Tag("ul").clazz("locolist"); - - for (Locomotive loco : this.locos) { - Tag li = new Tag("li"); - loco.link(loco.name()+(loco.stockId.isEmpty() ? "" : " ("+loco.stockId+")")).addTo(li).content(NBSP); - loco.button(t("turn within train"),Map.of(ACTION,ACTION_TURN)).addTo(li); - button(t("delete"),Map.of(ACTION,ACTION_DROP,LOCO_ID,loco.id().toString())).addTo(li); - li.addTo(locoList); - } - - Form addLocoForm = new Form("append-loco-form"); - addLocoForm.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 (Car loco : BaseClass.listElements(Locomotive.class)) { - if (isNull(loco.train())) select.addOption(loco.id(), loco); - } - if (!select.children().isEmpty()) { - select.addTo(addLocoForm); - new Button(t("add"),addLocoForm).addTo(addLocoForm); - addLocoForm.addTo(new Tag("li")).addTo(locoList); - } - return locoList.addTo(locoProp); - } - - public List locos(){ - return new Vector(locos); - } - - public static Object manager() { Window win = new Window("train-manager", t("Train manager")); new Tag("h4").content(t("known trains")).addTo(win); @@ -481,11 +452,6 @@ public class Train extends BaseClass implements Comparable { private int maxSpeed() { int maxSpeed = Integer.MAX_VALUE; - for (Locomotive loco : locos) { - int max = loco.maxSpeed(); - if (max == 0) continue; - maxSpeed = Math.min(max, maxSpeed); - } for (Car car : cars) { int max = car.maxSpeed(); if (max == 0) continue; @@ -493,9 +459,21 @@ public class Train extends BaseClass implements Comparable { } return maxSpeed; } + + public Window moveUp(Car car) { + for (int i = 1; i isSet(car.name())).findFirst().get().name()); } private Train name(String newName) { @@ -513,7 +491,6 @@ public class Train extends BaseClass implements Comparable { Tag propList = new Tag("ul").clazz("proplist"); - locoList().addTo(propList); carList().addTo(propList); if (isSet(currentBlock)) currentBlock.button(currentBlock.toString()).addTo(new Tag("li").content(t("Current location:")+NBSP)).addTo(propList); @@ -579,7 +556,6 @@ public class Train extends BaseClass implements Comparable { if (child == currentBlock) currentBlock = null; if (child == destination) destination = null; cars.remove(child); - locos.remove(child); trace.remove(child); super.removeChild(child); } @@ -657,7 +633,7 @@ public class Train extends BaseClass implements Comparable { public void setSpeed(int newSpeed) { speed = Math.min(newSpeed,maxSpeed()); if (speed < 0) speed = 0; - for (Locomotive loco : locos) loco.setSpeed(speed); + cars.stream().filter(c -> c instanceof Locomotive).forEach(car -> ((Locomotive)car).setSpeed(speed)); plan.stream(t("Set {} to {} {}",this,speed,speedUnit)); } @@ -781,19 +757,18 @@ public class Train extends BaseClass implements Comparable { public SortedSet tags() { TreeSet list = new TreeSet(tags); - for (Locomotive loco : locos) list.addAll(loco.tags()); + //for (Locomotive loco : locos) list.addAll(loco.tags()); for (Car car:cars) list.addAll(car.tags()); return list; } @Override public String toString() { - return isSet(name) ? name : locos.firstElement().name(); + return name(); } public Tag turn() { LOG.debug("train.turn()"); - for (Locomotive loco : locos) loco.turn(); if (isSet(direction)) { direction = direction.inverse(); reverseTrace();