001package jmri.jmrit.logixng.expressions.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.Base;
011import jmri.jmrit.logixng.MaleSocket;
012import jmri.jmrit.logixng.StringExpressionManager;
013import jmri.jmrit.logixng.expressions.*;
014
015/**
016 * Configures an StringExpressionConstant object with a Swing JPanel.
017 * 
018 * @author Daniel Bergqvist Copyright 2021
019 */
020public class StringExpressionConstantSwing extends AbstractStringExpressionSwing {
021
022    private JTextField _constant;
023    
024    @Override
025    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
026        StringExpressionConstant expression = (StringExpressionConstant)object;
027        panel = new JPanel();
028        JLabel label = new JLabel(Bundle.getMessage("StringExpressionConstant_Constant"));
029        _constant = new JTextField();
030        _constant.setColumns(20);
031        if (expression != null) _constant.setText(expression.getValue());
032        panel.add(label);
033        panel.add(_constant);
034    }
035    
036    /** {@inheritDoc} */
037    @Override
038    public boolean validate(@Nonnull List<String> errorMessages) {
039        return true;
040    }
041    
042    /** {@inheritDoc} */
043    @Override
044    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
045        StringExpressionConstant expression = new StringExpressionConstant(systemName, userName);
046        updateObject(expression);
047        return InstanceManager.getDefault(StringExpressionManager.class).registerExpression(expression);
048    }
049    
050    /** {@inheritDoc} */
051    @Override
052    public void updateObject(@Nonnull Base object) {
053        if (!(object instanceof StringExpressionConstant)) {
054            throw new IllegalArgumentException("object must be an StringExpressionConstant but is a: "+object.getClass().getName());
055        }
056        
057        StringExpressionConstant expression = (StringExpressionConstant)object;
058        
059        if (!_constant.getText().isEmpty()) {
060            expression.setValue(_constant.getText());
061        } else {
062            expression.setValue("");
063        }
064    }
065    
066    /** {@inheritDoc} */
067    @Override
068    public String toString() {
069        return Bundle.getMessage("StringExpressionConstant_Short");
070    }
071    
072    @Override
073    public void dispose() {
074    }
075    
076}