001package jmri.jmrit.logixng.actions.swing;
002
003import java.awt.GridBagConstraints;
004import java.awt.GridBagLayout;
005import java.util.List;
006
007import javax.annotation.CheckForNull;
008import javax.annotation.Nonnull;
009import javax.swing.*;
010
011import jmri.*;
012import jmri.jmrit.logixng.*;
013import jmri.jmrit.logixng.actions.ActionListenOnBeansLocalVariable;
014import jmri.jmrit.logixng.actions.NamedBeanType;
015import jmri.util.swing.JComboBoxUtil;
016
017/**
018 * Configures an ActionListenOnBeansLocalVariable object with a Swing JPanel.
019 *
020 * @author Daniel Bergqvist Copyright 2022
021 */
022public class ActionListenOnBeansLocalVariableSwing extends AbstractDigitalActionSwing {
023
024    private JComboBox<NamedBeanType> _namedBeanTypeComboBox;
025    private JCheckBox _listenOnAllPropertiesCheckBox;
026    private JTextField _localVariableBeanToListenOn;
027    private JTextField _localVariableNamedBean;
028    private JTextField _localVariableEvent;
029    private JTextField _localVariableNewValue;
030
031    @Override
032    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
033        if ((object != null) && !(object instanceof ActionListenOnBeansLocalVariable)) {
034            throw new IllegalArgumentException("object must be an ActionListenOnBeansLocalVariable but is a: "+object.getClass().getName());
035        }
036
037        ActionListenOnBeansLocalVariable action = (ActionListenOnBeansLocalVariable)object;
038
039        JLabel namedBeanTypeLabel = new JLabel(Bundle.getMessage("ActionListenOnBeansLocalVariableSwing_NamedBeanType"));
040        _namedBeanTypeComboBox = new JComboBox<>();
041        for (NamedBeanType item : NamedBeanType.values()) {
042            _namedBeanTypeComboBox.addItem(item);
043        }
044        JComboBoxUtil.setupComboBoxMaxRows(_namedBeanTypeComboBox);
045
046        JLabel listenOnAllPropertiesLabel = new JLabel(Bundle.getMessage("ActionListenOnBeansLocalVariableSwing_ListenOnAllPropertiesCheckBox"));
047        _listenOnAllPropertiesCheckBox = new JCheckBox();
048
049        JLabel localVariableBeanToListenOnLabel = new JLabel(Bundle.getMessage("ActionListenOnBeansLocalVariableSwing_LocalVariableBeanToListenOn"));
050        _localVariableBeanToListenOn = new JTextField(20);
051
052        JLabel localVariableNamedBeanLabel = new JLabel(Bundle.getMessage("ActionListenOnBeansSwing_LocalVariableNamedBean"));
053        _localVariableNamedBean = new JTextField(20);
054
055        JLabel localVariableEventLabel = new JLabel(Bundle.getMessage("ActionListenOnBeansSwing_LocalVariableEvent"));
056        _localVariableEvent = new JTextField(20);
057
058        JLabel localVariableNewValueLabel = new JLabel(Bundle.getMessage("ActionListenOnBeansSwing_LocalVariableNewValue"));
059        _localVariableNewValue = new JTextField(20);
060
061        panel = new JPanel();
062        panel.setLayout(new GridBagLayout());
063        GridBagConstraints constraint = new GridBagConstraints();
064        constraint.gridwidth = 1;
065        constraint.gridheight = 1;
066        constraint.gridx = 0;
067        constraint.gridy = 0;
068        constraint.anchor = GridBagConstraints.EAST;
069        panel.add(namedBeanTypeLabel, constraint);
070        namedBeanTypeLabel.setLabelFor(_namedBeanTypeComboBox);
071        constraint.gridy = 1;
072        panel.add(listenOnAllPropertiesLabel, constraint);
073        listenOnAllPropertiesLabel.setLabelFor(_listenOnAllPropertiesCheckBox);
074        constraint.gridy = 2;
075        panel.add(localVariableBeanToListenOnLabel, constraint);
076        localVariableBeanToListenOnLabel.setLabelFor(_localVariableBeanToListenOn);
077        constraint.gridy = 3;
078        panel.add(localVariableNamedBeanLabel, constraint);
079        localVariableNamedBeanLabel.setLabelFor(_localVariableNamedBean);
080        constraint.gridy = 4;
081        panel.add(localVariableEventLabel, constraint);
082        localVariableEventLabel.setLabelFor(_localVariableEvent);
083        constraint.gridy = 5;
084        panel.add(localVariableNewValueLabel, constraint);
085        localVariableNewValueLabel.setLabelFor(_localVariableNewValue);
086
087        // Add some space
088        constraint.gridx = 1;
089        constraint.gridy = 0;
090        panel.add(new JLabel(" "), constraint);
091
092        constraint.gridx = 2;
093        constraint.gridy = 0;
094        constraint.anchor = GridBagConstraints.WEST;
095        panel.add(_namedBeanTypeComboBox, constraint);
096        constraint.gridy = 1;
097        panel.add(_listenOnAllPropertiesCheckBox, constraint);
098        constraint.gridy = 2;
099        panel.add(_localVariableBeanToListenOn, constraint);
100        constraint.gridy = 3;
101        panel.add(_localVariableNamedBean, constraint);
102        constraint.gridy = 4;
103        panel.add(_localVariableEvent, constraint);
104        constraint.gridy = 5;
105        panel.add(_localVariableNewValue, constraint);
106
107        if (action != null) {
108            _namedBeanTypeComboBox.setSelectedItem(action.getNamedBeanType());
109            _listenOnAllPropertiesCheckBox.setSelected(action.getListenOnAllProperties());
110
111            _localVariableBeanToListenOn.setText(action.getLocalVariableBeanToListenOn());
112
113            _localVariableNamedBean.setText(action.getLocalVariableNamedBean());
114            _localVariableEvent.setText(action.getLocalVariableEvent());
115            _localVariableNewValue.setText(action.getLocalVariableNewValue());
116        }
117    }
118
119    /** {@inheritDoc} */
120    @Override
121    public boolean validate(@Nonnull List<String> errorMessages) {
122        return true;
123    }
124
125    /** {@inheritDoc} */
126    @Override
127    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
128        ActionListenOnBeansLocalVariable action = new ActionListenOnBeansLocalVariable(systemName, userName);
129        updateObject(action);
130        return InstanceManager.getDefault(DigitalActionManager.class).registerAction(action);
131    }
132
133    /** {@inheritDoc} */
134    @Override
135    public void updateObject(@Nonnull Base object) {
136        if (!(object instanceof ActionListenOnBeansLocalVariable)) {
137            throw new IllegalArgumentException("object must be an ActionListenOnBeansLocalVariable but is a: "+object.getClass().getName());
138        }
139
140        ActionListenOnBeansLocalVariable action = (ActionListenOnBeansLocalVariable)object;
141        if (_namedBeanTypeComboBox.getSelectedIndex() != -1) {
142            action.setNamedBeanType(_namedBeanTypeComboBox.getItemAt(_namedBeanTypeComboBox.getSelectedIndex()));
143        }
144        action.setListenOnAllProperties(_listenOnAllPropertiesCheckBox.isSelected());
145
146        action.setLocalVariableBeanToListenOn(_localVariableBeanToListenOn.getText());
147
148        action.setLocalVariableNamedBean(_localVariableNamedBean.getText());
149        action.setLocalVariableEvent(_localVariableEvent.getText());
150        action.setLocalVariableNewValue(_localVariableNewValue.getText());
151    }
152
153    /** {@inheritDoc} */
154    @Override
155    public String toString() {
156        return Bundle.getMessage("ActionListenOnBeansLocalVariable_Short");
157    }
158
159    @Override
160    public void dispose() {
161    }
162
163
164//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionListenOnBeansLocalVariableSwing.class);
165
166}