001package jmri.implementation;
002
003import java.beans.PropertyChangeEvent;
004import jmri.Conditional;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * A service class for monitoring a bound property in one of the JMRI Named
010 * beans For use with properties having two states which are determined by
011 * equality to a String value (e.g. Internal Memory).
012 * <p>
013 * This file is part of JMRI.
014 * <p>
015 * JMRI is free software; you can redistribute it and/or modify it under the
016 * terms of version 2 of the GNU General Public License as published by the Free
017 * Software Foundation. See the "COPYING" file for a copy of this license.
018 * <p>
019 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
020 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
021 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
022 *
023 * @author Pete Cressman Copyright (C) 2009
024 * @since 2.5.1
025 */
026public class JmriMemoryPropertyListener extends JmriSimplePropertyListener {
027
028    String _data;
029
030    JmriMemoryPropertyListener(String propName, int type, String name, Conditional.Type varType,
031            Conditional client, String data) {
032        super(propName, type, name, varType, client);
033        _data = data;
034    }
035
036    @Override
037    public void propertyChange(PropertyChangeEvent evt) {
038        log.debug("\"{}\" sent PropertyChangeEvent {}, old value =\"{}\", new value =\"{}, enabled = {}", _varName, evt.getPropertyName(), evt.getOldValue(), evt.getNewValue(), _enabled);
039        if (getPropertyName().equals(evt.getPropertyName())) {
040            String newValue = (String) evt.getNewValue();
041            String oldValue = (String) evt.getOldValue();
042            if (newValue == null) {
043                return;
044            }
045            if (oldValue == null) {
046                return;
047            }
048            if (newValue.equals(_data) || oldValue.equals(_data)) {
049                // property has changed to/from the watched state, calculate
050                super.propertyChange(evt);
051            }
052        }
053    }
054    private final static Logger log = LoggerFactory.getLogger(JmriMemoryPropertyListener.class);
055}