* added new Loop action
* added TrainSpeed condition * improved gui
This commit is contained in:
@@ -119,8 +119,9 @@ public abstract class Condition extends BaseClass {
|
||||
OrCondition.class,
|
||||
PushPullTrain.class,
|
||||
TrainHasTag.class,
|
||||
TrainLength.class,
|
||||
TrainSelect.class,
|
||||
TrainLength.class);
|
||||
TrainSpeed.class);
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
|
||||
@@ -12,43 +12,44 @@ import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class TrainLength extends Condition {
|
||||
|
||||
private static final String MAX_LENGTH = "max_length";
|
||||
private int maxLength = 0;
|
||||
private static final String LENGTH = "length";
|
||||
private int treshold = 0;
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
if (isNull(context.train())) return false;
|
||||
return (context.train().length() < maxLength) != inverted;
|
||||
int len = context.train().length();
|
||||
return inverted ? len > treshold : len < treshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(MAX_LENGTH, maxLength);
|
||||
return super.json().put(LENGTH, treshold);
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(MAX_LENGTH)) maxLength = json.getInt(MAX_LENGTH);
|
||||
if (json.has(LENGTH)) treshold = json.getInt(LENGTH);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Maximum train length"),new Input(MAX_LENGTH, maxLength).numeric());
|
||||
formInputs.add(t("Maximum train length"),new Input(LENGTH, treshold).numeric().addTo(new Tag("span")).content(lengthUnit));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t(inverted ? "train is longer than {}" : "train is shorter than {}",maxLength) ;
|
||||
return t(inverted ? "train is longer than {} {}" : "train is shorter than {} {}",treshold,lengthUnit) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (params.containsKey(MAX_LENGTH)) try {
|
||||
int ml = Integer.parseInt(params.get(MAX_LENGTH));
|
||||
if (params.containsKey(LENGTH)) try {
|
||||
int ml = Integer.parseInt(params.get(LENGTH));
|
||||
if (ml < 1) throw new NumberFormatException(t("length must be larger than zero!"));
|
||||
maxLength = ml;
|
||||
treshold = ml;
|
||||
} catch (NumberFormatException nfe) {
|
||||
Window win = properties();
|
||||
win.children().insertElementAt(new Tag("div").content(nfe.getMessage()),1);
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class TrainSpeed extends Condition {
|
||||
|
||||
private static final String SPEED = "speed";
|
||||
private int treshold = 0;
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
if (isNull(context.train())) return false;
|
||||
return inverted ? context.train().speed > treshold : context.train().speed < treshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(SPEED, treshold);
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(SPEED)) treshold = json.getInt(SPEED);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Train speed"),new Input(SPEED, treshold).numeric().addTo(new Tag("span")).content(speedUnit));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t(inverted ? "train is faster than {} {}" : "train is slower than {} {}",treshold,speedUnit) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (params.containsKey(SPEED)) try {
|
||||
int ml = Integer.parseInt(params.get(SPEED));
|
||||
if (ml < 0) throw new NumberFormatException(t("speed must be non-negative!"));
|
||||
treshold = ml;
|
||||
} catch (NumberFormatException nfe) {
|
||||
Window win = properties();
|
||||
win.children().insertElementAt(new Tag("div").content(nfe.getMessage()),1);
|
||||
return win;
|
||||
}
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user