You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.5 KiB
115 lines
3.5 KiB
package de.srsoftware.web4rail.actions; |
|
|
|
import java.util.HashMap; |
|
import java.util.List; |
|
|
|
import org.json.JSONObject; |
|
|
|
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.Fieldset; |
|
import de.srsoftware.web4rail.tags.Input; |
|
|
|
public class DelayedAction extends ActionList { |
|
|
|
public static final String DELAY = "delay"; |
|
public static final String MIN_DELAY = "min_delay"; |
|
public static final String MAX_DELAY = "max_delay"; |
|
private static final int DEFAULT_DELAY = 1000; |
|
private int min_delay = DEFAULT_DELAY; |
|
private int max_delay = DEFAULT_DELAY; |
|
|
|
public DelayedAction(BaseClass parent) { |
|
super(parent); |
|
} |
|
|
|
public boolean equals(DelayedAction other) { |
|
return (min_delay+":"+max_delay+":"+actions).equals(other.min_delay+":"+other.max_delay+":"+other.actions); |
|
} |
|
|
|
@Override |
|
public boolean fire(Context context) { |
|
Application.threadPool.execute(new Thread() { |
|
public void run() { |
|
try { |
|
Thread.sleep(min_delay + (min_delay < max_delay ? random.nextInt(max_delay - min_delay) : 0)); |
|
LOG.debug("{} ms passed by, firing actions:",min_delay); |
|
} catch (InterruptedException e) { |
|
LOG.warn("Interrupted Exception thrown while waiting:",e); |
|
} |
|
DelayedAction.super.fire(context); |
|
}; |
|
}); |
|
return true; |
|
} |
|
|
|
@Override |
|
public JSONObject json() { |
|
return super.json().put(MIN_DELAY, min_delay).put(MAX_DELAY, max_delay); |
|
} |
|
|
|
public DelayedAction load(JSONObject json) { |
|
super.load(json); |
|
if (json.has(DELAY)) { |
|
min_delay = json.getInt(DELAY); |
|
max_delay = json.getInt(DELAY); |
|
} |
|
if (json.has(MIN_DELAY)) min_delay = json.getInt(MIN_DELAY); |
|
if (json.has(MAX_DELAY)) max_delay = json.getInt(MAX_DELAY); |
|
return this; |
|
} |
|
|
|
@Override |
|
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) { |
|
formInputs.add(t("Minimum delay"),new Input(MIN_DELAY,min_delay).numeric().addTo(new Tag("span")).content(NBSP+"ms")); |
|
formInputs.add(t("Maximum delay"),new Input(MAX_DELAY,max_delay).numeric().addTo(new Tag("span")).content(NBSP+"ms")); |
|
return super.properties(preForm, formInputs, postForm); |
|
} |
|
|
|
public DelayedAction setMaxDelay(int max_delay) { |
|
this.max_delay = max_delay; |
|
return this; |
|
} |
|
|
|
public DelayedAction setMinDelay(int min_delay) { |
|
this.min_delay = min_delay; |
|
return this; |
|
} |
|
|
|
@Override |
|
public String toString() { |
|
return t("Wait {} ms, then:",min_delay < max_delay ? min_delay+"…"+max_delay : min_delay); |
|
} |
|
|
|
@Override |
|
protected Object update(HashMap<String, String> params) { |
|
String d = params.get(MIN_DELAY); |
|
if (isSet(d)) try { |
|
int ms = Integer.parseInt(d); |
|
if (ms < 0) throw new NumberFormatException(t("Delay must not be less than zero!")); |
|
min_delay = ms; |
|
} catch (NumberFormatException nfe) { |
|
Window props = properties(); |
|
props.children().insertElementAt(new Tag("div").content(nfe.getMessage()), 2); |
|
return props; |
|
} |
|
d = params.get(MAX_DELAY); |
|
if (isSet(d)) try { |
|
int ms = Integer.parseInt(d); |
|
if (ms < 0) throw new NumberFormatException(t("Delay must not be less than zero!")); |
|
max_delay = ms; |
|
} catch (NumberFormatException nfe) { |
|
Window props = properties(); |
|
props.children().insertElementAt(new Tag("div").content(nfe.getMessage()), 2); |
|
return props; |
|
} |
|
if (min_delay > max_delay) { |
|
int dummy = min_delay; |
|
min_delay = max_delay; |
|
max_delay = dummy; |
|
} |
|
return super.update(params); |
|
} |
|
}
|
|
|