implemented storing of loco and car properties
This commit is contained in:
2
pom.xml
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.4.0</version>
|
<version>0.4.1</version>
|
||||||
<name>Web4Rail</name>
|
<name>Web4Rail</name>
|
||||||
<description>Java Model Railway Control</description>
|
<description>Java Model Railway Control</description>
|
||||||
<url>https://github.com/StephanRichter/Web4Rail</url>
|
<url>https://github.com/StephanRichter/Web4Rail</url>
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ body{
|
|||||||
height: 60px !important;
|
height: 60px !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
label{
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
svg circle,
|
svg circle,
|
||||||
svg line,
|
svg line,
|
||||||
svg polygon,
|
svg polygon,
|
||||||
|
|||||||
@@ -111,6 +111,7 @@ public class Plan {
|
|||||||
private static final String ACTION_CAR = "car";
|
private static final String ACTION_CAR = "car";
|
||||||
public static final String ACTION_ADD_LOCO = "addLoco";
|
public static final String ACTION_ADD_LOCO = "addLoco";
|
||||||
public static final String ACTION_ADD_TRAIN = "addTrain";
|
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>();
|
public HashMap<String,Tile> tiles = new HashMap<String,Tile>();
|
||||||
private HashSet<Block> blocks = new HashSet<Block>();
|
private HashSet<Block> blocks = new HashSet<Block>();
|
||||||
@@ -172,10 +173,19 @@ public class Plan {
|
|||||||
return blocks;
|
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));
|
Car car = Car.get(params.get(Car.ID));
|
||||||
if (car == null) return t("No car with id {} found!",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 {
|
private Object click(Tile tile) throws IOException {
|
||||||
@@ -375,6 +385,7 @@ public class Plan {
|
|||||||
case ACTION_TRAIN:
|
case ACTION_TRAIN:
|
||||||
return trainAction(params);
|
return trainAction(params);
|
||||||
case ACTION_CAR:
|
case ACTION_CAR:
|
||||||
|
case ACTION_UPDATE_CAR:
|
||||||
return carAction(params);
|
return carAction(params);
|
||||||
case ACTION_CLICK:
|
case ACTION_CLICK:
|
||||||
return click(get(params.get(Tile.ID),true));
|
return click(get(params.get(Tile.ID),true));
|
||||||
|
|||||||
@@ -10,21 +10,34 @@ import java.util.HashMap;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import de.keawe.tools.translations.Translation;
|
import de.keawe.tools.translations.Translation;
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Application;
|
import de.srsoftware.web4rail.Application;
|
||||||
|
import de.srsoftware.web4rail.Plan;
|
||||||
import de.srsoftware.web4rail.Window;
|
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 {
|
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 ID = "id";
|
||||||
public static final String NAME = "name";
|
public static final String NAME = "name";
|
||||||
private static final String LENGTH = "length";
|
private static final String LENGTH = "length";
|
||||||
private static final String SHOW = "show";
|
private static final String SHOW = "show";
|
||||||
static HashMap<String,Car> cars = new HashMap<String, Car>();
|
private static final String STOCK_ID = "stock-id";
|
||||||
public int length;
|
|
||||||
private String name;
|
|
||||||
private String id;
|
private String id;
|
||||||
|
private String name;
|
||||||
|
public int length;
|
||||||
|
private String stockId = "";
|
||||||
private Train train;
|
private Train train;
|
||||||
|
|
||||||
public Car(String name) {
|
public Car(String name) {
|
||||||
@@ -63,6 +76,7 @@ public class Car {
|
|||||||
json.put(ID,id);
|
json.put(ID,id);
|
||||||
json.put(NAME, name);
|
json.put(NAME, name);
|
||||||
json.put(LENGTH, length);
|
json.put(LENGTH, length);
|
||||||
|
json.put(STOCK_ID, stockId);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,14 +103,36 @@ public class Car {
|
|||||||
protected void load(JSONObject json) {
|
protected void load(JSONObject json) {
|
||||||
if (json.has(ID)) id = json.getString(ID);
|
if (json.has(ID)) id = json.getString(ID);
|
||||||
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
||||||
|
if (json.has(STOCK_ID)) stockId = json.getString(STOCK_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
String name(){
|
String name(){
|
||||||
return 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() {
|
public Object properties() {
|
||||||
Window win = new Window("car-props", t("Properties of {}",this));
|
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");
|
Tag list = new Tag("ul");
|
||||||
if (train != null) {
|
if (train != null) {
|
||||||
train.link("span").addTo(new Tag("li").content(t("Train:")+" ")).addTo(list);
|
train.link("span").addTo(new Tag("li").content(t("Train:")+" ")).addTo(list);
|
||||||
@@ -126,4 +162,11 @@ public class Car {
|
|||||||
public void train(Train train) {
|
public void train(Train train) {
|
||||||
this.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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.srsoftware.web4rail.moving;
|
package de.srsoftware.web4rail.moving;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
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.Form;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
import de.srsoftware.web4rail.tags.Label;
|
import de.srsoftware.web4rail.tags.Label;
|
||||||
|
import de.srsoftware.web4rail.tags.Radio;
|
||||||
|
|
||||||
public class Locomotive extends Car {
|
public class Locomotive extends Car {
|
||||||
|
|
||||||
|
public enum Protocol{
|
||||||
|
DCC14,DCC27,DCC28,DCC128,MOTO;
|
||||||
|
}
|
||||||
|
|
||||||
private static final String REVERSE = "reverse";
|
private static final String REVERSE = "reverse";
|
||||||
public static final String LOCOMOTIVE = "locomotive";
|
public static final String LOCOMOTIVE = "locomotive";
|
||||||
|
private static final String PROTOCOL = "protocol";
|
||||||
|
private static final String ADDRESS = "address";
|
||||||
private boolean reverse = false;
|
private boolean reverse = false;
|
||||||
|
private Protocol proto = Protocol.DCC128;
|
||||||
|
private int address = 3;
|
||||||
|
|
||||||
public Locomotive(String name) {
|
public Locomotive(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
@@ -32,6 +42,9 @@ public class Locomotive extends Car {
|
|||||||
JSONObject json = super.json();
|
JSONObject json = super.json();
|
||||||
JSONObject loco = new JSONObject();
|
JSONObject loco = new JSONObject();
|
||||||
loco.put(REVERSE, reverse);
|
loco.put(REVERSE, reverse);
|
||||||
|
loco.put(PROTOCOL, proto);
|
||||||
|
loco.put(ADDRESS, address);
|
||||||
|
|
||||||
json.put(LOCOMOTIVE, loco);
|
json.put(LOCOMOTIVE, loco);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@@ -47,12 +60,12 @@ public class Locomotive extends Car {
|
|||||||
@Override
|
@Override
|
||||||
protected void load(JSONObject json) {
|
protected void load(JSONObject json) {
|
||||||
super.load(json);
|
super.load(json);
|
||||||
if (json.has(REVERSE)) reverse = json.getBoolean(REVERSE);
|
if (json.has(LOCOMOTIVE)) {
|
||||||
}
|
JSONObject loco = json.getJSONObject(LOCOMOTIVE);
|
||||||
|
if (loco.has(REVERSE)) reverse = loco.getBoolean(REVERSE);
|
||||||
public void setSpeed(int v) {
|
if (loco.has(PROTOCOL)) proto = Protocol.valueOf(loco.getString(PROTOCOL));
|
||||||
// TODO Auto-generated method stub
|
if (loco.has(ADDRESS)) address = loco.getInt(ADDRESS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object manager() {
|
public static Object manager() {
|
||||||
@@ -76,4 +89,30 @@ public class Locomotive extends Car {
|
|||||||
return 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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ public abstract class Tile {
|
|||||||
new Button(t("save")).addTo(form);
|
new Button(t("save")).addTo(form);
|
||||||
form.addTo(window);
|
form.addTo(window);
|
||||||
} else {
|
} 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) {
|
if (route != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user