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