001package jmri.jmrit.logixng.expressions.swing;
002
003import java.util.*;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.JmriException;
011import jmri.jmrit.logixng.Base;
012import jmri.jmrit.logixng.DigitalExpressionManager;
013import jmri.jmrit.logixng.MaleSocket;
014import jmri.jmrit.logixng.expressions.Antecedent;
015
016/**
017 * Configures an ExpressionTurnout object with a Swing JPanel.
018 * 
019 * @author Daniel Bergqvist Copyright 2021
020 */
021public class AntecedentSwing extends AbstractDigitalExpressionSwing {
022
023    private JTextField _antecedent;
024    
025    @Override
026    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
027        Antecedent expression = (Antecedent)object;
028        panel = new JPanel();
029        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
030        JPanel antecedentPanel = new JPanel();
031        JLabel label = new JLabel(Bundle.getMessage("Antecedent_Antecedent"));
032        _antecedent = new JTextField();
033        _antecedent.setColumns(40);
034        if (expression != null) _antecedent.setText(expression.getAntecedent());
035        antecedentPanel.add(label);
036        antecedentPanel.add(_antecedent);
037        panel.add(antecedentPanel);
038        JLabel descriptionLabel = new JLabel(Bundle.getMessage("Antecedent_Description"));
039        JPanel subPanel = new JPanel();
040        subPanel.add(descriptionLabel);
041        panel.add(subPanel);
042    }
043    
044    /** {@inheritDoc} */
045    @Override
046    public boolean validate(@Nonnull List<String> errorMessages) {
047        return true;
048    }
049    
050    /** {@inheritDoc} */
051    @Override
052    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
053        Antecedent expression = new Antecedent(systemName, userName);
054        updateObject(expression);
055        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
056    }
057    
058    /** {@inheritDoc} */
059    @Override
060    public void updateObject(@Nonnull Base object) {
061        if (!(object instanceof Antecedent)) {
062            throw new IllegalArgumentException("object must be an Antecedent but is a: "+object.getClass().getName());
063        }
064        
065        Antecedent expression = (Antecedent)object;
066        
067        try {
068            expression.setAntecedent(_antecedent.getText());
069        } catch (JmriException ex) {
070            log.error("Error when parsing formula", ex);
071        }
072    }
073    
074    /** {@inheritDoc} */
075    @Override
076    public String toString() {
077        return Bundle.getMessage("Antecedent_Short");
078    }
079    
080    @Override
081    public void dispose() {
082    }
083    
084    
085    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AntecedentSwing.class);
086    
087}