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