started major refactoring
This commit is contained in:
@@ -32,7 +32,7 @@ import de.srsoftware.web4rail.tags.TextArea;
|
||||
|
||||
public class Car extends BaseClass implements Comparable<Car>{
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
|
||||
static HashMap<Integer,Car> cars = new HashMap<Integer, Car>();
|
||||
static HashMap<Id,Car> cars = new HashMap<Id, Car>();
|
||||
|
||||
public static final String NAME = "name";
|
||||
private static final String LENGTH = "length";
|
||||
@@ -41,7 +41,6 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
private static final String MAX_SPEED = "max_speed";
|
||||
protected HashSet<String> tags = new HashSet<String>();
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
public int length;
|
||||
protected String stockId = "";
|
||||
@@ -54,14 +53,9 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
this(name,null);
|
||||
}
|
||||
|
||||
public Car(String name, Integer id) {
|
||||
public Car(String name, Id id) {
|
||||
this.name = name;
|
||||
if (id == null) {
|
||||
try { // make sure multiple consecutive creations get different ids
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {}
|
||||
id = Application.createId();
|
||||
}
|
||||
if (isNull(id)) id = new Id();
|
||||
this.id = id;
|
||||
cars.put(id, this);
|
||||
}
|
||||
@@ -100,12 +94,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
}
|
||||
|
||||
public static Car get(Object id) {
|
||||
return isNull(id) ? null : cars.get(Integer.parseInt(""+id)); // try to get by id
|
||||
}
|
||||
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
return isNull(id) ? null : cars.get(new Id(""+id)); // try to get by id
|
||||
}
|
||||
|
||||
public JSONObject json() {
|
||||
@@ -130,7 +119,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
public Tag link(String...args) {
|
||||
String tx = args.length<1 ? name()+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
return link(type, Map.of(REALM,REALM_CAR,ID,id,ACTION,ACTION_PROPS), tx);
|
||||
return link(type, tx);
|
||||
}
|
||||
|
||||
static Vector<Car> list() {
|
||||
@@ -148,7 +137,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
while (line != null) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
String name = json.getString(Car.NAME);
|
||||
int id = json.getInt(ID);
|
||||
Id id = Id.from(json);
|
||||
Car car = json.has(Locomotive.LOCOMOTIVE) ? new Locomotive(name, id) : new Car(name,id);
|
||||
car.load(json).plan(plan);
|
||||
|
||||
@@ -158,7 +147,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
}
|
||||
|
||||
protected Car load(JSONObject json) {
|
||||
if (json.has(ID)) id = json.getInt(ID);
|
||||
if (json.has(ID)) id = Id.from(json);
|
||||
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
||||
if (json.has(MAX_SPEED)) maxSpeed = json.getInt(MAX_SPEED);
|
||||
if (json.has(NOTES)) notes = json.getString(NOTES);
|
||||
@@ -249,7 +238,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Entry<Integer, Car> entry: cars.entrySet()) file.write(entry.getValue().json()+"\n");
|
||||
for (Entry<Id, Car> entry: cars.entrySet()) file.write(entry.getValue().json()+"\n");
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
super(name);
|
||||
}
|
||||
|
||||
public Locomotive(String name, Integer id) {
|
||||
public Locomotive(String name, Id id) {
|
||||
super(name,id);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
}
|
||||
|
||||
public static Tag cockpit(Object locoOrTrain) {
|
||||
int id = 0;
|
||||
Id id = null;
|
||||
int speed = 0;
|
||||
String realm = null;
|
||||
Train train = null;
|
||||
@@ -95,7 +95,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
} else if (locoOrTrain instanceof Train) {
|
||||
train = (Train)locoOrTrain;
|
||||
realm = REALM_TRAIN;
|
||||
id = train.id;
|
||||
id = train.id();
|
||||
speed = train.speed;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,7 @@ import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.PathFinder;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
@@ -30,7 +28,6 @@ import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Range;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -49,9 +46,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
private static final String CAR_ID = "carId";
|
||||
public static final String LOCO_ID = "locoId";
|
||||
private static final String TRACE = "trace";
|
||||
private static final HashMap<Integer, Train> trains = new HashMap<>();
|
||||
private static final HashMap<Id, Train> trains = new HashMap<>();
|
||||
public static final String ID = "id";
|
||||
public int id;
|
||||
|
||||
private static final String NAME = "name";
|
||||
private String name = null;
|
||||
@@ -117,8 +113,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
this(loco,null);
|
||||
}
|
||||
|
||||
public Train(Locomotive loco, Integer id) {
|
||||
if (isNull(id)) id = Application.createId();
|
||||
public Train(Locomotive loco, Id id) {
|
||||
if (isNull(id)) id = new Id();
|
||||
this.id = id;
|
||||
add(loco);
|
||||
trains.put(id, this);
|
||||
@@ -136,7 +132,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
return t("No train id passed!");
|
||||
}
|
||||
int id = Integer.parseInt(params.get(Train.ID));
|
||||
Id id = Id.from(params);
|
||||
Train train = trains.get(id);
|
||||
if (isNull(train)) return(t("No train with id {}!",id));
|
||||
switch (action) {
|
||||
@@ -320,7 +316,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return properties();
|
||||
}
|
||||
|
||||
public static Train get(int id) {
|
||||
public static Train get(Id id) {
|
||||
return trains.get(id);
|
||||
}
|
||||
|
||||
@@ -344,15 +340,15 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (isSet(route)) json.put(ROUTE, route.id());
|
||||
if (isSet(direction)) json.put(DIRECTION, direction);
|
||||
|
||||
Vector<Integer> locoIds = new Vector<Integer>();
|
||||
Vector<Id> locoIds = new Vector<Id>();
|
||||
for (Locomotive loco : locos) locoIds.add(loco.id());
|
||||
json.put(LOCOS, locoIds);
|
||||
|
||||
Vector<Integer> carIds = new Vector<Integer>();
|
||||
Vector<Id> carIds = new Vector<Id>();
|
||||
for (Car car : cars) carIds.add(car.id());
|
||||
json.put(CARS,carIds);
|
||||
|
||||
Vector<String> tileIds = new Vector<String>();
|
||||
Vector<Id> tileIds = new Vector<Id>();
|
||||
for (Tile tile : trace) tileIds.add(tile.id());
|
||||
json.put(TRACE, tileIds);
|
||||
|
||||
@@ -377,7 +373,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
public Tag link(String...args) {
|
||||
String tx = args.length<1 ? name()+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
return link(type, Map.of(REALM,REALM_TRAIN,ID,id,ACTION,ACTION_PROPS), tx);
|
||||
return link(type, tx);
|
||||
}
|
||||
|
||||
public static TreeSet<Train> list() {
|
||||
@@ -390,9 +386,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
while (isSet(line)) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
|
||||
int id = json.getInt(ID);
|
||||
|
||||
Train train = new Train(null,id);
|
||||
Train train = new Train(null,Id.from(json));
|
||||
train.plan(plan).load(json);
|
||||
|
||||
line = file.readLine();
|
||||
@@ -405,8 +399,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (json.has(DIRECTION)) direction = Direction.valueOf(json.getString(DIRECTION));
|
||||
if (json.has(NAME)) name = json.getString(NAME);
|
||||
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(elem.toString(), false).set(this)); });
|
||||
if (json.has(BLOCK)) currentBlock = (Block) plan.get(json.getString(BLOCK), false).set(this); // do not move this up! during set, other fields will be referenced!
|
||||
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!
|
||||
for (Object id : json.getJSONArray(CARS)) add(Car.get(id));
|
||||
for (Object id : json.getJSONArray(LOCOS)) add((Locomotive) Car.get(id));
|
||||
return this;
|
||||
@@ -516,7 +510,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Tag properties() {
|
||||
@Override
|
||||
public Window properties() {
|
||||
Window window = new Window("train-properties",t("Properties of {}",this));
|
||||
|
||||
Locomotive.cockpit(this).addTo(window);
|
||||
@@ -547,14 +542,12 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (isNull(destination)) {
|
||||
new Button(t("Select from plan"),"return selectDest("+id+");").addTo(dest);
|
||||
} else {
|
||||
link("span",Map.of(REALM,REALM_PLAN,ID,destination.id(),ACTION,ACTION_CLICK),destination.toString()).addTo(dest);
|
||||
link("span",destination,Map.of(REALM,REALM_PLAN,ID,destination.id().toString(),ACTION,ACTION_CLICK)).addTo(dest);
|
||||
new Button(t("Drop"),Map.of(REALM,REALM_TRAIN,ID,id,ACTION,ACTION_MOVE,DESTINATION,"")).addTo(dest);
|
||||
}
|
||||
|
||||
dest.addTo(propList);
|
||||
if (isSet(route)) {
|
||||
link("li", Map.of(REALM,REALM_ROUTE,ID,route.id(),ACTION,ACTION_PROPS), route).addTo(propList);
|
||||
}
|
||||
if (isSet(route)) link("li", route).addTo(propList);
|
||||
int ms = maxSpeed();
|
||||
if (ms < Integer.MAX_VALUE) new Tag("li").content(t("Max. Speed")+": "+maxSpeed()+NBSP+speedUnit).addTo(propList);
|
||||
|
||||
@@ -594,7 +587,10 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
public void reserveNext() {
|
||||
Context context = new Context(null, route, this, route.endBlock(), route.endDirection);
|
||||
Context context = new Context(this)
|
||||
.route(route)
|
||||
.block(route.endBlock())
|
||||
.direction(route.endDirection);
|
||||
Route nextRoute = PathFinder.chooseRoute(context);
|
||||
if (isNull(nextRoute)) return;
|
||||
|
||||
@@ -622,7 +618,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Entry<Integer, Train> entry:trains.entrySet()) {
|
||||
for (Entry<Id, Train> entry:trains.entrySet()) {
|
||||
Train train = entry.getValue();
|
||||
file.write(train.json()+"\n");
|
||||
}
|
||||
@@ -653,7 +649,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
destination = null;
|
||||
return t("Dropped destination of {}.",this);
|
||||
}
|
||||
Tile tile = plan.get(dest, true);
|
||||
Tile tile = plan.get(new Id(dest), true);
|
||||
if (isNull(tile)) return t("Tile {} not known!",dest);
|
||||
if (tile instanceof Block) {
|
||||
destination = (Block) tile;
|
||||
@@ -781,11 +777,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
return properties();
|
||||
}
|
||||
|
||||
private static String t(String message, Object...fills) {
|
||||
return Translation.get(Application.class, message, fills);
|
||||
}
|
||||
|
||||
|
||||
public SortedSet<String> tags() {
|
||||
TreeSet<String> list = new TreeSet<String>(tags);
|
||||
|
||||
Reference in New Issue
Block a user