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