001package jmri.jmrit.logixng.actions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.Sensor;
011import jmri.SensorManager;
012import jmri.jmrit.logixng.*;
013import jmri.jmrit.logixng.actions.ActionSensor;
014import jmri.jmrit.logixng.actions.ActionSensor.SensorState;
015import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
016import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
017import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing;
018
019/**
020 * Configures an ActionSensor object with a Swing JPanel.
021 *
022 * @author Daniel Bergqvist Copyright 2021
023 */
024public class ActionSensorSwing extends AbstractDigitalActionSwing {
025
026    private LogixNG_SelectNamedBeanSwing<Sensor> _selectNamedBeanSwing;
027    private LogixNG_SelectEnumSwing<SensorState> _selectEnumSwing;
028
029
030    public ActionSensorSwing() {
031    }
032
033    public ActionSensorSwing(JDialog dialog) {
034        super.setJDialog(dialog);
035    }
036
037    @Override
038    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
039        ActionSensor action = (ActionSensor)object;
040
041        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
042                InstanceManager.getDefault(SensorManager.class), getJDialog(), this);
043
044        _selectEnumSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
045
046        panel = new JPanel();
047
048        JPanel _tabbedPaneNamedBean;
049        JPanel _tabbedPaneEnum;
050
051        if (action != null) {
052            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean());
053            _tabbedPaneEnum = _selectEnumSwing.createPanel(action.getSelectEnum(), SensorState.values());
054        } else {
055            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
056            _tabbedPaneEnum = _selectEnumSwing.createPanel(null, SensorState.values());
057        }
058
059        JComponent[] components = new JComponent[]{
060            _tabbedPaneNamedBean,
061            _tabbedPaneEnum};
062
063        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
064                Bundle.getMessage("ActionSensor_Components"), components);
065
066        for (JComponent c : componentList) panel.add(c);
067    }
068
069    /** {@inheritDoc} */
070    @Override
071    public boolean validate(@Nonnull List<String> errorMessages) {
072        // Create a temporary action to test formula
073        ActionSensor action = new ActionSensor("IQDA1", null);
074
075        _selectNamedBeanSwing.validate(action.getSelectNamedBean(), errorMessages);
076        _selectEnumSwing.validate(action.getSelectEnum(), errorMessages);
077
078        return errorMessages.isEmpty();
079    }
080
081    /** {@inheritDoc} */
082    @Override
083    public String getAutoSystemName() {
084        return InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName();
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
090        ActionSensor action = new ActionSensor(systemName, userName);
091        updateObject(action);
092        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public void updateObject(@Nonnull Base object) {
098        if (! (object instanceof ActionSensor)) {
099            throw new IllegalArgumentException("object must be an ActionSensor but is a: "+object.getClass().getName());
100        }
101        ActionSensor action = (ActionSensor)object;
102        _selectNamedBeanSwing.updateObject(action.getSelectNamedBean());
103        _selectEnumSwing.updateObject(action.getSelectEnum());
104    }
105
106    /** {@inheritDoc} */
107    @Override
108    public String toString() {
109        return Bundle.getMessage("Sensor_Short");
110    }
111
112    @Override
113    public void dispose() {
114        _selectNamedBeanSwing.dispose();
115        _selectEnumSwing.dispose();
116    }
117
118
119//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionSensorSwing.class);
120
121}