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 sets the lock of a turnout.
015 *
016 * @author Daniel Bergqvist Copyright 2021
017 */
018public class ActionTurnoutLock extends AbstractDigitalAction
019        implements PropertyChangeListener {
020
021    private final LogixNG_SelectNamedBean<Turnout> _selectNamedBean =
022            new LogixNG_SelectNamedBean<>(
023                    this, Turnout.class, InstanceManager.getDefault(TurnoutManager.class), this);
024
025    private final LogixNG_SelectEnum<TurnoutLock> _selectEnum =
026            new LogixNG_SelectEnum<>(this, TurnoutLock.values(), TurnoutLock.Unlock, this);
027
028
029    public ActionTurnoutLock(String sys, String user)
030            throws BadUserNameException, BadSystemNameException {
031        super(sys, user);
032    }
033
034    @Override
035    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
036        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
037        String sysName = systemNames.get(getSystemName());
038        String userName = userNames.get(getSystemName());
039        if (sysName == null) sysName = manager.getAutoSystemName();
040        ActionTurnoutLock copy = new ActionTurnoutLock(sysName, userName);
041        copy.setComment(getComment());
042        _selectNamedBean.copy(copy._selectNamedBean);
043        _selectEnum.copy(copy._selectEnum);
044        return manager.registerAction(copy);
045    }
046
047    public LogixNG_SelectNamedBean<Turnout> getSelectNamedBean() {
048        return _selectNamedBean;
049    }
050
051    public LogixNG_SelectEnum<TurnoutLock> getSelectEnum() {
052        return _selectEnum;
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    public Category getCategory() {
058        return Category.ITEM;
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    public void execute() throws JmriException {
064        Turnout turnout = _selectNamedBean.evaluateNamedBean(getConditionalNG());
065
066        if (turnout == null) return;
067
068        TurnoutLock lock = _selectEnum.evaluateEnum(getConditionalNG());
069
070        if (lock == TurnoutLock.Toggle) {
071            if (turnout.getLocked(Turnout.CABLOCKOUT)) {
072                lock = TurnoutLock.Unlock;
073            } else {
074                lock = TurnoutLock.Lock;
075            }
076        }
077
078        // Variables used in lambda must be effectively final
079        TurnoutLock theLock = lock;
080
081        ThreadingUtil.runOnLayoutWithJmriException(() -> {
082            if (theLock == TurnoutLock.Lock) {
083                turnout.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
084            } else if (theLock == TurnoutLock.Unlock) {
085                turnout.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, false);
086            }
087        });
088    }
089
090    @Override
091    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
092        throw new UnsupportedOperationException("Not supported.");
093    }
094
095    @Override
096    public int getChildCount() {
097        return 0;
098    }
099
100    @Override
101    public String getShortDescription(Locale locale) {
102        return Bundle.getMessage(locale, "TurnoutLock_Short");
103    }
104
105    @Override
106    public String getLongDescription(Locale locale) {
107        String namedBean = _selectNamedBean.getDescription(locale);
108        String state = _selectEnum.getDescription(locale);
109
110        return Bundle.getMessage(locale, "TurnoutLock_Long", namedBean, state);
111    }
112
113    /** {@inheritDoc} */
114    @Override
115    public void setup() {
116        // Do nothing
117    }
118
119    /** {@inheritDoc} */
120    @Override
121    public void registerListenersForThisClass() {
122        _selectNamedBean.registerListeners();
123        _selectEnum.registerListeners();
124    }
125
126    /** {@inheritDoc} */
127    @Override
128    public void unregisterListenersForThisClass() {
129        _selectNamedBean.unregisterListeners();
130        _selectEnum.unregisterListeners();
131    }
132
133    /** {@inheritDoc} */
134    @Override
135    public void disposeMe() {
136    }
137
138
139    public enum TurnoutLock {
140        Lock(Bundle.getMessage("TurnoutLock_Lock")),
141        Unlock(Bundle.getMessage("TurnoutLock_Unlock")),
142        Toggle(Bundle.getMessage("TurnoutLock_Toggle"));
143
144        private final String _text;
145
146        private TurnoutLock(String text) {
147            this._text = text;
148        }
149
150        @Override
151        public String toString() {
152            return _text;
153        }
154
155    }
156
157    /** {@inheritDoc} */
158    @Override
159    public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) {
160        _selectNamedBean.getUsageDetail(level, bean, report, cdl, this, LogixNG_SelectNamedBean.Type.Action);
161    }
162
163    /** {@inheritDoc} */
164    @Override
165    public void propertyChange(PropertyChangeEvent evt) {
166        getConditionalNG().execute();
167    }
168
169//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionTurnoutLock.class);
170
171}