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.ActionRequestUpdateOfSensor;
014import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
016
017/**
018 * Configures an ActionRequestUpdateOfSensor object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2024
021 */
022public class ActionRequestUpdateOfSensorSwing extends AbstractDigitalActionSwing {
023
024    private LogixNG_SelectNamedBeanSwing<Sensor> _selectNamedBeanSwing;
025
026
027    public ActionRequestUpdateOfSensorSwing() {
028    }
029
030    public ActionRequestUpdateOfSensorSwing(JDialog dialog) {
031        super.setJDialog(dialog);
032    }
033
034    @Override
035    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
036        ActionRequestUpdateOfSensor action = (ActionRequestUpdateOfSensor)object;
037
038        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
039                InstanceManager.getDefault(SensorManager.class), getJDialog(), this);
040
041        panel = new JPanel();
042        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
043
044        JPanel innerPanel = new JPanel();
045
046        JPanel _tabbedPaneNamedBean;
047
048        if (action != null) {
049            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean());
050        } else {
051            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
052        }
053
054        JComponent[] components = new JComponent[]{
055            _tabbedPaneNamedBean
056        };
057
058        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
059                Bundle.getMessage("ActionRequestUpdateOfSensor_Components"), components);
060
061        for (JComponent c : componentList) innerPanel.add(c);
062
063        JPanel infoPanel = new JPanel();
064        infoPanel.add(new JLabel(Bundle.getMessage("ActionRequestUpdateOfSensor_Info")));
065
066        panel.add(innerPanel);
067        panel.add(infoPanel);
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public boolean validate(@Nonnull List<String> errorMessages) {
073        // Create a temporary action to test formula
074        ActionRequestUpdateOfSensor action = new ActionRequestUpdateOfSensor("IQDA1", null);
075
076        _selectNamedBeanSwing.validate(action.getSelectNamedBean(), 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        ActionRequestUpdateOfSensor action = new ActionRequestUpdateOfSensor(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 ActionRequestUpdateOfSensor)) {
099            throw new IllegalArgumentException("object must be an ActionRequestUpdateOfSensor but is a: "+object.getClass().getName());
100        }
101        ActionRequestUpdateOfSensor action = (ActionRequestUpdateOfSensor)object;
102        _selectNamedBeanSwing.updateObject(action.getSelectNamedBean());
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public String toString() {
108        return Bundle.getMessage("ActionRequestUpdateOfSensor_Short");
109    }
110
111    @Override
112    public void dispose() {
113        _selectNamedBeanSwing.dispose();
114    }
115
116
117//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionRequestUpdateOfSensorSwing.class);
118
119}