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.*;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.actions.AnalogActionLightIntensity;
012import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
013
014/**
015 * Configures an AnalogActionLightIntensity object with a Swing JPanel.
016 *
017 * @author Daniel Bergqvist Copyright 2021
018 */
019public class AnalogActionLightIntensitySwing extends AbstractAnalogActionSwing {
020
021    private LogixNG_SelectNamedBeanSwing<VariableLight> _selectNamedBeanSwing;
022
023
024    @Override
025    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
026        AnalogActionLightIntensity action = (AnalogActionLightIntensity)object;
027
028        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
029                InstanceManager.getDefault(VariableLightManager.class), getJDialog(), this);
030
031        panel = new JPanel();
032        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
033
034        JPanel _tabbedPaneNamedBean;
035        if (action != null) {
036            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean());
037        } else {
038            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
039        }
040
041        panel.add(_tabbedPaneNamedBean);
042
043        panel.add(javax.swing.Box.createVerticalStrut(10));
044
045        JPanel labelPanel = new JPanel();
046        labelPanel.add(new JLabel(Bundle.getMessage("AnalogActionLightIntensity_Descr")));
047        panel.add(labelPanel);
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public boolean validate(@Nonnull List<String> errorMessages) {
053        // Create a temporary action to test formula
054        AnalogActionLightIntensity action = new AnalogActionLightIntensity("IQAA1", null);
055
056        _selectNamedBeanSwing.validate(action.getSelectNamedBean(), errorMessages);
057        return errorMessages.isEmpty();
058    }
059
060    /** {@inheritDoc} */
061    @Override
062    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
063        AnalogActionLightIntensity action = new AnalogActionLightIntensity(systemName, userName);
064        updateObject(action);
065        return InstanceManager.getDefault(AnalogActionManager.class).registerAction(action);
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public void updateObject(@Nonnull Base object) {
071        if (! (object instanceof AnalogActionLightIntensity)) {
072            throw new IllegalArgumentException("object must be an AnalogActionLightIntensity but is a: "+object.getClass().getName());
073        }
074        AnalogActionLightIntensity action = (AnalogActionLightIntensity)object;
075        _selectNamedBeanSwing.updateObject(action.getSelectNamedBean());
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public String toString() {
081        return Bundle.getMessage("AnalogActionLightIntensity_Short");
082    }
083
084    @Override
085    public void dispose() {
086    }
087
088}