overhauled Car.manager and Locomotive.manager to show max speeds
correctly, implemented saving of reverse speeds
This commit is contained in:
@@ -9,8 +9,8 @@ import java.util.HashMap;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -31,7 +31,6 @@ import de.srsoftware.web4rail.tags.Table;
|
|||||||
|
|
||||||
public class Car extends BaseClass implements Comparable<Car>{
|
public class Car extends BaseClass implements Comparable<Car>{
|
||||||
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
|
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
|
||||||
static HashMap<Id,Car> cars = new HashMap<Id, Car>();
|
|
||||||
|
|
||||||
public static final String NAME = "name";
|
public static final String NAME = "name";
|
||||||
public static boolean FORWARD = true;
|
public static boolean FORWARD = true;
|
||||||
@@ -57,14 +56,13 @@ public class Car extends BaseClass implements Comparable<Car>{
|
|||||||
|
|
||||||
public Car(String name, Id id) {
|
public Car(String name, Id id) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
if (isNull(id)) id = new Id();
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
cars.put(id, this);
|
register();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object action(HashMap<String, String> params,Plan plan) throws IOException {
|
public static Object action(HashMap<String, String> params,Plan plan) throws IOException {
|
||||||
String id = params.get(ID);
|
String id = params.get(ID);
|
||||||
Car car = id == null ? null : Car.get(id);
|
Car car = id == null ? null : Car.get(new Id(id));
|
||||||
|
|
||||||
switch (params.get(ACTION)) {
|
switch (params.get(ACTION)) {
|
||||||
case ACTION_ADD:
|
case ACTION_ADD:
|
||||||
@@ -105,15 +103,12 @@ public class Car extends BaseClass implements Comparable<Car>{
|
|||||||
return (stockId+":"+name).compareTo(o.stockId+":"+o.name);
|
return (stockId+":"+name).compareTo(o.stockId+":"+o.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Car get(Object id) {
|
|
||||||
return isNull(id) ? null : cars.get(new Id(""+id)); // try to get by id
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
JSONObject json = super.json();
|
JSONObject json = super.json();
|
||||||
json.put(NAME, name);
|
json.put(NAME, name);
|
||||||
json.put(LENGTH, length);
|
json.put(LENGTH, length);
|
||||||
if (maxSpeedForward != 0) json.put(MAX_SPEED, maxSpeedForward);
|
if (maxSpeedForward != 0) json.put(MAX_SPEED, maxSpeedForward);
|
||||||
|
if (maxSpeedReverse != 0) json.put(MAX_SPEED_REVERSE, maxSpeedReverse);
|
||||||
json.put(STOCK_ID, stockId);
|
json.put(STOCK_ID, stockId);
|
||||||
if (!tags.isEmpty()) json.put(TAGS, tags);
|
if (!tags.isEmpty()) json.put(TAGS, tags);
|
||||||
return json;
|
return json;
|
||||||
@@ -133,7 +128,6 @@ public class Car extends BaseClass implements Comparable<Car>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void loadAll(String filename, Plan plan) throws IOException {
|
public static void loadAll(String filename, Plan plan) throws IOException {
|
||||||
cars.clear();
|
|
||||||
BufferedReader file = new BufferedReader(new FileReader(filename, UTF8));
|
BufferedReader file = new BufferedReader(new FileReader(filename, UTF8));
|
||||||
String line = file.readLine();
|
String line = file.readLine();
|
||||||
while (line != null) {
|
while (line != null) {
|
||||||
@@ -167,26 +161,30 @@ public class Car extends BaseClass implements Comparable<Car>{
|
|||||||
new Tag("p").content(t("Click on a name to edit the entry.")).addTo(win);
|
new Tag("p").content(t("Click on a name to edit the entry.")).addTo(win);
|
||||||
|
|
||||||
Table table = new Table().addHead(t("Stock ID"),t("Name"),t("Max. Speed",speedUnit),t("Length"),t("Train"),t("Tags"),t("Actions"));
|
Table table = new Table().addHead(t("Stock ID"),t("Name"),t("Max. Speed",speedUnit),t("Length"),t("Train"),t("Tags"),t("Actions"));
|
||||||
cars.values()
|
List<Car> cars = BaseClass.listElements(Car.class)
|
||||||
.stream()
|
.stream()
|
||||||
.filter(car -> !(car instanceof Locomotive))
|
.filter(car -> !(car instanceof Locomotive))
|
||||||
.sorted((c1,c2)->{
|
.sorted((c1,c2)->{
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(c1.stockId)-Integer.parseInt(c2.stockId);
|
return Integer.parseInt(c1.stockId)-Integer.parseInt(c2.stockId);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
return c1.stockId.compareTo(c2.stockId);
|
return c1.stockId.compareTo(c2.stockId);
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
}).collect(Collectors.toList());
|
||||||
.forEach(car -> table.addRow(
|
for (Car car : cars) {
|
||||||
|
String maxSpeed = (car.maxSpeedForward == 0 ? "–":""+car.maxSpeedForward)+NBSP;
|
||||||
|
if (car.maxSpeedReverse != car.maxSpeedForward) maxSpeed += "("+car.maxSpeedReverse+")"+NBSP;
|
||||||
|
|
||||||
|
table.addRow(
|
||||||
car.stockId,
|
car.stockId,
|
||||||
car.link(),
|
car.link(),
|
||||||
car.maxSpeedForward == 0 ? "–":(car.maxSpeedForward+NBSP+speedUnit),
|
maxSpeed+speedUnit,
|
||||||
car.length+NBSP+lengthUnit,
|
car.length+NBSP+lengthUnit,
|
||||||
isSet(car.train) ? car.train.link("span", car.train) : "",
|
isSet(car.train) ? car.train.link("span", car.train) : "",
|
||||||
String.join(", ", car.tags()),
|
String.join(", ", car.tags()),
|
||||||
car.cloneButton()
|
car.cloneButton());
|
||||||
));
|
}
|
||||||
table.addTo(win);
|
table.addTo(win);
|
||||||
|
|
||||||
Form form = new Form("add-car-form");
|
Form form = new Form("add-car-form");
|
||||||
@@ -240,7 +238,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
|||||||
|
|
||||||
public static void saveAll(String filename) throws IOException {
|
public static void saveAll(String filename) throws IOException {
|
||||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||||
for (Entry<Id, Car> entry: cars.entrySet()) file.write(entry.getValue().json()+"\n");
|
for (Car car : BaseClass.listElements(Car.class)) file.write(car.json()+"\n");
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class Locomotive extends Car implements Constants,Device{
|
|||||||
|
|
||||||
public static Object action(HashMap<String, String> params, Plan plan) throws IOException {
|
public static Object action(HashMap<String, String> params, Plan plan) throws IOException {
|
||||||
String id = params.get(ID);
|
String id = params.get(ID);
|
||||||
Locomotive loco = id == null ? null : Locomotive.get(id);
|
Locomotive loco = id == null ? null : BaseClass.get(new Id(id));
|
||||||
switch (params.get(ACTION)) {
|
switch (params.get(ACTION)) {
|
||||||
case ACTION_ADD:
|
case ACTION_ADD:
|
||||||
new Locomotive(params.get(Locomotive.NAME)).parent(plan).register();
|
new Locomotive(params.get(Locomotive.NAME)).parent(plan).register();
|
||||||
@@ -163,12 +163,6 @@ public class Locomotive extends Car implements Constants,Device{
|
|||||||
return properties();
|
return properties();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Locomotive get(Object id) {
|
|
||||||
Car car = Car.get(id);
|
|
||||||
if (car instanceof Locomotive) return (Locomotive) car;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
if (init) return;
|
if (init) return;
|
||||||
String proto = null;
|
String proto = null;
|
||||||
|
|||||||
@@ -178,8 +178,9 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
|
|
||||||
private Object addCar(HashMap<String, String> params) {
|
private Object addCar(HashMap<String, String> params) {
|
||||||
LOG.debug("addCar({})",params);
|
LOG.debug("addCar({})",params);
|
||||||
if (!params.containsKey(CAR_ID)) return t("No car id passed to Train.addCar!");
|
String carId = params.get(CAR_ID);
|
||||||
Car car = Car.get(params.get(CAR_ID));
|
if (isNull(carId)) return t("No car id passed to Train.addCar!");
|
||||||
|
Car car = BaseClass.get(new Id(carId));
|
||||||
if (isNull(car)) return t("No car with id \"{}\" known!",params.get(CAR_ID));
|
if (isNull(car)) return t("No car with id \"{}\" known!",params.get(CAR_ID));
|
||||||
add(car);
|
add(car);
|
||||||
return properties();
|
return properties();
|
||||||
@@ -266,7 +267,9 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Object create(HashMap<String, String> params, Plan plan) {
|
private static Object create(HashMap<String, String> params, Plan plan) {
|
||||||
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
|
String locoId = params.get(Train.LOCO_ID);
|
||||||
|
if (isNull(locoId)) return t("Need loco id to create new train!");
|
||||||
|
Locomotive loco = BaseClass.get(new Id(locoId));
|
||||||
if (isNull(loco)) return t("unknown locomotive: {}",params.get(ID));
|
if (isNull(loco)) return t("unknown locomotive: {}",params.get(ID));
|
||||||
Train train = new Train(loco);
|
Train train = new Train(loco);
|
||||||
train.parent(plan);
|
train.parent(plan);
|
||||||
@@ -307,8 +310,9 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object dropCar(HashMap<String, String> params) {
|
private Object dropCar(HashMap<String, String> params) {
|
||||||
Car car = Car.get(params.get(CAR_ID));
|
String carId = params.get(CAR_ID);
|
||||||
|
if (isNull(carId)) return t("Cannot drop car without car id!");
|
||||||
|
Car car = BaseClass.get(new Id(carId));
|
||||||
if (isSet(car)) {
|
if (isSet(car)) {
|
||||||
cars.remove(car);
|
cars.remove(car);
|
||||||
car.train(null);
|
car.train(null);
|
||||||
@@ -402,9 +406,9 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
if (json.has(TRACE)) json.getJSONArray(TRACE).forEach(elem -> { trace.add(plan.get(new Id(elem.toString()), false).set(this)); });
|
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(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
|
if (json.has(LOCOS)) { // for downward compatibility
|
||||||
for (Object id : json.getJSONArray(LOCOS)) add((Locomotive) Car.get(id));
|
for (Object id : json.getJSONArray(LOCOS)) add(BaseClass.get(new Id(""+id)));
|
||||||
}
|
}
|
||||||
for (Object id : json.getJSONArray(CARS)) add(Car.get(id));
|
for (Object id : json.getJSONArray(CARS)) add(BaseClass.get(new Id(""+id)));
|
||||||
super.load(json);
|
super.load(json);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user