001package jmri.managers;
002
003import javax.annotation.*;
004import java.beans.PropertyChangeEvent;
005
006import jmri.InstanceManager;
007import jmri.Light;
008import jmri.LightManager;
009import jmri.Manager;
010import jmri.VariableLight;
011import jmri.VariableLightManager;
012import jmri.SystemConnectionMemo;
013
014/**
015 * Default implementation of a VariableLightManager.
016 *
017 * @author Bob Jacobsen       Copyright (C) 2004
018 * @author Daniel Bergqvist   Copyright (C) 2020
019 */
020public class DefaultVariableLightManager extends AbstractManager<VariableLight>
021        implements VariableLightManager {
022
023    /**
024     * Create a new VariableLightManager instance.
025     * 
026     * @param memo the system connection
027     */
028    public DefaultVariableLightManager(SystemConnectionMemo memo) {
029        super(memo);
030    }
031
032    /**
033     * Initializes a new VariableLightManager instance.
034     * @return itself
035     */
036    public DefaultVariableLightManager init() {
037        LightManager lm = InstanceManager.getDefault(LightManager.class);
038        lm.addPropertyChangeListener("beans", this);
039        for (Light l : lm.getNamedBeanSet()) {
040            if (l instanceof VariableLight) {
041                super.register((VariableLight) l);
042            }
043        }
044        return this;
045    }
046
047    /** {@inheritDoc} */
048    @Override
049    public void dispose() {
050        super.dispose();
051    }
052
053    /** {@inheritDoc} */
054    @Override
055    public int getXMLOrder() {
056        return Manager.LIGHTS;
057    }
058
059    /** {@inheritDoc} */
060    @Override
061    public char typeLetter() {
062        return 'L';
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    @Nonnull 
068    public String getBeanTypeHandled(boolean plural) {
069        return Bundle.getMessage(plural ? "BeanNameVariableLights" : "BeanNameVariableLight");
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public Class<VariableLight> getNamedBeanClass() {
077        return VariableLight.class;
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    @OverridingMethodsMustInvokeSuper
083    public void register(@Nonnull VariableLight s) {
084        throw new UnsupportedOperationException("Not supported. Use LightManager.register() instead");
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    @OverridingMethodsMustInvokeSuper
090    public void deregister(@Nonnull VariableLight s) {
091        throw new UnsupportedOperationException("Not supported. Use LightManager.deregister() instead");
092    }
093
094    /** {@inheritDoc} */
095    @Override
096    @OverridingMethodsMustInvokeSuper
097    public void deleteBean(@Nonnull VariableLight n, @Nonnull String property) {
098        throw new UnsupportedOperationException("Not supported. Use LightManager.deleteBean() instead");
099    }
100
101    @Override
102    public void propertyChange(PropertyChangeEvent e) {
103        super.propertyChange(e);
104
105        if ("beans".equals(e.getPropertyName())) {
106
107            // A NamedBean is added
108            if ((e.getNewValue() != null)
109                    && (e.getNewValue() instanceof VariableLight)) {
110                super.register((VariableLight) e.getNewValue());
111            }
112
113            // A NamedBean is removed
114            if ((e.getOldValue() != null)
115                    && (e.getOldValue() instanceof VariableLight)) {
116                super.deregister((VariableLight) e.getOldValue());
117            }
118        }
119    }
120
121
122//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultVariableLightManager.class);
123
124}