re-implemented Delayed Action
This commit is contained in:
@@ -113,6 +113,10 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
ConditionalAction ca = (ConditionalAction) action;
|
||||
ca.children().addTo(act, context);
|
||||
}
|
||||
if (action instanceof DelayedAction) {
|
||||
DelayedAction da = (DelayedAction) action;
|
||||
da.children().addTo(act, context);
|
||||
}
|
||||
act.addTo(ul);
|
||||
first = false;
|
||||
}
|
||||
@@ -130,7 +134,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void fire(Context context) {
|
||||
public boolean fire(Context context) {
|
||||
LOG.debug("Firing {}",this);
|
||||
|
||||
for (Action action : this) {
|
||||
@@ -140,6 +144,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
LOG.warn("Action did not fire properly: {}",action,e);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int id() {
|
||||
|
||||
@@ -60,19 +60,10 @@ public class ConditionalAction extends Action {
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
for (Condition condition : conditions) {
|
||||
if (condition.fulfilledBy(context) != condition.inverted) return fireActions(context);
|
||||
if (condition.fulfilledBy(context) != condition.inverted) return actions.fire(context);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean fireActions(Context context) {
|
||||
for (Action action : actions) try {
|
||||
action.fire(context);
|
||||
} catch (IOException e) {
|
||||
LOG.warn("Was not able to fire {}",action);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
|
||||
@@ -2,122 +2,106 @@ package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
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.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
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 class DelayedAction extends Action{
|
||||
|
||||
public class DelayedAction extends Action {
|
||||
|
||||
private static final String ACTIONS = "actions";
|
||||
public static final String DELAY = "delay";
|
||||
private static final int DEFAULT_DELAY = 1000;
|
||||
private int delay = DEFAULT_DELAY;
|
||||
private Action action = null;
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
if (action != null) try {
|
||||
action.fire(context);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
};
|
||||
}.start();
|
||||
return true;
|
||||
private ActionList actions = new ActionList();
|
||||
|
||||
private Tag actionsForm(HashMap<String, String> params) {
|
||||
Fieldset fieldset = new Fieldset(t("Actions"));
|
||||
actions.addTo(fieldset, params.get(CONTEXT));
|
||||
return fieldset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(DELAY, delay);
|
||||
json.put(ACTION, action.json());
|
||||
return json;
|
||||
public ActionList children() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public static DelayedAction load(JSONObject json) {
|
||||
DelayedAction da = new DelayedAction();
|
||||
da.delay = json.getInt(DELAY);
|
||||
da.action = Action.load(json.getJSONObject(ACTION));
|
||||
return da;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Window properties(HashMap<String, String> params) {
|
||||
Window win = super.properties(params);
|
||||
private Tag delayForm(HashMap<String, String> params) {
|
||||
Fieldset fieldset = new Fieldset(t("Delay"));
|
||||
|
||||
Form form = new Form("action-prop-form-"+id);
|
||||
new Input(REALM,REALM_ACTIONS).hideIn(form);
|
||||
new Input(ID,params.get(ID)).hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||
|
||||
if (action == null) {
|
||||
Select select = new Select(TYPE);
|
||||
List<Class<? extends Action>> classes = List.of(
|
||||
ConditionalAction.class,
|
||||
SetSpeed.class,
|
||||
SetSignalsToStop.class,
|
||||
FinishRoute.class,
|
||||
TurnTrain.class,
|
||||
StopAuto.class,
|
||||
PowerOff.class,
|
||||
SetRelay.class,
|
||||
DelayedAction.class
|
||||
);
|
||||
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||
select.addTo(new Label(t("Action type:")+NBSP)).addTo(form);
|
||||
} else {
|
||||
action.link(0, params.get(CONTEXT)).addTo(new Label(t("Action: "))).addTo(form);
|
||||
}
|
||||
Label label = new Label(t("Delay:")+NBSP);
|
||||
new Input(DELAY, delay).numeric().addTo(label).content(NBSP+t("miliseconds"));
|
||||
label.addTo(form);
|
||||
new Button(t("Apply"),form).addTo(form).addTo(win);
|
||||
return win;
|
||||
|
||||
new Input(DELAY,delay).numeric().addTo(new Label(t("Delay")+NBSP)).content(" ms").addTo(form);
|
||||
return new Button(t("Apply"),form).addTo(form).addTo(fieldset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warn("Interrupted Exception thrown while waiting:",e);
|
||||
}
|
||||
actions.fire(context);
|
||||
};
|
||||
}.start();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (action == null) return t("Click here to set up delayed action!");
|
||||
return t("Wait {} ms, then {}",delay,action);
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(DELAY, delay);
|
||||
json.put(ACTIONS, actions.json());
|
||||
return json;
|
||||
}
|
||||
|
||||
public static DelayedAction load(JSONObject json) {
|
||||
DelayedAction action = new DelayedAction();
|
||||
action.delay = json.getInt(DELAY);
|
||||
if (json.has(ACTIONS)) action.actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
return action;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Window properties(HashMap<String, String> params) {
|
||||
Window win = super.properties(params);
|
||||
delayForm(params).addTo(win);
|
||||
actionsForm(params).addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return t("Wait {} ms, then:",delay);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
|
||||
String type = params.get(TYPE);
|
||||
if (type != null) action = Action.create(type);
|
||||
|
||||
String error = null;
|
||||
String ms = params.get(DELAY);
|
||||
if (ms == null) {
|
||||
ms = ""+DEFAULT_DELAY;
|
||||
} else {
|
||||
try {
|
||||
int d = Integer.parseInt(ms);
|
||||
if (d<0) error = t("Delay must not be less than zero!");
|
||||
if (error == null) {
|
||||
this.delay = d;
|
||||
return t("Action updated!");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
error = t("Not a valid number!");
|
||||
}
|
||||
}
|
||||
Window win = properties(params);
|
||||
return new Tag("span").content(error).addTo(win);
|
||||
String d = params.get(DELAY);
|
||||
if (d != null) try {
|
||||
int ms = Integer.parseInt(d);
|
||||
if (ms < 0) throw new NumberFormatException(t("Delay must not be less than zero!"));
|
||||
delay = ms;
|
||||
} catch (NumberFormatException nfe) {
|
||||
Window props = properties(params);
|
||||
props.children().insertElementAt(new Tag("div").content(nfe.getMessage()), 2);
|
||||
return props;
|
||||
}
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user