implemented inversion of conditions
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.8.1</version>
|
||||
<version>0.8.2</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -60,7 +60,7 @@ public class ConditionalAction extends Action {
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
for (Condition condition : conditions) {
|
||||
if (condition.fulfilledBy(context)) return fireActions(context);
|
||||
if (condition.fulfilledBy(context) != condition.inverted) return fireActions(context);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -106,7 +106,7 @@ public class ConditionalAction extends Action {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (conditions.isEmpty()) return t("Invalid condition");
|
||||
if (conditions.isEmpty()) return t("[Click here to add condition]");
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i<conditions.size(); i++) {
|
||||
if (i>0) sb.append(t(" or "));
|
||||
|
||||
@@ -4,6 +4,8 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
@@ -12,12 +14,17 @@ import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public abstract class Condition implements Constants {
|
||||
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Condition.class);
|
||||
private static final String INVERTED = "inverted";
|
||||
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>();
|
||||
|
||||
public abstract boolean fulfilledBy(Context context);
|
||||
public boolean inverted = false;
|
||||
protected int id;
|
||||
|
||||
public Condition() {
|
||||
@@ -65,8 +72,23 @@ public abstract class Condition implements Constants {
|
||||
String json = new JSONObject(Map.of(REALM,REALM_CONDITION,ID,id,ACTION,ACTION_PROPS,CONTEXT,context)).toString().replace("\"", "'");
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(toString());
|
||||
}
|
||||
|
||||
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);
|
||||
return form;
|
||||
}
|
||||
|
||||
protected abstract Window properties(HashMap<String, String> params);
|
||||
protected Window properties(HashMap<String, String> params) {
|
||||
Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName()));
|
||||
Tag form = propForm(params);
|
||||
new Checkbox(INVERTED, t("inverted"), inverted).addTo(form);
|
||||
new Button(t("Apply"),"return submitForm('condition-props-"+id+"');").addTo(form).addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
public static String t(String text, Object...fills) {
|
||||
return Translation.get(Application.class, text, fills);
|
||||
@@ -77,5 +99,8 @@ public abstract class Condition implements Constants {
|
||||
return t("invalid condition");
|
||||
}
|
||||
|
||||
protected abstract Object update(HashMap<String, String> params);
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
inverted = "on".equals(params.get(INVERTED));
|
||||
return t("updated {}.",this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,9 @@ import java.util.HashMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.actions.Action.Context;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class TrainSelect extends Condition {
|
||||
@@ -33,23 +30,16 @@ public class TrainSelect extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(HashMap<String, String> params) {
|
||||
Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName()));
|
||||
String formId = "conditional-props-"+id;
|
||||
Form form = new Form(formId);
|
||||
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);
|
||||
public Tag propForm(HashMap<String, String> params) {
|
||||
Tag form = super.propForm(params);
|
||||
Train.selector(train, null).addTo(new Label(t("Select train:")+NBSP)).addTo(form);
|
||||
new Button(t("Apply"),"return submitForm('"+formId+"');").addTo(form).addTo(win);
|
||||
return win;
|
||||
return form;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (train == null) return super.toString();
|
||||
return t("Train = {}",train);
|
||||
if (train == null) return t("[Click here to select train!]");
|
||||
return t(inverted?"Train ≠ {}":"Train = {}",train);
|
||||
}
|
||||
|
||||
private TrainSelect train(Train train) {
|
||||
@@ -65,6 +55,6 @@ public class TrainSelect extends Condition {
|
||||
Train train = Train.get(tid);
|
||||
if (train == null) return t("No train with id {} found!",tid);
|
||||
this.train = train;
|
||||
return t("Updated condition");
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user