001package jmri.beans;
002
003import java.beans.IndexedPropertyChangeEvent;
004import java.beans.PropertyVetoException;
005import java.util.Set;
006
007/**
008 * A Bean with support for {@link java.beans.VetoableChangeListener}s.
009 *
010 * @author Randall Wood Copyright 2015, 2016, 2020
011 */
012public class ConstrainedArbitraryBean extends ConstrainedBean {
013
014    protected final ArbitraryPropertySupport arbitraryPropertySupport = new ArbitraryPropertySupport(this);
015
016    @Override
017    public void setProperty(String key, Object value) {
018        try {
019            this.fireVetoableChange(key, getProperty(key), value);
020            this.arbitraryPropertySupport.setProperty(key, value);
021        } catch (PropertyVetoException ex) {
022            // fire a property change that does not have the new value to indicate
023            // to any other listeners that the property was "reset" back to its
024            // original value as a result of the veto
025            this.firePropertyChange(key, getProperty(key), getProperty(key));
026        }
027    }
028
029    @Override
030    public void setIndexedProperty(String key, int index, Object value) {
031        try {
032            Object old = this.getIndexedPropertyOrNull(key, index);
033            this.fireVetoableChange(new IndexedPropertyChangeEvent(this, key, old, value, index));
034            this.arbitraryPropertySupport.setIndexedProperty(key, index, value);
035        } catch (PropertyVetoException ex) {
036            // fire a property change that does not have the new value to indicate
037            // to any other listeners that the property was "reset" back to its
038            // original value as a result of the veto
039            this.fireIndexedPropertyChange(key, index, getProperty(key), getProperty(key));
040        }
041    }
042
043    @Override
044    public Object getIndexedProperty(String key, int index) {
045        return this.arbitraryPropertySupport.getIndexedProperty(key, index);
046    }
047
048    @Override
049    public Object getProperty(String key) {
050        return this.arbitraryPropertySupport.getProperty(key);
051    }
052
053    @Override
054    public boolean hasProperty(String key) {
055        return this.arbitraryPropertySupport.hasProperty(key);
056    }
057
058    @Override
059    public boolean hasIndexedProperty(String key) {
060        return this.arbitraryPropertySupport.hasIndexedProperty(key);
061    }
062
063    @Override
064    public Set<String> getPropertyNames() {
065        return this.arbitraryPropertySupport.getPropertyNames();
066    }
067
068}