implemented methods to add cars to train.

This commit is contained in:
Stephan Richter
2020-11-01 20:22:31 +01:00
parent 6ed1255076
commit b89f89f298
7 changed files with 74 additions and 12 deletions

View File

@@ -112,7 +112,7 @@ public class Application implements Constants{
case REALM_ACTIONS:
return ActionList.process(params,plan);
case REALM_CAR:
return Car.action(params);
return Car.action(params,plan);
case REALM_CONTACT:
return Contact.process(params);
case REALM_CONDITION:

View File

@@ -794,6 +794,7 @@ public class Plan implements Constants{
Tag tiles = new Tag("div").clazz("list").content("");
new Div(ACTION_PROPS).clazz(REALM_TRAIN).content(t("Manage trains")).addTo(tiles);
new Div(ACTION_PROPS).clazz(REALM_LOCO).content(t("Manage locos")).addTo(tiles);
new Div(ACTION_PROPS).clazz(REALM_CAR).content(t("Manage cars")).addTo(tiles);
return tiles.addTo(tileMenu);
}

View File

@@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.TreeSet;
import java.util.Vector;
import org.json.JSONObject;
import org.slf4j.Logger;
@@ -60,19 +61,44 @@ public class Car implements Constants {
cars.put(id, this);
}
public static Object action(HashMap<String, String> params) throws IOException {
Car car = Car.get(params.get(Car.ID));
if (car == null) return t("No car with id {} found!",params.get(Car.ID));
public static Object action(HashMap<String, String> params,Plan plan) throws IOException {
String id = params.get(ID);
Car car = id == null ? null : Car.get(id);
switch (params.get(ACTION)) {
case ACTION_ADD:
return new Car(params.get(Car.NAME)).plan(plan);
case ACTION_PROPS:
return car.properties();
return car == null ? Car.manager() : car.properties();
case ACTION_UPDATE:
return car.update(params);
}
if (car instanceof Locomotive) return Locomotive.action(params);
if (car instanceof Locomotive) return Locomotive.action(params,plan);
return t("Unknown action: {}",params.get(ACTION));
}
public static Object manager() {
Window win = new Window("car-manager", t("Car manager"));
new Tag("h4").content(t("known cars")).addTo(win);
Tag list = new Tag("ul");
for (Car car : cars.values()) {
if (!(car instanceof Locomotive)) {
car.link("li").addTo(list);
}
}
list.addTo(win);
Form form = new Form();
new Input(ACTION, ACTION_ADD).hideIn(form);
new Input(REALM,REALM_CAR).hideIn(form);
Fieldset fieldset = new Fieldset(t("add new car"));
new Input(Locomotive.NAME, t("new car")).addTo(new Label(t("Name:")+" ")).addTo(fieldset);
new Button(t("Apply")).addTo(fieldset);
fieldset.addTo(form).addTo(win);
return win;
}
protected Tag cockpit() {
return null;
}
@@ -106,6 +132,14 @@ public class Car implements Constants {
return new Tag(tagClass).clazz("link").attr("onclick","car("+id+",'"+ACTION_PROPS+"')").content(name());
}
static Vector<Car> list() {
Vector<Car> cars = new Vector<Car>();
for (Car car : Car.cars.values()) {
if (!(car instanceof Locomotive)) cars.add(car);
}
return cars;
}
public static void loadAll(String filename, Plan plan) throws IOException {
cars.clear();
BufferedReader file = new BufferedReader(new FileReader(filename));
@@ -194,7 +228,7 @@ public class Car implements Constants {
@Override
public String toString() {
return getClass().getSimpleName()+"("+name()+")";
return name;
}
public void train(Train train) {

View File

@@ -177,8 +177,8 @@ public class Locomotive extends Car implements Constants,Device{
return json;
}
static Vector<Locomotive> list() {
Vector<Locomotive> locos = new Vector<Locomotive>();
static Vector<Car> list() {
Vector<Car> locos = new Vector<Car>();
for (Car car : Car.cars.values()) {
if (car instanceof Locomotive) locos.add((Locomotive) car);
}

View File

@@ -182,6 +182,28 @@ public class Train implements Comparable<Train>,Constants {
this.block = block;
}
private Tag carList() {
Tag locoProp = new Tag("li").content(t("Cars:"));
Tag locoList = new Tag("ul").clazz("carlist");
for (Car car : this.cars) car.link("li").addTo(locoList);
Tag addCarForm = new Form().content(t("add car:")+"&nbsp;");
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 : Car.list()) {
if (!this.cars.contains(car)) select.addOption(car.id(), car);
}
if (!select.children().isEmpty()) {
select.addTo(addCarForm);
new Button(t("add")).addTo(addCarForm);
addCarForm.addTo(new Tag("li")).addTo(locoList);
}
return locoList.addTo(locoProp);
}
private static Object create(HashMap<String, String> params, Plan plan) {
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
if (loco == null) return t("unknown locomotive: {}",params.get(ID));
@@ -273,7 +295,7 @@ public class Train implements Comparable<Train>,Constants {
new Input(ACTION, ACTION_ADD).hideIn(addLocoForm);
new Input(ID,id).hideIn(addLocoForm);
Select select = new Select(CAR_ID);
for (Locomotive loco : Locomotive.list()) {
for (Car loco : Locomotive.list()) {
if (!this.locos.contains(loco)) select.addOption(loco.id(), loco);
}
if (!select.children().isEmpty()) {
@@ -300,7 +322,7 @@ public class Train implements Comparable<Train>,Constants {
new Input(Train.NAME, t("new train")).addTo(new Label(t("Name:")+" ")).addTo(fieldset);
Select select = new Select(LOCO_ID);
for (Locomotive loco : Locomotive.list()) select.addOption(loco.id(),loco.name());
for (Car loco : Locomotive.list()) select.addOption(loco.id(),loco.name());
select.addTo(new Label(t("Locomotive:")+" ")).addTo(fieldset);
new Button(t("Apply")).addTo(fieldset);
@@ -354,6 +376,8 @@ public class Train implements Comparable<Train>,Constants {
Tag propList = new Tag("ul").clazz("proplist");
locoList().addTo(propList);
carList().addTo(propList);
new Tag("li").content(t("length: {}",length())).addTo(propList);
if (block != null) {
new Tag("li").content(t("Current location: {}",block)).addTo(propList);