Browse Source

implemented adding of locos to train

lookup-tables
Stephan Richter 5 years ago
parent
commit
7b8aea89f2
  1. 2
      pom.xml
  2. 3
      resources/css/style.css
  3. 71
      src/main/java/de/srsoftware/web4rail/moving/Train.java
  4. 6
      src/main/java/de/srsoftware/web4rail/tiles/Block.java
  5. 4
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java
  6. 2
      src/main/java/de/srsoftware/web4rail/tiles/Turnout.java

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId> <groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId> <artifactId>web4rail</artifactId>
<version>0.6.6</version> <version>0.6.7</version>
<name>Web4Rail</name> <name>Web4Rail</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Java Model Railway Control</description> <description>Java Model Railway Control</description>

3
resources/css/style.css

@ -210,3 +210,6 @@ fieldset{
background: red; background: red;
} }
ul{
clear: both;
}

71
src/main/java/de/srsoftware/web4rail/moving/Train.java

@ -96,6 +96,7 @@ public class Train implements Constants {
} }
public static final String LOCO_ID = "locoId"; public static final String LOCO_ID = "locoId";
private static final String CAR_ID = "carId";
public int speed = 0; public int speed = 0;
private Autopilot autopilot = null; private Autopilot autopilot = null;
@ -129,20 +130,36 @@ public class Train implements Constants {
Train train = trains.get(id); Train train = trains.get(id);
if (train == null) return(t("No train with id {}!",id)); if (train == null) return(t("No train with id {}!",id));
switch (action) { switch (action) {
case ACTION_PROPS: case ACTION_ADD:
return train.props(); return train.addCar(params);
case ACTION_AUTO: case ACTION_AUTO:
return train.automatic(); return train.automatic();
case ACTION_PROPS:
return train.props();
case ACTION_START: case ACTION_START:
return train.start(); return train.start();
case ACTION_STOP: case ACTION_STOP:
return train.stop(); return train.stop();
case ACTION_TURN:
return train.turn();
case ACTION_UPDATE: case ACTION_UPDATE:
return train.update(params); return train.update(params);
} }
return t("Unknown action: {}",params.get(ACTION)); return t("Unknown action: {}",params.get(ACTION));
} }
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!");
Car car = Car.get(params.get(CAR_ID));
if (car == null) return t("No car with id \"{}\" known!",params.get(CAR_ID));
if (car instanceof Locomotive) {
locos.add((Locomotive) car);
} else cars.add(car);
return this;
}
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)); Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
if (loco == null) return t("unknown locomotive: {}",params.get(ID)); if (loco == null) return t("unknown locomotive: {}",params.get(ID));
@ -288,27 +305,39 @@ public class Train implements Constants {
} }
public Tag props() { public Tag props() {
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName())); Window window = new Window("train-properties",t("Properties of {}",this));
Form form = new Form();
Fieldset fieldset = new Fieldset(t("Train properties")); Fieldset fieldset = new Fieldset(t("Train properties"));
Form form = new Form();
new Input(ACTION,ACTION_UPDATE).hideIn(form); new Input(ACTION,ACTION_UPDATE).hideIn(form);
new Input(REALM,REALM_TRAIN).hideIn(form); new Input(REALM,REALM_TRAIN).hideIn(form);
new Input(ID,id).hideIn(form); new Input(ID,id).hideIn(form);
new Input(NAME,name()).addTo(fieldset); new Input(NAME,name).addTo(fieldset);
new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull).addTo(fieldset); new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull).addTo(fieldset);
new Button(t("save")).addTo(fieldset).addTo(form).addTo(window); new Button(t("save")).addTo(form).addTo(fieldset);
Tag list = new Tag("ul"); new Button(t("Turn"), "train("+id+",'"+ACTION_TURN+"')").addTo(fieldset).addTo(window);
if (!locos.isEmpty()) {
Tag locos = new Tag("li").content(t("Locomotives:")); Tag propList = new Tag("ul");
Tag l2 = new Tag("ul");
for (Locomotive loco : this.locos) loco.link("li").addTo(l2); Tag locoProp = new Tag("li").content(t("Locomotives:"));
l2.addTo(locos).addTo(list); Tag locoList = new Tag("ul");
for (Locomotive loco : this.locos) loco.link("li").addTo(locoList);
Tag addLocoForm = new Form().content(t("add locomotive:")+"&nbsp;");
new Input(REALM, REALM_TRAIN).hideIn(addLocoForm);
new Input(ACTION, ACTION_ADD).hideIn(addLocoForm);
new Input(ID,id).hideIn(addLocoForm);
Select select = new Select(CAR_ID);
for (Locomotive loco : Locomotive.list()) {
if (!this.locos.contains(loco)) select.addOption(loco.id(), loco);
} }
select.addTo(addLocoForm);
new Button(t("add")).addTo(addLocoForm).addTo(new Tag("li")).addTo(locoList).addTo(locoProp).addTo(propList);
if (block != null) { if (block != null) {
new Tag("li").content(t("Current location: {}",block)).addTo(list); new Tag("li").content(t("Current location: {}",block)).addTo(propList);
Tag actions = new Tag("li").clazz().content(t("Actions: ")); Tag actions = new Tag("li").clazz().content(t("Actions: "));
new Button(t("start"),"train("+id+",'"+ACTION_START+"')").addTo(actions); new Button(t("start"),"train("+id+",'"+ACTION_START+"')").addTo(actions);
if (autopilot == null) { if (autopilot == null) {
@ -316,16 +345,16 @@ public class Train implements Constants {
} else { } else {
new Button(t("stop"),"train("+id+",'"+ACTION_STOP+"')").addTo(actions); new Button(t("stop"),"train("+id+",'"+ACTION_STOP+"')").addTo(actions);
} }
actions.addTo(list); actions.addTo(propList);
} }
if (route != null) { if (route != null) {
new Tag("li").content(t("Current route: {}",route)).addTo(list); new Tag("li").content(t("Current route: {}",route)).addTo(propList);
} }
if (direction != null) new Tag("li").content(t("Direction: heading {}",direction)).addTo(list); if (direction != null) new Tag("li").content(t("Direction: heading {}",direction)).addTo(propList);
list.addTo(window); propList.addTo(window);
return window; return window;
} }
@ -378,6 +407,7 @@ public class Train implements Constants {
} }
route.unlock(); route.unlock();
this.block.train(this); // re-set train on previous block this.block.train(this); // re-set train on previous block
this.route = null;
return error; return error;
} }
@ -393,16 +423,17 @@ public class Train implements Constants {
@Override @Override
public String toString() { public String toString() {
return name(); return name != null ? name : locos.firstElement().name();
} }
private void turn() throws IOException { private Object turn() throws IOException {
LOG.debug("train.turn()"); LOG.debug("train.turn()");
if (direction != null) { if (direction != null) {
direction = direction.inverse(); direction = direction.inverse();
for (Locomotive loco : locos) loco.turn(); for (Locomotive loco : locos) loco.turn();
} }
if (block != null) block.train(this); if (block != null) plan.place(block.train(this));
return t("{} turned.",this);
} }

6
src/main/java/de/srsoftware/web4rail/tiles/Block.java

@ -105,11 +105,11 @@ public abstract class Block extends StretchableTile{
return getClass().getSimpleName()+"("+name+") @ ("+x+","+y+")"; return getClass().getSimpleName()+"("+name+") @ ("+x+","+y+")";
} }
public void train(Train newTrain) throws IOException { public Tile train(Train newTrain) throws IOException {
if (train == newTrain) return; if (train == newTrain) return this;
if (train != null) train.block(null); // vorherigen Zug rauswerfen if (train != null) train.block(null); // vorherigen Zug rauswerfen
if (newTrain != null) newTrain.block(this); if (newTrain != null) newTrain.block(this);
super.train(newTrain); return super.train(newTrain);
} }
@Override @Override

4
src/main/java/de/srsoftware/web4rail/tiles/Tile.java

@ -341,9 +341,9 @@ public abstract class Tile implements Constants{
return train; return train;
} }
public void train(Train train) throws IOException { public Tile train(Train train) throws IOException {
this.train = train; this.train = train;
plan.place(this); return plan.place(this);
} }
public void unlock() throws IOException { public void unlock() throws IOException {

2
src/main/java/de/srsoftware/web4rail/tiles/Turnout.java

@ -153,7 +153,7 @@ public abstract class Turnout extends Tile implements Device{
@Override @Override
protected void onFailure(Reply reply) { protected void onFailure(Reply reply) {
super.onFailure(reply); super.onFailure(reply);
plan.stream(t("Unable to switch {}: {}",this,reply.message())); plan.stream(t("Unable to switch \"{}\": {}",Turnout.this,reply.message()));
} }
}).reply(); }).reply();

Loading…
Cancel
Save