improved actions and conditions code,

added TrainLength condition,
added FreeStartBlock action
This commit is contained in:
Stephan Richter
2020-11-01 17:35:42 +01:00
parent a89d522ea2
commit d40ea949be
14 changed files with 264 additions and 148 deletions

View File

@@ -1,7 +1,10 @@
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;
import org.json.JSONObject;
import org.slf4j.Logger;
@@ -18,10 +21,13 @@ import de.srsoftware.web4rail.tags.Button;
import de.srsoftware.web4rail.tags.Checkbox;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Label;
import de.srsoftware.web4rail.tags.Select;
public abstract class Condition implements Constants {
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<Integer, Condition> conditions = new HashMap<Integer, Condition>();
public abstract boolean fulfilledBy(Context context);
public boolean inverted = false;
@@ -55,6 +61,15 @@ public abstract class Condition implements Constants {
return t("Unknown action: {}",action);
}
public static Condition create(String type) {
try {
return (Condition) Class.forName(PREFIX+"."+type).getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public JSONObject json() {
JSONObject json = new JSONObject().put(TYPE, getClass().getSimpleName());
if (inverted) json.put(INVERTED, true);
@@ -63,12 +78,7 @@ public abstract class Condition implements Constants {
public static Condition load(JSONObject json) {
String type = json.getString(TYPE);
Condition condition = null;
switch (type) {
case "TrainSelect":
condition = TrainSelect.load(json);
break;
}
Condition condition = Condition.create(type);
if (condition != null) condition.inverted = json.has(INVERTED) && json.getBoolean(INVERTED);
return condition;
}
@@ -95,6 +105,23 @@ public abstract class Condition implements Constants {
return win;
}
public static Tag selector() {
Select select = new Select(REALM_CONDITION);
TreeMap<String, String> names = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
for (Class<? extends Condition> clazz : list()) {
String s = t(clazz.getSimpleName());
names.put(s, clazz.getSimpleName());
}
for (Entry<String, String> entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey());
return select.addTo(new Label(t("Action type:")+NBSP));
}
private static List<Class<? extends Condition>> list() {
return List.of(TrainSelect.class,TrainLength.class);
}
public static String t(String text, Object...fills) {
return Translation.get(Application.class, text, fills);
}
@@ -108,4 +135,8 @@ public abstract class Condition implements Constants {
inverted = "on".equals(params.get(INVERTED));
return t("updated {}.",this);
}
public int id() {
return id;
}
}

View File

@@ -0,0 +1,57 @@
package de.srsoftware.web4rail.conditions;
import java.util.HashMap;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.actions.Action.Context;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Label;
public class TrainLength extends Condition {
private static final String MAX_LENGTH = "max_length";
private int maxLength = 0;
@Override
public boolean fulfilledBy(Context context) {
return context.train.length() < maxLength;
}
@Override
public JSONObject json() {
return super.json().put(MAX_LENGTH, maxLength);
}
public static TrainLength load(JSONObject json) {
TrainLength tl = new TrainLength();
if (json.has(MAX_LENGTH)) tl.maxLength = json.getInt(MAX_LENGTH);
return tl;
}
@Override
public Tag propForm(HashMap<String, String> params) {
return new Input(MAX_LENGTH, maxLength).addTo(new Label(t("Maximum train length:")+NBSP)).addTo(super.propForm(params));
}
@Override
public String toString() {
return t(inverted ? "train is longer than {}" : "train is shorter than {}",maxLength) ;
}
@Override
protected Object update(HashMap<String, String> params) {
if (params.containsKey(MAX_LENGTH)) try {
int ml = Integer.parseInt(params.get(MAX_LENGTH));
if (ml < 1) throw new NumberFormatException(t("length must be larger than zero!"));
maxLength = ml;
} catch (NumberFormatException nfe) {
Window win = properties(params);
win.children().insertElementAt(new Tag("div").content(nfe.getMessage()),1);
return win;
}
return super.update(params);
}
}