implemented storing of loco and car properties

This commit is contained in:
Stephan Richter
2020-09-30 22:28:07 +02:00
parent 29888379c6
commit e8d5e1ab56
6 changed files with 111 additions and 14 deletions

View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<name>Web4Rail</name>
<description>Java Model Railway Control</description>
<url>https://github.com/StephanRichter/Web4Rail</url>

View File

@@ -30,6 +30,10 @@ body{
height: 60px !important;
}
label{
display: flex;
}
svg circle,
svg line,
svg polygon,

View File

@@ -111,6 +111,7 @@ public class Plan {
private static final String ACTION_CAR = "car";
public static final String ACTION_ADD_LOCO = "addLoco";
public static final String ACTION_ADD_TRAIN = "addTrain";
public static final String ACTION_UPDATE_CAR = "updateCar";
public HashMap<String,Tile> tiles = new HashMap<String,Tile>();
private HashSet<Block> blocks = new HashSet<Block>();
@@ -172,10 +173,19 @@ public class Plan {
return blocks;
}
private Object carAction(HashMap<String, String> params) {
private Object carAction(HashMap<String, String> params) throws IOException {
Car car = Car.get(params.get(Car.ID));
if (car == null) return t("No car with id {} found!",params.get(Car.ID));
return car.properties();
switch (params.get(ACTION)) {
case ACTION_CAR:
return car.properties();
case ACTION_UPDATE_CAR:
car.update(params);
break;
}
return html();
}
private Object click(Tile tile) throws IOException {
@@ -375,6 +385,7 @@ public class Plan {
case ACTION_TRAIN:
return trainAction(params);
case ACTION_CAR:
case ACTION_UPDATE_CAR:
return carAction(params);
case ACTION_CLICK:
return click(get(params.get(Tile.ID),true));

View File

@@ -10,21 +10,34 @@ import java.util.HashMap;
import java.util.Map.Entry;
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.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 Car {
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
static HashMap<String,Car> cars = new HashMap<String, Car>();
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 static final String STOCK_ID = "stock-id";
private String id;
private String name;
public int length;
private String stockId = "";
private Train train;
public Car(String name) {
@@ -63,6 +76,7 @@ public class Car {
json.put(ID,id);
json.put(NAME, name);
json.put(LENGTH, length);
json.put(STOCK_ID, stockId);
return json;
}
@@ -89,14 +103,36 @@ public class Car {
protected void load(JSONObject json) {
if (json.has(ID)) id = json.getString(ID);
if (json.has(LENGTH)) length = json.getInt(LENGTH);
if (json.has(STOCK_ID)) stockId = json.getString(STOCK_ID);
}
String name(){
return name;
}
public Tag propertyForm() {
Form form = new Form();
new Input("action", Plan.ACTION_UPDATE_CAR).hideIn(form);
new Input(ID,id()).hideIn(form);
Fieldset fieldset = new Fieldset("Basic properties");
new Input(NAME,name).addTo(new Label(t("Name"))).addTo(fieldset);
new Input(STOCK_ID,stockId).addTo(new Label(t("Stock ID"))).addTo(fieldset);
new Input(LENGTH,length).attr("type", "number").addTo(new Label(t("Length"))).addTo(fieldset);
fieldset.addTo(form);
return form;
}
public Object properties() {
Window win = new Window("car-props", t("Properties of {}",this));
Tag form = propertyForm();
if (form!=null && form.children().size()>2) {
new Button(t("save")).addTo(form);
form.addTo(win);
} else {
win.content(t("This tile ({}) has no editable properties",getClass().getSimpleName()));
}
Tag list = new Tag("ul");
if (train != null) {
train.link("span").addTo(new Tag("li").content(t("Train:")+" ")).addTo(list);
@@ -126,4 +162,11 @@ public class Car {
public void train(Train train) {
this.train = train;
}
public Object update(HashMap<String, String> params) {
if (params.containsKey(NAME)) name = params.get(NAME);
if (params.containsKey(STOCK_ID)) stockId = params.get(STOCK_ID);
if (params.containsKey(LENGTH)) length = Integer.parseInt(params.get(LENGTH));
return null;
}
}

View File

@@ -1,5 +1,6 @@
package de.srsoftware.web4rail.moving;
import java.util.HashMap;
import java.util.Vector;
import org.json.JSONObject;
@@ -12,12 +13,21 @@ 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.Radio;
public class Locomotive extends Car {
public enum Protocol{
DCC14,DCC27,DCC28,DCC128,MOTO;
}
private static final String REVERSE = "reverse";
public static final String LOCOMOTIVE = "locomotive";
private static final String PROTOCOL = "protocol";
private static final String ADDRESS = "address";
private boolean reverse = false;
private Protocol proto = Protocol.DCC128;
private int address = 3;
public Locomotive(String name) {
super(name);
@@ -32,6 +42,9 @@ public class Locomotive extends Car {
JSONObject json = super.json();
JSONObject loco = new JSONObject();
loco.put(REVERSE, reverse);
loco.put(PROTOCOL, proto);
loco.put(ADDRESS, address);
json.put(LOCOMOTIVE, loco);
return json;
}
@@ -47,13 +60,13 @@ public class Locomotive extends Car {
@Override
protected void load(JSONObject json) {
super.load(json);
if (json.has(REVERSE)) reverse = json.getBoolean(REVERSE);
}
public void setSpeed(int v) {
// TODO Auto-generated method stub
}
if (json.has(LOCOMOTIVE)) {
JSONObject loco = json.getJSONObject(LOCOMOTIVE);
if (loco.has(REVERSE)) reverse = loco.getBoolean(REVERSE);
if (loco.has(PROTOCOL)) proto = Protocol.valueOf(loco.getString(PROTOCOL));
if (loco.has(ADDRESS)) address = loco.getInt(ADDRESS);
}
}
public static Object manager() {
Window win = new Window("loco-manager", t("Locomotive manager"));
@@ -75,5 +88,31 @@ public class Locomotive extends Car {
fieldset.addTo(form).addTo(win);
return win;
}
@Override
public Tag propertyForm() {
Tag form = super.propertyForm();
Fieldset fieldset = new Fieldset("Decoder settings");
Label protocol = new Label(t("Protocol:"));
for (Protocol proto : Protocol.values()) {
new Radio(PROTOCOL, proto.toString(), t(proto.toString()), proto == this.proto).addTo(protocol);
}
protocol.addTo(fieldset);
new Input(ADDRESS, address).attr("type", "number").addTo(new Label(t("Address:"))).addTo(fieldset);
fieldset.addTo(form);
return form;
}
public void setSpeed(int v) {
// TODO Auto-generated method stub
}
@Override
public Object update(HashMap<String, String> params) {
super.update(params);
if (params.containsKey(PROTOCOL)) proto = Protocol.valueOf(params.get(PROTOCOL));
if (params.containsKey(ADDRESS)) address = Integer.parseInt(params.get(ADDRESS));
return t("Updated locomotive.");
}
}

View File

@@ -200,7 +200,7 @@ public abstract class Tile {
new Button(t("save")).addTo(form);
form.addTo(window);
} else {
window.content(t("This tile ({}) has no properties",getClass().getSimpleName()));
window.content(t("This tile ({}) has no editable properties",getClass().getSimpleName()));
}
if (route != null) {