001package jmri.jmrit.logixng.expressions;
002
003import java.util.Locale;
004
005import java.util.Map;
006
007import jmri.InstanceManager;
008import jmri.jmrit.logixng.*;
009
010/**
011 * Constant value.
012 * This can be useful for example by the ActionThrottle.
013 * 
014 * @author Daniel Bergqvist Copyright 2019
015 */
016public class StringExpressionConstant extends AbstractStringExpression {
017
018    private String _value = "";
019    
020    public StringExpressionConstant(String sys, String user)
021            throws BadUserNameException, BadSystemNameException {
022        
023        super(sys, user);
024    }
025    
026    @Override
027    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) {
028        StringExpressionManager manager = InstanceManager.getDefault(StringExpressionManager.class);
029        String sysName = systemNames.get(getSystemName());
030        String userName = userNames.get(getSystemName());
031        if (sysName == null) sysName = manager.getAutoSystemName();
032        StringExpressionConstant copy = new StringExpressionConstant(sysName, userName);
033        copy.setComment(getComment());
034        copy.setValue(_value);
035        return manager.registerExpression(copy);
036    }
037    
038    /** {@inheritDoc} */
039    @Override
040    public Category getCategory() {
041        return Category.ITEM;
042    }
043    
044    public void setValue(String value) {
045        assertListenersAreNotRegistered(log, "setValue");
046        _value = value;
047    }
048    
049    public String getValue() {
050        return _value;
051    }
052    
053    /** {@inheritDoc} */
054    @Override
055    public String evaluate() {
056        return _value;
057    }
058    
059    @Override
060    public FemaleSocket getChild(int index)
061            throws IllegalArgumentException, UnsupportedOperationException {
062        throw new UnsupportedOperationException("Not supported.");
063    }
064
065    @Override
066    public int getChildCount() {
067        return 0;
068    }
069
070    @Override
071    public String getShortDescription(Locale locale) {
072        return Bundle.getMessage(locale, "StringExpressionConstant_Short");
073    }
074
075    @Override
076    public String getLongDescription(Locale locale) {
077        if (_value == null) {
078            return Bundle.getMessage(locale, "StringExpressionConstant_LongNull");
079        } else {
080            return Bundle.getMessage(locale, "StringExpressionConstant_LongValue", _value);
081        }
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public void setup() {
087        // Do nothing
088    }
089    
090    /** {@inheritDoc} */
091    @Override
092    public void registerListenersForThisClass() {
093        // This class does not have any listeners registered, but we still don't
094        // want a caller to change the value then listeners are registered.
095        // So we set this property to warn the caller when the caller is using
096        // the class in the wrong way.
097        _listenersAreRegistered = true;
098    }
099    
100    /** {@inheritDoc} */
101    @Override
102    public void unregisterListenersForThisClass() {
103        _listenersAreRegistered = false;
104    }
105    
106    /** {@inheritDoc} */
107    @Override
108    public void disposeMe() {
109    }
110    
111    
112    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StringExpressionConstant.class);
113    
114}