|
|
|
@ -2,122 +2,106 @@ package de.srsoftware.web4rail.actions;
@@ -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; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public ActionList children() { |
|
|
|
|
return actions; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
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) { |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
if (action != null) try { |
|
|
|
|
action.fire(context); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
actions.fire(context); |
|
|
|
|
}; |
|
|
|
|
}.start(); |
|
|
|
|
return true; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public JSONObject json() { |
|
|
|
|
JSONObject json = super.json(); |
|
|
|
|
json.put(DELAY, delay); |
|
|
|
|
json.put(ACTION, action.json()); |
|
|
|
|
json.put(ACTIONS, actions.json()); |
|
|
|
|
return json; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static DelayedAction load(JSONObject json) { |
|
|
|
|
DelayedAction da = new DelayedAction(); |
|
|
|
|
da.delay = json.getInt(DELAY); |
|
|
|
|
da.action = Action.load(json.getJSONObject(ACTION)); |
|
|
|
|
return da; |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
delayForm(params).addTo(win); |
|
|
|
|
actionsForm(params).addTo(win); |
|
|
|
|
return win; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String toString() { |
|
|
|
|
if (action == null) return t("Click here to set up delayed action!"); |
|
|
|
|
return t("Wait {} ms, then {}",delay,action); |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|