implemented removing cars from train
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.9.12</version>
|
||||
<version>0.9.13</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map.Entry;
|
||||
@@ -30,7 +29,7 @@ import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class Car implements Constants {
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
|
||||
static HashMap<String,Car> cars = new HashMap<String, Car>();
|
||||
static HashMap<Integer,Car> cars = new HashMap<Integer, Car>();
|
||||
|
||||
public static final String NAME = "name";
|
||||
private static final String LENGTH = "length";
|
||||
@@ -38,7 +37,7 @@ public class Car implements Constants {
|
||||
private static final String TAGS = "tags";
|
||||
protected HashSet<String> tags = new HashSet<String>();
|
||||
|
||||
private String id;
|
||||
private int id;
|
||||
private String name;
|
||||
public int length;
|
||||
private String stockId = "";
|
||||
@@ -49,13 +48,13 @@ public class Car implements Constants {
|
||||
this(name,null);
|
||||
}
|
||||
|
||||
public Car(String name, String id) {
|
||||
public Car(String name, Integer id) {
|
||||
this.name = name;
|
||||
if (id == null) {
|
||||
try { // make sure multiple consecutive creations get different ids
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {}
|
||||
id = ""+new Date().getTime();
|
||||
id = Application.createId();
|
||||
}
|
||||
this.id = id;
|
||||
cars.put(id, this);
|
||||
@@ -103,18 +102,12 @@ public class Car implements Constants {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Car get(String nameOrId) {
|
||||
Car car = cars.get(nameOrId); // try to get by id
|
||||
if (car == null) { // try to get by name
|
||||
for (Car c : cars.values()) {
|
||||
if (c.name.equals(nameOrId)) car = c;
|
||||
}
|
||||
}
|
||||
return car;
|
||||
public static Car get(Object id) {
|
||||
return cars.get(Integer.parseInt(""+id)); // try to get by id
|
||||
}
|
||||
|
||||
|
||||
public String id() {
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -147,7 +140,7 @@ public class Car implements Constants {
|
||||
while (line != null) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
String name = json.getString(Car.NAME);
|
||||
String id = json.getString(ID);
|
||||
int id = json.getInt(ID);
|
||||
Car car = json.has(Locomotive.LOCOMOTIVE) ? new Locomotive(name, id) : new Car(name,id);
|
||||
car.load(json).plan(plan);
|
||||
|
||||
@@ -157,7 +150,7 @@ public class Car implements Constants {
|
||||
}
|
||||
|
||||
protected Car load(JSONObject json) {
|
||||
if (json.has(ID)) id = json.getString(ID);
|
||||
if (json.has(ID)) id = json.getInt(ID);
|
||||
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
||||
if (json.has(STOCK_ID)) stockId = json.getString(STOCK_ID);
|
||||
if (json.has(TAGS)) json.getJSONArray(TAGS).forEach(elem -> { tags.add(elem.toString()); });
|
||||
@@ -211,10 +204,7 @@ public class Car implements Constants {
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Entry<String, Car> entry: cars.entrySet()) {
|
||||
Car c = entry.getValue();
|
||||
file.write(c.json()+"\n");
|
||||
}
|
||||
for (Entry<Integer, Car> entry: cars.entrySet()) file.write(entry.getValue().json()+"\n");
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
super(name);
|
||||
}
|
||||
|
||||
public Locomotive(String name, String id) {
|
||||
public Locomotive(String name, Integer id) {
|
||||
super(name,id);
|
||||
}
|
||||
|
||||
@@ -120,8 +120,8 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
return setSpeed(speed + steps);
|
||||
}
|
||||
|
||||
public static Locomotive get(String nameOrId) {
|
||||
Car car = Car.get(nameOrId);
|
||||
public static Locomotive get(Object id) {
|
||||
Car car = Car.get(id);
|
||||
if (car instanceof Locomotive) return (Locomotive) car;
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -131,6 +131,8 @@ public class Train implements Comparable<Train>,Constants {
|
||||
return train.addCar(params);
|
||||
case ACTION_AUTO:
|
||||
return train.automatic();
|
||||
case ACTION_DROP:
|
||||
return train.dropCar(params);
|
||||
case ACTION_PROPS:
|
||||
return train.props();
|
||||
case ACTION_QUIT:
|
||||
@@ -147,6 +149,14 @@ public class Train implements Comparable<Train>,Constants {
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private Object dropCar(HashMap<String, String> params) {
|
||||
String carId = params.get(CAR_ID);
|
||||
if (carId != null) cars.remove(Car.get(carId));
|
||||
String locoId = params.get(LOCO_ID);
|
||||
if (locoId != null) locos.remove(Car.get(locoId));
|
||||
return props();
|
||||
}
|
||||
|
||||
private Object addCar(HashMap<String, String> params) {
|
||||
LOG.debug("addCar({})",params);
|
||||
if (!params.containsKey(CAR_ID)) return t("No car id passed to Train.addCar!");
|
||||
@@ -186,7 +196,13 @@ public class Train implements Comparable<Train>,Constants {
|
||||
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);
|
||||
for (Car car : this.cars) {
|
||||
Tag li = new Tag("li");
|
||||
car.link("span").addTo(li).content(NBSP);
|
||||
Map<String, Object> params = Map.of(REALM,REALM_TRAIN,ID,id,ACTION,ACTION_DROP,CAR_ID,car.id());
|
||||
new Button("delete",params).addTo(li);
|
||||
li.addTo(locoList);
|
||||
}
|
||||
|
||||
Tag addCarForm = new Form().content(t("add car:")+" ");
|
||||
new Input(REALM, REALM_TRAIN).hideIn(addCarForm);
|
||||
@@ -230,10 +246,10 @@ public class Train implements Comparable<Train>,Constants {
|
||||
if (direction != null) json.put(DIRECTION, direction);
|
||||
json.put(PUSH_PULL, pushPull);
|
||||
if (name != null)json.put(NAME, name);
|
||||
Vector<String> locoIds = new Vector<String>();
|
||||
Vector<Integer> locoIds = new Vector<Integer>();
|
||||
for (Locomotive loco : locos) locoIds.add(loco.id());
|
||||
json.put(LOCOS, locoIds);
|
||||
Vector<String> carIds = new Vector<String>();
|
||||
Vector<Integer> carIds = new Vector<Integer>();
|
||||
for (Car car : cars) carIds.add(car.id());
|
||||
json.put(CARS,carIds);
|
||||
if (!tags.isEmpty()) json.put(TAGS, tags);
|
||||
@@ -277,6 +293,7 @@ public class Train implements Comparable<Train>,Constants {
|
||||
for (Object id : json.getJSONArray(CARS)) add(Car.get((String)id));
|
||||
for (Object id : json.getJSONArray(LOCOS)) add((Locomotive) Car.get((String)id));
|
||||
if (json.has(TAGS)) json.getJSONArray(TAGS).forEach(elem -> { tags.add(elem.toString()); });
|
||||
if (json.has(DIRECTION)) direction = Direction.valueOf(json.getString(DIRECTION));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -285,9 +302,12 @@ public class Train implements Comparable<Train>,Constants {
|
||||
Tag locoList = new Tag("ul").clazz("locolist");
|
||||
|
||||
for (Locomotive loco : this.locos) {
|
||||
Tag li = loco.link("li");
|
||||
Tag li = new Tag("li");
|
||||
loco.link("span").addTo(li);
|
||||
Map<String, Object> props = Map.of(REALM,REALM_LOCO,ID,loco.id(),ACTION,ACTION_TURN);
|
||||
new Button(t("turn within train"),props).addTo(li).addTo(locoList);
|
||||
Map<String, Object> params = Map.of(REALM,REALM_TRAIN,ID,id,ACTION,ACTION_DROP,LOCO_ID,loco.id());
|
||||
new Button("delete",params).addTo(li);
|
||||
}
|
||||
|
||||
Tag addLocoForm = new Form().content(t("add locomotive:")+" ");
|
||||
|
||||
Reference in New Issue
Block a user