@ -15,15 +15,18 @@ import de.srsoftware.web4rail.tags.Input;
public class DelayedAction extends ActionList {
public class DelayedAction extends ActionList {
public static final String DELAY = "delay" ;
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 static final int DEFAULT_DELAY = 1000 ;
private int delay = DEFAULT_DELAY ;
private int min_delay = DEFAULT_DELAY ;
private int max_delay = DEFAULT_DELAY ;
public DelayedAction ( BaseClass parent ) {
public DelayedAction ( BaseClass parent ) {
super ( parent ) ;
super ( parent ) ;
}
}
public boolean equals ( DelayedAction other ) {
public boolean equals ( DelayedAction other ) {
return ( delay + ":" + actions ) . equals ( other . delay + ":" + other . actions ) ;
return ( min_delay + ":" + max_ delay+ ":" + actions ) . equals ( other . min_delay + ":" + other . max_ delay+ ":" + other . actions ) ;
}
}
@Override
@Override
@ -31,8 +34,8 @@ public class DelayedAction extends ActionList {
Application . threadPool . execute ( new Thread ( ) {
Application . threadPool . execute ( new Thread ( ) {
public void run ( ) {
public void run ( ) {
try {
try {
Thread . sleep ( delay ) ;
Thread . sleep ( min_ delay + ( min_delay < max_delay ? random . nextInt ( max_delay - min_delay ) : 0 ) ) ;
LOG . debug ( "{} ms passed by, firing actions:" , delay ) ;
LOG . debug ( "{} ms passed by, firing actions:" , min_ delay) ;
} catch ( InterruptedException e ) {
} catch ( InterruptedException e ) {
LOG . warn ( "Interrupted Exception thrown while waiting:" , e ) ;
LOG . warn ( "Interrupted Exception thrown while waiting:" , e ) ;
}
}
@ -44,38 +47,69 @@ public class DelayedAction extends ActionList {
@Override
@Override
public JSONObject json ( ) {
public JSONObject json ( ) {
return super . json ( ) . put ( DELAY , delay ) ;
return super . json ( ) . put ( MIN_ DELAY, min_delay ) . put ( MAX_DELAY , max_ delay) ;
}
}
public DelayedAction load ( JSONObject json ) {
public DelayedAction load ( JSONObject json ) {
super . load ( json ) ;
super . load ( json ) ;
delay = json . getInt ( DELAY ) ;
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 ;
return this ;
}
}
@Override
@Override
protected Window properties ( List < Fieldset > preForm , FormInput formInputs , List < Fieldset > postForm ) {
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" ) ) ;
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 ) ;
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
@Override
public String toString ( ) {
public String toString ( ) {
return t ( "Wait {} ms, then:" , delay ) ;
return t ( "Wait {} ms, then:" , min_delay < max_delay ? min_delay + "…" + max_delay : min_ delay) ;
}
}
@Override
@Override
protected Object update ( HashMap < String , String > params ) {
protected Object update ( HashMap < String , String > params ) {
String d = params . get ( DELAY ) ;
String d = params . get ( MIN_DELAY ) ;
if ( d ! = null ) try {
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 ) ;
int ms = Integer . parseInt ( d ) ;
if ( ms < 0 ) throw new NumberFormatException ( t ( "Delay must not be less than zero!" ) ) ;
if ( ms < 0 ) throw new NumberFormatException ( t ( "Delay must not be less than zero!" ) ) ;
delay = ms ;
max_ delay = ms ;
} catch ( NumberFormatException nfe ) {
} catch ( NumberFormatException nfe ) {
Window props = properties ( ) ;
Window props = properties ( ) ;
props . children ( ) . insertElementAt ( new Tag ( "div" ) . content ( nfe . getMessage ( ) ) , 2 ) ;
props . children ( ) . insertElementAt ( new Tag ( "div" ) . content ( nfe . getMessage ( ) ) , 2 ) ;
return props ;
return props ;
}
}
if ( min_delay > max_delay ) {
int dummy = min_delay ;
min_delay = max_delay ;
max_delay = dummy ;
}
return super . update ( params ) ;
return super . update ( params ) ;
}
}
}
}