001package jmri.jmrit.logixng.expressions.swing;
002
003import java.io.IOException;
004import java.util.List;
005
006import javax.annotation.CheckForNull;
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.*;
011import jmri.jmrit.logixng.*;
012import jmri.jmrit.logixng.expressions.ExpressionLinuxLinePower;
013import jmri.jmrit.logixng.expressions.ExpressionLinuxLinePower.NoPowerSuppliesException;
014import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
015import jmri.util.swing.JComboBoxUtil;
016
017/**
018 * Configures an ExpressionLinuxLinePower object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright (C) 2023
021 */
022public class ExpressionLinuxLinePowerSwing extends AbstractDigitalExpressionSwing {
023
024    private JComboBox<Is_IsNot_Enum> _is_IsNot_ComboBox;
025
026
027    public ExpressionLinuxLinePowerSwing() {
028    }
029
030    public ExpressionLinuxLinePowerSwing(JDialog dialog) {
031        super.setJDialog(dialog);
032    }
033
034    @Override
035    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
036        ExpressionLinuxLinePower expression = (ExpressionLinuxLinePower)object;
037
038        panel = new JPanel();
039        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
040
041        _is_IsNot_ComboBox = new JComboBox<>();
042        for (Is_IsNot_Enum e : Is_IsNot_Enum.values()) {
043            _is_IsNot_ComboBox.addItem(e);
044        }
045        JComboBoxUtil.setupComboBoxMaxRows(_is_IsNot_ComboBox);
046
047        if (expression != null) {
048            _is_IsNot_ComboBox.setSelectedItem(expression.get_Is_IsNot());
049        }
050
051        JComponent[] components = new JComponent[]{
052            _is_IsNot_ComboBox
053        };
054
055        JPanel innerPanel = new JPanel();
056        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
057                Bundle.getMessage("ExpressionLinuxLinePower_Components"), components);
058
059        for (JComponent c : componentList) innerPanel.add(c);
060
061        panel.add(innerPanel);
062
063        try {
064            ExpressionLinuxLinePower tempExpression = new ExpressionLinuxLinePower("IQDE1", null);
065            tempExpression.isLinePowerOnline();
066        } catch (NoPowerSuppliesException e) {
067            JPanel p = new JPanel();
068            p.add(new JLabel(Bundle.getMessage("ExpressionLinuxLinePower_NoPowerSuppliesFound")));
069            panel.add(p);
070        } catch (IOException | JmriException e) {
071            JPanel p = new JPanel();
072            p.add(new JLabel(Bundle.getMessage("ExpressionLinuxLinePower_Error")));
073            panel.add(p);
074        }
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public boolean validate(@Nonnull List<String> errorMessages) {
080        return true;
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
086        ExpressionLinuxLinePower expression = new ExpressionLinuxLinePower(systemName, userName);
087        updateObject(expression);
088        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
089    }
090
091    /** {@inheritDoc} */
092    @Override
093    public void updateObject(@Nonnull Base object) {
094        if (! (object instanceof ExpressionLinuxLinePower)) {
095            throw new IllegalArgumentException("object must be an ExpressionLinuxLinePower but is a: "+object.getClass().getName());
096        }
097        ExpressionLinuxLinePower expression = (ExpressionLinuxLinePower)object;
098
099        expression.set_Is_IsNot((Is_IsNot_Enum)_is_IsNot_ComboBox.getSelectedItem());
100    }
101
102    /** {@inheritDoc} */
103    @Override
104    public String toString() {
105        return Bundle.getMessage("LinuxLinePower_Short");
106    }
107
108    @Override
109    public void dispose() {
110    }
111
112
113//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionLinuxLinePowerSwing.class);
114
115}