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    /**
088     * Add a Conditional to be calculated when the property changes. A
089     * Conditional that is already a client is not added again, so that it is
090     * calculated only once for each change.
091     *
092     * @param client the Conditional to calculate
093     */
094    public void addConditional(Conditional client) {
095        if (!_clients.contains(client)) {
096            _clients.add(client);
097        }
098    }
099
100    public void setEnabled(boolean state) {
101        _enabled = state;
102    }
103
104    public void calculateClient(int idx, PropertyChangeEvent evt) {
105        _clients.get(idx).calculate(_enabled, evt);
106    }
107
108    /**
109     * When _enabled is false, Conditional.calculate will compute the state of
110     * the conditional, but will not trigger its actions. When _enabled is true,
111     * Conditional.calculates its state and trigger its actions if its state has
112     * changed.
113     */
114    @Override
115    public void propertyChange(PropertyChangeEvent evt) {
116        //log.debug("\""+_varName+"\" sent PropertyChangeEvent "+evt.getPropertyName()+
117        //    ", old value =\""+evt.getOldValue()+"\", new value =\""+evt.getNewValue()+
118        //    ", enabled = "+_enabled);
119        Object newValue = evt.getNewValue();
120        if (newValue != null && newValue.equals(evt.getOldValue())) {
121            return;
122        }
123        for (int i = 0; i < _clients.size(); i++) {
124            _clients.get(i).calculate(_enabled, evt);
125        }
126    }
127}