preparing menus for editing cars and trains
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.3.6</version>
|
||||
<version>0.3.7</version>
|
||||
<name>Web4Rail</name>
|
||||
<description>Java Model Railway Control</description>
|
||||
<url>https://github.com/StephanRichter/Web4Rail</url>
|
||||
|
||||
@@ -23,6 +23,10 @@ function addTile(x,y){
|
||||
return request({action:mode,tile:selected.id,x:x,y:y});
|
||||
}
|
||||
|
||||
function car(id,mode){
|
||||
return request({action:"car",id:id,mode:mode});
|
||||
}
|
||||
|
||||
function clickTile(x,y){
|
||||
var id = x+"-"+y;
|
||||
console.log("clickTile:",id);
|
||||
@@ -171,6 +175,7 @@ window.onload = function () {
|
||||
$('.menu .addtile .list svg').click(enableAdding);
|
||||
$('.menu .move .list div').click(enableMove);
|
||||
$('.menu .actions .list > div').click(runAction);
|
||||
$('.menu .trains .list > div').click(runAction);
|
||||
$(PLAN).click(planClick);
|
||||
(new EventSource("stream")).onmessage = stream;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
import de.srsoftware.web4rail.moving.Locomotive;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Div;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
@@ -105,6 +106,8 @@ public class Plan {
|
||||
private static final String ROUTE = "route";
|
||||
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
|
||||
private static final String ACTION_TRAIN = "train";
|
||||
private static final String ACTION_LOCOS = "locos";
|
||||
private static final String ACTION_TRAINS = "trains";
|
||||
|
||||
public HashMap<String,Tile> tiles = new HashMap<String,Tile>();
|
||||
private HashSet<Block> blocks = new HashSet<Block>();
|
||||
@@ -351,6 +354,8 @@ public class Plan {
|
||||
return click(get(params.get(Tile.ID),true));
|
||||
case ACTION_ANALYZE:
|
||||
return analyze();
|
||||
case ACTION_LOCOS:
|
||||
return Locomotive.manager();
|
||||
case ACTION_MOVE:
|
||||
return moveTile(params.get(DIRECTION),params.get(Tile.ID));
|
||||
case ACTION_ROUTE:
|
||||
@@ -359,6 +364,8 @@ public class Plan {
|
||||
return saveTo(params.get(FILE));
|
||||
case ACTION_TRAIN:
|
||||
return trainAction(params);
|
||||
case ACTION_TRAINS:
|
||||
return Train.manager();
|
||||
case ACTION_UPDATE:
|
||||
return update(params);
|
||||
default:
|
||||
@@ -499,9 +506,10 @@ public class Plan {
|
||||
}
|
||||
|
||||
private Tag trainMenu() throws IOException {
|
||||
Tag tileMenu = new Tag("div").clazz("trains").title(t("Manage trains")).content(t("Trains"));
|
||||
|
||||
Tag tileMenu = new Tag("div").clazz("trains").content(t("Trains"));
|
||||
StringBuffer tiles = new StringBuffer();
|
||||
tiles.append(new Div("trains").content(t("Manage trains")));
|
||||
tiles.append(new Div("locos").content(t("Manage locos")));
|
||||
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,15 @@ import java.util.Map.Entry;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
|
||||
public class Car {
|
||||
private static final String ID = "id";
|
||||
private 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;
|
||||
@@ -37,9 +42,8 @@ public class Car {
|
||||
}
|
||||
|
||||
public static Car get(String nameOrId) {
|
||||
HashMap<String, Car> cs = cars;
|
||||
Car car = cars.get(nameOrId);
|
||||
if (car == null) {
|
||||
Car car = cars.get(nameOrId); // try to get by id
|
||||
if (car == null) { // try to get by name
|
||||
for (Car c : cars.values()) {
|
||||
if (c.name.equals(nameOrId)) car = c;
|
||||
}
|
||||
@@ -59,6 +63,10 @@ public class Car {
|
||||
json.put(LENGTH, length);
|
||||
return json;
|
||||
}
|
||||
|
||||
public Tag link(String tagClass) {
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","car("+id+",'"+Car.SHOW+"')").content(name());
|
||||
}
|
||||
|
||||
String name(){
|
||||
return name;
|
||||
@@ -93,4 +101,8 @@ public class Car {
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
protected static String t(String txt, Object...fills) {
|
||||
return Translation.get(Application.class, txt, fills);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package de.srsoftware.web4rail.moving;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
|
||||
public class Locomotive extends Car {
|
||||
|
||||
private static final String REVERSE = "reverse";
|
||||
@@ -30,8 +33,23 @@ public class Locomotive extends Car {
|
||||
super.load(json);
|
||||
if (json.has(REVERSE)) reverse = json.getBoolean(REVERSE);
|
||||
}
|
||||
|
||||
public void setSpeed(int v) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public static Object manager() {
|
||||
Window win = new Window("loco-manager", t("Locomotive manager"));
|
||||
new Tag("h4").content(t("known locomotives")).addTo(win);
|
||||
Tag list = new Tag("ul");
|
||||
for (Car car : cars.values()) {
|
||||
if (car instanceof Locomotive) {
|
||||
Locomotive loco = (Locomotive) car;
|
||||
loco.link("li").addTo(list);
|
||||
}
|
||||
}
|
||||
list.addTo(win);
|
||||
return win;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,24 +107,6 @@ public class Train {
|
||||
trains.put(id, this);
|
||||
}
|
||||
|
||||
|
||||
private JSONObject json() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put(ID, id);
|
||||
json.put(NAME,name);
|
||||
if (route != null) json.put(ROUTE, route.id());
|
||||
if (direction != null) json.put(DIRECTION, direction);
|
||||
json.put(PUSH_PULL, pushPull);
|
||||
Vector<String> locoIds = new Vector<String>();
|
||||
for (Locomotive loco : locos) locoIds.add(loco.id());
|
||||
json.put(LOCOS, locoIds);
|
||||
Vector<String> carIds = new Vector<String>();
|
||||
for (Car car : cars) carIds.add(car.id());
|
||||
json.put(CARS,carIds);
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
public static Object action(HashMap<String, String> params) throws IOException {
|
||||
if (!params.containsKey(Train.ID)) return t("No train id passed!");
|
||||
long id = Long.parseLong(params.get(Train.ID));
|
||||
@@ -176,6 +158,22 @@ public class Train {
|
||||
public Train heading(Direction dir) {
|
||||
direction = dir;
|
||||
return this;
|
||||
}
|
||||
|
||||
private JSONObject json() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put(ID, id);
|
||||
json.put(NAME,name);
|
||||
if (route != null) json.put(ROUTE, route.id());
|
||||
if (direction != null) json.put(DIRECTION, direction);
|
||||
json.put(PUSH_PULL, pushPull);
|
||||
Vector<String> locoIds = new Vector<String>();
|
||||
for (Locomotive loco : locos) locoIds.add(loco.id());
|
||||
json.put(LOCOS, locoIds);
|
||||
Vector<String> carIds = new Vector<String>();
|
||||
for (Car car : cars) carIds.add(car.id());
|
||||
json.put(CARS,carIds);
|
||||
return json;
|
||||
}
|
||||
|
||||
public int length() {
|
||||
@@ -185,6 +183,10 @@ public class Train {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Tag link(String tagClass) {
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","train("+id+",'"+Train.MODE_SHOW+"')").content(name());
|
||||
}
|
||||
|
||||
public static void loadAll(String filename) throws IOException {
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
String line = file.readLine();
|
||||
@@ -206,6 +208,17 @@ public class Train {
|
||||
for (Object id : json.getJSONArray(LOCOS)) add((Locomotive) Car.get((String)id));
|
||||
for (Object id : json.getJSONArray(CARS)) add(Car.get((String)id));
|
||||
}
|
||||
|
||||
public static Object manager() {
|
||||
Window win = new Window("train-manager", t("Train manager"));
|
||||
new Tag("h4").content(t("known trains")).addTo(win);
|
||||
Tag list = new Tag("ul");
|
||||
for (Train train : trains.values()) {
|
||||
train.link("li").addTo(list);
|
||||
}
|
||||
list.addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
String result = (name != null ? name : locos.firstElement().name());
|
||||
@@ -242,13 +255,14 @@ public class Train {
|
||||
new Tag("li").content(t("Current location: {}",block)).addTo(list);
|
||||
new Tag("li").content(t("Direction: heading {}",direction)).addTo(list);
|
||||
|
||||
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_START+"')").content(t("start")).addTo(list).addTo(window);
|
||||
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_START+"')").content(t("start")).addTo(list);
|
||||
if (autopilot == null) {
|
||||
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_AUTO+"')").content(t("auto")).addTo(list).addTo(window);
|
||||
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_AUTO+"')").content(t("auto")).addTo(list);
|
||||
} else {
|
||||
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_STOP+"')").content(t("stop")).addTo(list).addTo(window);
|
||||
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_STOP+"')").content(t("stop")).addTo(list);
|
||||
}
|
||||
|
||||
list.addTo(window);
|
||||
return window;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ public abstract class Block extends StretchableTile{
|
||||
|
||||
if (train != null) {
|
||||
new Tag("h4").content(t("Train:")).addTo(window);
|
||||
new Tag("span").clazz("link").attr("onclick","train("+train.id+",'"+Train.MODE_SHOW+"')").content(train.name()).addTo(window);
|
||||
train.link("span").addTo(window);
|
||||
new Tag("span").clazz("link").attr("onclick","train("+train.id+",'"+Train.MODE_START+"')").content(" - "+t("start")).addTo(window);
|
||||
}
|
||||
return window;
|
||||
|
||||
Reference in New Issue
Block a user