001package jmri.implementation;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.util.ArrayList;
006import jmri.Conditional;
007import jmri.NamedBean;
008import jmri.NamedBeanHandle;
009
010/**
011 * A service base class for monitoring a bound property in one of the JMRI Named
012 * beans (Turnout, Sensor, etc).
013 * <p>
014 * This file is part of JMRI.
015 * <p>
016 * JMRI is free software; you can redistribute it and/or modify it under the
017 * terms of version 2 of the GNU General Public License as published by the Free
018 * Software Foundation. See the "COPYING" file for a copy of this license.
019 * <p>
020 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
021 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
022 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
023 *
024 * @author Pete Cressman Copyright (C) 2009
025 * @since 2.5.1
026 */
027public class JmriSimplePropertyListener implements PropertyChangeListener {
028
029    int _type;
030    String _varName;
031    Conditional.Type _varType;
032    String _propertyName;
033    ArrayList<Conditional> _clients;
034    boolean _enabled;
035    NamedBeanHandle<?> _namedBean;
036
037    JmriSimplePropertyListener(String propName, int type, String varName, Conditional.Type varType, Conditional client) {
038        _propertyName = propName;
039        _type = type;
040        _varName = varName;
041        _varType = varType;
042        _clients = new ArrayList<Conditional>();
043        _clients.add(client);
044        _enabled = true;
045    }
046
047    JmriSimplePropertyListener(String propName, int type, NamedBeanHandle<?> namedBean, Conditional.Type varType, Conditional client) {
048        _propertyName = propName;
049        _type = type;
050        _namedBean = namedBean;
051        _varType = varType;
052        _clients = new ArrayList<Conditional>();
053        _clients.add(client);
054        _enabled = true;
055    }
056
057    NamedBeanHandle<?> getNamedBean() {
058        return _namedBean;
059    }
060
061    public NamedBean getBean() {
062        if (_namedBean != null) {
063            return  _namedBean.getBean();
064        }
065        return null;
066    }
067
068    public int getType() {
069        return _type;
070    }
071
072    public String getPropertyName() {
073        return _propertyName;
074    }
075
076    public Conditional.Type getVarType() {
077        return _varType;
078    }
079
080    public String getDevName() {
081        if (_namedBean != null) {
082            return _namedBean.getName();
083        }
084        return _varName;
085    }
086
087    public void addConditional(Conditional client) {
088        _clients.add(client);
089    }
090
091    public void setEnabled(boolean state) {
092        _enabled = state;
093    }
094
095    public void calculateClient(int idx, PropertyChangeEvent evt) {
096        _clients.get(idx).calculate(_enabled, evt);
097    }
098
099    /**
100     * When _enabled is false, Conditional.calculate will compute the state of
101     * the conditional, but will not trigger its actions. When _enabled is true,
102     * Conditional.calculates its state and trigger its actions if its state has
103     * changed.
104     */
105    @Override
106    public void propertyChange(PropertyChangeEvent evt) {
107        //log.debug("\""+_varName+"\" sent PropertyChangeEvent "+evt.getPropertyName()+
108        //    ", old value =\""+evt.getOldValue()+"\", new value =\""+evt.getNewValue()+
109        //    ", enabled = "+_enabled);
110        Object newValue = evt.getNewValue();
111        if (newValue != null && newValue.equals(evt.getOldValue())) {
112            return;
113        }
114        for (int i = 0; i < _clients.size(); i++) {
115            _clients.get(i).calculate(_enabled, evt);
116        }
117    }
118}