001package jmri.managers;
002
003import java.util.List;
004import javax.annotation.Nonnull;
005import javax.annotation.CheckForNull;
006import jmri.AddressedProgrammer;
007import jmri.AddressedProgrammerManager;
008import jmri.GlobalProgrammerManager;
009import jmri.Programmer;
010import jmri.ProgrammingMode;
011import jmri.beans.PropertyChangeSupport;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Provides a very basic implementation of a programmer manager by providing a
017 * union of the AddressedProgrammerManager and GlobalProgrammerManager
018 * interfaces.
019 * <p>
020 * This implementation requires a service-mode Programmer at construction time
021 * and returns that Programmer for all global programming mode requests. This
022 * implementation of AddressedProgrammerManager always returns null for Op Mode,
023 * or addressed programmer requests, indicating there is no programmer of that
024 * type.
025 *
026 * @see jmri.AddressedProgrammerManager
027 * @see jmri.GlobalProgrammerManager
028 * @author Bob Jacobsen Copyright (C) 2001, 2015, 2016
029 */
030public class DefaultProgrammerManager extends PropertyChangeSupport implements AddressedProgrammerManager, GlobalProgrammerManager {
031
032    // For the record, these were the original numerical definitions:
033    //     public static final ProgrammingMode NONE              = new ProgrammingMode("NONE", 0);
034    //     public static final ProgrammingMode REGISTERMODE      = new ProgrammingMode("REGISTERMODE", 11);
035    //     public static final ProgrammingMode PAGEMODE          = new ProgrammingMode("PAGEMODE", 21);
036    //     public static final ProgrammingMode DIRECTBITMODE     = new ProgrammingMode("DIRECTBITMODE", 31);
037    //     public static final ProgrammingMode DIRECTBYTEMODE    = new ProgrammingMode("DIRECTBYTEMODE", 32);
038    //     public static final ProgrammingMode ADDRESSMODE       = new ProgrammingMode("ADDRESSMODE", 41);
039    //     public static final ProgrammingMode OPSBYTEMODE       = new ProgrammingMode("OPSBYTEMODE", 101);
040    //     public static final ProgrammingMode OPSBITMODE        = new ProgrammingMode("OPSBITMODE", 102);
041    //     public static final ProgrammingMode OPSACCBYTEMODE    = new ProgrammingMode("OPSACCBYTEMODE", 111);
042    //     public static final ProgrammingMode OPSACCBITMODE     = new ProgrammingMode("OPSACCBITMODE", 112);
043    //     public static final ProgrammingMode OPSACCEXTBYTEMODE = new ProgrammingMode("OPSACCEXTBYTEMODE", 121);
044    //     public static final ProgrammingMode OPSACCEXTBITMODE  = new ProgrammingMode("OPSACCEXTBITMODE", 122);
045    private Programmer programmer;
046
047    /**
048     * Constructor when no global programmer is available.
049     */
050    public DefaultProgrammerManager() {
051        this(null);  // indicates not present
052    }
053
054    /**
055     * Constructor with a programmer.
056     *
057     * @param programmer the programmer to use; if null, acts as if no
058     *                   programmer is available
059     */
060    public DefaultProgrammerManager(@CheckForNull Programmer programmer) {
061        this.programmer = programmer;
062    }
063
064    /**
065     * Constructor with a programmer and associated connection.
066     *
067     * @param programmer the programmer to use; if null, acts as if no
068     *                   programmer is available
069     * @param memo       the associated connection
070     */
071    public DefaultProgrammerManager(@CheckForNull Programmer programmer, @Nonnull jmri.SystemConnectionMemo memo) {
072        this(programmer);
073        this.userName = memo.getUserName();
074    }
075
076    private String userName = "<Default>";
077
078    /**
079     * Provides the human-readable representation for including
080     * ProgrammerManagers directly in user interface components, so it should
081     * return a user-provided name for this particular one.
082     */
083    @Override
084    public String getUserName() {
085        return userName;
086    }
087
088    /**
089     * Provides the human-readable representation for including
090     * ProgrammerManagers directly in user interface components, so it should
091     * return a user-provided name for this particular one.
092     */
093    @Override
094    public String toString() {
095        return getUserName();
096    }
097
098    @Override
099    public Programmer getGlobalProgrammer() {
100        log.debug("return default service-mode programmer of type {}", (programmer != null ? programmer.getClass() : "(null)"));
101        return programmer;
102    }
103
104    @Override
105    public AddressedProgrammer getAddressedProgrammer(boolean pLongAddress, int pAddress) {
106        return null;
107    }
108
109    @Override
110    public Programmer reserveGlobalProgrammer() {
111        return programmer;
112    }
113
114    @Override
115    public void releaseGlobalProgrammer(@Nonnull Programmer p) {
116    }
117
118    @Override
119    public AddressedProgrammer reserveAddressedProgrammer(boolean pLongAddress, int pAddress) {
120        return null;
121    }
122
123    @Override
124    public void releaseAddressedProgrammer(@Nonnull AddressedProgrammer p) {
125    }
126
127    /**
128     * {@inheritDoc}
129     *
130     * @return always false in this implementation
131     */
132    @Override
133    public boolean isAddressedModePossible() {
134        return false;
135    }
136
137    /**
138     * {@inheritDoc}
139     *
140     * @return always false in this implementation
141     */
142    @Override
143    public boolean isAddressedModePossible(@Nonnull jmri.LocoAddress l) {
144        return isAddressedModePossible();
145    }
146
147    /**
148     * {@inheritDoc}
149     *
150     * @return always false in this implementation
151     */
152    @Override
153    public boolean isGlobalProgrammerAvailable() {
154        return true;
155    }
156
157    /**
158     * {@inheritDoc}
159     *
160     * @return a default list of programming modes that most
161     *         {@link jmri.AddressedProgrammer}s make available
162     */
163    @Override
164    public List<ProgrammingMode> getDefaultModes() {
165        List<ProgrammingMode> retval = new java.util.ArrayList<>();
166        retval.add(ProgrammingMode.OPSBYTEMODE);
167        return retval;
168    }
169
170    public void dispose() {
171        if (programmer != null) {
172            programmer.dispose();
173        }
174    }
175
176    private final static Logger log = LoggerFactory.getLogger(DefaultProgrammerManager.class);
177}