001package apps.startup;
002
003import jmri.util.startup.AbstractStartupModel;
004import jmri.JmriException;
005
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Startup action that causes JMRI to pause before triggering the next startup
011 * action.
012 *
013 * @author Randall Wood (c) 2016
014 */
015public class StartupPauseModel extends AbstractStartupModel {
016
017    public static final int DEFAULT_DELAY = 10;
018    private int delay = -1; // default to invalid duration
019    private final static Logger log = LoggerFactory.getLogger(StartupPauseModel.class);
020
021    @Override
022    public String getName() {
023        return Bundle.getMessage("StartupPauseModel.name", this.getDelay()); // NOI18N
024    }
025
026    /**
027     * {@inheritDoc}
028     *
029     * @return true if duration greater than or equal to 0; false otherwise
030     */
031    @Override
032    public boolean isValid() {
033        return this.getDelay() >= 0;
034    }
035
036    /**
037     * Get the delay this action will pause the startup action processing.
038     *
039     * @return seconds delay
040     */
041    public int getDelay() {
042        return this.delay;
043    }
044
045    /**
046     * Set the delay this action will pause the startup action processing.
047     *
048     * @param delay delay in seconds
049     */
050    public void setDelay(int delay) {
051        this.delay = delay;
052    }
053
054    @Override
055    public void performAction() throws JmriException {
056        if (delay > 0) {
057            log.info("Pausing startup actions processing for {} seconds.", delay);
058            try {
059                // delay is in seconds ; sleep takes long, not int
060                Thread.sleep(delay * (long) 1000);
061            } catch (InterruptedException ex) {
062                // warn the user that the pause was not as long as expected
063                // this does not throw an error displayed to the user; should it?
064                log.warn("Pause in startup actions interrupted.");
065            }
066        }
067    }
068
069}