implemented removing of conditions from conditional action
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>1.0.14</version>
|
||||
<version>1.0.15</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -19,6 +19,7 @@ Analyze : analysieren
|
||||
Apply : Übernehmen
|
||||
Auto pilot : Autopilot
|
||||
Availability : Verfügbarkeit
|
||||
Back : zurück
|
||||
Basic properties : Grundlegende Eigenschaften
|
||||
BlockFree : Blockbelegung
|
||||
Block {} is free : Block {} ist frei
|
||||
|
||||
@@ -7,11 +7,19 @@ import java.util.Random;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
|
||||
public abstract class BaseClass implements Constants{
|
||||
protected static Plan plan; // the track layout in use
|
||||
public static final Random random = new Random();
|
||||
|
||||
public static Button contextButton(String context,String text) {
|
||||
String[] parts = context.split(":");
|
||||
String realm = parts[0];
|
||||
String id = parts.length>1 ? parts[1] : null;
|
||||
return new Button(text,Map.of(REALM,realm,ID,id,ACTION,ACTION_PROPS));
|
||||
}
|
||||
|
||||
public static Tag link(String tagClass,Map<String,Object> params,Object caption) {
|
||||
String json = new JSONObject(params).toString().replace("\"", "'");
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(caption.toString());
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
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.Div;
|
||||
@@ -714,7 +715,10 @@ public class Plan extends BaseClass{
|
||||
return route(Integer.parseInt(id)).properties(params);
|
||||
case REALM_PLAN:
|
||||
Tile tile = get(id, false);
|
||||
return tile == null? null : tile.propMenu();
|
||||
return isNull(tile) ? null : tile.propMenu();
|
||||
case REALM_ACTIONS:
|
||||
Action action = Action.get(Integer.parseInt(id));
|
||||
return (isSet(action)) ? action.properties(params) : null;
|
||||
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -546,7 +546,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
for (int i=0; i<arr.length(); i++) {
|
||||
JSONObject json = arr.getJSONObject(i);
|
||||
Condition condition = Condition.create(json.getString(TYPE));
|
||||
if (isSet(condition)) conditions.add(condition.load(json));
|
||||
if (isSet(condition)) conditions.add(condition.parent(this).load(json));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -602,6 +602,11 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
return win;
|
||||
}
|
||||
|
||||
public Route remove(Condition condition) {
|
||||
conditions.remove(condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean reset() {
|
||||
setSignals(Signal.STOP);
|
||||
@@ -710,7 +715,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
Condition condition = Condition.create(params.get(REALM_CONDITION));
|
||||
if (isSet(condition)) {
|
||||
conditions.add(condition);
|
||||
conditions.add(condition.parent(this));
|
||||
return properties(params);
|
||||
}
|
||||
String message = t("{} updated.",this);
|
||||
|
||||
@@ -43,12 +43,17 @@ public class ConditionalAction extends Action {
|
||||
|
||||
private Tag conditionForm(HashMap<String, String> params) {
|
||||
Fieldset fieldset = new Fieldset(t("Conditions"));
|
||||
String context = params.get(CONTEXT);
|
||||
|
||||
new Tag("p").content(t("Actions will only fire, if all conditions are fullfilled.")).addTo(fieldset);
|
||||
if (!conditions.isEmpty()) {
|
||||
Tag list = new Tag("ul");
|
||||
for (Condition condition : conditions) {
|
||||
link("li", Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_PROPS,CONTEXT,params.get(CONTEXT)), condition).addTo(list);
|
||||
HashMap<String,Object> props = new HashMap<String, Object>(Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_PROPS,CONTEXT, context));
|
||||
Tag li = link("span", props, condition+NBSP).addTo(new Tag("li"));
|
||||
props.put(ACTION, ACTION_DROP);
|
||||
props.put(CONTEXT,REALM_ACTIONS+":"+id());
|
||||
new Button(t("delete"), props).addTo(li).addTo(list);
|
||||
}
|
||||
list.addTo(fieldset);
|
||||
}
|
||||
@@ -57,12 +62,14 @@ public class ConditionalAction extends Action {
|
||||
new Input(REALM,REALM_ACTIONS).hideIn(form);
|
||||
new Input(ID,params.get(ID)).hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||
new Input(CONTEXT,context).hideIn(form);
|
||||
|
||||
Condition.selector().addTo(form);
|
||||
return new Button(t("Add condition"),form).addTo(form).addTo(fieldset);
|
||||
|
||||
new Button(t("Add condition"),form).addTo(form);
|
||||
return contextButton(context,t("Back")).addTo(form).addTo(fieldset);
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(ConditionalAction other) {
|
||||
return (conditions()+":"+actions).equals(other.conditions()+":"+other.actions);
|
||||
}
|
||||
@@ -92,7 +99,7 @@ public class ConditionalAction extends Action {
|
||||
if (o instanceof JSONObject) {
|
||||
JSONObject j = (JSONObject) o;
|
||||
Condition condition = Condition.create(j.getString(TYPE));
|
||||
if (condition != null) conditions.add(condition.load(j));
|
||||
if (isSet(condition)) conditions.add(condition.parent(this).load(j));
|
||||
}
|
||||
}
|
||||
actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
@@ -107,6 +114,11 @@ public class ConditionalAction extends Action {
|
||||
return win;
|
||||
}
|
||||
|
||||
public ConditionalAction remove(Condition condition) {
|
||||
conditions.remove(condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (conditions.isEmpty()) return t("[Click here to add condition]");
|
||||
@@ -117,8 +129,8 @@ public class ConditionalAction extends Action {
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
String conditionClass = params.get(REALM_CONDITION);
|
||||
Condition condition = Condition.create(conditionClass);
|
||||
if (condition == null) return t("Unknown type of condition: {}",conditionClass);
|
||||
conditions.add(condition);
|
||||
if (isNull(condition)) return t("Unknown type of condition: {}",conditionClass);
|
||||
conditions.add(condition.parent(this));
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@@ -14,8 +15,10 @@ 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.Action.Context;
|
||||
import de.srsoftware.web4rail.actions.ConditionalAction;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
@@ -30,6 +33,7 @@ public abstract class Condition extends BaseClass {
|
||||
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>();
|
||||
public boolean inverted = false;
|
||||
protected int id;
|
||||
private Object parent;
|
||||
|
||||
public Condition() {
|
||||
this(Application.createId());
|
||||
@@ -55,6 +59,9 @@ public abstract class Condition extends BaseClass {
|
||||
case ACTION_UPDATE:
|
||||
condition.update(params);
|
||||
return plan.showContext(params);
|
||||
case ACTION_DROP:
|
||||
condition.drop();
|
||||
return plan.showContext(params);
|
||||
}
|
||||
return t("Unknown action: {}",action);
|
||||
}
|
||||
@@ -69,6 +76,18 @@ 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 int id() {
|
||||
@@ -81,11 +100,20 @@ public abstract class Condition extends BaseClass {
|
||||
return json;
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
inverted = json.has(INVERTED) && json.getBoolean(INVERTED);
|
||||
return this;
|
||||
/**
|
||||
* If arguments are given, the first is taken as content, the second as tag type.
|
||||
* If no content is supplied, name is set as content.
|
||||
* If no type is supplied, "span" is preset.
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public Tag link(String...args) {
|
||||
String tx = args.length<1 ? toString()+NBSP : args[0];
|
||||
String type = args.length<2 ? "span" : args[1];
|
||||
String context = args.length<3 ? null : args[2];
|
||||
return link(type, Map.of(REALM,REALM_CONDITION,ID,id(),ACTION,ACTION_PROPS,CONTEXT,context), tx);
|
||||
}
|
||||
|
||||
|
||||
private static List<Class<? extends Condition>> list() {
|
||||
return List.of(
|
||||
BlockFree.class,
|
||||
@@ -95,12 +123,18 @@ public abstract class Condition extends BaseClass {
|
||||
TrainLength.class);
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
inverted = json.has(INVERTED) && json.getBoolean(INVERTED);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Tag propForm(HashMap<String, String> params) {
|
||||
Form form = new Form("condition-props-"+id);
|
||||
new Input(REALM,REALM_CONDITION).hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(ID,id).hideIn(form);
|
||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||
String context = params.get(CONTEXT);
|
||||
if (isSet(context)) new Input(CONTEXT,context).hideIn(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
@@ -138,4 +172,9 @@ public abstract class Condition extends BaseClass {
|
||||
inverted = "on".equals(params.get(INVERTED));
|
||||
return t("updated {}.",this);
|
||||
}
|
||||
|
||||
public Condition parent(Object parent) {
|
||||
this.parent = parent;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class Contact extends Tile{
|
||||
public void run() {
|
||||
try {
|
||||
for (int ticks = 0; ticks<10; ticks++) {
|
||||
if (!aborted) sleep(100);
|
||||
if (!aborted) sleep(10);
|
||||
}
|
||||
timer = null;
|
||||
if (aborted) return;
|
||||
|
||||
Reference in New Issue
Block a user