more refactoring

This commit is contained in:
Stephan Richter
2020-12-03 16:04:21 +01:00
parent 5aa66099fe
commit 4180d72d43
20 changed files with 156 additions and 177 deletions

View File

@@ -13,7 +13,6 @@ 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;
@@ -32,6 +31,12 @@ public abstract class Action extends BaseClass {
parent(parent);
}
public BaseClass context() {
BaseClass context = this;
while (context instanceof Action && isSet(context.parent())) context = context.parent();
return context;
}
public static Action create(String type,BaseClass parent) {
try {
return (Action) Class.forName(PREFIX+"."+type).getDeclaredConstructor(BaseClass.class).newInstance(parent);
@@ -41,16 +46,6 @@ public abstract class Action extends BaseClass {
return null;
}
public boolean drop() {
BaseClass parent = parent();
if (parent instanceof ActionList) {
ActionList actionList = (ActionList) parent;
return actionList.drop(this);
}
LOG.error("Action.drop() called on Action ({}) whose parent ({}) is not an ActionList!",this,parent);
return false;
}
public boolean equals(Action other) {
return this.toString().equals(other.toString());
}
@@ -104,10 +99,10 @@ public abstract class Action extends BaseClass {
return false;
}
@Override
/* @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);
@@ -134,6 +129,6 @@ public abstract class Action extends BaseClass {
@Override
protected Object update(HashMap<String, String> params) {
super.update(params);
return properties();
return context().properties();
}
}

View File

@@ -3,6 +3,7 @@ package de.srsoftware.web4rail.actions;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.json.JSONArray;
@@ -29,12 +30,12 @@ public class ActionList extends Action implements Iterable<Action>{
actions = new Vector<Action>();
}
private Object actionTypeForm(Window win, String context) {
private Tag actionTypeForm() {
Window win = new Window("add-action-form", t("Add action to action list"));
Form typeForm = new Form("add-action-to-"+id);
new Input(REALM, REALM_ACTIONS).hideIn(typeForm);
new Input(ID,id).hideIn(typeForm);
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
new Input(CONTEXT,context).hideIn(typeForm);
Action.selector().addTo(typeForm);
return new Button(t("Create action"),typeForm).addTo(typeForm).addTo(win);
}
@@ -45,18 +46,14 @@ public class ActionList extends Action implements Iterable<Action>{
}
private Object addActionForm(HashMap<String, String> params, Plan plan) {
Window win = new Window("add-action-form", t("Add action to action list"));
String type = params.get(TYPE);
String context = params.get(CONTEXT);
if (type == null) return actionTypeForm(win,context);
if (isNull(type)) return actionTypeForm();
Action action = Action.create(type,this);
if (action instanceof Action) {
add(action);
return parent().properties();
return context().properties();
}
actionTypeForm(win,context);
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win);
return win;
return new Tag("span").content(t("Unknown action type: {}",type)).addTo(actionTypeForm());
}
public void addActionsFrom(ActionList other) {
@@ -117,18 +114,18 @@ public class ActionList extends Action implements Iterable<Action>{
}
public Tag list() {
Button button = button(t("add action"), contextAction(ACTION_ADD_ACTION));
Tag span = new Tag("span");
button.addTo(span);
button(t("add action"), Map.of(ACTION, ACTION_ADD)).addTo(span);
if (!isEmpty()) {
Tag list = new Tag("ol");
boolean first = true;
for (Action action : actions) {
Tag item = action.link("span",action).addTo(new Tag("li"));
Tag item = action.link("span",action).addTo(new Tag("li")).content(NBSP);
action.button("-", Map.of(ACTION,ACTION_DROP)).addTo(item);
if (first) {
first = false;
} else action.button("", contextAction(ACTION_MOVE)).addTo(item.content(NBSP));
} else action.button("", Map.of(ACTION,ACTION_MOVE)).addTo(item);
if (action instanceof ActionList) ((ActionList) action).list().addTo(item);
item.addTo(list);
}
@@ -174,7 +171,10 @@ public class ActionList extends Action implements Iterable<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.properties() : t("No action with id {} found!",actionId);
if (isNull(action)) return t("No action with id {} found!",actionId);
BaseClass context = action.context();
action.remove();
return context.properties();
case ACTION_MOVE:
return action.moveUp() ? action.properties() : t("No action with id {} found!",actionId);
case ACTION_PROPS:
@@ -189,7 +189,7 @@ public class ActionList extends Action implements Iterable<Action>{
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
Fieldset fieldset = new Fieldset(t("Actions"));
list().addTo(fieldset);
preForm.add(fieldset);
postForm.add(fieldset);
return super.properties(preForm, formInputs, postForm);
}

View File

@@ -2,68 +2,29 @@ package de.srsoftware.web4rail.actions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.conditions.Condition;
import de.srsoftware.web4rail.tags.Button;
import de.srsoftware.web4rail.conditions.ConditionList;
import de.srsoftware.web4rail.tags.Fieldset;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
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 ConditionList conditions = new ConditionList();
public ConditionalAction(BaseClass parent) {
super(parent);
}
private StringBuffer conditions() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i<conditions.size(); i++) {
if (i>0) sb.append(t(" and "));
sb.append(conditions.get(i).toString());
}
return sb;
}
private Fieldset conditionForm() {
Fieldset fieldset = new Fieldset(t("Conditions"));
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) {
Tag li = link("span", condition+NBSP,Map.of()).addTo(new Tag("li"));
HashMap<String,Object> props = new HashMap<String, Object>(Map.of(REALM,REALM_CONDITION,ID,condition.id(),ACTION,ACTION_DROP,CONTEXT, REALM_ACTIONS+":"+id()));
new Button(t("delete"), props).addTo(li).addTo(list);
}
list.addTo(fieldset);
}
Form form = new Form("action-prop-form-"+id);
new Input(REALM,REALM_ACTIONS).hideIn(form);
new Input(ID,id()).hideIn(form);
new Input(ACTION,ACTION_UPDATE).hideIn(form);
Condition.selector().addTo(form);
new Button(t("Add condition"),form).addTo(form);
button(t("Back")).addTo(form).addTo(fieldset);
return fieldset;
conditions.parent(this);
}
public boolean equals(ConditionalAction other) {
return (conditions()+":"+actions).equals(other.conditions()+":"+other.actions);
return (conditions+":"+actions).equals(other.conditions+":"+other.actions);
}
@Override
@@ -103,10 +64,7 @@ public class ConditionalAction extends ActionList {
@Override
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
preForm.add(conditionForm());
Fieldset fieldset = new Fieldset(t("Actions"));
list().addTo(fieldset);
postForm.add(fieldset);
preForm.add(conditions.list());
return super.properties(preForm, formInputs, postForm);
}
@@ -118,8 +76,8 @@ public class ConditionalAction extends ActionList {
@Override
public String toString() {
if (conditions.isEmpty()) return t("[Click here to add condition]");
return t("if ({}):",conditions());
if (conditions.isEmpty()) return "["+t("Click here to add conditions")+"]";
return t("if ({}):",conditions);
}
@Override

View File

@@ -61,9 +61,6 @@ public class DelayedAction extends ActionList {
@Override
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"));
list().addTo(fieldset);
postForm.add(fieldset);
return super.properties(preForm, formInputs, postForm);
}

View File

@@ -68,10 +68,9 @@ public class SetDisplayText extends TextAction{
}
@Override
protected Object update(HashMap<String, String> params) {
super.update(params);
protected Object update(HashMap<String, String> params) {
String displayId = params.get(TextDisplay.class.getSimpleName());
if (isSet(displayId)) display = (TextDisplay) plan.get(new Id(displayId), false);
return properties();
return super.update(params);
}
}

View File

@@ -55,6 +55,6 @@ public abstract class TextAction extends Action {
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
text = params.get(TEXT);
return properties();
return super.update(params);
}
}