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.Block;
010import jmri.BlockManager;
011import jmri.InstanceManager;
012import jmri.jmrit.logixng.*;
013import jmri.jmrit.logixng.actions.ActionBlock;
014import jmri.jmrit.logixng.actions.ActionBlock.DirectOperation;
015import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
016import jmri.jmrit.logixng.util.swing.*;
017
018
019/**
020 * Configures an ActionBlock object with a Swing JPanel.
021 *
022 * @author Daniel Bergqvist  Copyright 2021
023 * @author Dave Sand         Copyright 2021
024 */
025public class ActionBlockSwing extends AbstractDigitalActionSwing {
026
027    private LogixNG_SelectNamedBeanSwing<Block> _selectNamedBeanSwing;
028    private LogixNG_SelectEnumSwing<DirectOperation> _selectOperationSwing;
029    private LogixNG_SelectStringSwing _selectBlockValueSwing;
030
031
032    public ActionBlockSwing() {
033    }
034
035    public ActionBlockSwing(JDialog dialog) {
036        super.setJDialog(dialog);
037    }
038
039    @Override
040    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
041        ActionBlock action = (ActionBlock)object;
042
043        _selectNamedBeanSwing = new LogixNG_SelectNamedBeanSwing<>(
044                InstanceManager.getDefault(BlockManager.class), getJDialog(), this);
045
046        _selectOperationSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
047
048        _selectBlockValueSwing = new LogixNG_SelectStringSwing(getJDialog(), this);
049
050       panel = new JPanel();
051
052        JPanel tabbedPaneNamedBean;
053        JPanel tabbedPaneOperation;
054        JPanel tabbedPaneBlockValue;
055
056        if (action != null) {
057            tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(action.getSelectNamedBean());
058            tabbedPaneOperation = _selectOperationSwing.createPanel(action.getSelectEnum(), DirectOperation.values());
059            tabbedPaneBlockValue = _selectBlockValueSwing.createPanel(action.getSelectBlockValue());
060        } else {
061            tabbedPaneNamedBean = _selectNamedBeanSwing.createPanel(null);
062            tabbedPaneOperation = _selectOperationSwing.createPanel(null, DirectOperation.values());
063            tabbedPaneBlockValue = _selectBlockValueSwing.createPanel(null);
064        }
065
066        _selectOperationSwing.addAddressingListener((evt) -> { setDataPanelState(); });
067        _selectOperationSwing.addEnumListener((evt) -> { setDataPanelState(); });
068
069        setDataPanelState();
070
071        JComponent[] components = new JComponent[]{
072            tabbedPaneNamedBean,
073            tabbedPaneOperation,
074            tabbedPaneBlockValue};
075
076        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
077                Bundle.getMessage("ActionBlock_Components"), components);
078
079        for (JComponent c : componentList) panel.add(c);
080    }
081
082    private void setDataPanelState() {
083        boolean newState = _selectOperationSwing
084                .isEnumSelectedOrIndirectAddressing(DirectOperation.SetValue);
085        _selectBlockValueSwing.setEnabled(newState);
086    }
087
088    /** {@inheritDoc} */
089    @Override
090    public boolean validate(@Nonnull List<String> errorMessages) {
091
092        ActionBlock action = new ActionBlock("IQDA2", null);
093        _selectNamedBeanSwing.validate(action.getSelectNamedBean(), errorMessages);
094        _selectOperationSwing.validate(action.getSelectEnum(), errorMessages);
095        _selectBlockValueSwing.validate(action.getSelectBlockValue(), errorMessages);
096
097        return errorMessages.isEmpty();
098    }
099
100    /** {@inheritDoc} */
101    @Override
102    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
103        ActionBlock action = new ActionBlock(systemName, userName);
104        updateObject(action);
105        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
106    }
107
108    /** {@inheritDoc} */
109    @Override
110    public void updateObject(@Nonnull Base object) {
111        if (! (object instanceof ActionBlock)) {
112            throw new IllegalArgumentException("object must be an ActionBlock but is a: "+object.getClass().getName());
113        }
114        ActionBlock action = (ActionBlock) object;
115
116        _selectNamedBeanSwing.updateObject(action.getSelectNamedBean());
117        _selectOperationSwing.updateObject(action.getSelectEnum());
118        _selectBlockValueSwing.updateObject(action.getSelectBlockValue());
119    }
120
121    /** {@inheritDoc} */
122    @Override
123    public String toString() {
124        return Bundle.getMessage("ActionBlock_Short");
125    }
126
127    @Override
128    public void dispose() {
129        _selectNamedBeanSwing.dispose();
130        _selectOperationSwing.dispose();
131        _selectBlockValueSwing.dispose();
132    }
133
134
135//     private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionBlockSwing.class);
136
137}