001package jmri.jmrit.logixng.expressions;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.util.Locale;
006import java.util.Map;
007
008import jmri.InstanceManager;
009import jmri.JmriException;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.util.parser.ParserException;
012import jmri.jmrix.*;
013import static jmri.jmrix.JmrixConfigPane.NONE_SELECTED;
014
015/**
016 * Returns true if there is a connection of specified type.
017 *
018 * @author Daniel Bergqvist Copyright 2022
019 */
020public class ConnectionName extends AbstractDigitalExpression
021        implements PropertyChangeListener {
022
023    private String _manufacturer = NONE_SELECTED;
024    private String _connectionName = NONE_SELECTED;
025
026
027    public ConnectionName(String sys, String user)
028            throws BadUserNameException, BadSystemNameException {
029        super(sys, user);
030    }
031
032    @Override
033    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
034        DigitalExpressionManager manager = InstanceManager.getDefault(DigitalExpressionManager.class);
035        String sysName = systemNames.get(getSystemName());
036        String userName = userNames.get(getSystemName());
037        if (sysName == null) sysName = manager.getAutoSystemName();
038        ConnectionName copy = new ConnectionName(sysName, userName);
039        copy.setComment(getComment());
040        copy._manufacturer = _manufacturer;
041        copy._connectionName = _connectionName;
042        return manager.registerExpression(copy);
043    }
044
045    public void setManufacturer(String manufacturer) {
046        _manufacturer = manufacturer;
047    }
048
049    public String getManufacturer() {
050        return _manufacturer;
051    }
052
053    public void setConnectionName(String name) {
054        _connectionName = name;
055    }
056
057    public String getConnectionName() {
058        return _connectionName;
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    public Category getCategory() {
064        return Category.OTHER;
065    }
066
067    /** {@inheritDoc} */
068    @Override
069    public boolean evaluate() throws JmriException {
070        ConnectionConfigManager manager = InstanceManager.getDefault(ConnectionConfigManager.class);
071        for (ConnectionConfig cc : manager.getConnections()) {
072            if (cc.getManufacturer().equals(_manufacturer) && cc.name().equals(_connectionName)) {
073                return true;
074            }
075        }
076        return false;
077    }
078
079    @Override
080    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
081        throw new UnsupportedOperationException("Not supported.");
082    }
083
084    @Override
085    public int getChildCount() {
086        return 0;
087    }
088
089    @Override
090    public String getShortDescription(Locale locale) {
091        return Bundle.getMessage(locale, "ConnectionName_Short");
092    }
093
094    @Override
095    public String getLongDescription(Locale locale) {
096        return Bundle.getMessage(locale, "ConnectionName_Long", _manufacturer, _connectionName);
097    }
098
099    /** {@inheritDoc} */
100    @Override
101    public void setup() {
102        // Do nothing
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public void registerListenersForThisClass() {
108    }
109
110    /** {@inheritDoc} */
111    @Override
112    public void unregisterListenersForThisClass() {
113    }
114
115    /** {@inheritDoc} */
116    @Override
117    public void propertyChange(PropertyChangeEvent evt) {
118        getConditionalNG().execute();
119    }
120
121    /** {@inheritDoc} */
122    @Override
123    public void disposeMe() {
124    }
125
126
127//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConnectionName.class);
128}