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 requests an update from the layout of the state of a sensor.
015 *
016 * @author Daniel Bergqvist Copyright 2024
017 */
018public class ActionRequestUpdateOfSensor extends AbstractDigitalAction
019        implements PropertyChangeListener {
020
021    private final LogixNG_SelectNamedBean<Sensor> _selectNamedBean =
022            new LogixNG_SelectNamedBean<>(
023                    this, Sensor.class, InstanceManager.getDefault(SensorManager.class), this);
024
025
026    public ActionRequestUpdateOfSensor(String sys, String user)
027            throws BadUserNameException, BadSystemNameException {
028        super(sys, user);
029    }
030
031    @Override
032    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
033        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
034        String sysName = systemNames.get(getSystemName());
035        String userName = userNames.get(getSystemName());
036        if (sysName == null) sysName = manager.getAutoSystemName();
037        ActionRequestUpdateOfSensor copy = new ActionRequestUpdateOfSensor(sysName, userName);
038        copy.setComment(getComment());
039        _selectNamedBean.copy(copy._selectNamedBean);
040        return manager.registerAction(copy);
041    }
042
043    public LogixNG_SelectNamedBean<Sensor> getSelectNamedBean() {
044        return _selectNamedBean;
045    }
046
047    /** {@inheritDoc} */
048    @Override
049    public Category getCategory() {
050        return Category.ITEM;
051    }
052
053    /** {@inheritDoc} */
054    @Override
055    public void execute() throws JmriException {
056        Sensor sensor = _selectNamedBean.evaluateNamedBean(getConditionalNG());
057
058        if (sensor == null) return;
059
060        ThreadingUtil.runOnLayoutWithJmriException(() -> {
061            sensor.requestUpdateFromLayout();
062        });
063    }
064
065    @Override
066    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
067        throw new UnsupportedOperationException("Not supported.");
068    }
069
070    @Override
071    public int getChildCount() {
072        return 0;
073    }
074
075    @Override
076    public String getShortDescription(Locale locale) {
077        return Bundle.getMessage(locale, "ActionRequestUpdateOfSensor_Short");
078    }
079
080    @Override
081    public String getLongDescription(Locale locale) {
082        String namedBean = _selectNamedBean.getDescription(locale);
083        return Bundle.getMessage(locale, "ActionRequestUpdateOfSensor_Long", namedBean);
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public void setup() {
089        // Do nothing
090    }
091
092    /** {@inheritDoc} */
093    @Override
094    public void registerListenersForThisClass() {
095        _selectNamedBean.registerListeners();
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    public void unregisterListenersForThisClass() {
101        _selectNamedBean.unregisterListeners();
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) {
107        _selectNamedBean.getUsageDetail(level, bean, report, cdl, this, LogixNG_SelectNamedBean.Type.Action);
108    }
109
110    /** {@inheritDoc} */
111    @Override
112    public void propertyChange(PropertyChangeEvent evt) {
113        getConditionalNG().execute();
114    }
115
116    /** {@inheritDoc} */
117    @Override
118    public void disposeMe() {
119    }
120
121//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionRequestUpdateOfSensor.class);
122}