001package jmri.jmrix.grapevine;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.jmrix.ConfiguringSystemConnectionMemo;
009import jmri.jmrix.DefaultSystemConnectionMemo;
010import jmri.util.NamedBeanComparator;
011
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Minimum required SystemConnectionMemo for Grapevine.
017 * Expanded for multichar/multiconnection support. Nodes are handled bij superclass.
018 *
019 * @author Randall Wood randall.h.wood@alexandriasoftware.com
020 */
021public class GrapevineSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
022
023    public GrapevineSystemConnectionMemo() {
024        this("G", Bundle.getMessage("MenuSystem"));
025    }
026
027    public GrapevineSystemConnectionMemo(@Nonnull String prefix, @Nonnull String name) {
028        super(prefix, name);
029
030        InstanceManager.store(this, GrapevineSystemConnectionMemo.class);
031
032        // create and register the ComponentFactory for the GUI (menu)
033        InstanceManager.store(cf = new jmri.jmrix.grapevine.swing.GrapevineComponentFactory(this),
034                jmri.jmrix.swing.ComponentFactory.class);
035
036        log.debug("Created GrapevineSystemConnectionMemo, prefix = {}", prefix);
037    }
038
039    private SerialTrafficController tc = null;
040    jmri.jmrix.swing.ComponentFactory cf = null;
041
042    /**
043     * Set the traffic controller instance associated with this connection memo.
044     *
045     * @param tc jmri.jmrix.grapevine.SerialTrafficController object to use.
046     */
047    public void setTrafficController(SerialTrafficController tc){
048        this.tc = tc;
049        log.debug("Memo {} set GrapevineTrafficController {}", getUserName(), tc);
050    }
051
052    /**
053     * Get the traffic controller instance associated with this connection memo.
054     * @return traffic controller, one is provided if null.
055     */
056    public SerialTrafficController getTrafficController(){
057        if (tc == null) {
058            setTrafficController(new SerialTrafficController(this));
059            log.debug("Auto create of SerialTrafficController for initial configuration");
060        }
061        return tc;
062    }
063
064    /**
065     * Provide Grapevine menu strings.
066     *
067     * @return bundle file containing action - menuitem pairs
068     */
069    @Override
070    protected ResourceBundle getActionModelResourceBundle() {
071        return ResourceBundle.getBundle("jmri.jmrix.grapevine.GrapevineActionListBundle");
072    }
073
074    @Override
075    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
076        return new NamedBeanComparator<>();
077    }
078
079    /**
080    * Configure the common managers for Grapevine connections. This puts the
081    * common manager config in one place.
082    */
083    @Override
084    public void configureManagers() {
085        setTurnoutManager(new SerialTurnoutManager(this));
086        InstanceManager.setTurnoutManager(getTurnoutManager());
087
088        setLightManager(new SerialLightManager(this));
089        InstanceManager.setLightManager(getLightManager());
090
091        setSensorManager(new SerialSensorManager(this));
092        InstanceManager.setSensorManager(getSensorManager());
093
094        register();
095    }
096
097    /**
098     * Provide access to the SensorManager for this particular connection.
099     * <p>
100     * NOTE: SensorManager defaults to NULL
101     * @return sensor manager.
102     */
103    public SensorManager getSensorManager() {
104        log.debug("getSensorManager {}", get(SensorManager.class) != null ? "OK": "returned NULL");
105        return get(SensorManager.class);
106    }
107
108    public void setSensorManager(SerialSensorManager s) {
109        store(s,SensorManager.class);
110        getTrafficController().setSensorManager(s);
111    }
112
113    /**
114     * Provide access to the TurnoutManager for this particular connection.
115     * <p>
116     * NOTE: TurnoutManager defaults to NULL
117     * @return turnout manager.
118     */
119    public TurnoutManager getTurnoutManager() {
120        log.debug("getTurnoutManager {}", get(TurnoutManager.class) != null ? "OK": "returned NULL");
121        return get(TurnoutManager.class);
122    }
123
124    public void setTurnoutManager(SerialTurnoutManager t) {
125        store(t,TurnoutManager.class);
126        // not accessible, needed?
127    }
128
129    /**
130     * Provide access to the LightManager for this particular connection.
131     * <p>
132     * NOTE: LightManager defaults to NULL
133     * @return light manager.
134     */
135    public LightManager getLightManager() {
136        log.debug("getLightManager {}", get(LightManager.class) != null ? "OK": "returned NULL");
137        return get(LightManager.class);
138
139    }
140
141    public void setLightManager(SerialLightManager l) {
142        store(l,LightManager.class);
143        // not accessible, needed?
144    }
145
146    @Override
147    public void dispose() {
148        tc = null;
149        InstanceManager.deregister(this, GrapevineSystemConnectionMemo.class);
150        if (cf != null) {
151            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
152        }
153        super.dispose();
154    }
155
156    private final static Logger log = LoggerFactory.getLogger(GrapevineSystemConnectionMemo.class);
157
158}