001package jmri.jmrit.logixng.expressions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JTextField;
010
011import jmri.InstanceManager;
012import jmri.jmrit.logixng.Base;
013import jmri.jmrit.logixng.AnalogExpressionManager;
014import jmri.jmrit.logixng.MaleSocket;
015import jmri.jmrit.logixng.expressions.AnalogExpressionConstant;
016
017/**
018 * Configures an ActionTurnout object with a Swing JPanel.
019 * 
020 * @author Daniel Bergqvist Copyright 2021
021 */
022public class AnalogExpressionConstantSwing extends AbstractAnalogExpressionSwing {
023
024    private JTextField _constant;
025    
026    @Override
027    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
028        AnalogExpressionConstant expression = (AnalogExpressionConstant)object;
029        panel = new JPanel();
030        JLabel label = new JLabel(Bundle.getMessage("AnalogExpressionConstant_Constant"));
031        _constant = new JTextField();
032        _constant.setColumns(10);
033        if (expression != null) _constant.setText(String.format("%1.2f", expression.getValue()));
034        panel.add(label);
035        panel.add(_constant);
036    }
037    
038    /** {@inheritDoc} */
039    @Override
040    public boolean validate(@Nonnull List<String> errorMessages) {
041        if (_constant.getText().isEmpty()) return true;
042        
043        try {
044            Double.parseDouble(_constant.getText());
045        } catch (NumberFormatException e) {
046            errorMessages.add(Bundle.getMessage("AnalogExpressionConstant_NotANumber", _constant.getText()));
047            return false;
048        }
049        return true;
050    }
051    
052    /** {@inheritDoc} */
053    @Override
054    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
055        AnalogExpressionConstant expression = new AnalogExpressionConstant(systemName, userName);
056        updateObject(expression);
057        return InstanceManager.getDefault(AnalogExpressionManager.class).registerExpression(expression);
058    }
059    
060    /** {@inheritDoc} */
061    @Override
062    public void updateObject(@Nonnull Base object) {
063        if (!(object instanceof AnalogExpressionConstant)) {
064            throw new IllegalArgumentException("object must be an AnalogExpressionConstant but is a: "+object.getClass().getName());
065        }
066        
067        AnalogExpressionConstant expression = (AnalogExpressionConstant)object;
068        
069        if (!_constant.getText().isEmpty()) {
070            expression.setValue(Double.parseDouble(_constant.getText()));
071        } else {
072            expression.setValue(0.0);
073        }
074    }
075    
076    /** {@inheritDoc} */
077    @Override
078    public String toString() {
079        return Bundle.getMessage("AnalogExpressionConstant_Short");
080    }
081    
082    @Override
083    public void dispose() {
084    }
085    
086}