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