implemented Locomotive and Train management
This commit is contained in:
@@ -14,16 +14,18 @@ import org.json.JSONObject;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
|
||||
public class Car {
|
||||
private static final String ID = "id";
|
||||
private static final String NAME = "name";
|
||||
public static final String ID = "id";
|
||||
public static final String NAME = "name";
|
||||
private static final String LENGTH = "length";
|
||||
private static final String SHOW = "show";
|
||||
static HashMap<String,Car> cars = new HashMap<String, Car>();
|
||||
public int length;
|
||||
private String name;
|
||||
private String id;
|
||||
private Train train;
|
||||
|
||||
public Car(String name) {
|
||||
this(name,null);
|
||||
@@ -67,11 +69,7 @@ public class Car {
|
||||
public Tag link(String tagClass) {
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","car("+id+",'"+Car.SHOW+"')").content(name());
|
||||
}
|
||||
|
||||
String name(){
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public static void loadAll(String filename) throws IOException {
|
||||
cars.clear();
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
@@ -93,6 +91,20 @@ public class Car {
|
||||
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
||||
}
|
||||
|
||||
String name(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public Object properties() {
|
||||
Window win = new Window("car-props", t("Properties of {}",this));
|
||||
Tag list = new Tag("ul");
|
||||
if (train != null) {
|
||||
train.link("span").addTo(new Tag("li").content(t("Train:")+" ")).addTo(list);
|
||||
}
|
||||
list.addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Entry<String, Car> entry: cars.entrySet()) {
|
||||
@@ -105,4 +117,13 @@ public class Car {
|
||||
protected static String t(String txt, Object...fills) {
|
||||
return Translation.get(Application.class, txt, fills);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName()+"("+name()+")";
|
||||
}
|
||||
|
||||
public void train(Train train) {
|
||||
this.train = train;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
package de.srsoftware.web4rail.moving;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class Locomotive extends Car {
|
||||
|
||||
@@ -28,6 +36,14 @@ public class Locomotive extends Car {
|
||||
return json;
|
||||
}
|
||||
|
||||
static Vector<Locomotive> list() {
|
||||
Vector<Locomotive> locos = new Vector<Locomotive>();
|
||||
for (Car car : Car.cars.values()) {
|
||||
if (car instanceof Locomotive) locos.add((Locomotive) car);
|
||||
}
|
||||
return locos;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void load(JSONObject json) {
|
||||
super.load(json);
|
||||
@@ -50,6 +66,14 @@ public class Locomotive extends Car {
|
||||
}
|
||||
}
|
||||
list.addTo(win);
|
||||
|
||||
Form form = new Form();
|
||||
new Input(Plan.ACTION, Plan.ACTION_ADD_LOCO).hideIn(form);
|
||||
Fieldset fieldset = new Fieldset(t("add new locomotive"));
|
||||
new Input(Locomotive.NAME, t("new locomotive")).addTo(new Label(t("Name:")+" ")).addTo(fieldset);
|
||||
new Button(t("save")).addTo(fieldset);
|
||||
fieldset.addTo(form).addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -19,11 +20,17 @@ import org.slf4j.LoggerFactory;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
import de.srsoftware.web4rail.tiles.Signal;
|
||||
@@ -93,6 +100,8 @@ public class Train {
|
||||
|
||||
private static final String MODE_STOP = "stop";
|
||||
|
||||
public static final String LOCO_ID = "locoId";
|
||||
|
||||
public int speed = 0;
|
||||
private Autopilot autopilot = null;
|
||||
|
||||
@@ -135,7 +144,8 @@ public class Train {
|
||||
if (car == null) return;
|
||||
if (car instanceof Locomotive) {
|
||||
locos.add((Locomotive) car);
|
||||
} else cars.add(car);
|
||||
} else cars.add(car);
|
||||
car.train(this);
|
||||
}
|
||||
|
||||
private String automatic() {
|
||||
@@ -146,7 +156,7 @@ public class Train {
|
||||
return t("{} now in auto-mode",this);
|
||||
}
|
||||
|
||||
public void block(Block block) {
|
||||
public void block(Block block) throws IOException {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
@@ -187,6 +197,10 @@ public class Train {
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","train("+id+",'"+Train.MODE_SHOW+"')").content(name());
|
||||
}
|
||||
|
||||
public static Collection<Train> list() {
|
||||
return trains.values();
|
||||
}
|
||||
|
||||
public static void loadAll(String filename) throws IOException {
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
String line = file.readLine();
|
||||
@@ -217,6 +231,20 @@ public class Train {
|
||||
train.link("li").addTo(list);
|
||||
}
|
||||
list.addTo(win);
|
||||
|
||||
Form form = new Form();
|
||||
new Input(Plan.ACTION, Plan.ACTION_ADD_TRAIN).hideIn(form);
|
||||
Fieldset fieldset = new Fieldset(t("add new train"));
|
||||
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());
|
||||
select.addTo(new Label(t("Locomotive:")+" ")).addTo(fieldset);
|
||||
|
||||
new Button(t("save")).addTo(fieldset);
|
||||
fieldset.addTo(form).addTo(win);
|
||||
|
||||
|
||||
return win;
|
||||
}
|
||||
|
||||
@@ -238,19 +266,20 @@ public class Train {
|
||||
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName()));
|
||||
|
||||
Form form = new Form();
|
||||
new Tag("input").attr("type", "hidden").attr("name","action").attr("value", "train").addTo(form);
|
||||
new Tag("input").attr("type", "hidden").attr("name",ID).attr("value", ""+id).addTo(form);
|
||||
new Tag("input").attr("type", "hidden").attr("name","mode").attr("value", MODE_UPDATE).addTo(form);
|
||||
new Input("action","train").hideIn(form);
|
||||
new Input(ID,id).hideIn(form);
|
||||
new Input("mode",MODE_UPDATE).hideIn(form);
|
||||
|
||||
Checkbox pp = new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull);
|
||||
pp.addTo(form);
|
||||
new Tag("button").attr("type", "submit").content(t("save")).addTo(form).addTo(window);
|
||||
new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull).addTo(form);
|
||||
new Button(t("save")).addTo(form).addTo(window);
|
||||
|
||||
Tag list = new Tag("ul");
|
||||
Tag locos = new Tag("li").content(t("Locomotives:"));
|
||||
Tag l2 = new Tag("ul");
|
||||
for (Locomotive loco : this.locos) new Tag("li").content(loco.name()).addTo(l2);
|
||||
l2.addTo(locos).addTo(list);
|
||||
if (!locos.isEmpty()) {
|
||||
Tag locos = new Tag("li").content(t("Locomotives:"));
|
||||
Tag l2 = new Tag("ul");
|
||||
for (Locomotive loco : this.locos) loco.link("li").addTo(l2);
|
||||
l2.addTo(locos).addTo(list);
|
||||
}
|
||||
|
||||
if (block != null) {
|
||||
new Tag("li").content(t("Current location: {}",block)).addTo(list);
|
||||
@@ -317,7 +346,7 @@ public class Train {
|
||||
private Object stop() {
|
||||
autopilot.stop = true;
|
||||
autopilot = null;
|
||||
return t("{} stopping at next block {}");
|
||||
return t("{} stopping at next block.",this);
|
||||
}
|
||||
|
||||
private static String t(String message, Object...fills) {
|
||||
|
||||
Reference in New Issue
Block a user