overhauled registry
This commit is contained in:
@@ -164,6 +164,11 @@ public class Application extends BaseClass{
|
||||
return Files.probeContentType(file.toPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
throw new RuntimeException(this.getClass().getSimpleName()+".removeChild should never be called!");
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a response generated from the application to a given client
|
||||
* @param client
|
||||
|
||||
@@ -39,6 +39,7 @@ public abstract class BaseClass implements Constants{
|
||||
private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
|
||||
protected Id id = null;
|
||||
protected String notes;
|
||||
private static HashMap<Id,BaseClass> registry = new HashMap<BaseClass.Id, BaseClass>();
|
||||
|
||||
public static final Logger LOG = LoggerFactory.getLogger(BaseClass.class);
|
||||
private BaseClass parent;
|
||||
@@ -259,6 +260,17 @@ public abstract class BaseClass implements Constants{
|
||||
return form;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T get(Id id) {
|
||||
BaseClass element = registry.get(id);
|
||||
if (isNull(element)) return null;
|
||||
try {
|
||||
return (T) element;
|
||||
} catch (ClassCastException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Id id() {
|
||||
if (isNull(id)) id = new Id();
|
||||
return id;
|
||||
@@ -288,6 +300,25 @@ public abstract class BaseClass implements Constants{
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(caption.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends BaseClass> List<T> listElements(Class<T> cls) {
|
||||
ArrayList<T> result = new ArrayList<T>();
|
||||
for (BaseClass object : registry.values()) {
|
||||
if (isSet(object) && cls.isAssignableFrom(object.getClass())) {
|
||||
result.add((T) object);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public BaseClass load(JSONObject json) {
|
||||
if (json.has(ID)) {
|
||||
id = Id.from(json);
|
||||
register();
|
||||
}
|
||||
if (json.has(NOTES)) notes = json.getString(NOTES);
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String md5sum(Object o) {
|
||||
try {
|
||||
@@ -349,7 +380,19 @@ public abstract class BaseClass implements Constants{
|
||||
if (isSet(additionalProps)) props.putAll(additionalProps);
|
||||
return props;
|
||||
}
|
||||
|
||||
public BaseClass register() {
|
||||
registry.put(id(),this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BaseClass remove() {
|
||||
if (isSet(parent)) parent.removeChild(this);
|
||||
return registry.remove(id());
|
||||
}
|
||||
|
||||
protected abstract void removeChild(BaseClass child);
|
||||
|
||||
protected static String t(String txt, Object...fills) {
|
||||
return Translation.get(Application.class, txt, fills);
|
||||
}
|
||||
@@ -359,10 +402,4 @@ public abstract class BaseClass implements Constants{
|
||||
if (params.containsKey(NOTES)) notes = params.get(NOTES).trim();
|
||||
return this;
|
||||
}
|
||||
|
||||
public BaseClass load(JSONObject json) {
|
||||
if (json.has(ID)) id = Id.from(json);
|
||||
if (json.has(NOTES)) notes = json.getString(NOTES);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,18 +48,19 @@ public interface Constants {
|
||||
public static final String REALM_TRAIN = "train";
|
||||
|
||||
|
||||
public static final String BLOCK = "block";
|
||||
public static final String CONTACT = "contact";
|
||||
public static final String CONTEXT = "context";
|
||||
public static final String DEFAULT_SPEED_UNIT = "km/h";
|
||||
public static final String BLOCK = "block";
|
||||
public static final String CONTACT = "contact";
|
||||
public static final String CONTEXT = "context";
|
||||
public static final String DEFAULT_SPEED_UNIT = "km/h";
|
||||
public static final String DEFAULT_LENGTH_UNIT = "mm";
|
||||
public static final String DISABLED = "disabled";
|
||||
public static final String GITHUB_URL = "https://github.com/srsoftware-de/Web4Rail";
|
||||
public static final String ID = "id";
|
||||
public static final String NBSP = " ";
|
||||
public static final String NOTES = "notes";
|
||||
public static final String PORT = "port";
|
||||
public static final String RELAY = "relay";
|
||||
public static final String TYPE = "type";
|
||||
public static final Charset UTF8 = StandardCharsets.UTF_8;
|
||||
public static final String DISABLED = "disabled";
|
||||
public static final String GITHUB_URL = "https://github.com/srsoftware-de/Web4Rail";
|
||||
public static final String ID = "id";
|
||||
public static final String NBSP = " ";
|
||||
public static final String NOTES = "notes";
|
||||
public static final String PARENT = "parent";
|
||||
public static final String PORT = "port";
|
||||
public static final String RELAY = "relay";
|
||||
public static final String TYPE = "type";
|
||||
public static final Charset UTF8 = StandardCharsets.UTF_8;
|
||||
}
|
||||
|
||||
@@ -109,4 +109,9 @@ public class PathFinder extends BaseClass{
|
||||
|
||||
return selectetRoute;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,10 @@ import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.SortedSet;
|
||||
import java.util.Stack;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.json.JSONArray;
|
||||
@@ -27,7 +24,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.actions.Action;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
@@ -61,7 +57,6 @@ import de.srsoftware.web4rail.tiles.EndW;
|
||||
import de.srsoftware.web4rail.tiles.Eraser;
|
||||
import de.srsoftware.web4rail.tiles.Relay;
|
||||
import de.srsoftware.web4rail.tiles.Shadow;
|
||||
import de.srsoftware.web4rail.tiles.Signal;
|
||||
import de.srsoftware.web4rail.tiles.SignalE;
|
||||
import de.srsoftware.web4rail.tiles.SignalN;
|
||||
import de.srsoftware.web4rail.tiles.SignalS;
|
||||
@@ -140,10 +135,6 @@ public class Plan extends BaseClass{
|
||||
private static final String SPEED_UNIT = "speed_unit";
|
||||
private static final String LENGTH_UNIT = "length_unit";
|
||||
|
||||
public HashMap<Id,Tile> tiles = new HashMap<Id,Tile>(); // The list of tiles of this plan, i.e. the Track layout
|
||||
private HashSet<Block> blocks = new HashSet<Block>(); // the list of tiles, that are blocks
|
||||
private HashSet<Signal> signals = new HashSet<Signal>(); // the list of tiles, that are signals
|
||||
private HashMap<Id, Route> routes = new HashMap<Id, Route>(); // the list of routes of the track layout
|
||||
private ControlUnit controlUnit = new ControlUnit(this); // the control unit, to which the plan is connected
|
||||
private Contact learningContact;
|
||||
|
||||
@@ -241,8 +232,11 @@ public class Plan extends BaseClass{
|
||||
Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
|
||||
if (tile instanceof Eraser) {
|
||||
Tile erased = get(Tile.id(x,y),true);
|
||||
remove(erased);
|
||||
return erased == null ? null : t("Removed {}.",erased);
|
||||
if (isSet(erased)) {
|
||||
erased.remove();
|
||||
return t("Removed {}.",erased);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
//if (configJson != null) tile.configure(new JSONObject(configJson));
|
||||
set(x, y, tile);
|
||||
@@ -255,7 +249,7 @@ public class Plan extends BaseClass{
|
||||
*/
|
||||
private String analyze() {
|
||||
Vector<Route> routes = new Vector<Route>();
|
||||
for (Block block : blocks) {
|
||||
for (Block block : BaseClass.listElements(Block.class)) {
|
||||
if (block.name.equals("Huhu")) {
|
||||
System.err.println("Here we go!");
|
||||
}
|
||||
@@ -263,18 +257,11 @@ public class Plan extends BaseClass{
|
||||
routes.addAll(follow(new Route().begin(block,con.from.inverse()),con));
|
||||
}
|
||||
}
|
||||
for (Tile tile : tiles.values()) tile.routes().clear();
|
||||
for (Tile tile : BaseClass.listElements(Tile.class)) tile.routes().clear();
|
||||
for (Route route : routes) registerRoute(route.complete());
|
||||
return t("Found {} routes.",routes.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the list of blocks known to the plan, ordered by name
|
||||
*/
|
||||
public Collection<Block> blocks() {
|
||||
return new TreeSet<Block>(blocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* calls tile.click()
|
||||
* @param tile
|
||||
@@ -342,7 +329,7 @@ public class Plan extends BaseClass{
|
||||
*/
|
||||
public Tile get(Id tileId,boolean resolveShadows) {
|
||||
if (isNull(tileId)) return null;
|
||||
Tile tile = tiles.get(tileId);
|
||||
Tile tile = BaseClass.get(tileId);
|
||||
if (resolveShadows && tile instanceof Shadow) tile = ((Shadow)tile).overlay();
|
||||
return tile;
|
||||
}
|
||||
@@ -391,7 +378,7 @@ public class Plan extends BaseClass{
|
||||
*/
|
||||
public Page html() throws IOException {
|
||||
Page page = new Page().append("<div id=\"plan\"><div id=\"scroll\">");
|
||||
for (Tile tile: tiles.values()) {
|
||||
for (Tile tile: BaseClass.listElements(Tile.class)) {
|
||||
if (tile == null) continue;
|
||||
page.append("\t\t"+tile.tag(null)+"\n");
|
||||
}
|
||||
@@ -570,7 +557,7 @@ public class Plan extends BaseClass{
|
||||
tile = stack.pop();
|
||||
if (!(tile instanceof Shadow)) {
|
||||
LOG.debug("altering position of {}",tile);
|
||||
remove(tile);
|
||||
tile.remove();
|
||||
set(tile.x+xstep,tile.y+ystep,tile);
|
||||
}
|
||||
}
|
||||
@@ -628,7 +615,7 @@ public class Plan extends BaseClass{
|
||||
new Tag("h4").content(t("turnout properties")).addTo(win);
|
||||
Table table = new Table();
|
||||
table.addHead(t("Address"),t("Relay/Turnout"));
|
||||
tiles.values()
|
||||
BaseClass.listElements(Tile.class)
|
||||
.stream()
|
||||
.filter(tile -> tile instanceof Device )
|
||||
.map(tile -> (Device) tile)
|
||||
@@ -658,38 +645,19 @@ public class Plan extends BaseClass{
|
||||
*/
|
||||
Route registerRoute(Route newRoute) {
|
||||
newRoute.path().stream().filter(Tile::isSet).forEach(tile -> tile.add(newRoute));
|
||||
Id newRouteId = newRoute.id();
|
||||
Route existingRoute = routes.get(newRouteId);
|
||||
Route existingRoute = BaseClass.get(newRoute.id());
|
||||
if (isSet(existingRoute)) newRoute.addPropertiesFrom(existingRoute);
|
||||
routes.put(newRouteId, newRoute);
|
||||
newRoute.register();
|
||||
return newRoute;
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a tile from the track layout
|
||||
* @param tile
|
||||
*/
|
||||
private void remove(Tile tile) {
|
||||
if (isNull(tile)) return;
|
||||
removeTile(tile.x,tile.y);
|
||||
if (tile instanceof Block) blocks.remove(tile);
|
||||
for (int i=1; i<tile.width(); i++) removeTile(tile.x+i, tile.y); // remove shadow tiles
|
||||
for (int i=1; i<tile.height(); i++) removeTile(tile.x, tile.y+i); // remove shadow tiles
|
||||
if (tile != null) stream("remove "+tile.id());
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a route from the track layout
|
||||
* @param route
|
||||
* @return
|
||||
*/
|
||||
public String remove(Route route) {
|
||||
for (Tile tile : route.path()) tile.remove(route);
|
||||
for (Train train : Train.list()) {
|
||||
if (train.route == route) train.route = null;
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child instanceof Tile) {
|
||||
Tile tile = (Tile) child;
|
||||
for (int i=1; i<tile.width(); i++) removeTile(tile.x+i, tile.y); // remove shadow tiles
|
||||
for (int i=1; i<tile.height(); i++) removeTile(tile.x, tile.y+i); // remove shadow tiles
|
||||
}
|
||||
routes.remove(route.id());
|
||||
return t("Removed {}.",route);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -698,16 +666,8 @@ public class Plan extends BaseClass{
|
||||
* @param y
|
||||
*/
|
||||
private void removeTile(int x, int y) {
|
||||
LOG.debug("removed {} from tile list",tiles.remove(Tile.id(x, y)));
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a specific route from the list of routes assigned to this plan
|
||||
* @param routeId the id of the route requestd
|
||||
* @return
|
||||
*/
|
||||
public Route route(Id routeId) {
|
||||
return routes.get(routeId);
|
||||
Tile tile = BaseClass.get(Tile.id(x, y));
|
||||
if (isSet(tile)) tile.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -719,9 +679,10 @@ public class Plan extends BaseClass{
|
||||
private String saveTo(String name) throws IOException {
|
||||
if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");
|
||||
Car.saveAll(name+".cars");
|
||||
Tile.saveAll(tiles,name+".plan");
|
||||
Train.saveAll(name+".trains"); // refers to cars, blocks
|
||||
Route.saveAll(routes.values(),name+".routes"); // refers to tiles
|
||||
|
||||
Tile.saveAll(name+".plan");
|
||||
Train.saveAll(name+".trains"); // refers to cars, blocks
|
||||
Route.saveAll(name+".routes"); // refers to tiles
|
||||
controlUnit.save(name+".cu");
|
||||
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(name+".plan"));
|
||||
@@ -733,8 +694,8 @@ public class Plan extends BaseClass{
|
||||
|
||||
public JSONObject json() {
|
||||
JSONArray jTiles = new JSONArray();
|
||||
tiles.values().stream()
|
||||
.filter(tile -> isSet(tile))
|
||||
BaseClass.listElements(Tile.class)
|
||||
.stream()
|
||||
.filter(tile -> !(tile instanceof Shadow))
|
||||
.map(tile -> tile.json())
|
||||
.forEach(jTiles::put);
|
||||
@@ -754,8 +715,6 @@ public class Plan extends BaseClass{
|
||||
*/
|
||||
public void set(int x,int y,Tile tile) throws IOException {
|
||||
if (tile == null) return;
|
||||
if (tile instanceof Block) blocks.add((Block) tile);
|
||||
if (tile instanceof Signal) signals .add((Signal) tile);
|
||||
for (int i=1; i<tile.width(); i++) set(x+i,y,new Shadow(tile));
|
||||
for (int i=1; i<tile.height(); i++) set(x,y+i,new Shadow(tile));
|
||||
setIntern(x,y,tile);
|
||||
@@ -769,8 +728,7 @@ public class Plan extends BaseClass{
|
||||
* @param tile
|
||||
*/
|
||||
private void setIntern(int x, int y, Tile tile) {
|
||||
tile.position(x, y);
|
||||
tiles.put(tile.id(),tile);
|
||||
tile.position(x, y).register();
|
||||
}
|
||||
|
||||
public void sensor(int addr, boolean active) {
|
||||
@@ -788,34 +746,6 @@ public class Plan extends BaseClass{
|
||||
|
||||
if (isSet(contact)) contact.activate(active);
|
||||
}
|
||||
|
||||
/**
|
||||
* shows the properties of an entity specified in the params.context value
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public Tag showContext(HashMap<String, String> params) {
|
||||
String[] parts = params.get(CONTEXT).split(":");
|
||||
String realm = parts[0];
|
||||
Id id = parts.length>1 ? new Id(parts[1]) : null;
|
||||
switch (realm) {
|
||||
case REALM_ROUTE:
|
||||
return route(id).properties();
|
||||
case REALM_CONTACT:
|
||||
case REALM_PLAN:
|
||||
Tile tile = get(id, false);
|
||||
return isNull(tile) ? null : tile.properties();
|
||||
case REALM_ACTIONS:
|
||||
Action action = Action.get(id);
|
||||
return (isSet(action)) ? action.properties() : null;
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Signal> signals() {
|
||||
return new TreeSet<Signal>(signals);
|
||||
}
|
||||
|
||||
/**
|
||||
* sends some data to the clients
|
||||
|
||||
@@ -28,6 +28,11 @@ public class Range extends BaseClass{
|
||||
if (max - min == 0) return max - min;
|
||||
return min + random.nextInt(max - min);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -4,7 +4,6 @@ import java.io.BufferedWriter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -57,7 +56,6 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
private static final String ACTION_LISTS = "action_lists";
|
||||
private static final String BRAKE_TIMES = "brake_times";
|
||||
private static final String CONDITIONS = "conditions";
|
||||
private static final String DROP_CONDITION = "drop_condition";
|
||||
private static final String END_DIRECTION = "direction_end";
|
||||
private static final String ROUTES = "routes";
|
||||
private static final String SETUP_ACTIONS = "setup_actions";
|
||||
@@ -141,7 +139,6 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
private boolean disabled = false;
|
||||
private Block endBlock = null;
|
||||
public Direction endDirection;
|
||||
private Id id;
|
||||
private Vector<Tile> path;
|
||||
private Vector<Signal> signals;
|
||||
public Train train;
|
||||
@@ -151,7 +148,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
private ActionList startActions;
|
||||
private Block startBlock = null;
|
||||
public Direction startDirection;
|
||||
private HashSet<Contact> triggeredContacts = new HashSet<>();
|
||||
private HashSet<Contact> triggeredContacts = new HashSet<>();
|
||||
|
||||
public Route() {
|
||||
setupActions = new ActionList(this);
|
||||
@@ -165,23 +162,17 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Object action(HashMap<String, String> params) throws IOException {
|
||||
Route route = plan.route(Id.from(params));
|
||||
Route route = BaseClass.get(Id.from(params));
|
||||
if (isNull(route)) return t("Unknown route: {}",params.get(ID));
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_DROP:
|
||||
String message = plan.remove(route);
|
||||
String context = params.get(CONTEXT);
|
||||
if (isSet(context)) {
|
||||
plan.stream(message);
|
||||
return plan.showContext(params);
|
||||
}
|
||||
return message;
|
||||
route.remove();
|
||||
return t("Removed {}.",route);
|
||||
|
||||
case ACTION_PROPS:
|
||||
return route.properties();
|
||||
case ACTION_UPDATE:
|
||||
return route.update(params,plan);
|
||||
case DROP_CONDITION:
|
||||
return route.dropCodition(params);
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
@@ -419,12 +410,6 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
return disabled;
|
||||
}
|
||||
|
||||
private Object dropCodition(HashMap<String, String> params) {
|
||||
Id condId = Id.from(params,REALM_CONDITION);
|
||||
if (isSet(condId)) conditions.removeById(condId);
|
||||
return properties();
|
||||
}
|
||||
|
||||
public Block endBlock() {
|
||||
return endBlock;
|
||||
}
|
||||
@@ -435,7 +420,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
Tile lastTile = path.lastElement();
|
||||
if (lastTile instanceof Contact) {
|
||||
lastTile.set(null);
|
||||
if (isSet(train)) train.removeFromTrace(lastTile);
|
||||
if (isSet(train)) train.removeChild(lastTile);
|
||||
}
|
||||
if (isSet(train)) {
|
||||
train.set(endBlock);
|
||||
@@ -508,7 +493,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
json.put(BRAKE_TIMES, brakeTimes);
|
||||
|
||||
if (!conditions.isEmpty()) json.put(CONDITIONS, conditions.json());
|
||||
if (!conditions.isEmpty()) json.put(CONDITIONS, conditions.jsonArray());
|
||||
|
||||
JSONArray jTriggers = new JSONArray();
|
||||
for (Entry<String, ActionList> entry : triggers.entrySet()) {
|
||||
@@ -652,20 +637,32 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
|
||||
preForm.add(conditions.list(t("Route will only be available, if all conditions are fulfilled.")));
|
||||
preForm.add(contactsAndActions());
|
||||
|
||||
formInputs.add(t("Name"),new Input(NAME, name()));
|
||||
formInputs.add(t("State"),new Checkbox(DISABLED, t("disabled"), disabled));
|
||||
|
||||
postForm.add(basicProperties());
|
||||
if (!turnouts.isEmpty()) postForm.add(turnouts());
|
||||
preForm.add(conditions.list(t("Route will only be available, if all conditions are fulfilled.")));
|
||||
preForm.add(contactsAndActions());
|
||||
postForm.add(brakeTimes());
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
public Route remove(Condition condition) {
|
||||
conditions.remove(condition);
|
||||
return this;
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
conditions.remove(child);
|
||||
contacts.remove(child);
|
||||
if (child == endBlock) endBlock = null;
|
||||
path.remove(child);
|
||||
signals.remove(child);
|
||||
if (child == train) train = null;
|
||||
for (ActionList list : triggers.values()) list.removeChild(child);
|
||||
turnouts.remove(child);
|
||||
setupActions.removeChild(child);
|
||||
startActions.removeChild(child);
|
||||
if (child == startBlock) startBlock = null;
|
||||
triggeredContacts.remove(child);
|
||||
}
|
||||
|
||||
public boolean reset() {
|
||||
@@ -674,7 +671,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
Tile lastTile = path.lastElement();
|
||||
if (lastTile instanceof Contact) {
|
||||
lastTile.set(null);
|
||||
if (isSet(train)) train.removeFromTrace(lastTile);
|
||||
if (isSet(train)) train.removeChild(lastTile);
|
||||
}
|
||||
if (isSet(train)) {
|
||||
train.set(startBlock);
|
||||
@@ -686,10 +683,11 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void saveAll(Collection<Route> routes, String filename) throws IOException {
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
file.write("{\""+ROUTES+"\":[\n");
|
||||
int count = 0;
|
||||
List<Route> routes = BaseClass.listElements(Route.class);
|
||||
for (Route route : routes) {
|
||||
file.write(route.json().toString());
|
||||
if (++count < routes.size()) file.write(",");
|
||||
@@ -777,13 +775,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
if (isSet(condition)) {
|
||||
condition.parent(this);
|
||||
conditions.add(condition);
|
||||
return properties();
|
||||
}
|
||||
String message = t("{} updated.",this);
|
||||
if (params.containsKey(CONTEXT)) {
|
||||
plan.stream(message);
|
||||
return plan.showContext(params);
|
||||
}
|
||||
return message;
|
||||
return properties();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
|
||||
@@ -103,6 +104,10 @@ public abstract class Action extends BaseClass {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Window properties() { // goes up to first ancestor, which is not an Action
|
||||
return parent().properties();
|
||||
}
|
||||
|
||||
public static Tag selector() {
|
||||
Select select = new Select(TYPE);
|
||||
@@ -129,13 +134,6 @@ public abstract class Action extends BaseClass {
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
super.update(params);
|
||||
BaseClass parent = parent();
|
||||
if (isNull(parent)) return properties();
|
||||
if (parent instanceof ActionList) {
|
||||
ActionList al = (ActionList) parent;
|
||||
return al.parent().properties();
|
||||
}
|
||||
return parent.properties();
|
||||
|
||||
return properties();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
@@ -18,10 +19,10 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class ActionList extends Action{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
||||
public class ActionList extends Action implements Iterable<Action>{
|
||||
static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
||||
|
||||
private Vector<Action> actions;
|
||||
protected Vector<Action> actions;
|
||||
|
||||
public ActionList(BaseClass parent) {
|
||||
super(parent);
|
||||
@@ -51,7 +52,7 @@ public class ActionList extends Action{
|
||||
Action action = Action.create(type,this);
|
||||
if (action instanceof Action) {
|
||||
add(action);
|
||||
return plan.showContext(params);
|
||||
return parent().properties();
|
||||
}
|
||||
actionTypeForm(win,context);
|
||||
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win);
|
||||
@@ -80,15 +81,11 @@ public class ActionList extends Action{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return actions.isEmpty();
|
||||
}
|
||||
|
||||
public boolean drop(Action action) {
|
||||
return actions.remove(action);
|
||||
}
|
||||
|
||||
|
||||
public boolean fire(Context context) {
|
||||
if (!isEmpty()) LOG.debug(t("Firing {}"),this);
|
||||
for (Action action : actions) {
|
||||
@@ -97,6 +94,22 @@ public class ActionList extends Action{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Action> iterator() {
|
||||
return actions.iterator();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return actions.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
String cls = getClass().getSimpleName();
|
||||
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
|
||||
}
|
||||
|
||||
public JSONArray jsonArray() {
|
||||
JSONArray result = new JSONArray();
|
||||
for (Action action : actions) result.put(action.json());
|
||||
@@ -104,7 +117,7 @@ public class ActionList extends Action{
|
||||
}
|
||||
|
||||
public Tag list() {
|
||||
Button button = button(t("Add action"), contextAction(ACTION_ADD_ACTION));
|
||||
Button button = button(t("add action"), contextAction(ACTION_ADD_ACTION));
|
||||
Tag span = new Tag("span");
|
||||
button.addTo(span);
|
||||
|
||||
@@ -116,6 +129,7 @@ public class ActionList extends Action{
|
||||
if (first) {
|
||||
first = false;
|
||||
} else action.button("↑", contextAction(ACTION_MOVE)).addTo(item.content(NBSP));
|
||||
if (action instanceof ActionList) ((ActionList) action).list().addTo(item);
|
||||
item.addTo(list);
|
||||
}
|
||||
list.addTo(span);
|
||||
@@ -160,9 +174,9 @@ public class ActionList extends Action{
|
||||
if (isNull(actionList)) return t("Id ({}) does not belong to ActionList!",actionId);
|
||||
return actionList.addActionForm(params,plan);
|
||||
case ACTION_DROP:
|
||||
return action.drop() ? action.parent().properties() : t("No action with id {} found!",actionId);
|
||||
return action.drop() ? action.properties() : t("No action with id {} found!",actionId);
|
||||
case ACTION_MOVE:
|
||||
return action.moveUp() ? action.parent().properties() : t("No action with id {} found!",actionId);
|
||||
return action.moveUp() ? action.properties() : t("No action with id {} found!",actionId);
|
||||
case ACTION_PROPS:
|
||||
return action.properties();
|
||||
case ACTION_UPDATE:
|
||||
@@ -178,4 +192,9 @@ public class ActionList extends Action{
|
||||
preForm.add(fieldset);
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeChild(BaseClass child) {
|
||||
actions.remove(child);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,8 @@ public class BrakeCancel extends Action {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,5 +15,9 @@ public class BrakeStart extends Action {
|
||||
LOG.debug("Started brake process...");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,8 @@ public class BrakeStop extends Action {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,22 +17,16 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class ConditionalAction extends Action {
|
||||
public class ConditionalAction extends ActionList {
|
||||
|
||||
private static final String CONDITIONS = "conditions";
|
||||
private static final String ACTIONS = "actions";
|
||||
private Vector<Condition> conditions = new Vector<Condition>();
|
||||
private ActionList actions;
|
||||
|
||||
public ConditionalAction(BaseClass parent) {
|
||||
super(parent);
|
||||
actions = new ActionList(this);
|
||||
}
|
||||
|
||||
public ActionList children() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
private StringBuffer conditions() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i<conditions.size(); i++) {
|
||||
@@ -77,7 +71,7 @@ public class ConditionalAction extends Action {
|
||||
for (Condition condition : conditions) {
|
||||
if (!condition.fulfilledBy(context)) return true;
|
||||
}
|
||||
return actions.fire(context.clone()); // actions, that happen within the conditional action list must not modify the global context.
|
||||
return super.fire(context.clone()); // actions, that happen within the conditional action list must not modify the global context.
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,7 +80,7 @@ public class ConditionalAction extends Action {
|
||||
JSONArray conditions = new JSONArray();
|
||||
for (Condition condition : this.conditions) conditions.put(condition.json());
|
||||
json.put(CONDITIONS, conditions);
|
||||
json.put(ACTIONS, actions.jsonArray());
|
||||
json.put(ACTIONS, super.jsonArray());
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -103,7 +97,7 @@ public class ConditionalAction extends Action {
|
||||
}
|
||||
}
|
||||
}
|
||||
actions.load(json.getJSONArray(ACTIONS));
|
||||
super.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -111,15 +105,15 @@ public class ConditionalAction extends Action {
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
preForm.add(conditionForm());
|
||||
Fieldset fieldset = new Fieldset(t("Actions"));
|
||||
actions.list().addTo(fieldset);
|
||||
list().addTo(fieldset);
|
||||
postForm.add(fieldset);
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
|
||||
}
|
||||
|
||||
public ConditionalAction remove(Condition condition) {
|
||||
conditions.remove(condition);
|
||||
return this;
|
||||
@Override
|
||||
public void removeChild(BaseClass child) {
|
||||
conditions.remove(child);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,22 +11,16 @@ import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class DelayedAction extends Action {
|
||||
public class DelayedAction extends ActionList {
|
||||
|
||||
|
||||
private static final String ACTIONS = "actions";
|
||||
public static final String DELAY = "delay";
|
||||
private static final int DEFAULT_DELAY = 1000;
|
||||
private int delay = DEFAULT_DELAY;
|
||||
private ActionList actions;
|
||||
|
||||
public DelayedAction(BaseClass parent) {
|
||||
super(parent);
|
||||
actions = new ActionList(this);
|
||||
}
|
||||
|
||||
public ActionList children() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public boolean equals(DelayedAction other) {
|
||||
@@ -43,7 +37,7 @@ public class DelayedAction extends Action {
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warn("Interrupted Exception thrown while waiting:",e);
|
||||
}
|
||||
actions.fire(context);
|
||||
DelayedAction.super.fire(context);
|
||||
};
|
||||
}.start();
|
||||
return true;
|
||||
@@ -53,14 +47,14 @@ public class DelayedAction extends Action {
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(DELAY, delay);
|
||||
json.put(ACTIONS, actions.jsonArray());
|
||||
json.put(ACTIONS, jsonArray());
|
||||
return json;
|
||||
}
|
||||
|
||||
public DelayedAction load(JSONObject json) {
|
||||
super.load(json);
|
||||
delay = json.getInt(DELAY);
|
||||
if (json.has(ACTIONS)) actions.load(json.getJSONArray(ACTIONS));
|
||||
if (json.has(ACTIONS)) super.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -68,7 +62,7 @@ public class DelayedAction extends Action {
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Delay"),new Input(DELAY,delay).numeric().addTo(new Tag("span")).content(NBSP+"ms"));
|
||||
Fieldset fieldset = new Fieldset(t("Actions"));
|
||||
actions.list().addTo(fieldset);
|
||||
list().addTo(fieldset);
|
||||
postForm.add(fieldset);
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,11 @@ public class DetermineTrainInBlock extends Action {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == block) block = null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return isSet(block) ? t("Determine, which train is in {}",block) : t("[Click here to select block!]");
|
||||
};
|
||||
|
||||
@@ -15,4 +15,9 @@ public class FinishRoute extends Action {
|
||||
if (isSet(route)) route.finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,5 +29,9 @@ public class PreserveRoute extends Action {
|
||||
train.reserveNext();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,11 @@ public class SendCommand extends Action{
|
||||
formInputs.add(t("Command to send to control unit"),new Input(COMMAND, command));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -12,11 +12,11 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
||||
|
||||
public class SetContextTrain extends Action {
|
||||
|
||||
private Train train = null;
|
||||
|
||||
public SetContextTrain(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
private Train train = null;
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
@@ -55,6 +55,11 @@ public class SetContextTrain extends Action {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == train) train = null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return isSet(train) ? t("Set {} as context",train) : "["+t("Click here to select train!")+"]";
|
||||
};
|
||||
|
||||
@@ -13,12 +13,12 @@ import de.srsoftware.web4rail.tiles.TextDisplay;
|
||||
|
||||
public class SetDisplayText extends TextAction{
|
||||
|
||||
private TextDisplay display;
|
||||
private static final String DISPLAY = "display";
|
||||
|
||||
public SetDisplayText(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
private TextDisplay display;
|
||||
private static final String DISPLAY = "display";
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
@@ -51,6 +51,11 @@ public class SetDisplayText extends TextAction{
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == display) display = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select display"),TextDisplay.selector(display, null));
|
||||
|
||||
@@ -68,6 +68,11 @@ public class SetPower extends Action{
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (pc) {
|
||||
|
||||
@@ -61,6 +61,11 @@ public class SetRelay extends Action {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == relay) relay = null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if (isNull(relay)) return "["+t("click here to setup relay")+"]";
|
||||
return t("Set {} to {}",relay,state?relay.stateLabelA:relay.stateLabelB);
|
||||
|
||||
@@ -51,7 +51,7 @@ public class SetSignal extends Action {
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
Select select = new Select(SIGNAL);
|
||||
for (Signal signal : plan.signals()) {
|
||||
for (Signal signal : BaseClass.listElements(Signal.class)) {
|
||||
Tag option = select.addOption(signal.id(),signal.title());
|
||||
if (signal == this.signal) option.attr("selected", "selected");
|
||||
}
|
||||
@@ -66,6 +66,11 @@ public class SetSignal extends Action {
|
||||
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == signal) signal = null;
|
||||
}
|
||||
|
||||
public SetSignal set(Signal sig) {
|
||||
signal = sig;
|
||||
|
||||
@@ -46,6 +46,11 @@ public class SetSpeed extends Action{
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("Set speed to {} {}",speed,speedUnit);
|
||||
|
||||
@@ -21,6 +21,11 @@ public class ShowText extends TextAction{
|
||||
return new Label(t("Text to display on clients:")+NBSP);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("Display \"{}\" on clients.",text);
|
||||
|
||||
@@ -14,4 +14,9 @@ public class StopAllTrains extends Action {
|
||||
Train.list().forEach(train -> train.stopNow());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,8 @@ public class StopAuto extends Action {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,11 @@ public class TriggerContact extends Action {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == contact) contact = null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return isSet(contact) ? t("Trigger {}",contact) : "["+t("click here to setup contact")+"]";
|
||||
};
|
||||
|
||||
@@ -16,4 +16,9 @@ public class TurnTrain extends Action{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
@@ -13,6 +14,11 @@ public class BlockFree extends Condition {
|
||||
|
||||
private static final String BLOCK = Block.class.getSimpleName();
|
||||
private Block block;
|
||||
|
||||
private BlockFree block(Block block) {
|
||||
this.block = block;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
@@ -35,17 +41,17 @@ public class BlockFree extends Condition {
|
||||
formInputs.add(t("Select block"), Block.selector(block, null));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == block) block = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (block == null) return t("[Click here to select block!]");
|
||||
return t(inverted ? "Block {} is occupied":"Block {} is free",block);
|
||||
}
|
||||
|
||||
private BlockFree block(Block block) {
|
||||
this.block = block;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,9 +15,7 @@ import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.ConditionalAction;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
@@ -27,9 +25,7 @@ public abstract class Condition extends BaseClass {
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Condition.class);
|
||||
private static final String INVERTED = "inverted";
|
||||
private static final String PREFIX = Condition.class.getPackageName();
|
||||
private static HashMap<Id, Condition> conditions = new HashMap<Id, Condition>();
|
||||
public boolean inverted = false;
|
||||
private Object parent;
|
||||
|
||||
public Condition() {
|
||||
this(new Id());
|
||||
@@ -37,7 +33,7 @@ public abstract class Condition extends BaseClass {
|
||||
|
||||
public Condition(Id id) {
|
||||
this.id = id;
|
||||
conditions.put(id, this);
|
||||
register();
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params,Plan plan) {
|
||||
@@ -47,18 +43,18 @@ public abstract class Condition extends BaseClass {
|
||||
Id cid = Id.from(params);
|
||||
|
||||
if (isSet(cid)) {
|
||||
Condition condition = conditions.get(cid);
|
||||
if (condition == null) return t("No condition with id {}!",cid);
|
||||
Condition condition = BaseClass.get(cid);
|
||||
if (isNull(condition)) return t("No condition with id {}!",cid);
|
||||
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return condition.properties();
|
||||
case ACTION_UPDATE:
|
||||
condition.update(params);
|
||||
return plan.showContext(params);
|
||||
return condition.parent().properties();
|
||||
case ACTION_DROP:
|
||||
condition.drop();
|
||||
return plan.showContext(params);
|
||||
condition.remove();
|
||||
return condition.parent().properties();
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
}
|
||||
@@ -74,27 +70,18 @@ public abstract class Condition extends BaseClass {
|
||||
|
||||
private static Object addCondition(HashMap<String, String> params) {
|
||||
String type = params.get(REALM_CONDITION);
|
||||
String context = params.get(CONTEXT);
|
||||
if (isNull(type)) return t("No type supplied to addCondition!");
|
||||
if (isNull(context)) return t("No context supplied to addCondtion!");
|
||||
|
||||
Id parentId = Id.from(params, PARENT);
|
||||
if (isNull(parentId)) return t("No parent id supplied to addCondition");
|
||||
|
||||
BaseClass parent = BaseClass.get(parentId);
|
||||
if (isNull(parent)) return t("No condition with id {} found!",parentId);
|
||||
|
||||
Condition condition = Condition.create(type);
|
||||
if (isNull(condition)) return t("Unknown type \"{}\" of condition!",type);
|
||||
String[] parts = context.split(":");
|
||||
Id contextId = new Id(parts[1]);
|
||||
String realm = parts[0];
|
||||
switch (realm) {
|
||||
case REALM_ROUTE:
|
||||
Route route = plan.route(contextId);
|
||||
if (isNull(route)) return t("Unknown route: {}",contextId);
|
||||
route.add(condition);
|
||||
return route.properties();
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return t("Cannot handle context of type {} in addCondition!");
|
||||
return condition.parent(parent).properties();
|
||||
}
|
||||
|
||||
public static Condition create(String type) {
|
||||
@@ -107,18 +94,6 @@ public abstract class Condition extends BaseClass {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void drop() {
|
||||
if (parent instanceof ConditionalAction) {
|
||||
ConditionalAction ca = (ConditionalAction) parent;
|
||||
ca.remove(this);
|
||||
}
|
||||
if (parent instanceof Route) {
|
||||
Route route = (Route) parent;
|
||||
route.remove(this);
|
||||
}
|
||||
conditions.remove(this.id());
|
||||
}
|
||||
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
|
||||
public JSONObject json() {
|
||||
@@ -186,11 +161,6 @@ public abstract class Condition extends BaseClass {
|
||||
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
inverted = "on".equals(params.get(INVERTED));
|
||||
return t("updated {}.",this);
|
||||
}
|
||||
|
||||
public Condition parent(Object parent) {
|
||||
this.parent = parent;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,57 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass.Context;
|
||||
import de.srsoftware.web4rail.BaseClass.Id;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class ConditionList extends Vector<Condition> implements Constants{
|
||||
public class ConditionList extends Condition implements Iterable<Condition>{
|
||||
|
||||
private static final long serialVersionUID = 5826717120751473807L;
|
||||
private Vector<Condition> conditions = new Vector<Condition>();
|
||||
|
||||
public void add(Condition condition) {
|
||||
conditions.add(condition);
|
||||
}
|
||||
|
||||
public void addAll(ConditionList conditions) {
|
||||
this.conditions.addAll(conditions.conditions);
|
||||
}
|
||||
|
||||
|
||||
public boolean fulfilledBy(Context context) {
|
||||
for (Condition condition : this) {
|
||||
for (Condition condition : conditions) {
|
||||
if (!condition.fulfilledBy(context)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return conditions.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Condition> iterator() {
|
||||
return conditions.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
String cls = getClass().getSimpleName();
|
||||
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
|
||||
}
|
||||
|
||||
public JSONArray json() {
|
||||
public JSONArray jsonArray() {
|
||||
JSONArray json = new JSONArray();
|
||||
for (Condition condition : this) json.put(condition.json());
|
||||
for (Condition condition : conditions) json.put(condition.json());
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -42,39 +64,43 @@ public class ConditionList extends Vector<Condition> implements Constants{
|
||||
if (caption != null) new Tag("p").content(caption).addTo(fieldset);
|
||||
Tag list = new Tag("ul");
|
||||
newConditionForm().addTo(new Tag("li")).addTo(list);
|
||||
forEach(condition -> condition.link("li", condition).addTo(list));
|
||||
conditions.forEach(condition -> condition.link("li", condition).addTo(list));
|
||||
list.addTo(fieldset);
|
||||
return fieldset;
|
||||
}
|
||||
|
||||
public void load(JSONArray arr) {
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
if (condition != null) {
|
||||
condition.parent(this);
|
||||
conditions.add(condition.load(json));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Form newConditionForm() {
|
||||
Form form = new Form("add-condition-form");
|
||||
new Input(REALM, REALM_CONDITION).hideIn(form);
|
||||
new Input(ACTION,ACTION_ADD).hideIn(form);
|
||||
// new Input(CONTEXT,context).hideIn(form); TODO: add context
|
||||
new Input(PARENT,id());
|
||||
Condition.selector().addTo(form);
|
||||
new Button(t("Add condition"), form).addTo(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
public void load(JSONArray arr) {
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
if (condition != null) add(condition.parent(this).load(json));
|
||||
}
|
||||
|
||||
public boolean remove(Object condition) {
|
||||
return conditions.remove(condition);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
conditions.remove(child);
|
||||
}
|
||||
|
||||
public void removeById(Id cid) {
|
||||
for (Condition condition : this) {
|
||||
if (condition.id().equals(cid)) {
|
||||
remove(condition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String t(String tx, Object...fills) {
|
||||
return Translation.get(Application.class, tx, fills);
|
||||
public Stream<Condition> stream() {
|
||||
return conditions.stream();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,19 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
|
||||
public class OrCondition extends Condition{
|
||||
public class OrCondition extends ConditionList{
|
||||
|
||||
private static final String CONDITIONS = "conditions";
|
||||
private ConditionList conditions = new ConditionList();
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
for (Condition condition : conditions) {
|
||||
for (Condition condition : this) {
|
||||
if (condition.fulfilledBy(context)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(CONDITIONS, conditions.json());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(CONDITIONS)) conditions.load(json.getJSONArray(CONDITIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
// add conditions
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return conditions.isEmpty() ? t("Click here to select conditions") : String.join(" "+t("OR")+" ", conditions.stream().map(Object::toString).collect(Collectors.toList()));
|
||||
return isEmpty() ? t("Click here to select conditions") : String.join(" "+t("OR")+" ", stream().map(Object::toString).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class PushPullTrain extends Condition {
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
return context.train().pushPull != inverted;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
@@ -37,6 +38,11 @@ public class TrainHasTag extends Condition {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (tag == null) return t("[Click to setup tag]");
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.List;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
@@ -37,7 +38,12 @@ public class TrainLength extends Condition {
|
||||
formInputs.add(t("Maximum train length"),new Input(MAX_LENGTH, maxLength).numeric());
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t(inverted ? "train is longer than {}" : "train is shorter than {}",maxLength) ;
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -36,6 +37,11 @@ public class TrainSelect extends Condition {
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == train) train = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (train == null) return t("[Click here to select train!]");
|
||||
|
||||
@@ -213,6 +213,12 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
|
||||
return super.properties(preForm,formInputs,postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == train) train = null;
|
||||
}
|
||||
|
||||
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
|
||||
@@ -77,7 +77,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private Block currentBlock,destination = null;
|
||||
LinkedList<Tile> trace = new LinkedList<Tile>();
|
||||
|
||||
|
||||
private class Autopilot extends Thread{
|
||||
boolean stop = false;
|
||||
int waitTime = 100;
|
||||
@@ -566,9 +566,15 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return t("{} stopping at next block.",this);
|
||||
} else return t("autopilot not active.");
|
||||
}
|
||||
|
||||
public void removeFromTrace(Tile tile) {
|
||||
trace.remove(tile);
|
||||
|
||||
@Override
|
||||
public void removeChild(BaseClass child) {
|
||||
if (child == route) route = null;
|
||||
if (child == currentBlock) currentBlock = null;
|
||||
if (child == destination) destination = null;
|
||||
cars.remove(child);
|
||||
locos.remove(child);
|
||||
trace.remove(child);
|
||||
}
|
||||
|
||||
public void reserveNext() {
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Range;
|
||||
@@ -233,11 +234,16 @@ public abstract class Block extends StretchableTile implements Comparable<Block>
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
public static Select selector(Block preselected,Collection<Block> exclude) {
|
||||
if (isNull(exclude)) exclude = new Vector<Block>();
|
||||
Select select = new Select(Block.class.getSimpleName());
|
||||
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
||||
for (Block block : plan.blocks()) {
|
||||
for (Block block : BaseClass.listElements(Block.class)) {
|
||||
if (exclude.contains(block)) continue;
|
||||
Tag opt = select.addOption(block.id(), block);
|
||||
if (block == preselected) opt.attr("selected", "selected");
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.Map;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
@@ -100,6 +101,12 @@ public abstract class Bridge extends Tile {
|
||||
return t("Click other bridge to connect to!");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == counterpart) counterpart = null;
|
||||
plan.place(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag tag(Map<String, Object> replacements) throws IOException {
|
||||
Tag tag = super.tag(replacements);
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.TreeMap;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.ActionList;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -165,6 +166,11 @@ public class Contact extends Tile{
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == actions) actions = null;
|
||||
}
|
||||
|
||||
public static Select selector(Contact preselect) {
|
||||
TreeMap<String,Contact> sortedSet = new TreeMap<String, Contact>(); // Map from Name to Contact
|
||||
for (Contact contact : contactsById.values()) sortedSet.put(contact.toString(), contact);
|
||||
|
||||
@@ -2,9 +2,15 @@ package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
|
||||
public abstract class Cross extends Tile {
|
||||
public abstract Map<Connector,Turnout.State> offsetConnections(Direction from);
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -31,4 +32,9 @@ public class CrossPlus extends Tile{
|
||||
public List<Direction> possibleDirections() {
|
||||
return List.of(Direction.EAST,Direction.WEST,Direction.NORTH,Direction.SOUTH);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -21,4 +22,9 @@ public class DiagES extends Tile{
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -21,4 +22,9 @@ public class DiagNE extends Tile{
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -20,4 +21,9 @@ public class DiagSW extends Tile{
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -22,4 +23,10 @@ public class DiagWN extends Tile{
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class EndE extends Tile{
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class EndN extends Tile{
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class EndS extends Tile{
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
public class EndW extends Tile{}
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class EndW extends Tile{
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
public class Eraser extends Tile {
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class Eraser extends Tile {
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.concurrent.TimeoutException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Command;
|
||||
import de.srsoftware.web4rail.Command.Reply;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
@@ -40,7 +41,6 @@ public class Relay extends Tile implements Device{
|
||||
private String name = t("Relay");
|
||||
protected boolean state = true;
|
||||
|
||||
private static final HashMap<Id,Relay> relays = new HashMap<Id, Relay>();
|
||||
public static final boolean STATE_A = true,STATE_B=false;
|
||||
private static final String LABEL_A = "label_a";
|
||||
private static final String LABEL_B = "label_b";
|
||||
@@ -125,7 +125,6 @@ public class Relay extends Tile implements Device{
|
||||
@Override
|
||||
public Tile position(int x, int y) {
|
||||
super.position(x, y);
|
||||
relays.put(id(), this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -161,6 +160,11 @@ public class Relay extends Tile implements Device{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
public Relay setLabel(boolean state, String tx) {
|
||||
if (state) {
|
||||
stateLabelA = tx;
|
||||
@@ -235,19 +239,11 @@ public class Relay extends Tile implements Device{
|
||||
return super.update(params);
|
||||
}
|
||||
|
||||
public static Collection<Relay> list() {
|
||||
return relays.values();
|
||||
}
|
||||
|
||||
public static Relay get(Id relayId) {
|
||||
return relays.get(relayId);
|
||||
}
|
||||
|
||||
public static Select selector(Relay preselected, Collection<Relay> exclude) {
|
||||
if (isNull(exclude)) exclude = new Vector<Relay>();
|
||||
Select select = new Select(Relay.class.getSimpleName());
|
||||
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
||||
for (Relay relay : Relay.list()) {
|
||||
for (Relay relay : BaseClass.listElements(Relay.class)) {
|
||||
if (exclude.contains(relay)) continue;
|
||||
Tag opt = select.addOption(relay.id, relay);
|
||||
if (relay == preselected) opt.attr("selected", "selected");
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -28,6 +29,14 @@ public class Shadow extends Tile{
|
||||
return overlay;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == overlay) {
|
||||
overlay = null;
|
||||
remove();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag tag(Map<String, Object> replacements) throws IOException {
|
||||
Tag tag = super.tag(replacements);
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.TreeSet;
|
||||
import java.util.Vector;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
|
||||
public abstract class Signal extends Tile implements Comparable<Signal>{
|
||||
@@ -37,6 +38,11 @@ public abstract class Signal extends Tile implements Comparable<Signal>{
|
||||
}
|
||||
|
||||
public abstract boolean isAffectedFrom(Direction dir);
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
public boolean state(String state) {
|
||||
this.state = state;
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -23,18 +24,23 @@ public class StraightH extends StretchableTile{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int width() {
|
||||
return stretch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Direction> possibleDirections() {
|
||||
return List.of(Direction.EAST,Direction.WEST);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String stretchType() {
|
||||
return t("Width");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int width() {
|
||||
return stretch;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
@@ -33,6 +34,11 @@ public class StraightV extends StretchableTile{
|
||||
return List.of(Direction.NORTH,Direction.SOUTH);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String stretchType() {
|
||||
return t("Height");
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.Vector;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
@@ -43,16 +44,20 @@ public class TextDisplay extends StretchableTile {
|
||||
formInputs.add(t("Text"),new Input(TEXT, text));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
public static Select selector(TextDisplay preselected,Collection<TextDisplay> exclude) {
|
||||
if (isNull(exclude)) exclude = new Vector<TextDisplay>();
|
||||
Select select = new Select(TextDisplay.class.getSimpleName());
|
||||
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
||||
for (Tile tile : plan.tiles.values()) {
|
||||
if (!(tile instanceof TextDisplay)) continue;
|
||||
if (exclude.contains(tile)) continue;
|
||||
Tag opt = select.addOption(tile.id(), tile);
|
||||
if (tile == preselected) opt.attr("selected", "selected");
|
||||
for (TextDisplay display : BaseClass.listElements(TextDisplay.class)) {
|
||||
if (exclude.contains(display)) continue;
|
||||
Tag opt = select.addOption(display.id(), display);
|
||||
if (display == preselected) opt.attr("selected", "selected");
|
||||
}
|
||||
return select;
|
||||
}
|
||||
|
||||
@@ -251,10 +251,12 @@ public abstract class Tile extends BaseClass{
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
public void remove(Route route) {
|
||||
routes.remove(route);
|
||||
@Override
|
||||
public BaseClass remove() {
|
||||
plan.stream("remove "+id());
|
||||
return super.remove();
|
||||
}
|
||||
|
||||
|
||||
private static String replace(String line, Entry<String, Object> replacement) {
|
||||
String key = replacement.getKey();
|
||||
Object val = replacement.getValue();
|
||||
@@ -280,9 +282,9 @@ public abstract class Tile extends BaseClass{
|
||||
return routes;
|
||||
}
|
||||
|
||||
public static void saveAll(HashMap<Id, Tile> tiles ,String filename) throws IOException {
|
||||
public static void saveAll(String filename) throws IOException {
|
||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||
for (Tile tile : tiles.values()) {
|
||||
for (Tile tile : BaseClass.listElements(Tile.class)) {
|
||||
if (isNull(tile) || tile instanceof Shadow) continue;
|
||||
file.append(tile.json()+"\n");
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.concurrent.TimeoutException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Command;
|
||||
import de.srsoftware.web4rail.Command.Reply;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
@@ -139,6 +140,11 @@ public abstract class Turnout extends Tile implements Device{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
// this class has no child elements
|
||||
}
|
||||
|
||||
public State state() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class TurnoutL extends Turnout {
|
||||
public abstract class TurnoutL extends Turnout {
|
||||
|
||||
private static final String LEFT = "left";
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class TurnoutR extends Turnout {
|
||||
public abstract class TurnoutR extends Turnout {
|
||||
|
||||
private static final String RIGHT = "right";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user