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.Turnout;
011import jmri.TurnoutManager;
012import jmri.jmrit.logixng.*;
013import jmri.jmrit.logixng.actions.ActionTurnoutLock;
014import jmri.jmrit.logixng.actions.ActionTurnoutLock.TurnoutLock;
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 ActionTurnoutLock object with a Swing JPanel.
021 *
022 * @author Daniel Bergqvist Copyright 2021
023 */
024public class ActionTurnoutLockSwing extends AbstractDigitalActionSwing {
025
026    private LogixNG_SelectNamedBeanSwing<Turnout> _selectNamedBeanSwing;
027    private LogixNG_SelectEnumSwing<TurnoutLock> _selectEnumSwing;
028
029
030    public ActionTurnoutLockSwing() {
031    }
032
033    public ActionTurnoutLockSwing(JDialog dialog) {
034        super.setJDialog(dialog);
035    }
036
037    @Override
038    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
039        ActionTurnoutLock action = (ActionTurnoutLock)object;
040
041        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
042                InstanceManager.getDefault(TurnoutManager.class), getJDialog(), this);
043
044        _selectEnumSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
045
046        panel = new JPanel();
047
048        JPanel _tabbedPaneNamedBean;
049        JPanel _tabbedPaneEnum;
050
051        if (action != null) {
052            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean());
053            _tabbedPaneEnum = _selectEnumSwing.createPanel(action.getSelectEnum(), TurnoutLock.values());
054        } else {
055            _tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
056            _tabbedPaneEnum = _selectEnumSwing.createPanel(null, TurnoutLock.values());
057        }
058
059        JComponent[] components = new JComponent[]{
060            _tabbedPaneNamedBean,
061            _tabbedPaneEnum};
062
063        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
064                Bundle.getMessage("ActionTurnoutLock_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        ActionTurnoutLock action = new ActionTurnoutLock("IQDA1", null);
074
075        _selectNamedBeanSwing.validate(action.getSelectNamedBean(), errorMessages);
076        _selectEnumSwing.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        ActionTurnoutLock action = new ActionTurnoutLock(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 ActionTurnoutLock)) {
093            throw new IllegalArgumentException("object must be an ActionTurnoutLock but is a: "+object.getClass().getName());
094        }
095        ActionTurnoutLock action = (ActionTurnoutLock)object;
096        _selectNamedBeanSwing.updateObject(action.getSelectNamedBean());
097        _selectEnumSwing.updateObject(action.getSelectEnum());
098    }
099
100    /** {@inheritDoc} */
101    @Override
102    public String toString() {
103        return Bundle.getMessage("TurnoutLock_Short");
104    }
105
106    @Override
107    public void dispose() {
108        _selectNamedBeanSwing.dispose();
109        _selectEnumSwing.dispose();
110    }
111
112
113//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionTurnoutLockSwing.class);
114
115}