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