001package jmri.jmrit.logixng.expressions.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.expressions.ExpressionSensorEdge;
012import jmri.jmrit.logixng.expressions.ExpressionSensorEdge.SensorState;
013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
014import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectNamedBeanSwing;
016
017/**
018 * Configures an ExpressionSensorEdge object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2022
021 */
022public class ExpressionSensorEdgeSwing extends AbstractDigitalExpressionSwing {
023
024    private LogixNG_SelectNamedBeanSwing<Sensor> _selectNamedBeanSwing;
025    private LogixNG_SelectEnumSwing<SensorState> _selectEnumFromStateSwing;
026    private LogixNG_SelectEnumSwing<SensorState> _selectEnumToStateSwing;
027    private JCheckBox _onlyTrueOnceCheckBox;
028
029
030    public ExpressionSensorEdgeSwing() {
031    }
032
033    public ExpressionSensorEdgeSwing(JDialog dialog) {
034        super.setJDialog(dialog);
035    }
036
037    @Override
038    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
039        ExpressionSensorEdge expression = (ExpressionSensorEdge)object;
040
041        if (expression == null) {
042            // Create a temporary expression since only direct addressing is allowed
043            expression = new ExpressionSensorEdge("IQDE1", null);
044        }
045
046        panel = new JPanel();
047        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
048
049        JPanel innerPanel = new JPanel();
050
051        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
052                InstanceManager.getDefault(SensorManager.class), getJDialog(), this);
053
054        _selectEnumFromStateSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
055        _selectEnumToStateSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
056        _onlyTrueOnceCheckBox = new JCheckBox(Bundle.getMessage("ExpressionSensorEdge_OnlyTrueOnce"));
057
058        JPanel _tabbedPaneNamedBean;
059        JPanel _tabbedPaneEnumFromState;
060        JPanel _tabbedPaneEnumToState;
061
062        _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(expression.getSelectNamedBean());
063        _tabbedPaneEnumFromState = _selectEnumFromStateSwing.createPanel(expression.getSelectEnumFromState(), SensorState.values());
064        _tabbedPaneEnumToState = _selectEnumToStateSwing.createPanel(expression.getSelectEnumToState(), SensorState.values());
065        _onlyTrueOnceCheckBox.setSelected(expression.getOnlyTrueOnce());
066
067
068        JComponent[] components = new JComponent[]{
069            _tabbedPaneNamedBean,
070            _tabbedPaneEnumFromState,
071            _tabbedPaneEnumToState};
072
073        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
074                Bundle.getMessage("ExpressionSensorEdge_Components"), components);
075
076        for (JComponent c : componentList) innerPanel.add(c);
077
078        panel.add(innerPanel);
079        panel.add(_onlyTrueOnceCheckBox);
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public boolean validate(@Nonnull List<String> errorMessages) {
085        // Create a temporary expression to test formula
086        ExpressionSensorEdge expression = new ExpressionSensorEdge("IQDE1", null);
087
088        _selectNamedBeanSwing.validate(expression.getSelectNamedBean(), errorMessages);
089        _selectEnumFromStateSwing.validate(expression.getSelectEnumFromState(), errorMessages);
090        _selectEnumToStateSwing.validate(expression.getSelectEnumToState(), errorMessages);
091
092        return errorMessages.isEmpty();
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public String getAutoSystemName() {
098        return InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName();
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
104        ExpressionSensorEdge expression = new ExpressionSensorEdge(systemName, userName);
105        updateObject(expression);
106        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
107    }
108
109    /** {@inheritDoc} */
110    @Override
111    public void updateObject(@Nonnull Base object) {
112        if (! (object instanceof ExpressionSensorEdge)) {
113            throw new IllegalArgumentException("object must be an ExpressionSensorEdge but is a: "+object.getClass().getName());
114        }
115        ExpressionSensorEdge expression = (ExpressionSensorEdge)object;
116
117        _selectNamedBeanSwing.updateObject(expression.getSelectNamedBean());
118        _selectEnumFromStateSwing.updateObject(expression.getSelectEnumFromState());
119        _selectEnumToStateSwing.updateObject(expression.getSelectEnumToState());
120        expression.setOnlyTrueOnce(_onlyTrueOnceCheckBox.isSelected());
121    }
122
123    /** {@inheritDoc} */
124    @Override
125    public String toString() {
126        return Bundle.getMessage("SensorEdge_Short");
127    }
128
129    @Override
130    public void dispose() {
131    }
132
133
134//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSensorEdgeSwing.class);
135
136}