|
|
|
@ -1,7 +1,6 @@
@@ -1,7 +1,6 @@
|
|
|
|
|
package de.srsoftware.web4rail.actions; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
@ -14,6 +13,7 @@ import de.keawe.tools.translations.Translation;
@@ -14,6 +13,7 @@ import de.keawe.tools.translations.Translation;
|
|
|
|
|
import de.srsoftware.tools.Tag; |
|
|
|
|
import de.srsoftware.web4rail.Application; |
|
|
|
|
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; |
|
|
|
@ -30,48 +30,78 @@ public class ActionList extends Vector<Action> implements Constants{
@@ -30,48 +30,78 @@ public class ActionList extends Vector<Action> implements Constants{
|
|
|
|
|
private static final HashMap<Integer, ActionList> actionLists = new HashMap<Integer, ActionList>(); |
|
|
|
|
|
|
|
|
|
public ActionList() { |
|
|
|
|
id = new Date().hashCode(); |
|
|
|
|
id = Application.createId(); |
|
|
|
|
actionLists.put(id,this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void fire(Context context) { |
|
|
|
|
LOG.debug("Firing {}",this); |
|
|
|
|
|
|
|
|
|
for (Action action : this) { |
|
|
|
|
try { |
|
|
|
|
action.fire(context); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
LOG.warn("Action did not fire properly: {}",action,e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static Integer actionId(HashMap<String, String> params) { |
|
|
|
|
if (!params.containsKey(ID)) return null; |
|
|
|
|
String[] parts = params.get(ID).split("/"); |
|
|
|
|
if (parts.length<2) return null; |
|
|
|
|
return Integer.parseInt(parts[1]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean drop(int actionId) { |
|
|
|
|
for (Action action : this) { |
|
|
|
|
if (action.id() == actionId) { |
|
|
|
|
this.remove(action); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
private static Integer actionListId(HashMap<String, String> params) { |
|
|
|
|
if (!params.containsKey(ID)) return null; |
|
|
|
|
String[] parts = params.get(ID).split("/"); |
|
|
|
|
return Integer.parseInt(parts[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean moveUp(int actionId) { |
|
|
|
|
for (int i=1; i<size(); i++) { |
|
|
|
|
if (actionId == elementAt(i).id()) { |
|
|
|
|
Action action = remove(i); |
|
|
|
|
insertElementAt(action, i-1); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object actionTypeForm(Window win, String context) { |
|
|
|
|
String formId ="add-action-to-"+id; |
|
|
|
|
Tag typeForm = new Form(formId); |
|
|
|
|
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); |
|
|
|
|
Select select = new Select(TYPE); |
|
|
|
|
List<Class<? extends Action>> classes = List.of( |
|
|
|
|
SpeedReduction.class, |
|
|
|
|
SetSignalsToStop.class, |
|
|
|
|
FinishRoute.class, |
|
|
|
|
TurnTrain.class, |
|
|
|
|
ConditionalAction.class); |
|
|
|
|
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName()); |
|
|
|
|
select.addTo(new Label("Action type:")).addTo(typeForm); |
|
|
|
|
return new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(typeForm).addTo(win); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
switch (type) { |
|
|
|
|
case "ConditionalAction": |
|
|
|
|
add(new ConditionalAction()); |
|
|
|
|
break; |
|
|
|
|
case "FinishRoute": |
|
|
|
|
add(new FinishRoute()); |
|
|
|
|
break; |
|
|
|
|
case "SetSignalsToStop": |
|
|
|
|
add(new SetSignalsToStop()); |
|
|
|
|
break; |
|
|
|
|
case "SpeedReduction": |
|
|
|
|
add(new SpeedReduction(0)); |
|
|
|
|
break; |
|
|
|
|
case "TurnTrain": |
|
|
|
|
add(new TurnTrain()); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
actionTypeForm(win,context); |
|
|
|
|
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win); |
|
|
|
|
return win; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
return plan.showContext(params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void addTo(Tag link) { |
|
|
|
|
|
|
|
|
|
public void addTo(Tag link, String context) { |
|
|
|
|
Map<String, Object> props = new HashMap<String, Object>(Map.of( |
|
|
|
|
REALM,REALM_ACTIONS, |
|
|
|
|
ID,id, |
|
|
|
|
ACTION,ACTION_ADD)); |
|
|
|
|
ACTION,ACTION_ADD, |
|
|
|
|
CONTEXT,context)); |
|
|
|
|
new Button(t("add action"),props).addTo(link); |
|
|
|
|
|
|
|
|
|
props.put(ACTION,ACTION_PROPS); |
|
|
|
@ -80,7 +110,7 @@ public class ActionList extends Vector<Action> implements Constants{
@@ -80,7 +110,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
|
|
|
|
boolean first = true; |
|
|
|
|
for (Action action : this) { |
|
|
|
|
props.put(ID, id+"/"+action.id()); |
|
|
|
|
Tag act = new Tag("li").content(action.toString()); |
|
|
|
|
Tag act = action.link(id,context).addTo(new Tag("li")); |
|
|
|
|
if (!first) { |
|
|
|
|
props.put(ACTION, ACTION_MOVE); |
|
|
|
|
new Button("↑",props).addTo(act); |
|
|
|
@ -94,79 +124,92 @@ public class ActionList extends Vector<Action> implements Constants{
@@ -94,79 +124,92 @@ public class ActionList extends Vector<Action> implements Constants{
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static String t(String text,Object...fills) { |
|
|
|
|
return Translation.get(Application.class, text, fills); |
|
|
|
|
public boolean drop(int actionId) { |
|
|
|
|
for (Action action : this) { |
|
|
|
|
if (action.id() == actionId) { |
|
|
|
|
this.remove(action); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void fire(Context context) { |
|
|
|
|
LOG.debug("Firing {}",this); |
|
|
|
|
|
|
|
|
|
for (Action action : this) { |
|
|
|
|
try { |
|
|
|
|
action.fire(context); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
LOG.warn("Action did not fire properly: {}",action,e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Action getAction(int actionId) { |
|
|
|
|
for (Action action : this) { |
|
|
|
|
if (action.id == actionId) return action; |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Object process(HashMap<String, String> params) { |
|
|
|
|
if (!params.containsKey(ID)) return t("No action list id passed to ActionList.process()!"); |
|
|
|
|
String[] parts = params.get(ID).split("/"); |
|
|
|
|
int listId = Integer.parseInt(parts[0]); |
|
|
|
|
int actionId = parts.length>1 ? Integer.parseInt(parts[1]) : 0; |
|
|
|
|
public int id() { |
|
|
|
|
return id; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean moveUp(int actionId) { |
|
|
|
|
for (int i=1; i<size(); i++) { |
|
|
|
|
if (actionId == elementAt(i).id()) { |
|
|
|
|
Action action = remove(i); |
|
|
|
|
insertElementAt(action, i-1); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Object process(HashMap<String, String> params, Plan plan) { |
|
|
|
|
Integer listId = actionListId(params); |
|
|
|
|
if (listId == null) return t("No action list id passed to ActionList.process()!"); |
|
|
|
|
ActionList actionList = actionLists.get(listId); |
|
|
|
|
if (actionList == null) return t("No action list with id {} found!",listId); |
|
|
|
|
|
|
|
|
|
Integer actionId = actionId(params); |
|
|
|
|
String action = params.get(ACTION); |
|
|
|
|
if (action == null) return t("No action passed to ActionList.process()!"); |
|
|
|
|
|
|
|
|
|
switch (action) { |
|
|
|
|
case ACTION_ADD: |
|
|
|
|
return actionList.addActionForm(params); |
|
|
|
|
return actionList.addActionForm(params,plan); |
|
|
|
|
case ACTION_DROP: |
|
|
|
|
return actionList.drop(actionId) ? t("Action removed") : t("No action with id {} found!",actionId); |
|
|
|
|
return actionList.drop(actionId) ? plan.showContext(params) : t("No action with id {} found!",actionId); |
|
|
|
|
case ACTION_MOVE: |
|
|
|
|
return actionList.moveUp(actionId) ? t("Action moved") : t("No action with id {} found!",actionId); |
|
|
|
|
return actionList.moveUp(actionId) ? plan.showContext(params) : t("No action with id {} found!",actionId); |
|
|
|
|
case ACTION_PROPS: |
|
|
|
|
return actionList.propsOf(params); |
|
|
|
|
case ACTION_UPDATE: |
|
|
|
|
return actionList.update(actionId,params,plan); |
|
|
|
|
} |
|
|
|
|
return t("Unknown action: {}",action); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object addActionForm(HashMap<String, String> params) { |
|
|
|
|
Window win = new Window("add-action-form", t("Add action to action list")); |
|
|
|
|
String formId ="add-action-to-"+id; |
|
|
|
|
Tag typeForm = new Form(formId); |
|
|
|
|
new Input(REALM, REALM_ACTIONS).hideIn(typeForm); |
|
|
|
|
new Input(ID,id).hideIn(typeForm); |
|
|
|
|
new Input(ACTION,ACTION_ADD).hideIn(typeForm); |
|
|
|
|
String type = params.get(TYPE); |
|
|
|
|
if (type == null) return actionTypeForm(win); |
|
|
|
|
|
|
|
|
|
switch (type) { |
|
|
|
|
case "FinishRoute": |
|
|
|
|
add(new FinishRoute()); |
|
|
|
|
break; |
|
|
|
|
case "SetSignalsToStop": |
|
|
|
|
add(new SetSignalsToStop()); |
|
|
|
|
break; |
|
|
|
|
case "SpeedReduction": |
|
|
|
|
return SpeedReduction.propForm(this,params); |
|
|
|
|
case "TurnTrain": |
|
|
|
|
add(new TurnTrain()); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
actionTypeForm(win); |
|
|
|
|
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win); |
|
|
|
|
return win; |
|
|
|
|
} |
|
|
|
|
return t("Action added!"); |
|
|
|
|
|
|
|
|
|
private Object propsOf(HashMap<String, String> params) { |
|
|
|
|
int actionId = actionId(params); |
|
|
|
|
Action action = getAction(actionId); |
|
|
|
|
if (action != null) return action.properties(params); |
|
|
|
|
return t("No action with id {} found!",actionId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object actionTypeForm(Window win) { |
|
|
|
|
String formId ="add-action-to-"+id; |
|
|
|
|
Tag typeForm = new Form(formId); |
|
|
|
|
new Input(REALM, REALM_ACTIONS).hideIn(typeForm); |
|
|
|
|
new Input(ID,id).hideIn(typeForm); |
|
|
|
|
new Input(ACTION,ACTION_ADD).hideIn(typeForm); |
|
|
|
|
Select select = new Select(TYPE); |
|
|
|
|
List<Class<? extends Action>> classes = List.of( |
|
|
|
|
SpeedReduction.class, |
|
|
|
|
SetSignalsToStop.class, |
|
|
|
|
FinishRoute.class, |
|
|
|
|
TurnTrain.class, |
|
|
|
|
ConditionalAction.class); |
|
|
|
|
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName()); |
|
|
|
|
select.addTo(new Label("Action type:")).addTo(typeForm); |
|
|
|
|
return new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(typeForm).addTo(win); |
|
|
|
|
|
|
|
|
|
private static String t(String text,Object...fills) { |
|
|
|
|
return Translation.get(Application.class, text, fills); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int id() { |
|
|
|
|
return id; |
|
|
|
|
|
|
|
|
|
private Object update(int actionId, HashMap<String, String> params, Plan plan) { |
|
|
|
|
Action action = getAction(actionId); |
|
|
|
|
if (action != null) { |
|
|
|
|
plan.stream(action.update(params).toString()); |
|
|
|
|
return plan.showContext(params); |
|
|
|
|
} |
|
|
|
|
return t("No action with id {} found.",actionId); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|