001package jmri.jmrit.logixng.actions;
002
003import java.util.*;
004
005import jmri.*;
006import jmri.jmrit.logixng.*;
007import jmri.jmrit.logixng.util.*;
008import jmri.jmrit.logixng.util.parser.*;
009import jmri.util.ThreadingUtil;
010
011/**
012 * This action triggers a route.
013 * <p>
014 * This action has the Operation enum, similar to EnableLogix and other actions,
015 * despite that's not needed since this action only has one option. But it's
016 * here in case someone wants to add more options later.
017 *
018 * @author Daniel Bergqvist Copyright 2021
019 */
020public class TriggerRoute extends AbstractDigitalAction {
021
022    private final LogixNG_SelectNamedBean<Route> _selectNamedBean =
023            new LogixNG_SelectNamedBean<>(
024                    this, Route.class, InstanceManager.getDefault(RouteManager.class));
025
026    private final LogixNG_SelectEnum<Operation> _selectEnum =
027            new LogixNG_SelectEnum<>(this, Operation.values(), Operation.TriggerRoute);
028
029
030    public TriggerRoute(String sys, String user)
031            throws BadUserNameException, BadSystemNameException {
032        super(sys, user);
033    }
034
035    @Override
036    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
037        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
038        String sysName = systemNames.get(getSystemName());
039        String userName = userNames.get(getSystemName());
040        if (sysName == null) sysName = manager.getAutoSystemName();
041        TriggerRoute copy = new TriggerRoute(sysName, userName);
042        copy.setComment(getComment());
043        _selectNamedBean.copy(copy._selectNamedBean);
044        _selectEnum.copy(copy._selectEnum);
045        return manager.registerAction(copy);
046    }
047
048    public LogixNG_SelectNamedBean<Route> getSelectNamedBean() {
049        return _selectNamedBean;
050    }
051
052    public LogixNG_SelectEnum<Operation> getSelectEnum() {
053        return _selectEnum;
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public LogixNG_Category getCategory() {
059        return LogixNG_Category.ITEM;
060    }
061
062    /** {@inheritDoc} */
063    @Override
064    public void execute() throws JmriException {
065        Route route = _selectNamedBean.evaluateNamedBean(getConditionalNG());
066
067        if (route == null) return;
068
069        Operation oper = _selectEnum.evaluateEnum(getConditionalNG());
070
071        ThreadingUtil.runOnLayoutWithJmriException(() -> {
072            if (oper == Operation.TriggerRoute) {
073                route.setRoute();
074            } else {
075                throw new IllegalArgumentException("invalid oper: " + oper.name());
076            }
077        });
078    }
079
080    @Override
081    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
082        throw new UnsupportedOperationException("Not supported.");
083    }
084
085    @Override
086    public int getChildCount() {
087        return 0;
088    }
089
090    @Override
091    public String getShortDescription(Locale locale) {
092        return Bundle.getMessage(locale, "TriggerRoute_Short");
093    }
094
095    @Override
096    public String getLongDescription(Locale locale) {
097        String namedBean = _selectNamedBean.getDescription(locale);
098        String state = _selectEnum.getDescription(locale);
099
100        return Bundle.getMessage(locale, "TriggerRoute_Long", namedBean, state);
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public void setup() {
106        // Do nothing
107    }
108
109    /** {@inheritDoc} */
110    @Override
111    public void disposeMe() {
112    }
113
114
115    public enum Operation {
116        TriggerRoute(Bundle.getMessage("TriggerRoute_TriggerRoute"));
117
118        private final String _text;
119
120        private Operation(String text) {
121            this._text = text;
122        }
123
124        @Override
125        public String toString() {
126            return _text;
127        }
128
129    }
130
131    /** {@inheritDoc} */
132    @Override
133    public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) {
134        _selectNamedBean.getUsageDetail(level, bean, report, cdl, this, LogixNG_SelectNamedBean.Type.Action);
135    }
136
137//    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TriggerRoute.class);
138
139}