001package apps.startup;
002
003import jmri.util.startup.AbstractStartupModel;
004
005import java.util.Locale;
006
007import jmri.InstanceManager;
008import jmri.JmriException;
009import jmri.Route;
010import jmri.RouteManager;
011import jmri.util.prefs.InitializationException;
012
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Startup model that stores the user name of a {@link jmri.Route} so it can be
018 * set at application startup.
019 *
020 * @author Randall Wood (C) 2016
021 */
022public class TriggerRouteModel extends AbstractStartupModel {
023
024    private final static Logger log = LoggerFactory.getLogger(TriggerRouteModel.class);
025
026    /**
027     * Get the user name of the Route.
028     *
029     * @return the user name
030     */
031    public String getUserName() {
032        return this.getName();
033    }
034
035    /**
036     * Set the user name of the Route.
037     *
038     * @param name user name to use
039     */
040    public void setUserName(String name) {
041        this.setName(name);
042    }
043
044    /**
045     * Get the route.
046     *
047     * @return the route
048     */
049    public Route getRoute() {
050        return InstanceManager.getDefault(RouteManager.class).getByUserName(this.getUserName());
051    }
052
053    @Override
054    public void performAction() throws JmriException {
055        log.info("Setting route \"{}\" at startup.", this.getUserName());
056
057        try {
058            this.getRoute().setRoute();
059        } catch (NullPointerException ex) {
060            log.error("Unable to set route \"{}\"; it has not been defined. Is its panel loaded?", this.getUserName());
061            // it would be better to use a RouteNotFoundException if one existed
062            InitializationException exception = new InitializationException(Bundle.getMessage(Locale.ENGLISH, "TriggerRouteModel.RouteNotDefined", this.getUserName()),
063                    Bundle.getMessage("TriggerRouteModel.RouteNotDefined", this.getUserName()), ex);
064            this.addException(exception);
065            throw new JmriException(exception);
066        }
067
068    }
069}