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